blob: 905c0e1cf5aff162a12c0c2d1a68523d7517990c [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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040032#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080033#include <linux/swap.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080034#include <linux/uio.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070035#include <linux/khugepaged.h>
Matt Mackall853ac432009-01-06 14:40:20 -080036
37static struct vfsmount *shm_mnt;
38
39#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * This virtual memory filesystem is heavily based on the ramfs. It
42 * extends ramfs by the ability to use swap and honor resource limits
43 * which makes it a completely usable filesystem.
44 */
45
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070046#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070047#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010048#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080049#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/string.h>
52#include <linux/slab.h>
53#include <linux/backing-dev.h>
54#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070057#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070058#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070059#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070060#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/security.h>
62#include <linux/swapops.h>
63#include <linux/mempolicy.h>
64#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000065#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070066#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070067#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080068#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040069#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070070#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070071#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070072#include <uapi/linux/memfd.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/pgtable.h>
76
Mel Gormandd56b042015-11-06 16:28:43 -080077#include "internal.h"
78
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030079#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
80#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Pretend that each entry is of this size in directory's i_size */
83#define BOGO_DIRENT_SIZE 20
84
Hugh Dickins69f07ec2011-08-03 16:21:26 -070085/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
86#define SHORT_SYMLINK_LEN 128
87
Hugh Dickins1aac1402012-05-29 15:06:42 -070088/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070089 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
90 * inode->i_private (with i_mutex making sure that it has only one user at
91 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -070092 */
93struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -070094 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -070095 pgoff_t start; /* start of range currently being fallocated */
96 pgoff_t next; /* the next page offset to be fallocated */
97 pgoff_t nr_falloced; /* how many new pages have been fallocated */
98 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
99};
100
Andrew Mortonb76db732008-02-08 04:21:49 -0800101#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800102static unsigned long shmem_default_max_blocks(void)
103{
104 return totalram_pages / 2;
105}
106
107static unsigned long shmem_default_max_inodes(void)
108{
109 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
110}
Andrew Mortonb76db732008-02-08 04:21:49 -0800111#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800112
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700113static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
114static int shmem_replace_page(struct page **pagep, gfp_t gfp,
115 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700116static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700117 struct page **pagep, enum sgp_type sgp,
118 gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700119
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700120int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700121 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700122{
123 return shmem_getpage_gfp(inode, index, pagep, sgp,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700124 mapping_gfp_mask(inode->i_mapping), NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
128{
129 return sb->s_fs_info;
130}
131
132/*
133 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
134 * for shared memory and for shared anonymous (/dev/zero) mappings
135 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
136 * consistent with the pre-accounting of private mappings ...
137 */
138static inline int shmem_acct_size(unsigned long flags, loff_t size)
139{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000140 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000141 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static inline void shmem_unacct_size(unsigned long flags, loff_t size)
145{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000146 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 vm_unacct_memory(VM_ACCT(size));
148}
149
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700150static inline int shmem_reacct_size(unsigned long flags,
151 loff_t oldsize, loff_t newsize)
152{
153 if (!(flags & VM_NORESERVE)) {
154 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
155 return security_vm_enough_memory_mm(current->mm,
156 VM_ACCT(newsize) - VM_ACCT(oldsize));
157 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
158 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
159 }
160 return 0;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700165 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
167 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
168 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700169static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700171 if (!(flags & VM_NORESERVE))
172 return 0;
173
174 return security_vm_enough_memory_mm(current->mm,
175 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static inline void shmem_unacct_blocks(unsigned long flags, long pages)
179{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000180 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300181 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Hugh Dickins759b9772007-03-05 00:30:28 -0800184static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700185static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800186static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800187static const struct inode_operations shmem_inode_operations;
188static const struct inode_operations shmem_dir_inode_operations;
189static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400190static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800193static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800195static int shmem_reserve_inode(struct super_block *sb)
196{
197 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
198 if (sbinfo->max_inodes) {
199 spin_lock(&sbinfo->stat_lock);
200 if (!sbinfo->free_inodes) {
201 spin_unlock(&sbinfo->stat_lock);
202 return -ENOSPC;
203 }
204 sbinfo->free_inodes--;
205 spin_unlock(&sbinfo->stat_lock);
206 }
207 return 0;
208}
209
210static void shmem_free_inode(struct super_block *sb)
211{
212 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
213 if (sbinfo->max_inodes) {
214 spin_lock(&sbinfo->stat_lock);
215 sbinfo->free_inodes++;
216 spin_unlock(&sbinfo->stat_lock);
217 }
218}
219
Randy Dunlap46711812008-03-19 17:00:41 -0700220/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700221 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * @inode: inode to recalc
223 *
224 * We have to calculate the free blocks since the mm can drop
225 * undirtied hole pages behind our back.
226 *
227 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
228 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
229 *
230 * It has to be called with the spinlock held.
231 */
232static void shmem_recalc_inode(struct inode *inode)
233{
234 struct shmem_inode_info *info = SHMEM_I(inode);
235 long freed;
236
237 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
238 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700239 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
240 if (sbinfo->max_blocks)
241 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700243 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246}
247
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700248bool shmem_charge(struct inode *inode, long pages)
249{
250 struct shmem_inode_info *info = SHMEM_I(inode);
251 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700252 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700253
254 if (shmem_acct_block(info->flags, pages))
255 return false;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700256 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700257 info->alloced += pages;
258 inode->i_blocks += pages * BLOCKS_PER_PAGE;
259 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700260 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700261 inode->i_mapping->nrpages += pages;
262
263 if (!sbinfo->max_blocks)
264 return true;
265 if (percpu_counter_compare(&sbinfo->used_blocks,
266 sbinfo->max_blocks - pages) > 0) {
267 inode->i_mapping->nrpages -= pages;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700268 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700269 info->alloced -= pages;
270 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700271 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700272
273 return false;
274 }
275 percpu_counter_add(&sbinfo->used_blocks, pages);
276 return true;
277}
278
279void shmem_uncharge(struct inode *inode, long pages)
280{
281 struct shmem_inode_info *info = SHMEM_I(inode);
282 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700283 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700284
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700285 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700286 info->alloced -= pages;
287 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
288 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700289 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700290
291 if (sbinfo->max_blocks)
292 percpu_counter_sub(&sbinfo->used_blocks, pages);
293}
294
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700295/*
296 * Replace item expected in radix tree by a new item, while holding tree lock.
297 */
298static int shmem_radix_tree_replace(struct address_space *mapping,
299 pgoff_t index, void *expected, void *replacement)
300{
301 void **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700302 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700303
304 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700305 VM_BUG_ON(!replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700306 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700307 if (!pslot)
308 return -ENOENT;
309 item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700310 if (item != expected)
311 return -ENOENT;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700312 radix_tree_replace_slot(pslot, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700313 return 0;
314}
315
316/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700317 * Sometimes, before we decide whether to proceed or to fail, we must check
318 * that an entry was not already brought back from swap by a racing thread.
319 *
320 * Checking page is not enough: by the time a SwapCache page is locked, it
321 * might be reused, and again be SwapCache, using the same swap as before.
322 */
323static bool shmem_confirm_swap(struct address_space *mapping,
324 pgoff_t index, swp_entry_t swap)
325{
326 void *item;
327
328 rcu_read_lock();
329 item = radix_tree_lookup(&mapping->page_tree, index);
330 rcu_read_unlock();
331 return item == swp_to_radix_entry(swap);
332}
333
334/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700335 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
336 *
337 * SHMEM_HUGE_NEVER:
338 * disables huge pages for the mount;
339 * SHMEM_HUGE_ALWAYS:
340 * enables huge pages for the mount;
341 * SHMEM_HUGE_WITHIN_SIZE:
342 * only allocate huge pages if the page will be fully within i_size,
343 * also respect fadvise()/madvise() hints;
344 * SHMEM_HUGE_ADVISE:
345 * only allocate huge pages if requested with fadvise()/madvise();
346 */
347
348#define SHMEM_HUGE_NEVER 0
349#define SHMEM_HUGE_ALWAYS 1
350#define SHMEM_HUGE_WITHIN_SIZE 2
351#define SHMEM_HUGE_ADVISE 3
352
353/*
354 * Special values.
355 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
356 *
357 * SHMEM_HUGE_DENY:
358 * disables huge on shm_mnt and all mounts, for emergency use;
359 * SHMEM_HUGE_FORCE:
360 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
361 *
362 */
363#define SHMEM_HUGE_DENY (-1)
364#define SHMEM_HUGE_FORCE (-2)
365
366#ifdef CONFIG_TRANSPARENT_HUGEPAGE
367/* ifdef here to avoid bloating shmem.o when not necessary */
368
369int shmem_huge __read_mostly;
370
371static int shmem_parse_huge(const char *str)
372{
373 if (!strcmp(str, "never"))
374 return SHMEM_HUGE_NEVER;
375 if (!strcmp(str, "always"))
376 return SHMEM_HUGE_ALWAYS;
377 if (!strcmp(str, "within_size"))
378 return SHMEM_HUGE_WITHIN_SIZE;
379 if (!strcmp(str, "advise"))
380 return SHMEM_HUGE_ADVISE;
381 if (!strcmp(str, "deny"))
382 return SHMEM_HUGE_DENY;
383 if (!strcmp(str, "force"))
384 return SHMEM_HUGE_FORCE;
385 return -EINVAL;
386}
387
388static const char *shmem_format_huge(int huge)
389{
390 switch (huge) {
391 case SHMEM_HUGE_NEVER:
392 return "never";
393 case SHMEM_HUGE_ALWAYS:
394 return "always";
395 case SHMEM_HUGE_WITHIN_SIZE:
396 return "within_size";
397 case SHMEM_HUGE_ADVISE:
398 return "advise";
399 case SHMEM_HUGE_DENY:
400 return "deny";
401 case SHMEM_HUGE_FORCE:
402 return "force";
403 default:
404 VM_BUG_ON(1);
405 return "bad_val";
406 }
407}
408
409#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
410
411#define shmem_huge SHMEM_HUGE_DENY
412
413#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
414
415/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700416 * Like add_to_page_cache_locked, but error if expected item has gone.
417 */
418static int shmem_add_to_page_cache(struct page *page,
419 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700420 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700421{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700422 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700423
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700424 VM_BUG_ON_PAGE(PageTail(page), page);
425 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800426 VM_BUG_ON_PAGE(!PageLocked(page), page);
427 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700428 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700429
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700430 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700431 page->mapping = mapping;
432 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700433
Hugh Dickinsb065b432012-07-11 14:02:48 -0700434 spin_lock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700435 if (PageTransHuge(page)) {
436 void __rcu **results;
437 pgoff_t idx;
438 int i;
439
440 error = 0;
441 if (radix_tree_gang_lookup_slot(&mapping->page_tree,
442 &results, &idx, index, 1) &&
443 idx < index + HPAGE_PMD_NR) {
444 error = -EEXIST;
445 }
446
447 if (!error) {
448 for (i = 0; i < HPAGE_PMD_NR; i++) {
449 error = radix_tree_insert(&mapping->page_tree,
450 index + i, page + i);
451 VM_BUG_ON(error);
452 }
453 count_vm_event(THP_FILE_ALLOC);
454 }
455 } else if (!expected) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700456 error = radix_tree_insert(&mapping->page_tree, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700457 } else {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700458 error = shmem_radix_tree_replace(mapping, index, expected,
459 page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700460 }
461
Hugh Dickinsb065b432012-07-11 14:02:48 -0700462 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700463 mapping->nrpages += nr;
464 if (PageTransHuge(page))
465 __inc_zone_page_state(page, NR_SHMEM_THPS);
466 __mod_zone_page_state(page_zone(page), NR_FILE_PAGES, nr);
467 __mod_zone_page_state(page_zone(page), NR_SHMEM, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700468 spin_unlock_irq(&mapping->tree_lock);
469 } else {
470 page->mapping = NULL;
471 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700472 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700473 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700474 return error;
475}
476
477/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700478 * Like delete_from_page_cache, but substitutes swap for page.
479 */
480static void shmem_delete_from_page_cache(struct page *page, void *radswap)
481{
482 struct address_space *mapping = page->mapping;
483 int error;
484
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700485 VM_BUG_ON_PAGE(PageCompound(page), page);
486
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700487 spin_lock_irq(&mapping->tree_lock);
488 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
489 page->mapping = NULL;
490 mapping->nrpages--;
491 __dec_zone_page_state(page, NR_FILE_PAGES);
492 __dec_zone_page_state(page, NR_SHMEM);
493 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300494 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700495 BUG_ON(error);
496}
497
498/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700499 * Remove swap entry from radix tree, free the swap and its page cache.
500 */
501static int shmem_free_swap(struct address_space *mapping,
502 pgoff_t index, void *radswap)
503{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700504 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700505
506 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700507 old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700508 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700509 if (old != radswap)
510 return -ENOENT;
511 free_swap_and_cache(radix_to_swp_entry(radswap));
512 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700513}
514
515/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800516 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800517 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800518 *
519 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
520 * as long as the inode doesn't go away and racy results are not a problem.
521 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800522unsigned long shmem_partial_swap_usage(struct address_space *mapping,
523 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800524{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800525 struct radix_tree_iter iter;
526 void **slot;
527 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800528 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800529
530 rcu_read_lock();
531
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800532 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
533 if (iter.index >= end)
534 break;
535
536 page = radix_tree_deref_slot(slot);
537
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700538 if (radix_tree_deref_retry(page)) {
539 slot = radix_tree_iter_retry(&iter);
540 continue;
541 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800542
543 if (radix_tree_exceptional_entry(page))
544 swapped++;
545
546 if (need_resched()) {
547 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -0700548 slot = radix_tree_iter_next(&iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800549 }
550 }
551
552 rcu_read_unlock();
553
554 return swapped << PAGE_SHIFT;
555}
556
557/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800558 * Determine (in bytes) how many of the shmem object's pages mapped by the
559 * given vma is swapped out.
560 *
561 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
562 * as long as the inode doesn't go away and racy results are not a problem.
563 */
564unsigned long shmem_swap_usage(struct vm_area_struct *vma)
565{
566 struct inode *inode = file_inode(vma->vm_file);
567 struct shmem_inode_info *info = SHMEM_I(inode);
568 struct address_space *mapping = inode->i_mapping;
569 unsigned long swapped;
570
571 /* Be careful as we don't hold info->lock */
572 swapped = READ_ONCE(info->swapped);
573
574 /*
575 * The easier cases are when the shmem object has nothing in swap, or
576 * the vma maps it whole. Then we can simply use the stats that we
577 * already track.
578 */
579 if (!swapped)
580 return 0;
581
582 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
583 return swapped << PAGE_SHIFT;
584
585 /* Here comes the more involved part */
586 return shmem_partial_swap_usage(mapping,
587 linear_page_index(vma, vma->vm_start),
588 linear_page_index(vma, vma->vm_end));
589}
590
591/*
Hugh Dickins24513262012-01-20 14:34:21 -0800592 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
593 */
594void shmem_unlock_mapping(struct address_space *mapping)
595{
596 struct pagevec pvec;
597 pgoff_t indices[PAGEVEC_SIZE];
598 pgoff_t index = 0;
599
600 pagevec_init(&pvec, 0);
601 /*
602 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
603 */
604 while (!mapping_unevictable(mapping)) {
605 /*
606 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
607 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
608 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700609 pvec.nr = find_get_entries(mapping, index,
610 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800611 if (!pvec.nr)
612 break;
613 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700614 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800615 check_move_unevictable_pages(pvec.pages, pvec.nr);
616 pagevec_release(&pvec);
617 cond_resched();
618 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700619}
620
621/*
622 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700623 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700624 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700625static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
626 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700628 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300630 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
631 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
632 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
633 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700634 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700635 pgoff_t indices[PAGEVEC_SIZE];
636 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700637 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700638 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700640 if (lend == -1)
641 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700642
643 pagevec_init(&pvec, 0);
644 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700645 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700646 pvec.nr = find_get_entries(mapping, index,
647 min(end - index, (pgoff_t)PAGEVEC_SIZE),
648 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700649 if (!pvec.nr)
650 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700651 for (i = 0; i < pagevec_count(&pvec); i++) {
652 struct page *page = pvec.pages[i];
653
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700654 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700655 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700656 break;
657
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700658 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700659 if (unfalloc)
660 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700661 nr_swaps_freed += !shmem_free_swap(mapping,
662 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700663 continue;
664 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700665
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700666 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
667
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700668 if (!trylock_page(page))
669 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700670
671 if (PageTransTail(page)) {
672 /* Middle of THP: zero out the page */
673 clear_highpage(page);
674 unlock_page(page);
675 continue;
676 } else if (PageTransHuge(page)) {
677 if (index == round_down(end, HPAGE_PMD_NR)) {
678 /*
679 * Range ends in the middle of THP:
680 * zero out the page
681 */
682 clear_highpage(page);
683 unlock_page(page);
684 continue;
685 }
686 index += HPAGE_PMD_NR - 1;
687 i += HPAGE_PMD_NR - 1;
688 }
689
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700690 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700691 VM_BUG_ON_PAGE(PageTail(page), page);
692 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800693 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700694 truncate_inode_page(mapping, page);
695 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700696 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700697 unlock_page(page);
698 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700699 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800700 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700701 cond_resched();
702 index++;
703 }
704
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700705 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700706 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700707 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700708 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300709 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700710 if (start > end) {
711 top = partial_end;
712 partial_end = 0;
713 }
714 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700715 set_page_dirty(page);
716 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300717 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700718 }
719 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700720 if (partial_end) {
721 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700722 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700723 if (page) {
724 zero_user_segment(page, 0, partial_end);
725 set_page_dirty(page);
726 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300727 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700728 }
729 }
730 if (start >= end)
731 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700732
733 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700734 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700735 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700736
737 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700738 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700739 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700740 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700741 /* If all gone or hole-punch or unfalloc, we're done */
742 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700743 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700744 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700745 index = start;
746 continue;
747 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700748 for (i = 0; i < pagevec_count(&pvec); i++) {
749 struct page *page = pvec.pages[i];
750
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700751 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700752 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700753 break;
754
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700755 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700756 if (unfalloc)
757 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700758 if (shmem_free_swap(mapping, index, page)) {
759 /* Swap was replaced by page: retry */
760 index--;
761 break;
762 }
763 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700764 continue;
765 }
766
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700767 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700768
769 if (PageTransTail(page)) {
770 /* Middle of THP: zero out the page */
771 clear_highpage(page);
772 unlock_page(page);
773 /*
774 * Partial thp truncate due 'start' in middle
775 * of THP: don't need to look on these pages
776 * again on !pvec.nr restart.
777 */
778 if (index != round_down(end, HPAGE_PMD_NR))
779 start++;
780 continue;
781 } else if (PageTransHuge(page)) {
782 if (index == round_down(end, HPAGE_PMD_NR)) {
783 /*
784 * Range ends in the middle of THP:
785 * zero out the page
786 */
787 clear_highpage(page);
788 unlock_page(page);
789 continue;
790 }
791 index += HPAGE_PMD_NR - 1;
792 i += HPAGE_PMD_NR - 1;
793 }
794
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700795 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700796 VM_BUG_ON_PAGE(PageTail(page), page);
797 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800798 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700799 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700800 } else {
801 /* Page was replaced by swap: retry */
802 unlock_page(page);
803 index--;
804 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700805 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700806 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700807 unlock_page(page);
808 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700809 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800810 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700811 index++;
812 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700813
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700814 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700815 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700817 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700820void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
821{
822 shmem_undo_range(inode, lstart, lend, false);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700823 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700825EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Yu Zhao44a30222015-09-08 15:03:33 -0700827static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
828 struct kstat *stat)
829{
830 struct inode *inode = dentry->d_inode;
831 struct shmem_inode_info *info = SHMEM_I(inode);
832
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800833 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700834 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800835 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700836 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800837 }
Yu Zhao44a30222015-09-08 15:03:33 -0700838 generic_fillattr(inode, stat);
Yu Zhao44a30222015-09-08 15:03:33 -0700839 return 0;
840}
841
Hugh Dickins94c1e622011-06-27 16:18:03 -0700842static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
David Howells75c3cfa2015-03-17 22:26:12 +0000844 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -0700845 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int error;
847
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200848 error = inode_change_ok(inode, attr);
849 if (error)
850 return error;
851
Hugh Dickins94c1e622011-06-27 16:18:03 -0700852 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
853 loff_t oldsize = inode->i_size;
854 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000855
David Herrmann40e041a2014-08-08 14:25:27 -0700856 /* protected by i_mutex */
857 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
858 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
859 return -EPERM;
860
Hugh Dickins94c1e622011-06-27 16:18:03 -0700861 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700862 error = shmem_reacct_size(SHMEM_I(inode)->flags,
863 oldsize, newsize);
864 if (error)
865 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -0700866 i_size_write(inode, newsize);
867 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
868 }
Josef Bacikafa2db22015-06-24 16:58:45 -0700869 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -0700870 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800871 if (oldsize > holebegin)
872 unmap_mapping_range(inode->i_mapping,
873 holebegin, 0, 1);
874 if (info->alloced)
875 shmem_truncate_range(inode,
876 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700877 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800878 if (oldsize > holebegin)
879 unmap_mapping_range(inode->i_mapping,
880 holebegin, 0, 1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200884 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200885 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800886 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return error;
888}
889
Al Viro1f895f72010-06-05 19:10:41 -0400890static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 struct shmem_inode_info *info = SHMEM_I(inode);
893
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000894 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 shmem_unacct_size(info->flags, inode->i_size);
896 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000897 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800899 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800901 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Al Viro3ed47db2016-01-22 18:08:52 -0500903 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700904
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400905 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -0800906 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800907 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +0200908 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700911/*
912 * If swap found in inode, free it and move page from swapcache to filecache.
913 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700914static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700915 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700917 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700918 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700919 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700920 gfp_t gfp;
921 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700923 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700924 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700925 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -0700926 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800927
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800928 /*
929 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400930 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800931 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700932 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800933 */
934 if (shmem_swaplist.next != &info->swaplist)
935 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800936
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700937 gfp = mapping_gfp_mask(mapping);
938 if (shmem_should_replace_page(*pagep, gfp)) {
939 mutex_unlock(&shmem_swaplist_mutex);
940 error = shmem_replace_page(pagep, gfp, info, index);
941 mutex_lock(&shmem_swaplist_mutex);
942 /*
943 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -0700944 * allocation, but the inode might have been freed while we
945 * dropped it: although a racing shmem_evict_inode() cannot
946 * complete without emptying the radix_tree, our page lock
947 * on this swapcache page is not enough to prevent that -
948 * free_swap_and_cache() of our swap entry will only
949 * trylock_page(), removing swap from radix_tree whatever.
950 *
951 * We must not proceed to shmem_add_to_page_cache() if the
952 * inode has been freed, but of course we cannot rely on
953 * inode or mapping or info to check that. However, we can
954 * safely check if our swap entry is still in use (and here
955 * it can't have got reused for another page): if it's still
956 * in use, then the inode cannot have been freed yet, and we
957 * can safely proceed (if it's no longer in use, that tells
958 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700959 */
960 if (!page_swapcount(*pagep))
961 error = -ENOENT;
962 }
963
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800964 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700965 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
966 * but also to hold up shmem_evict_inode(): so inode cannot be freed
967 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800968 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700969 if (!error)
970 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700971 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700972 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700973 /*
974 * Truncation and eviction use free_swap_and_cache(), which
975 * only does trylock page: if we raced, best clean up here.
976 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700977 delete_from_swap_cache(*pagep);
978 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700979 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700980 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700981 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700982 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700983 swap_free(swap);
984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800986 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700990 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700992int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700994 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -0700996 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700997 int error = 0;
998
999 /*
1000 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001001 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001002 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001003 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001004 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001005
1006 /*
1007 * Charge page using GFP_KERNEL while we can wait, before taking
1008 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1009 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001010 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001011 error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
1012 false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001013 if (error)
1014 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001015 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001016 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001018 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001019 list_for_each_safe(this, next, &shmem_swaplist) {
1020 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001021 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001022 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001023 else
1024 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001025 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001026 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001027 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001028 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001030 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001031
Johannes Weiner00501b52014-08-08 14:19:20 -07001032 if (error) {
1033 if (error != -ENOMEM)
1034 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001035 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001036 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001037 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001038out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001039 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001040 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001041 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/*
1045 * Move the page from the page cache to the swap cache.
1046 */
1047static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1048{
1049 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001052 swp_entry_t swap;
1053 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001055 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 mapping = page->mapping;
1058 index = page->index;
1059 inode = mapping->host;
1060 info = SHMEM_I(inode);
1061 if (info->flags & VM_LOCKED)
1062 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001063 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto redirty;
1065
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001066 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001067 * Our capabilities prevent regular writeback or sync from ever calling
1068 * shmem_writepage; but a stacking filesystem might use ->writepage of
1069 * its underlying filesystem, in which case tmpfs should write out to
1070 * swap only in response to memory pressure, and not for the writeback
1071 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001072 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001073 if (!wbc->for_reclaim) {
1074 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1075 goto redirty;
1076 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001077
1078 /*
1079 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1080 * value into swapfile.c, the only way we can correctly account for a
1081 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001082 *
1083 * That's okay for a page already fallocated earlier, but if we have
1084 * not yet completed the fallocation, then (a) we want to keep track
1085 * of this page in case we have to undo it, and (b) it may not be a
1086 * good idea to continue anyway, once we're pushing into swap. So
1087 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001088 */
1089 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001090 if (inode->i_private) {
1091 struct shmem_falloc *shmem_falloc;
1092 spin_lock(&inode->i_lock);
1093 shmem_falloc = inode->i_private;
1094 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001095 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001096 index >= shmem_falloc->start &&
1097 index < shmem_falloc->next)
1098 shmem_falloc->nr_unswapped++;
1099 else
1100 shmem_falloc = NULL;
1101 spin_unlock(&inode->i_lock);
1102 if (shmem_falloc)
1103 goto redirty;
1104 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001105 clear_highpage(page);
1106 flush_dcache_page(page);
1107 SetPageUptodate(page);
1108 }
1109
Hugh Dickins48f170f2011-07-25 17:12:37 -07001110 swap = get_swap_page();
1111 if (!swap.val)
1112 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001113
Vladimir Davydov37e84352016-01-20 15:02:56 -08001114 if (mem_cgroup_try_charge_swap(page, swap))
1115 goto free_swap;
1116
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001117 /*
1118 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001119 * if it's not already there. Do it now before the page is
1120 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001121 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001122 * we've incremented swapped, because shmem_unuse_inode() will
1123 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001124 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001125 mutex_lock(&shmem_swaplist_mutex);
1126 if (list_empty(&info->swaplist))
1127 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001128
Hugh Dickins48f170f2011-07-25 17:12:37 -07001129 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001130 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001131 shmem_recalc_inode(inode);
1132 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001133 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001134
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001135 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001136 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1137
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001138 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001139 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001140 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142 }
1143
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001144 mutex_unlock(&shmem_swaplist_mutex);
Vladimir Davydov37e84352016-01-20 15:02:56 -08001145free_swap:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001146 swapcache_free(swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147redirty:
1148 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001149 if (wbc->for_reclaim)
1150 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1151 unlock_page(page);
1152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Hugh Dickins75edd342016-05-19 17:12:44 -07001155#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001156static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001157{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001158 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001159
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001160 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001161 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001162
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001163 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001164
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001165 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001166}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001167
1168static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1169{
1170 struct mempolicy *mpol = NULL;
1171 if (sbinfo->mpol) {
1172 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1173 mpol = sbinfo->mpol;
1174 mpol_get(mpol);
1175 spin_unlock(&sbinfo->stat_lock);
1176 }
1177 return mpol;
1178}
Hugh Dickins75edd342016-05-19 17:12:44 -07001179#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1180static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1181{
1182}
1183static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1184{
1185 return NULL;
1186}
1187#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1188#ifndef CONFIG_NUMA
1189#define vm_policy vm_private_data
1190#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001191
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001192static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1193 struct shmem_inode_info *info, pgoff_t index)
1194{
1195 /* Create a pseudo vma that just contains the policy */
1196 vma->vm_start = 0;
1197 /* Bias interleave by inode number to distribute better across nodes */
1198 vma->vm_pgoff = index + info->vfs_inode.i_ino;
1199 vma->vm_ops = NULL;
1200 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1201}
1202
1203static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1204{
1205 /* Drop reference taken by mpol_shared_policy_lookup() */
1206 mpol_cond_put(vma->vm_policy);
1207}
1208
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001209static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1210 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001213 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001215 shmem_pseudo_vma_init(&pvma, info, index);
Mel Gorman18a2f372012-12-05 14:01:41 -08001216 page = swapin_readahead(swap, gfp, &pvma, 0);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001217 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001218
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001219 return page;
1220}
Mel Gorman18a2f372012-12-05 14:01:41 -08001221
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001222static struct page *shmem_alloc_hugepage(gfp_t gfp,
1223 struct shmem_inode_info *info, pgoff_t index)
1224{
1225 struct vm_area_struct pvma;
1226 struct inode *inode = &info->vfs_inode;
1227 struct address_space *mapping = inode->i_mapping;
1228 pgoff_t idx, hindex = round_down(index, HPAGE_PMD_NR);
1229 void __rcu **results;
1230 struct page *page;
1231
1232 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1233 return NULL;
1234
1235 rcu_read_lock();
1236 if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
1237 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1238 rcu_read_unlock();
1239 return NULL;
1240 }
1241 rcu_read_unlock();
1242
1243 shmem_pseudo_vma_init(&pvma, info, hindex);
1244 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1245 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1246 shmem_pseudo_vma_destroy(&pvma);
1247 if (page)
1248 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001249 return page;
1250}
1251
1252static struct page *shmem_alloc_page(gfp_t gfp,
1253 struct shmem_inode_info *info, pgoff_t index)
1254{
1255 struct vm_area_struct pvma;
1256 struct page *page;
1257
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001258 shmem_pseudo_vma_init(&pvma, info, index);
1259 page = alloc_page_vma(gfp, &pvma, 0);
1260 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001261
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001262 return page;
1263}
1264
1265static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
1266 struct shmem_inode_info *info, struct shmem_sb_info *sbinfo,
1267 pgoff_t index, bool huge)
1268{
1269 struct page *page;
1270 int nr;
1271 int err = -ENOSPC;
1272
1273 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1274 huge = false;
1275 nr = huge ? HPAGE_PMD_NR : 1;
1276
1277 if (shmem_acct_block(info->flags, nr))
1278 goto failed;
1279 if (sbinfo->max_blocks) {
1280 if (percpu_counter_compare(&sbinfo->used_blocks,
1281 sbinfo->max_blocks - nr) > 0)
1282 goto unacct;
1283 percpu_counter_add(&sbinfo->used_blocks, nr);
1284 }
1285
1286 if (huge)
1287 page = shmem_alloc_hugepage(gfp, info, index);
1288 else
1289 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001290 if (page) {
1291 __SetPageLocked(page);
1292 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001293 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001294 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001295
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001296 err = -ENOMEM;
1297 if (sbinfo->max_blocks)
1298 percpu_counter_add(&sbinfo->used_blocks, -nr);
1299unacct:
1300 shmem_unacct_blocks(info->flags, nr);
1301failed:
1302 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001306 * When a page is moved from swapcache to shmem filecache (either by the
1307 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1308 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1309 * ignorance of the mapping it belongs to. If that mapping has special
1310 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1311 * we may need to copy to a suitable page before moving to filecache.
1312 *
1313 * In a future release, this may well be extended to respect cpuset and
1314 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1315 * but for now it is a simple matter of zone.
1316 */
1317static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1318{
1319 return page_zonenum(page) > gfp_zone(gfp);
1320}
1321
1322static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1323 struct shmem_inode_info *info, pgoff_t index)
1324{
1325 struct page *oldpage, *newpage;
1326 struct address_space *swap_mapping;
1327 pgoff_t swap_index;
1328 int error;
1329
1330 oldpage = *pagep;
1331 swap_index = page_private(oldpage);
1332 swap_mapping = page_mapping(oldpage);
1333
1334 /*
1335 * We have arrived here because our zones are constrained, so don't
1336 * limit chance of success by further cpuset and node constraints.
1337 */
1338 gfp &= ~GFP_CONSTRAINT_MASK;
1339 newpage = shmem_alloc_page(gfp, info, index);
1340 if (!newpage)
1341 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001342
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001344 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001345 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001346
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001347 SetPageUptodate(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001348 set_page_private(newpage, swap_index);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001349 SetPageSwapCache(newpage);
1350
1351 /*
1352 * Our caller will very soon move newpage out of swapcache, but it's
1353 * a nice clean interface for us to replace oldpage by newpage there.
1354 */
1355 spin_lock_irq(&swap_mapping->tree_lock);
1356 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1357 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001358 if (!error) {
1359 __inc_zone_page_state(newpage, NR_FILE_PAGES);
1360 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
1361 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001362 spin_unlock_irq(&swap_mapping->tree_lock);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001363
Hugh Dickins0142ef62012-06-07 14:21:09 -07001364 if (unlikely(error)) {
1365 /*
1366 * Is this possible? I think not, now that our callers check
1367 * both PageSwapCache and page_private after getting page lock;
1368 * but be defensive. Reverse old to newpage for clear and free.
1369 */
1370 oldpage = newpage;
1371 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001372 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001373 lru_cache_add_anon(newpage);
1374 *pagep = newpage;
1375 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001376
1377 ClearPageSwapCache(oldpage);
1378 set_page_private(oldpage, 0);
1379
1380 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001381 put_page(oldpage);
1382 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001383 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001384}
1385
1386/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001387 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 *
1389 * If we allocate a new one we do not mark it dirty. That's up to the
1390 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001391 * entry since a page cannot live in both the swap and page cache.
1392 *
1393 * fault_mm and fault_type are only supplied by shmem_fault:
1394 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001396static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001397 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
1398 struct mm_struct *fault_mm, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -07001401 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001403 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001404 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001405 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001407 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001408 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001410 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001411 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001413 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001415 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1416 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001418 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001419 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001420 if (radix_tree_exceptional_entry(page)) {
1421 swap = radix_to_swp_entry(page);
1422 page = NULL;
1423 }
1424
Hugh Dickins75edd342016-05-19 17:12:44 -07001425 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001426 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001427 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001428 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001429 }
1430
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001431 if (page && sgp == SGP_WRITE)
1432 mark_page_accessed(page);
1433
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001434 /* fallocated page? */
1435 if (page && !PageUptodate(page)) {
1436 if (sgp != SGP_READ)
1437 goto clear;
1438 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001439 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001440 page = NULL;
1441 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001442 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001443 *pagep = page;
1444 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001448 * Fast cache lookup did not find it:
1449 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001451 info = SHMEM_I(inode);
1452 sbinfo = SHMEM_SB(inode->i_sb);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001453 charge_mm = fault_mm ? : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (swap.val) {
1456 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001457 page = lookup_swap_cache(swap);
1458 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001459 /* Or update major stats only when swapin succeeds?? */
1460 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001461 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001462 count_vm_event(PGMAJFAULT);
1463 mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT);
1464 }
1465 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001466 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001467 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001468 error = -ENOMEM;
1469 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
1473 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001474 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001475 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001476 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001477 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001478 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001479 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001480 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001482 goto failed;
1483 }
1484 wait_on_page_writeback(page);
1485
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001486 if (shmem_should_replace_page(page, gfp)) {
1487 error = shmem_replace_page(&page, gfp, info, index);
1488 if (error)
1489 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001492 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001493 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001494 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001495 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001496 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001497 /*
1498 * We already confirmed swap under page lock, and make
1499 * no memory allocation here, so usually no possibility
1500 * of error; but free_swap_and_cache() only trylocks a
1501 * page, so it is just possible that the entry has been
1502 * truncated or holepunched since swap was confirmed.
1503 * shmem_undo_range() will have done some of the
1504 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001505 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001506 * Reset swap.val? No, leave it so "failed" goes back to
1507 * "repeat": reading a hole and writing should succeed.
1508 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001509 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001510 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001511 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001512 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001513 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001514 if (error)
1515 goto failed;
1516
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001517 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001518
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001519 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001520 info->swapped--;
1521 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001522 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001523
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001524 if (sgp == SGP_WRITE)
1525 mark_page_accessed(page);
1526
Hugh Dickins27ab7002011-07-25 17:12:36 -07001527 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001528 set_page_dirty(page);
1529 swap_free(swap);
1530
Hugh Dickins54af60422011-08-03 16:21:24 -07001531 } else {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001532 /* shmem_symlink() */
1533 if (mapping->a_ops != &shmem_aops)
1534 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001535 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001536 goto alloc_nohuge;
1537 if (shmem_huge == SHMEM_HUGE_FORCE)
1538 goto alloc_huge;
1539 switch (sbinfo->huge) {
1540 loff_t i_size;
1541 pgoff_t off;
1542 case SHMEM_HUGE_NEVER:
1543 goto alloc_nohuge;
1544 case SHMEM_HUGE_WITHIN_SIZE:
1545 off = round_up(index, HPAGE_PMD_NR);
1546 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1547 if (i_size >= HPAGE_PMD_SIZE &&
1548 i_size >> PAGE_SHIFT >= off)
1549 goto alloc_huge;
1550 /* fallthrough */
1551 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001552 if (sgp_huge == SGP_HUGE)
1553 goto alloc_huge;
1554 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001555 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001558alloc_huge:
1559 page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1560 index, true);
1561 if (IS_ERR(page)) {
1562alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1563 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001564 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001565 if (IS_ERR(page)) {
1566 error = PTR_ERR(page);
1567 page = NULL;
1568 goto failed;
1569 }
1570
1571 if (PageTransHuge(page))
1572 hindex = round_down(index, HPAGE_PMD_NR);
1573 else
1574 hindex = index;
1575
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001576 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001577 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001578
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001579 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001580 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001581 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001582 goto unacct;
1583 error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1584 compound_order(page));
Hugh Dickinsb065b432012-07-11 14:02:48 -07001585 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001586 error = shmem_add_to_page_cache(page, mapping, hindex,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001587 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001588 radix_tree_preload_end();
1589 }
1590 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001591 mem_cgroup_cancel_charge(page, memcg,
1592 PageTransHuge(page));
1593 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001594 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001595 mem_cgroup_commit_charge(page, memcg, false,
1596 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001597 lru_cache_add_anon(page);
1598
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001599 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001600 info->alloced += 1 << compound_order(page);
1601 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001602 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001603 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001604 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001605
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001606 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001607 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1608 */
1609 if (sgp == SGP_FALLOC)
1610 sgp = SGP_WRITE;
1611clear:
1612 /*
1613 * Let SGP_WRITE caller clear ends if write does not fill page;
1614 * but SGP_FALLOC on a page fallocated earlier must initialize
1615 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001616 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001617 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1618 struct page *head = compound_head(page);
1619 int i;
1620
1621 for (i = 0; i < (1 << compound_order(head)); i++) {
1622 clear_highpage(head + i);
1623 flush_dcache_page(head + i);
1624 }
1625 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001626 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001627 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001628
Hugh Dickins54af60422011-08-03 16:21:24 -07001629 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001630 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001631 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001632 if (alloced) {
1633 ClearPageDirty(page);
1634 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001635 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001636 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001637 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001638 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001639 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001640 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001641 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001642 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001643 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001644
Nick Piggind0217ac2007-07-19 01:47:03 -07001645 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001646 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001648unacct:
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001649 if (sbinfo->max_blocks)
1650 percpu_counter_sub(&sbinfo->used_blocks,
1651 1 << compound_order(page));
1652 shmem_unacct_blocks(info->flags, 1 << compound_order(page));
1653
1654 if (PageTransHuge(page)) {
1655 unlock_page(page);
1656 put_page(page);
1657 goto alloc_nohuge;
1658 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001659failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001660 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001661 error = -EEXIST;
1662unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001663 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001664 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001665 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001666 }
1667 if (error == -ENOSPC && !once++) {
1668 info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001669 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001670 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001671 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001672 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001673 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001674 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001675 goto repeat;
1676 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1680{
Al Viro496ad9a2013-01-23 17:07:38 -05001681 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001682 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001683 enum sgp_type sgp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001685 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001687 /*
1688 * Trinity finds that probing a hole which tmpfs is punching can
1689 * prevent the hole-punch from ever completing: which in turn
1690 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001691 * faulting pages into the hole while it's being punched. Although
1692 * shmem_undo_range() does remove the additions, it may be unable to
1693 * keep up, as each new page needs its own unmap_mapping_range() call,
1694 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1695 *
1696 * It does not matter if we sometimes reach this check just before the
1697 * hole-punch begins, so that one fault then races with the punch:
1698 * we just need to make racing faults a rare case.
1699 *
1700 * The implementation below would be much simpler if we just used a
1701 * standard mutex or completion: but we cannot take i_mutex in fault,
1702 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001703 */
1704 if (unlikely(inode->i_private)) {
1705 struct shmem_falloc *shmem_falloc;
1706
1707 spin_lock(&inode->i_lock);
1708 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001709 if (shmem_falloc &&
1710 shmem_falloc->waitq &&
1711 vmf->pgoff >= shmem_falloc->start &&
1712 vmf->pgoff < shmem_falloc->next) {
1713 wait_queue_head_t *shmem_falloc_waitq;
1714 DEFINE_WAIT(shmem_fault_wait);
1715
1716 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001717 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1718 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001719 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001720 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001721 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001722 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001723
1724 shmem_falloc_waitq = shmem_falloc->waitq;
1725 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1726 TASK_UNINTERRUPTIBLE);
1727 spin_unlock(&inode->i_lock);
1728 schedule();
1729
1730 /*
1731 * shmem_falloc_waitq points into the shmem_fallocate()
1732 * stack of the hole-punching task: shmem_falloc_waitq
1733 * is usually invalid by the time we reach here, but
1734 * finish_wait() does not dereference it in that case;
1735 * though i_lock needed lest racing with wake_up_all().
1736 */
1737 spin_lock(&inode->i_lock);
1738 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1739 spin_unlock(&inode->i_lock);
1740 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001741 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001742 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001743 }
1744
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001745 sgp = SGP_CACHE;
1746 if (vma->vm_flags & VM_HUGEPAGE)
1747 sgp = SGP_HUGE;
1748 else if (vma->vm_flags & VM_NOHUGEPAGE)
1749 sgp = SGP_NOHUGE;
1750
1751 error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001752 gfp, vma->vm_mm, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (error)
1754 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001758unsigned long shmem_get_unmapped_area(struct file *file,
1759 unsigned long uaddr, unsigned long len,
1760 unsigned long pgoff, unsigned long flags)
1761{
1762 unsigned long (*get_area)(struct file *,
1763 unsigned long, unsigned long, unsigned long, unsigned long);
1764 unsigned long addr;
1765 unsigned long offset;
1766 unsigned long inflated_len;
1767 unsigned long inflated_addr;
1768 unsigned long inflated_offset;
1769
1770 if (len > TASK_SIZE)
1771 return -ENOMEM;
1772
1773 get_area = current->mm->get_unmapped_area;
1774 addr = get_area(file, uaddr, len, pgoff, flags);
1775
1776 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1777 return addr;
1778 if (IS_ERR_VALUE(addr))
1779 return addr;
1780 if (addr & ~PAGE_MASK)
1781 return addr;
1782 if (addr > TASK_SIZE - len)
1783 return addr;
1784
1785 if (shmem_huge == SHMEM_HUGE_DENY)
1786 return addr;
1787 if (len < HPAGE_PMD_SIZE)
1788 return addr;
1789 if (flags & MAP_FIXED)
1790 return addr;
1791 /*
1792 * Our priority is to support MAP_SHARED mapped hugely;
1793 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
1794 * But if caller specified an address hint, respect that as before.
1795 */
1796 if (uaddr)
1797 return addr;
1798
1799 if (shmem_huge != SHMEM_HUGE_FORCE) {
1800 struct super_block *sb;
1801
1802 if (file) {
1803 VM_BUG_ON(file->f_op != &shmem_file_operations);
1804 sb = file_inode(file)->i_sb;
1805 } else {
1806 /*
1807 * Called directly from mm/mmap.c, or drivers/char/mem.c
1808 * for "/dev/zero", to create a shared anonymous object.
1809 */
1810 if (IS_ERR(shm_mnt))
1811 return addr;
1812 sb = shm_mnt->mnt_sb;
1813 }
1814 if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER)
1815 return addr;
1816 }
1817
1818 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
1819 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
1820 return addr;
1821 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
1822 return addr;
1823
1824 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
1825 if (inflated_len > TASK_SIZE)
1826 return addr;
1827 if (inflated_len < len)
1828 return addr;
1829
1830 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
1831 if (IS_ERR_VALUE(inflated_addr))
1832 return addr;
1833 if (inflated_addr & ~PAGE_MASK)
1834 return addr;
1835
1836 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
1837 inflated_addr += offset - inflated_offset;
1838 if (inflated_offset > offset)
1839 inflated_addr += HPAGE_PMD_SIZE;
1840
1841 if (inflated_addr > TASK_SIZE - len)
1842 return addr;
1843 return inflated_addr;
1844}
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001847static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Al Viro496ad9a2013-01-23 17:07:38 -05001849 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001850 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001853static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1854 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Al Viro496ad9a2013-01-23 17:07:38 -05001856 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001857 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001859 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1860 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862#endif
1863
1864int shmem_lock(struct file *file, int lock, struct user_struct *user)
1865{
Al Viro496ad9a2013-01-23 17:07:38 -05001866 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct shmem_inode_info *info = SHMEM_I(inode);
1868 int retval = -ENOMEM;
1869
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001870 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (lock && !(info->flags & VM_LOCKED)) {
1872 if (!user_shm_lock(inode->i_size, user))
1873 goto out_nomem;
1874 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001875 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 if (!lock && (info->flags & VM_LOCKED) && user) {
1878 user_shm_unlock(inode->i_size, user);
1879 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001880 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001885 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return retval;
1887}
1888
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001889static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 file_accessed(file);
1892 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001893 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
1894 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
1895 (vma->vm_end & HPAGE_PMD_MASK)) {
1896 khugepaged_enter(vma, vma->vm_flags);
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return 0;
1899}
1900
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001901static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001902 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 struct inode *inode;
1905 struct shmem_inode_info *info;
1906 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1907
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001908 if (shmem_reserve_inode(sb))
1909 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 inode = new_inode(sb);
1912 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001913 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001914 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001917 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 info = SHMEM_I(inode);
1919 memset(info, 0, (char *)inode - (char *)info);
1920 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07001921 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001922 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001924 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04001925 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 switch (mode & S_IFMT) {
1928 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001929 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 init_special_inode(inode, mode, dev);
1931 break;
1932 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001933 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 inode->i_op = &shmem_inode_operations;
1935 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001936 mpol_shared_policy_init(&info->policy,
1937 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 break;
1939 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001940 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Some things misbehave if size == 0 on a directory */
1942 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1943 inode->i_op = &shmem_dir_inode_operations;
1944 inode->i_fop = &simple_dir_operations;
1945 break;
1946 case S_IFLNK:
1947 /*
1948 * Must not load anything in the rbtree,
1949 * mpol_free_shared_policy will not be called.
1950 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001951 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001954 } else
1955 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return inode;
1957}
1958
Johannes Weiner0cd61442014-04-03 14:47:46 -07001959bool shmem_mapping(struct address_space *mapping)
1960{
Sasha Levinf0774d82015-02-23 05:38:00 -05001961 if (!mapping->host)
1962 return false;
1963
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001964 return mapping->host->i_sb->s_op == &shmem_ops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001968static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001969static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001971#ifdef CONFIG_TMPFS_XATTR
1972static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1973#else
1974#define shmem_initxattrs NULL
1975#endif
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001978shmem_write_begin(struct file *file, struct address_space *mapping,
1979 loff_t pos, unsigned len, unsigned flags,
1980 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Nick Piggin800d15a2007-10-16 01:25:03 -07001982 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07001983 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001984 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07001985
1986 /* i_mutex is held by caller */
1987 if (unlikely(info->seals)) {
1988 if (info->seals & F_SEAL_WRITE)
1989 return -EPERM;
1990 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1991 return -EPERM;
1992 }
1993
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001994 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07001995}
1996
1997static int
1998shmem_write_end(struct file *file, struct address_space *mapping,
1999 loff_t pos, unsigned len, unsigned copied,
2000 struct page *page, void *fsdata)
2001{
2002 struct inode *inode = mapping->host;
2003
Hugh Dickinsd3602442008-02-04 22:28:44 -08002004 if (pos + copied > inode->i_size)
2005 i_size_write(inode, pos + copied);
2006
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002007 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002008 struct page *head = compound_head(page);
2009 if (PageTransCompound(page)) {
2010 int i;
2011
2012 for (i = 0; i < HPAGE_PMD_NR; i++) {
2013 if (head + i == page)
2014 continue;
2015 clear_highpage(head + i);
2016 flush_dcache_page(head + i);
2017 }
2018 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002019 if (copied < PAGE_SIZE) {
2020 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002021 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002022 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002023 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002024 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002025 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002026 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002027 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002029
Nick Piggin800d15a2007-10-16 01:25:03 -07002030 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Al Viro2ba5bbe2014-04-02 20:00:02 -04002033static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Al Viro6e58e792014-02-03 17:07:03 -05002035 struct file *file = iocb->ki_filp;
2036 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002038 pgoff_t index;
2039 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002040 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002041 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002042 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002043 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002044
2045 /*
2046 * Might this read be for a stacking filesystem? Then when reading
2047 * holes of a sparse file, we actually need to allocate those pages,
2048 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2049 */
Al Viro777eda22014-12-17 04:46:46 -05002050 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002051 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002053 index = *ppos >> PAGE_SHIFT;
2054 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 for (;;) {
2057 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002058 pgoff_t end_index;
2059 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 loff_t i_size = i_size_read(inode);
2061
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (index > end_index)
2064 break;
2065 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (nr <= offset)
2068 break;
2069 }
2070
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002071 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002072 if (error) {
2073 if (error == -EINVAL)
2074 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 break;
2076 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002077 if (page) {
2078 if (sgp == SGP_CACHE)
2079 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002080 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /*
2084 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002085 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002087 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002091 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (nr <= offset) {
2093 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002094 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 break;
2096 }
2097 }
2098 nr -= offset;
2099
2100 if (page) {
2101 /*
2102 * If users can be writing to this page using arbitrary
2103 * virtual addresses, take care about potential aliasing
2104 * before reading the page on the kernel side.
2105 */
2106 if (mapping_writably_mapped(mapping))
2107 flush_dcache_page(page);
2108 /*
2109 * Mark the page accessed if we read the beginning.
2110 */
2111 if (!offset)
2112 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002115 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /*
2119 * Ok, we have the page, and it's up-to-date, so
2120 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002122 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002123 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 index += offset >> PAGE_SHIFT;
2126 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002128 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002129 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 break;
Al Viro6e58e792014-02-03 17:07:03 -05002131 if (ret < nr) {
2132 error = -EFAULT;
2133 break;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 cond_resched();
2136 }
2137
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002138 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002139 file_accessed(file);
2140 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Hugh Dickins708e3502011-07-25 17:12:32 -07002143static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
2144 struct pipe_inode_info *pipe, size_t len,
2145 unsigned int flags)
2146{
2147 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002148 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07002149 unsigned int loff, nr_pages, req_pages;
2150 struct page *pages[PIPE_DEF_BUFFERS];
2151 struct partial_page partial[PIPE_DEF_BUFFERS];
2152 struct page *page;
2153 pgoff_t index, end_index;
2154 loff_t isize, left;
2155 int error, page_nr;
2156 struct splice_pipe_desc spd = {
2157 .pages = pages,
2158 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002159 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07002160 .flags = flags,
2161 .ops = &page_cache_pipe_buf_ops,
2162 .spd_release = spd_release_page,
2163 };
2164
Hugh Dickins71f0e072011-07-25 17:12:33 -07002165 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07002166 if (unlikely(*ppos >= isize))
2167 return 0;
2168
2169 left = isize - *ppos;
2170 if (unlikely(left < len))
2171 len = left;
2172
2173 if (splice_grow_spd(pipe, &spd))
2174 return -ENOMEM;
2175
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002176 index = *ppos >> PAGE_SHIFT;
2177 loff = *ppos & ~PAGE_MASK;
2178 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Viroa786c062014-04-11 12:01:03 -04002179 nr_pages = min(req_pages, spd.nr_pages_max);
Hugh Dickins708e3502011-07-25 17:12:32 -07002180
Hugh Dickins708e3502011-07-25 17:12:32 -07002181 spd.nr_pages = find_get_pages_contig(mapping, index,
2182 nr_pages, spd.pages);
2183 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07002184 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002185
Hugh Dickins708e3502011-07-25 17:12:32 -07002186 while (spd.nr_pages < nr_pages) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002187 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002188 if (error)
2189 break;
2190 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07002191 spd.pages[spd.nr_pages++] = page;
2192 index++;
2193 }
2194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 index = *ppos >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002196 nr_pages = spd.nr_pages;
2197 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002198
Hugh Dickins708e3502011-07-25 17:12:32 -07002199 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
2200 unsigned int this_len;
2201
2202 if (!len)
2203 break;
2204
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002205 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
Hugh Dickins708e3502011-07-25 17:12:32 -07002206 page = spd.pages[page_nr];
2207
Hugh Dickins71f0e072011-07-25 17:12:33 -07002208 if (!PageUptodate(page) || page->mapping != mapping) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002209 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002210 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07002211 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002212 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002213 put_page(spd.pages[page_nr]);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002214 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07002215 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07002216
2217 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002218 end_index = (isize - 1) >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002219 if (unlikely(!isize || index > end_index))
2220 break;
2221
Hugh Dickins708e3502011-07-25 17:12:32 -07002222 if (end_index == index) {
2223 unsigned int plen;
2224
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002225 plen = ((isize - 1) & ~PAGE_MASK) + 1;
Hugh Dickins708e3502011-07-25 17:12:32 -07002226 if (plen <= loff)
2227 break;
2228
Hugh Dickins708e3502011-07-25 17:12:32 -07002229 this_len = min(this_len, plen - loff);
2230 len = this_len;
2231 }
2232
2233 spd.partial[page_nr].offset = loff;
2234 spd.partial[page_nr].len = this_len;
2235 len -= this_len;
2236 loff = 0;
2237 spd.nr_pages++;
2238 index++;
2239 }
2240
Hugh Dickins708e3502011-07-25 17:12:32 -07002241 while (page_nr < nr_pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002242 put_page(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07002243
2244 if (spd.nr_pages)
2245 error = splice_to_pipe(pipe, &spd);
2246
Eric Dumazet047fe362012-06-12 15:24:40 +02002247 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07002248
2249 if (error > 0) {
2250 *ppos += error;
2251 file_accessed(in);
2252 }
2253 return error;
2254}
2255
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002256/*
2257 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2258 */
2259static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002260 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002261{
2262 struct page *page;
2263 struct pagevec pvec;
2264 pgoff_t indices[PAGEVEC_SIZE];
2265 bool done = false;
2266 int i;
2267
2268 pagevec_init(&pvec, 0);
2269 pvec.nr = 1; /* start small: we may be there already */
2270 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002271 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002272 pvec.nr, pvec.pages, indices);
2273 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002274 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002275 index = end;
2276 break;
2277 }
2278 for (i = 0; i < pvec.nr; i++, index++) {
2279 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002280 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002281 done = true;
2282 break;
2283 }
2284 index = indices[i];
2285 }
2286 page = pvec.pages[i];
2287 if (page && !radix_tree_exceptional_entry(page)) {
2288 if (!PageUptodate(page))
2289 page = NULL;
2290 }
2291 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002292 (page && whence == SEEK_DATA) ||
2293 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002294 done = true;
2295 break;
2296 }
2297 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002298 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002299 pagevec_release(&pvec);
2300 pvec.nr = PAGEVEC_SIZE;
2301 cond_resched();
2302 }
2303 return index;
2304}
2305
Andrew Morton965c8e52012-12-17 15:59:39 -08002306static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002307{
2308 struct address_space *mapping = file->f_mapping;
2309 struct inode *inode = mapping->host;
2310 pgoff_t start, end;
2311 loff_t new_offset;
2312
Andrew Morton965c8e52012-12-17 15:59:39 -08002313 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2314 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002315 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002316 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002317 /* We're holding i_mutex so we can access i_size directly */
2318
2319 if (offset < 0)
2320 offset = -EINVAL;
2321 else if (offset >= inode->i_size)
2322 offset = -ENXIO;
2323 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002324 start = offset >> PAGE_SHIFT;
2325 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002326 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002327 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002328 if (new_offset > offset) {
2329 if (new_offset < inode->i_size)
2330 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002331 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002332 offset = -ENXIO;
2333 else
2334 offset = inode->i_size;
2335 }
2336 }
2337
Hugh Dickins387aae62013-08-04 11:30:25 -07002338 if (offset >= 0)
2339 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002340 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002341 return offset;
2342}
2343
David Herrmann05f65b52014-08-08 14:25:36 -07002344/*
2345 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
2346 * so reuse a tag which we firmly believe is never set or cleared on shmem.
2347 */
2348#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
2349#define LAST_SCAN 4 /* about 150ms max */
2350
2351static void shmem_tag_pins(struct address_space *mapping)
2352{
2353 struct radix_tree_iter iter;
2354 void **slot;
2355 pgoff_t start;
2356 struct page *page;
2357
2358 lru_add_drain();
2359 start = 0;
2360 rcu_read_lock();
2361
David Herrmann05f65b52014-08-08 14:25:36 -07002362 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
2363 page = radix_tree_deref_slot(slot);
2364 if (!page || radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002365 if (radix_tree_deref_retry(page)) {
2366 slot = radix_tree_iter_retry(&iter);
2367 continue;
2368 }
David Herrmann05f65b52014-08-08 14:25:36 -07002369 } else if (page_count(page) - page_mapcount(page) > 1) {
2370 spin_lock_irq(&mapping->tree_lock);
2371 radix_tree_tag_set(&mapping->page_tree, iter.index,
2372 SHMEM_TAG_PINNED);
2373 spin_unlock_irq(&mapping->tree_lock);
2374 }
2375
2376 if (need_resched()) {
2377 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002378 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002379 }
2380 }
2381 rcu_read_unlock();
2382}
2383
2384/*
2385 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
2386 * via get_user_pages(), drivers might have some pending I/O without any active
2387 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
2388 * and see whether it has an elevated ref-count. If so, we tag them and wait for
2389 * them to be dropped.
2390 * The caller must guarantee that no new user will acquire writable references
2391 * to those pages to avoid races.
2392 */
David Herrmann40e041a2014-08-08 14:25:27 -07002393static int shmem_wait_for_pins(struct address_space *mapping)
2394{
David Herrmann05f65b52014-08-08 14:25:36 -07002395 struct radix_tree_iter iter;
2396 void **slot;
2397 pgoff_t start;
2398 struct page *page;
2399 int error, scan;
2400
2401 shmem_tag_pins(mapping);
2402
2403 error = 0;
2404 for (scan = 0; scan <= LAST_SCAN; scan++) {
2405 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
2406 break;
2407
2408 if (!scan)
2409 lru_add_drain_all();
2410 else if (schedule_timeout_killable((HZ << scan) / 200))
2411 scan = LAST_SCAN;
2412
2413 start = 0;
2414 rcu_read_lock();
David Herrmann05f65b52014-08-08 14:25:36 -07002415 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
2416 start, SHMEM_TAG_PINNED) {
2417
2418 page = radix_tree_deref_slot(slot);
2419 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002420 if (radix_tree_deref_retry(page)) {
2421 slot = radix_tree_iter_retry(&iter);
2422 continue;
2423 }
David Herrmann05f65b52014-08-08 14:25:36 -07002424
2425 page = NULL;
2426 }
2427
2428 if (page &&
2429 page_count(page) - page_mapcount(page) != 1) {
2430 if (scan < LAST_SCAN)
2431 goto continue_resched;
2432
2433 /*
2434 * On the last scan, we clean up all those tags
2435 * we inserted; but make a note that we still
2436 * found pages pinned.
2437 */
2438 error = -EBUSY;
2439 }
2440
2441 spin_lock_irq(&mapping->tree_lock);
2442 radix_tree_tag_clear(&mapping->page_tree,
2443 iter.index, SHMEM_TAG_PINNED);
2444 spin_unlock_irq(&mapping->tree_lock);
2445continue_resched:
2446 if (need_resched()) {
2447 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002448 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002449 }
2450 }
2451 rcu_read_unlock();
2452 }
2453
2454 return error;
David Herrmann40e041a2014-08-08 14:25:27 -07002455}
2456
2457#define F_ALL_SEALS (F_SEAL_SEAL | \
2458 F_SEAL_SHRINK | \
2459 F_SEAL_GROW | \
2460 F_SEAL_WRITE)
2461
2462int shmem_add_seals(struct file *file, unsigned int seals)
2463{
2464 struct inode *inode = file_inode(file);
2465 struct shmem_inode_info *info = SHMEM_I(inode);
2466 int error;
2467
2468 /*
2469 * SEALING
2470 * Sealing allows multiple parties to share a shmem-file but restrict
2471 * access to a specific subset of file operations. Seals can only be
2472 * added, but never removed. This way, mutually untrusted parties can
2473 * share common memory regions with a well-defined policy. A malicious
2474 * peer can thus never perform unwanted operations on a shared object.
2475 *
2476 * Seals are only supported on special shmem-files and always affect
2477 * the whole underlying inode. Once a seal is set, it may prevent some
2478 * kinds of access to the file. Currently, the following seals are
2479 * defined:
2480 * SEAL_SEAL: Prevent further seals from being set on this file
2481 * SEAL_SHRINK: Prevent the file from shrinking
2482 * SEAL_GROW: Prevent the file from growing
2483 * SEAL_WRITE: Prevent write access to the file
2484 *
2485 * As we don't require any trust relationship between two parties, we
2486 * must prevent seals from being removed. Therefore, sealing a file
2487 * only adds a given set of seals to the file, it never touches
2488 * existing seals. Furthermore, the "setting seals"-operation can be
2489 * sealed itself, which basically prevents any further seal from being
2490 * added.
2491 *
2492 * Semantics of sealing are only defined on volatile files. Only
2493 * anonymous shmem files support sealing. More importantly, seals are
2494 * never written to disk. Therefore, there's no plan to support it on
2495 * other file types.
2496 */
2497
2498 if (file->f_op != &shmem_file_operations)
2499 return -EINVAL;
2500 if (!(file->f_mode & FMODE_WRITE))
2501 return -EPERM;
2502 if (seals & ~(unsigned int)F_ALL_SEALS)
2503 return -EINVAL;
2504
Al Viro59551022016-01-22 15:40:57 -05002505 inode_lock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002506
2507 if (info->seals & F_SEAL_SEAL) {
2508 error = -EPERM;
2509 goto unlock;
2510 }
2511
2512 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2513 error = mapping_deny_writable(file->f_mapping);
2514 if (error)
2515 goto unlock;
2516
2517 error = shmem_wait_for_pins(file->f_mapping);
2518 if (error) {
2519 mapping_allow_writable(file->f_mapping);
2520 goto unlock;
2521 }
2522 }
2523
2524 info->seals |= seals;
2525 error = 0;
2526
2527unlock:
Al Viro59551022016-01-22 15:40:57 -05002528 inode_unlock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002529 return error;
2530}
2531EXPORT_SYMBOL_GPL(shmem_add_seals);
2532
2533int shmem_get_seals(struct file *file)
2534{
2535 if (file->f_op != &shmem_file_operations)
2536 return -EINVAL;
2537
2538 return SHMEM_I(file_inode(file))->seals;
2539}
2540EXPORT_SYMBOL_GPL(shmem_get_seals);
2541
2542long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2543{
2544 long error;
2545
2546 switch (cmd) {
2547 case F_ADD_SEALS:
2548 /* disallow upper 32bit */
2549 if (arg > UINT_MAX)
2550 return -EINVAL;
2551
2552 error = shmem_add_seals(file, arg);
2553 break;
2554 case F_GET_SEALS:
2555 error = shmem_get_seals(file);
2556 break;
2557 default:
2558 error = -EINVAL;
2559 break;
2560 }
2561
2562 return error;
2563}
2564
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002565static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2566 loff_t len)
2567{
Al Viro496ad9a2013-01-23 17:07:38 -05002568 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002569 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002570 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002571 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002572 pgoff_t start, index, end;
2573 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002574
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002575 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2576 return -EOPNOTSUPP;
2577
Al Viro59551022016-01-22 15:40:57 -05002578 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002579
2580 if (mode & FALLOC_FL_PUNCH_HOLE) {
2581 struct address_space *mapping = file->f_mapping;
2582 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2583 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002584 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002585
David Herrmann40e041a2014-08-08 14:25:27 -07002586 /* protected by i_mutex */
2587 if (info->seals & F_SEAL_WRITE) {
2588 error = -EPERM;
2589 goto out;
2590 }
2591
Hugh Dickins8e205f72014-07-23 14:00:10 -07002592 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002593 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2594 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2595 spin_lock(&inode->i_lock);
2596 inode->i_private = &shmem_falloc;
2597 spin_unlock(&inode->i_lock);
2598
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002599 if ((u64)unmap_end > (u64)unmap_start)
2600 unmap_mapping_range(mapping, unmap_start,
2601 1 + unmap_end - unmap_start, 0);
2602 shmem_truncate_range(inode, offset, offset + len - 1);
2603 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002604
2605 spin_lock(&inode->i_lock);
2606 inode->i_private = NULL;
2607 wake_up_all(&shmem_falloc_waitq);
2608 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002609 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002610 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002611 }
2612
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002613 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2614 error = inode_newsize_ok(inode, offset + len);
2615 if (error)
2616 goto out;
2617
David Herrmann40e041a2014-08-08 14:25:27 -07002618 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2619 error = -EPERM;
2620 goto out;
2621 }
2622
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002623 start = offset >> PAGE_SHIFT;
2624 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002625 /* Try to avoid a swapstorm if len is impossible to satisfy */
2626 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2627 error = -ENOSPC;
2628 goto out;
2629 }
2630
Hugh Dickins8e205f72014-07-23 14:00:10 -07002631 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002632 shmem_falloc.start = start;
2633 shmem_falloc.next = start;
2634 shmem_falloc.nr_falloced = 0;
2635 shmem_falloc.nr_unswapped = 0;
2636 spin_lock(&inode->i_lock);
2637 inode->i_private = &shmem_falloc;
2638 spin_unlock(&inode->i_lock);
2639
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002640 for (index = start; index < end; index++) {
2641 struct page *page;
2642
2643 /*
2644 * Good, the fallocate(2) manpage permits EINTR: we may have
2645 * been interrupted because we are using up too much memory.
2646 */
2647 if (signal_pending(current))
2648 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002649 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2650 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002651 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002652 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002653 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002654 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002655 if (index > start) {
2656 shmem_undo_range(inode,
2657 (loff_t)start << PAGE_SHIFT,
2658 ((loff_t)index << PAGE_SHIFT) - 1, true);
2659 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002660 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002661 }
2662
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002663 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002664 * Inform shmem_writepage() how far we have reached.
2665 * No need for lock or barrier: we have the page lock.
2666 */
2667 shmem_falloc.next++;
2668 if (!PageUptodate(page))
2669 shmem_falloc.nr_falloced++;
2670
2671 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002672 * If !PageUptodate, leave it that way so that freeable pages
2673 * can be recognized if we need to rollback on error later.
2674 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002675 * than free the pages we are allocating (and SGP_CACHE pages
2676 * might still be clean: we now need to mark those dirty too).
2677 */
2678 set_page_dirty(page);
2679 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002680 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002681 cond_resched();
2682 }
2683
2684 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2685 i_size_write(inode, offset + len);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002686 inode->i_ctime = CURRENT_TIME;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002687undone:
2688 spin_lock(&inode->i_lock);
2689 inode->i_private = NULL;
2690 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002691out:
Al Viro59551022016-01-22 15:40:57 -05002692 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002693 return error;
2694}
2695
David Howells726c3342006-06-23 02:02:58 -07002696static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
David Howells726c3342006-06-23 02:02:58 -07002698 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002701 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002703 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002705 buf->f_bavail =
2706 buf->f_bfree = sbinfo->max_blocks -
2707 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002708 }
2709 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 buf->f_files = sbinfo->max_inodes;
2711 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
2713 /* else leave those fields 0 like simple_statfs */
2714 return 0;
2715}
2716
2717/*
2718 * File creation. Allocate an inode, and we're done..
2719 */
2720static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002721shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002723 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 int error = -ENOSPC;
2725
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002726 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002728 error = simple_acl_create(dir, inode);
2729 if (error)
2730 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002731 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002732 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002733 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002734 if (error && error != -EOPNOTSUPP)
2735 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002736
Al Viro718deb62009-12-16 19:35:36 -05002737 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 dir->i_size += BOGO_DIRENT_SIZE;
2739 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2740 d_instantiate(dentry, inode);
2741 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002744out_iput:
2745 iput(inode);
2746 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Al Viro60545d02013-06-07 01:20:27 -04002749static int
2750shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2751{
2752 struct inode *inode;
2753 int error = -ENOSPC;
2754
2755 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2756 if (inode) {
2757 error = security_inode_init_security(inode, dir,
2758 NULL,
2759 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002760 if (error && error != -EOPNOTSUPP)
2761 goto out_iput;
2762 error = simple_acl_create(dir, inode);
2763 if (error)
2764 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002765 d_tmpfile(dentry, inode);
2766 }
2767 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002768out_iput:
2769 iput(inode);
2770 return error;
Al Viro60545d02013-06-07 01:20:27 -04002771}
2772
Al Viro18bb1db2011-07-26 01:41:39 -04002773static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 int error;
2776
2777 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2778 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002779 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return 0;
2781}
2782
Al Viro4acdaf22011-07-26 01:42:34 -04002783static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002784 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
2786 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2787}
2788
2789/*
2790 * Link a file..
2791 */
2792static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2793{
David Howells75c3cfa2015-03-17 22:26:12 +00002794 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002795 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 /*
2798 * No ordinary (disk based) filesystem counts links as inodes;
2799 * but each new link needs a new dentry, pinning lowmem, and
2800 * tmpfs dentries cannot be pruned until they are unlinked.
2801 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002802 ret = shmem_reserve_inode(inode->i_sb);
2803 if (ret)
2804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 dir->i_size += BOGO_DIRENT_SIZE;
2807 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07002808 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002809 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 dget(dentry); /* Extra pinning count for the created dentry */
2811 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002812out:
2813 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2817{
David Howells75c3cfa2015-03-17 22:26:12 +00002818 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002820 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2821 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 dir->i_size -= BOGO_DIRENT_SIZE;
2824 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002825 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 dput(dentry); /* Undo the count from "create" - this does all the work */
2827 return 0;
2828}
2829
2830static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2831{
2832 if (!simple_empty(dentry))
2833 return -ENOTEMPTY;
2834
David Howells75c3cfa2015-03-17 22:26:12 +00002835 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002836 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return shmem_unlink(dir, dentry);
2838}
2839
Miklos Szeredi37456772014-07-23 15:15:34 +02002840static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2841{
David Howellse36cb0b2015-01-29 12:02:35 +00002842 bool old_is_dir = d_is_dir(old_dentry);
2843 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002844
2845 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2846 if (old_is_dir) {
2847 drop_nlink(old_dir);
2848 inc_nlink(new_dir);
2849 } else {
2850 drop_nlink(new_dir);
2851 inc_nlink(old_dir);
2852 }
2853 }
2854 old_dir->i_ctime = old_dir->i_mtime =
2855 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002856 d_inode(old_dentry)->i_ctime =
2857 d_inode(new_dentry)->i_ctime = CURRENT_TIME;
Miklos Szeredi37456772014-07-23 15:15:34 +02002858
2859 return 0;
2860}
2861
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002862static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2863{
2864 struct dentry *whiteout;
2865 int error;
2866
2867 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2868 if (!whiteout)
2869 return -ENOMEM;
2870
2871 error = shmem_mknod(old_dir, whiteout,
2872 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2873 dput(whiteout);
2874 if (error)
2875 return error;
2876
2877 /*
2878 * Cheat and hash the whiteout while the old dentry is still in
2879 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2880 *
2881 * d_lookup() will consistently find one of them at this point,
2882 * not sure which one, but that isn't even important.
2883 */
2884 d_rehash(whiteout);
2885 return 0;
2886}
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888/*
2889 * The VFS layer already does all the dentry stuff for rename,
2890 * we just have to decrement the usage count for the target if
2891 * it exists so that the VFS layer correctly free's it when it
2892 * gets overwritten.
2893 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002894static 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 -07002895{
David Howells75c3cfa2015-03-17 22:26:12 +00002896 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 int they_are_dirs = S_ISDIR(inode->i_mode);
2898
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002899 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002900 return -EINVAL;
2901
Miklos Szeredi37456772014-07-23 15:15:34 +02002902 if (flags & RENAME_EXCHANGE)
2903 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (!simple_empty(new_dentry))
2906 return -ENOTEMPTY;
2907
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002908 if (flags & RENAME_WHITEOUT) {
2909 int error;
2910
2911 error = shmem_whiteout(old_dir, old_dentry);
2912 if (error)
2913 return error;
2914 }
2915
David Howells75c3cfa2015-03-17 22:26:12 +00002916 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002918 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002919 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002920 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002923 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002924 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926
2927 old_dir->i_size -= BOGO_DIRENT_SIZE;
2928 new_dir->i_size += BOGO_DIRENT_SIZE;
2929 old_dir->i_ctime = old_dir->i_mtime =
2930 new_dir->i_ctime = new_dir->i_mtime =
2931 inode->i_ctime = CURRENT_TIME;
2932 return 0;
2933}
2934
2935static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2936{
2937 int error;
2938 int len;
2939 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002940 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 struct shmem_inode_info *info;
2942
2943 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002944 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return -ENAMETOOLONG;
2946
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002947 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (!inode)
2949 return -ENOSPC;
2950
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002951 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002952 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002953 if (error) {
2954 if (error != -EOPNOTSUPP) {
2955 iput(inode);
2956 return error;
2957 }
2958 error = 0;
2959 }
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 info = SHMEM_I(inode);
2962 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002963 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05002964 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
2965 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002966 iput(inode);
2967 return -ENOMEM;
2968 }
2969 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05002971 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002972 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (error) {
2974 iput(inode);
2975 return error;
2976 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07002977 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05002979 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002980 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002982 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002983 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 dir->i_size += BOGO_DIRENT_SIZE;
2986 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2987 d_instantiate(dentry, inode);
2988 dget(dentry);
2989 return 0;
2990}
2991
Al Virofceef392015-12-29 15:58:39 -05002992static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Al Virofceef392015-12-29 15:58:39 -05002994 mark_page_accessed(arg);
2995 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996}
2997
Al Viro6b255392015-11-17 10:20:54 -05002998static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002999 struct inode *inode,
3000 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003003 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003004 if (!dentry) {
3005 page = find_get_page(inode->i_mapping, 0);
3006 if (!page)
3007 return ERR_PTR(-ECHILD);
3008 if (!PageUptodate(page)) {
3009 put_page(page);
3010 return ERR_PTR(-ECHILD);
3011 }
3012 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003013 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003014 if (error)
3015 return ERR_PTR(error);
3016 unlock_page(page);
3017 }
Al Virofceef392015-12-29 15:58:39 -05003018 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003019 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
Eric Parisb09e0fa2011-05-24 17:12:39 -07003022#ifdef CONFIG_TMPFS_XATTR
3023/*
3024 * Superblocks without xattr inode operations may get some security.* xattr
3025 * support from the LSM "for free". As soon as we have any other xattrs
3026 * like ACLs, we also need to implement the security.* handlers at
3027 * filesystem level, though.
3028 */
3029
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003030/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003031 * Callback for security_inode_init_security() for acquiring xattrs.
3032 */
3033static int shmem_initxattrs(struct inode *inode,
3034 const struct xattr *xattr_array,
3035 void *fs_info)
3036{
3037 struct shmem_inode_info *info = SHMEM_I(inode);
3038 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003039 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003040 size_t len;
3041
3042 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003043 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003044 if (!new_xattr)
3045 return -ENOMEM;
3046
3047 len = strlen(xattr->name) + 1;
3048 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3049 GFP_KERNEL);
3050 if (!new_xattr->name) {
3051 kfree(new_xattr);
3052 return -ENOMEM;
3053 }
3054
3055 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3056 XATTR_SECURITY_PREFIX_LEN);
3057 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3058 xattr->name, len);
3059
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003060 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003061 }
3062
3063 return 0;
3064}
3065
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003066static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003067 struct dentry *unused, struct inode *inode,
3068 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003069{
Al Virob2968212016-04-10 20:48:24 -04003070 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003071
3072 name = xattr_full_name(handler, name);
3073 return simple_xattr_get(&info->xattrs, name, buffer, size);
3074}
3075
3076static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003077 struct dentry *unused, struct inode *inode,
3078 const char *name, const void *value,
3079 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003080{
Al Viro59301222016-05-27 10:19:30 -04003081 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003082
3083 name = xattr_full_name(handler, name);
3084 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3085}
3086
3087static const struct xattr_handler shmem_security_xattr_handler = {
3088 .prefix = XATTR_SECURITY_PREFIX,
3089 .get = shmem_xattr_handler_get,
3090 .set = shmem_xattr_handler_set,
3091};
3092
3093static const struct xattr_handler shmem_trusted_xattr_handler = {
3094 .prefix = XATTR_TRUSTED_PREFIX,
3095 .get = shmem_xattr_handler_get,
3096 .set = shmem_xattr_handler_set,
3097};
3098
Eric Parisb09e0fa2011-05-24 17:12:39 -07003099static const struct xattr_handler *shmem_xattr_handlers[] = {
3100#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003101 &posix_acl_access_xattr_handler,
3102 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003103#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003104 &shmem_security_xattr_handler,
3105 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003106 NULL
3107};
3108
Eric Parisb09e0fa2011-05-24 17:12:39 -07003109static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3110{
David Howells75c3cfa2015-03-17 22:26:12 +00003111 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003112 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003113}
3114#endif /* CONFIG_TMPFS_XATTR */
3115
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003116static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003118 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003119#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003120 .setxattr = generic_setxattr,
3121 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003122 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003123 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125};
3126
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003127static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003129 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003130#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003131 .setxattr = generic_setxattr,
3132 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003133 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003134 .removexattr = generic_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003135#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003136};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003137
David M. Grimes91828a42006-10-17 00:09:45 -07003138static struct dentry *shmem_get_parent(struct dentry *child)
3139{
3140 return ERR_PTR(-ESTALE);
3141}
3142
3143static int shmem_match(struct inode *ino, void *vfh)
3144{
3145 __u32 *fh = vfh;
3146 __u64 inum = fh[2];
3147 inum = (inum << 32) | fh[1];
3148 return ino->i_ino == inum && fh[0] == ino->i_generation;
3149}
3150
Christoph Hellwig480b1162007-10-21 16:42:13 -07003151static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3152 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003153{
David M. Grimes91828a42006-10-17 00:09:45 -07003154 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003155 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003156 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003157
Christoph Hellwig480b1162007-10-21 16:42:13 -07003158 if (fh_len < 3)
3159 return NULL;
3160
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003161 inum = fid->raw[2];
3162 inum = (inum << 32) | fid->raw[1];
3163
Christoph Hellwig480b1162007-10-21 16:42:13 -07003164 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3165 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003166 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07003167 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003168 iput(inode);
3169 }
3170
Christoph Hellwig480b1162007-10-21 16:42:13 -07003171 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003172}
3173
Al Virob0b03822012-04-02 14:34:06 -04003174static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3175 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003176{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303177 if (*len < 3) {
3178 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003179 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303180 }
David M. Grimes91828a42006-10-17 00:09:45 -07003181
Al Viro1d3382cb2010-10-23 15:19:20 -04003182 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003183 /* Unfortunately insert_inode_hash is not idempotent,
3184 * so as we hash inodes here rather than at creation
3185 * time, we need a lock to ensure we only try
3186 * to do it once
3187 */
3188 static DEFINE_SPINLOCK(lock);
3189 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003190 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003191 __insert_inode_hash(inode,
3192 inode->i_ino + inode->i_generation);
3193 spin_unlock(&lock);
3194 }
3195
3196 fh[0] = inode->i_generation;
3197 fh[1] = inode->i_ino;
3198 fh[2] = ((__u64)inode->i_ino) >> 32;
3199
3200 *len = 3;
3201 return 1;
3202}
3203
Christoph Hellwig39655162007-10-21 16:42:17 -07003204static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003205 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003206 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003207 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003208};
3209
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003210static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3211 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003214 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003215 uid_t uid;
3216 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003218 while (options != NULL) {
3219 this_char = options;
3220 for (;;) {
3221 /*
3222 * NUL-terminate this option: unfortunately,
3223 * mount options form a comma-separated list,
3224 * but mpol's nodelist may also contain commas.
3225 */
3226 options = strchr(options, ',');
3227 if (options == NULL)
3228 break;
3229 options++;
3230 if (!isdigit(*options)) {
3231 options[-1] = '\0';
3232 break;
3233 }
3234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (!*this_char)
3236 continue;
3237 if ((value = strchr(this_char,'=')) != NULL) {
3238 *value++ = 0;
3239 } else {
Joe Perches11705322016-03-17 14:19:50 -07003240 pr_err("tmpfs: No value for mount option '%s'\n",
3241 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003242 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 }
3244
3245 if (!strcmp(this_char,"size")) {
3246 unsigned long long size;
3247 size = memparse(value,&rest);
3248 if (*rest == '%') {
3249 size <<= PAGE_SHIFT;
3250 size *= totalram_pages;
3251 do_div(size, 100);
3252 rest++;
3253 }
3254 if (*rest)
3255 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003256 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003257 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003259 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 if (*rest)
3261 goto bad_val;
3262 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003263 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (*rest)
3265 goto bad_val;
3266 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003267 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003269 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (*rest)
3271 goto bad_val;
3272 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003273 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003275 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (*rest)
3277 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003278 sbinfo->uid = make_kuid(current_user_ns(), uid);
3279 if (!uid_valid(sbinfo->uid))
3280 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003282 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003284 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (*rest)
3286 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003287 sbinfo->gid = make_kgid(current_user_ns(), gid);
3288 if (!gid_valid(sbinfo->gid))
3289 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003290#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3291 } else if (!strcmp(this_char, "huge")) {
3292 int huge;
3293 huge = shmem_parse_huge(value);
3294 if (huge < 0)
3295 goto bad_val;
3296 if (!has_transparent_hugepage() &&
3297 huge != SHMEM_HUGE_NEVER)
3298 goto bad_val;
3299 sbinfo->huge = huge;
3300#endif
3301#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003302 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003303 mpol_put(mpol);
3304 mpol = NULL;
3305 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003306 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 } else {
Joe Perches11705322016-03-17 14:19:50 -07003309 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003310 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003313 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 return 0;
3315
3316bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003317 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003319error:
3320 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return 1;
3322
3323}
3324
3325static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3326{
3327 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003328 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003329 unsigned long inodes;
3330 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Greg Thelen5f001102013-02-22 16:36:01 -08003332 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003333 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003334 return error;
3335
3336 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003337 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003338 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003339 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003340 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003341 goto out;
3342 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003343 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003344 * but we must separately disallow unlimited->limited, because
3345 * in that case we have no record of how much is already in use.
3346 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003347 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003348 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003349 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003350 goto out;
3351
3352 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003353 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003354 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003355 sbinfo->max_inodes = config.max_inodes;
3356 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003357
Greg Thelen5f001102013-02-22 16:36:01 -08003358 /*
3359 * Preserve previous mempolicy unless mpol remount option was specified.
3360 */
3361 if (config.mpol) {
3362 mpol_put(sbinfo->mpol);
3363 sbinfo->mpol = config.mpol; /* transfers initial ref */
3364 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003365out:
3366 spin_unlock(&sbinfo->stat_lock);
3367 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003369
Al Viro34c80b12011-12-08 21:32:45 -05003370static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003371{
Al Viro34c80b12011-12-08 21:32:45 -05003372 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003373
3374 if (sbinfo->max_blocks != shmem_default_max_blocks())
3375 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003376 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003377 if (sbinfo->max_inodes != shmem_default_max_inodes())
3378 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
3379 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003380 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003381 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3382 seq_printf(seq, ",uid=%u",
3383 from_kuid_munged(&init_user_ns, sbinfo->uid));
3384 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3385 seq_printf(seq, ",gid=%u",
3386 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003387#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3388 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3389 if (sbinfo->huge)
3390 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3391#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003392 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003393 return 0;
3394}
David Herrmann9183df22014-08-08 14:25:29 -07003395
3396#define MFD_NAME_PREFIX "memfd:"
3397#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
3398#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
3399
3400#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
3401
3402SYSCALL_DEFINE2(memfd_create,
3403 const char __user *, uname,
3404 unsigned int, flags)
3405{
3406 struct shmem_inode_info *info;
3407 struct file *file;
3408 int fd, error;
3409 char *name;
3410 long len;
3411
3412 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
3413 return -EINVAL;
3414
3415 /* length includes terminating zero */
3416 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
3417 if (len <= 0)
3418 return -EFAULT;
3419 if (len > MFD_NAME_MAX_LEN + 1)
3420 return -EINVAL;
3421
3422 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
3423 if (!name)
3424 return -ENOMEM;
3425
3426 strcpy(name, MFD_NAME_PREFIX);
3427 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
3428 error = -EFAULT;
3429 goto err_name;
3430 }
3431
3432 /* terminating-zero may have changed after strnlen_user() returned */
3433 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
3434 error = -EFAULT;
3435 goto err_name;
3436 }
3437
3438 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
3439 if (fd < 0) {
3440 error = fd;
3441 goto err_name;
3442 }
3443
3444 file = shmem_file_setup(name, 0, VM_NORESERVE);
3445 if (IS_ERR(file)) {
3446 error = PTR_ERR(file);
3447 goto err_fd;
3448 }
3449 info = SHMEM_I(file_inode(file));
3450 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
3451 file->f_flags |= O_RDWR | O_LARGEFILE;
3452 if (flags & MFD_ALLOW_SEALING)
3453 info->seals &= ~F_SEAL_SEAL;
3454
3455 fd_install(fd, file);
3456 kfree(name);
3457 return fd;
3458
3459err_fd:
3460 put_unused_fd(fd);
3461err_name:
3462 kfree(name);
3463 return error;
3464}
3465
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003466#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468static void shmem_put_super(struct super_block *sb)
3469{
Hugh Dickins602586a2010-08-17 15:23:56 -07003470 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3471
3472 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003473 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003474 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 sb->s_fs_info = NULL;
3476}
3477
Kay Sievers2b2af542009-04-30 15:23:42 +02003478int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003481 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003482 int err = -ENOMEM;
3483
3484 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003485 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003486 L1_CACHE_BYTES), GFP_KERNEL);
3487 if (!sbinfo)
3488 return -ENOMEM;
3489
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003490 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003491 sbinfo->uid = current_fsuid();
3492 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003493 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003495#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 /*
3497 * Per default we only allow half of the physical ram per
3498 * tmpfs instance, limiting inodes to one per page of lowmem;
3499 * but the internal instance is left unlimited.
3500 */
Al Viroca4e0512013-08-31 12:57:10 -04003501 if (!(sb->s_flags & MS_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003502 sbinfo->max_blocks = shmem_default_max_blocks();
3503 sbinfo->max_inodes = shmem_default_max_inodes();
3504 if (shmem_parse_options(data, sbinfo, false)) {
3505 err = -EINVAL;
3506 goto failed;
3507 }
Al Viroca4e0512013-08-31 12:57:10 -04003508 } else {
3509 sb->s_flags |= MS_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 }
David M. Grimes91828a42006-10-17 00:09:45 -07003511 sb->s_export_op = &shmem_export_ops;
Hugh Dickins2f6e38f2012-05-29 15:06:38 -07003512 sb->s_flags |= MS_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513#else
3514 sb->s_flags |= MS_NOUSER;
3515#endif
3516
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003517 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003518 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003519 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003520 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003521
Hugh Dickins285b2c42011-08-03 16:21:20 -07003522 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003523 sb->s_blocksize = PAGE_SIZE;
3524 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 sb->s_magic = TMPFS_MAGIC;
3526 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003527 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003528#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003529 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003530#endif
3531#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003532 sb->s_flags |= MS_POSIXACL;
3533#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003534
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003535 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 if (!inode)
3537 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003538 inode->i_uid = sbinfo->uid;
3539 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003540 sb->s_root = d_make_root(inode);
3541 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003542 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 return 0;
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545failed:
3546 shmem_put_super(sb);
3547 return err;
3548}
3549
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003550static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
3552static struct inode *shmem_alloc_inode(struct super_block *sb)
3553{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003554 struct shmem_inode_info *info;
3555 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3556 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003558 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003561static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003562{
3563 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003564 if (S_ISLNK(inode->i_mode))
3565 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003566 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3567}
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569static void shmem_destroy_inode(struct inode *inode)
3570{
Al Viro09208d12011-07-26 03:15:03 -04003571 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003573 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003576static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003578 struct shmem_inode_info *info = foo;
3579 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003582static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3585 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003586 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return 0;
3588}
3589
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003590static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003592 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003595static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003597 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003599 .write_begin = shmem_write_begin,
3600 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003602#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003603 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003604#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003605 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606};
3607
Helge Deller15ad7cd2006-12-06 20:40:36 -08003608static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003610 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003612 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003613 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003614 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003615 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07003616 .splice_read = shmem_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003617 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003618 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619#endif
3620};
3621
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003622static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003623 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003624 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003625#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003626 .setxattr = generic_setxattr,
3627 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003628 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003629 .removexattr = generic_removexattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003630 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003631#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632};
3633
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003634static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635#ifdef CONFIG_TMPFS
3636 .create = shmem_create,
3637 .lookup = simple_lookup,
3638 .link = shmem_link,
3639 .unlink = shmem_unlink,
3640 .symlink = shmem_symlink,
3641 .mkdir = shmem_mkdir,
3642 .rmdir = shmem_rmdir,
3643 .mknod = shmem_mknod,
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003644 .rename2 = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003645 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003647#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003648 .setxattr = generic_setxattr,
3649 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003650 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003651 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003652#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003653#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003654 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003655 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003656#endif
3657};
3658
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003659static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003660#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003661 .setxattr = generic_setxattr,
3662 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003663 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003664 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003665#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003666#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003667 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003668 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670};
3671
Hugh Dickins759b9772007-03-05 00:30:28 -08003672static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 .alloc_inode = shmem_alloc_inode,
3674 .destroy_inode = shmem_destroy_inode,
3675#ifdef CONFIG_TMPFS
3676 .statfs = shmem_statfs,
3677 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003678 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679#endif
Al Viro1f895f72010-06-05 19:10:41 -04003680 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 .drop_inode = generic_delete_inode,
3682 .put_super = shmem_put_super,
3683};
3684
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003685static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003686 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003687 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688#ifdef CONFIG_NUMA
3689 .set_policy = shmem_set_policy,
3690 .get_policy = shmem_get_policy,
3691#endif
3692};
3693
Al Viro3c26ff62010-07-25 11:46:36 +04003694static struct dentry *shmem_mount(struct file_system_type *fs_type,
3695 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Al Viro3c26ff62010-07-25 11:46:36 +04003697 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003700static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 .owner = THIS_MODULE,
3702 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003703 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003705 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003708int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709{
3710 int error;
3711
Rob Landley16203a72013-09-11 14:26:12 -07003712 /* If rootfs called this, don't re-init */
3713 if (shmem_inode_cachep)
3714 return 0;
3715
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003716 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 if (error)
3718 goto out3;
3719
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003720 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003722 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 goto out2;
3724 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003725
Al Viroca4e0512013-08-31 12:57:10 -04003726 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 if (IS_ERR(shm_mnt)) {
3728 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003729 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 goto out1;
3731 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003732
3733#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3734 if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY)
3735 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3736 else
3737 shmem_huge = 0; /* just in case it was patched */
3738#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 return 0;
3740
3741out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003742 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003744 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745out3:
3746 shm_mnt = ERR_PTR(error);
3747 return error;
3748}
Matt Mackall853ac432009-01-06 14:40:20 -08003749
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003750#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
3751static ssize_t shmem_enabled_show(struct kobject *kobj,
3752 struct kobj_attribute *attr, char *buf)
3753{
3754 int values[] = {
3755 SHMEM_HUGE_ALWAYS,
3756 SHMEM_HUGE_WITHIN_SIZE,
3757 SHMEM_HUGE_ADVISE,
3758 SHMEM_HUGE_NEVER,
3759 SHMEM_HUGE_DENY,
3760 SHMEM_HUGE_FORCE,
3761 };
3762 int i, count;
3763
3764 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3765 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3766
3767 count += sprintf(buf + count, fmt,
3768 shmem_format_huge(values[i]));
3769 }
3770 buf[count - 1] = '\n';
3771 return count;
3772}
3773
3774static ssize_t shmem_enabled_store(struct kobject *kobj,
3775 struct kobj_attribute *attr, const char *buf, size_t count)
3776{
3777 char tmp[16];
3778 int huge;
3779
3780 if (count + 1 > sizeof(tmp))
3781 return -EINVAL;
3782 memcpy(tmp, buf, count);
3783 tmp[count] = '\0';
3784 if (count && tmp[count - 1] == '\n')
3785 tmp[count - 1] = '\0';
3786
3787 huge = shmem_parse_huge(tmp);
3788 if (huge == -EINVAL)
3789 return -EINVAL;
3790 if (!has_transparent_hugepage() &&
3791 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3792 return -EINVAL;
3793
3794 shmem_huge = huge;
3795 if (shmem_huge < SHMEM_HUGE_DENY)
3796 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3797 return count;
3798}
3799
3800struct kobj_attribute shmem_enabled_attr =
3801 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003802
3803bool shmem_huge_enabled(struct vm_area_struct *vma)
3804{
3805 struct inode *inode = file_inode(vma->vm_file);
3806 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3807 loff_t i_size;
3808 pgoff_t off;
3809
3810 if (shmem_huge == SHMEM_HUGE_FORCE)
3811 return true;
3812 if (shmem_huge == SHMEM_HUGE_DENY)
3813 return false;
3814 switch (sbinfo->huge) {
3815 case SHMEM_HUGE_NEVER:
3816 return false;
3817 case SHMEM_HUGE_ALWAYS:
3818 return true;
3819 case SHMEM_HUGE_WITHIN_SIZE:
3820 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3821 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3822 if (i_size >= HPAGE_PMD_SIZE &&
3823 i_size >> PAGE_SHIFT >= off)
3824 return true;
3825 case SHMEM_HUGE_ADVISE:
3826 /* TODO: implement fadvise() hints */
3827 return (vma->vm_flags & VM_HUGEPAGE);
3828 default:
3829 VM_BUG_ON(1);
3830 return false;
3831 }
3832}
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003833#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
3834
Matt Mackall853ac432009-01-06 14:40:20 -08003835#else /* !CONFIG_SHMEM */
3836
3837/*
3838 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3839 *
3840 * This is intended for small system where the benefits of the full
3841 * shmem code (swap-backed and resource-limited) are outweighed by
3842 * their complexity. On systems without swap this code should be
3843 * effectively equivalent, but much lighter weight.
3844 */
3845
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003846static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003847 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003848 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003849 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003850 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003851};
3852
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003853int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003854{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003855 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003856
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003857 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003858 BUG_ON(IS_ERR(shm_mnt));
3859
3860 return 0;
3861}
3862
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003863int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003864{
3865 return 0;
3866}
3867
Hugh Dickins3f96b792009-09-21 17:03:37 -07003868int shmem_lock(struct file *file, int lock, struct user_struct *user)
3869{
3870 return 0;
3871}
3872
Hugh Dickins24513262012-01-20 14:34:21 -08003873void shmem_unlock_mapping(struct address_space *mapping)
3874{
3875}
3876
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003877#ifdef CONFIG_MMU
3878unsigned long shmem_get_unmapped_area(struct file *file,
3879 unsigned long addr, unsigned long len,
3880 unsigned long pgoff, unsigned long flags)
3881{
3882 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3883}
3884#endif
3885
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003886void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003887{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003888 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003889}
3890EXPORT_SYMBOL_GPL(shmem_truncate_range);
3891
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003892#define shmem_vm_ops generic_file_vm_ops
3893#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003894#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003895#define shmem_acct_size(flags, size) 0
3896#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003897
3898#endif /* CONFIG_SHMEM */
3899
3900/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Al Viro34515382013-02-14 22:38:02 -05003902static struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04003903 .d_dname = simple_dname
Al Viro34515382013-02-14 22:38:02 -05003904};
3905
Eric Parisc7277092013-12-02 11:24:19 +00003906static struct file *__shmem_file_setup(const char *name, loff_t size,
3907 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Al Viro6b4d0b22013-02-14 21:37:26 -05003909 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04003911 struct path path;
Al Viro34515382013-02-14 22:38:02 -05003912 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 struct qstr this;
3914
3915 if (IS_ERR(shm_mnt))
Al Viro6b4d0b22013-02-14 21:37:26 -05003916 return ERR_CAST(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
Hugh Dickins285b2c42011-08-03 16:21:20 -07003918 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return ERR_PTR(-EINVAL);
3920
3921 if (shmem_acct_size(flags, size))
3922 return ERR_PTR(-ENOMEM);
3923
Al Viro6b4d0b22013-02-14 21:37:26 -05003924 res = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 this.name = name;
3926 this.len = strlen(name);
3927 this.hash = 0; /* will go */
Al Viro34515382013-02-14 22:38:02 -05003928 sb = shm_mnt->mnt_sb;
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003929 path.mnt = mntget(shm_mnt);
Al Viro34515382013-02-14 22:38:02 -05003930 path.dentry = d_alloc_pseudo(sb, &this);
Al Viro2c48b9c2009-08-09 00:52:35 +04003931 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 goto put_memory;
Al Viro34515382013-02-14 22:38:02 -05003933 d_set_d_op(path.dentry, &anon_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Al Viro6b4d0b22013-02-14 21:37:26 -05003935 res = ERR_PTR(-ENOSPC);
Al Viro34515382013-02-14 22:38:02 -05003936 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (!inode)
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003938 goto put_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Eric Parisc7277092013-12-02 11:24:19 +00003940 inode->i_flags |= i_flags;
Al Viro2c48b9c2009-08-09 00:52:35 +04003941 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003943 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003944 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
3945 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003946 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003947
Al Viro6b4d0b22013-02-14 21:37:26 -05003948 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04003949 &shmem_file_operations);
Al Viro6b4d0b22013-02-14 21:37:26 -05003950 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003951 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003952
Al Viro6b4d0b22013-02-14 21:37:26 -05003953 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955put_memory:
3956 shmem_unacct_size(flags, size);
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003957put_path:
3958 path_put(&path);
Al Viro6b4d0b22013-02-14 21:37:26 -05003959 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
Eric Parisc7277092013-12-02 11:24:19 +00003961
3962/**
3963 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3964 * kernel internal. There will be NO LSM permission checks against the
3965 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003966 * higher layer. The users are the big_key and shm implementations. LSM
3967 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003968 * @name: name for dentry (to be seen in /proc/<pid>/maps
3969 * @size: size to be set for the file
3970 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3971 */
3972struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3973{
3974 return __shmem_file_setup(name, size, flags, S_PRIVATE);
3975}
3976
3977/**
3978 * shmem_file_setup - get an unlinked file living in tmpfs
3979 * @name: name for dentry (to be seen in /proc/<pid>/maps
3980 * @size: size to be set for the file
3981 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3982 */
3983struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3984{
3985 return __shmem_file_setup(name, size, flags, 0);
3986}
Keith Packard395e0dd2008-06-20 00:08:06 -07003987EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Randy Dunlap46711812008-03-19 17:00:41 -07003989/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3992 */
3993int shmem_zero_setup(struct vm_area_struct *vma)
3994{
3995 struct file *file;
3996 loff_t size = vma->vm_end - vma->vm_start;
3997
Hugh Dickins66fc1302015-06-14 09:48:09 -07003998 /*
3999 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4000 * between XFS directory reading and selinux: since this file is only
4001 * accessible to the user through its mapping, use S_PRIVATE flag to
4002 * bypass file security, in the same way as shmem_kernel_file_setup().
4003 */
4004 file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 if (IS_ERR(file))
4006 return PTR_ERR(file);
4007
4008 if (vma->vm_file)
4009 fput(vma->vm_file);
4010 vma->vm_file = file;
4011 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004012
4013 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
4014 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4015 (vma->vm_end & HPAGE_PMD_MASK)) {
4016 khugepaged_enter(vma, vma->vm_flags);
4017 }
4018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return 0;
4020}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004021
4022/**
4023 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4024 * @mapping: the page's address_space
4025 * @index: the page index
4026 * @gfp: the page allocator flags to use if allocating
4027 *
4028 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4029 * with any new page allocations done using the specified allocation flags.
4030 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4031 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4032 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4033 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004034 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4035 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004036 */
4037struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4038 pgoff_t index, gfp_t gfp)
4039{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004040#ifdef CONFIG_SHMEM
4041 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004042 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004043 int error;
4044
4045 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004046 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
4047 gfp, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004048 if (error)
4049 page = ERR_PTR(error);
4050 else
4051 unlock_page(page);
4052 return page;
4053#else
4054 /*
4055 * The tiny !SHMEM case uses ramfs without swap
4056 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004057 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004058#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004059}
4060EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);