blob: 302ae4a0af3da86c046d2cb1b5a89682124e9f8d [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>
Matt Mackall853ac432009-01-06 14:40:20 -080035
36static struct vfsmount *shm_mnt;
37
38#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * This virtual memory filesystem is heavily based on the ramfs. It
41 * extends ramfs by the ability to use swap and honor resource limits
42 * which makes it a completely usable filesystem.
43 */
44
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070045#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070046#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010047#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080048#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/string.h>
51#include <linux/slab.h>
52#include <linux/backing-dev.h>
53#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070056#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070057#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070058#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070059#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/security.h>
61#include <linux/swapops.h>
62#include <linux/mempolicy.h>
63#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000064#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070065#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070066#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080067#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040068#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070069#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070070#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070071#include <uapi/linux/memfd.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <asm/pgtable.h>
75
Mel Gormandd56b042015-11-06 16:28:43 -080076#include "internal.h"
77
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030078#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
79#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* Pretend that each entry is of this size in directory's i_size */
82#define BOGO_DIRENT_SIZE 20
83
Hugh Dickins69f07ec2011-08-03 16:21:26 -070084/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
85#define SHORT_SYMLINK_LEN 128
86
Hugh Dickins1aac1402012-05-29 15:06:42 -070087/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070088 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
89 * inode->i_private (with i_mutex making sure that it has only one user at
90 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -070091 */
92struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -070093 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -070094 pgoff_t start; /* start of range currently being fallocated */
95 pgoff_t next; /* the next page offset to be fallocated */
96 pgoff_t nr_falloced; /* how many new pages have been fallocated */
97 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
98};
99
Hugh Dickins285b2c42011-08-03 16:21:20 -0700100/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 SGP_READ, /* don't exceed i_size, don't allocate page */
103 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700104 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
105 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Andrew Mortonb76db732008-02-08 04:21:49 -0800108#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800109static unsigned long shmem_default_max_blocks(void)
110{
111 return totalram_pages / 2;
112}
113
114static unsigned long shmem_default_max_inodes(void)
115{
116 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
117}
Andrew Mortonb76db732008-02-08 04:21:49 -0800118#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800119
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700120static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
121static int shmem_replace_page(struct page **pagep, gfp_t gfp,
122 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700123static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700124 struct page **pagep, enum sgp_type sgp,
125 gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700126
127static inline int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700128 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700129{
130 return shmem_getpage_gfp(inode, index, pagep, sgp,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700131 mapping_gfp_mask(inode->i_mapping), NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
135{
136 return sb->s_fs_info;
137}
138
139/*
140 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
141 * for shared memory and for shared anonymous (/dev/zero) mappings
142 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
143 * consistent with the pre-accounting of private mappings ...
144 */
145static inline int shmem_acct_size(unsigned long flags, loff_t size)
146{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000147 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000148 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static inline void shmem_unacct_size(unsigned long flags, loff_t size)
152{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000153 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 vm_unacct_memory(VM_ACCT(size));
155}
156
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700157static inline int shmem_reacct_size(unsigned long flags,
158 loff_t oldsize, loff_t newsize)
159{
160 if (!(flags & VM_NORESERVE)) {
161 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
162 return security_vm_enough_memory_mm(current->mm,
163 VM_ACCT(newsize) - VM_ACCT(oldsize));
164 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
165 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
166 }
167 return 0;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700172 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
174 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
175 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700176static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700178 if (!(flags & VM_NORESERVE))
179 return 0;
180
181 return security_vm_enough_memory_mm(current->mm,
182 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static inline void shmem_unacct_blocks(unsigned long flags, long pages)
186{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000187 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300188 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Hugh Dickins759b9772007-03-05 00:30:28 -0800191static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700192static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800193static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800194static const struct inode_operations shmem_inode_operations;
195static const struct inode_operations shmem_dir_inode_operations;
196static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400197static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800200static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800202static int shmem_reserve_inode(struct super_block *sb)
203{
204 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
205 if (sbinfo->max_inodes) {
206 spin_lock(&sbinfo->stat_lock);
207 if (!sbinfo->free_inodes) {
208 spin_unlock(&sbinfo->stat_lock);
209 return -ENOSPC;
210 }
211 sbinfo->free_inodes--;
212 spin_unlock(&sbinfo->stat_lock);
213 }
214 return 0;
215}
216
217static void shmem_free_inode(struct super_block *sb)
218{
219 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
220 if (sbinfo->max_inodes) {
221 spin_lock(&sbinfo->stat_lock);
222 sbinfo->free_inodes++;
223 spin_unlock(&sbinfo->stat_lock);
224 }
225}
226
Randy Dunlap46711812008-03-19 17:00:41 -0700227/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700228 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @inode: inode to recalc
230 *
231 * We have to calculate the free blocks since the mm can drop
232 * undirtied hole pages behind our back.
233 *
234 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
235 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
236 *
237 * It has to be called with the spinlock held.
238 */
239static void shmem_recalc_inode(struct inode *inode)
240{
241 struct shmem_inode_info *info = SHMEM_I(inode);
242 long freed;
243
244 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
245 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700246 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
247 if (sbinfo->max_blocks)
248 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700250 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700255bool shmem_charge(struct inode *inode, long pages)
256{
257 struct shmem_inode_info *info = SHMEM_I(inode);
258 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
259
260 if (shmem_acct_block(info->flags, pages))
261 return false;
262 spin_lock(&info->lock);
263 info->alloced += pages;
264 inode->i_blocks += pages * BLOCKS_PER_PAGE;
265 shmem_recalc_inode(inode);
266 spin_unlock(&info->lock);
267 inode->i_mapping->nrpages += pages;
268
269 if (!sbinfo->max_blocks)
270 return true;
271 if (percpu_counter_compare(&sbinfo->used_blocks,
272 sbinfo->max_blocks - pages) > 0) {
273 inode->i_mapping->nrpages -= pages;
274 spin_lock(&info->lock);
275 info->alloced -= pages;
276 shmem_recalc_inode(inode);
277 spin_unlock(&info->lock);
278
279 return false;
280 }
281 percpu_counter_add(&sbinfo->used_blocks, pages);
282 return true;
283}
284
285void shmem_uncharge(struct inode *inode, long pages)
286{
287 struct shmem_inode_info *info = SHMEM_I(inode);
288 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
289
290 spin_lock(&info->lock);
291 info->alloced -= pages;
292 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
293 shmem_recalc_inode(inode);
294 spin_unlock(&info->lock);
295
296 if (sbinfo->max_blocks)
297 percpu_counter_sub(&sbinfo->used_blocks, pages);
298}
299
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700300/*
301 * Replace item expected in radix tree by a new item, while holding tree lock.
302 */
303static int shmem_radix_tree_replace(struct address_space *mapping,
304 pgoff_t index, void *expected, void *replacement)
305{
306 void **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700307 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700308
309 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700310 VM_BUG_ON(!replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700311 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700312 if (!pslot)
313 return -ENOENT;
314 item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700315 if (item != expected)
316 return -ENOENT;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700317 radix_tree_replace_slot(pslot, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700318 return 0;
319}
320
321/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700322 * Sometimes, before we decide whether to proceed or to fail, we must check
323 * that an entry was not already brought back from swap by a racing thread.
324 *
325 * Checking page is not enough: by the time a SwapCache page is locked, it
326 * might be reused, and again be SwapCache, using the same swap as before.
327 */
328static bool shmem_confirm_swap(struct address_space *mapping,
329 pgoff_t index, swp_entry_t swap)
330{
331 void *item;
332
333 rcu_read_lock();
334 item = radix_tree_lookup(&mapping->page_tree, index);
335 rcu_read_unlock();
336 return item == swp_to_radix_entry(swap);
337}
338
339/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700340 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
341 *
342 * SHMEM_HUGE_NEVER:
343 * disables huge pages for the mount;
344 * SHMEM_HUGE_ALWAYS:
345 * enables huge pages for the mount;
346 * SHMEM_HUGE_WITHIN_SIZE:
347 * only allocate huge pages if the page will be fully within i_size,
348 * also respect fadvise()/madvise() hints;
349 * SHMEM_HUGE_ADVISE:
350 * only allocate huge pages if requested with fadvise()/madvise();
351 */
352
353#define SHMEM_HUGE_NEVER 0
354#define SHMEM_HUGE_ALWAYS 1
355#define SHMEM_HUGE_WITHIN_SIZE 2
356#define SHMEM_HUGE_ADVISE 3
357
358/*
359 * Special values.
360 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
361 *
362 * SHMEM_HUGE_DENY:
363 * disables huge on shm_mnt and all mounts, for emergency use;
364 * SHMEM_HUGE_FORCE:
365 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
366 *
367 */
368#define SHMEM_HUGE_DENY (-1)
369#define SHMEM_HUGE_FORCE (-2)
370
371#ifdef CONFIG_TRANSPARENT_HUGEPAGE
372/* ifdef here to avoid bloating shmem.o when not necessary */
373
374int shmem_huge __read_mostly;
375
376static int shmem_parse_huge(const char *str)
377{
378 if (!strcmp(str, "never"))
379 return SHMEM_HUGE_NEVER;
380 if (!strcmp(str, "always"))
381 return SHMEM_HUGE_ALWAYS;
382 if (!strcmp(str, "within_size"))
383 return SHMEM_HUGE_WITHIN_SIZE;
384 if (!strcmp(str, "advise"))
385 return SHMEM_HUGE_ADVISE;
386 if (!strcmp(str, "deny"))
387 return SHMEM_HUGE_DENY;
388 if (!strcmp(str, "force"))
389 return SHMEM_HUGE_FORCE;
390 return -EINVAL;
391}
392
393static const char *shmem_format_huge(int huge)
394{
395 switch (huge) {
396 case SHMEM_HUGE_NEVER:
397 return "never";
398 case SHMEM_HUGE_ALWAYS:
399 return "always";
400 case SHMEM_HUGE_WITHIN_SIZE:
401 return "within_size";
402 case SHMEM_HUGE_ADVISE:
403 return "advise";
404 case SHMEM_HUGE_DENY:
405 return "deny";
406 case SHMEM_HUGE_FORCE:
407 return "force";
408 default:
409 VM_BUG_ON(1);
410 return "bad_val";
411 }
412}
413
414#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
415
416#define shmem_huge SHMEM_HUGE_DENY
417
418#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
419
420/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700421 * Like add_to_page_cache_locked, but error if expected item has gone.
422 */
423static int shmem_add_to_page_cache(struct page *page,
424 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700425 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700426{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700427 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700428
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700429 VM_BUG_ON_PAGE(PageTail(page), page);
430 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800431 VM_BUG_ON_PAGE(!PageLocked(page), page);
432 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700433 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700434
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700435 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700436 page->mapping = mapping;
437 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700438
Hugh Dickinsb065b432012-07-11 14:02:48 -0700439 spin_lock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700440 if (PageTransHuge(page)) {
441 void __rcu **results;
442 pgoff_t idx;
443 int i;
444
445 error = 0;
446 if (radix_tree_gang_lookup_slot(&mapping->page_tree,
447 &results, &idx, index, 1) &&
448 idx < index + HPAGE_PMD_NR) {
449 error = -EEXIST;
450 }
451
452 if (!error) {
453 for (i = 0; i < HPAGE_PMD_NR; i++) {
454 error = radix_tree_insert(&mapping->page_tree,
455 index + i, page + i);
456 VM_BUG_ON(error);
457 }
458 count_vm_event(THP_FILE_ALLOC);
459 }
460 } else if (!expected) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700461 error = radix_tree_insert(&mapping->page_tree, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700462 } else {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700463 error = shmem_radix_tree_replace(mapping, index, expected,
464 page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700465 }
466
Hugh Dickinsb065b432012-07-11 14:02:48 -0700467 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700468 mapping->nrpages += nr;
469 if (PageTransHuge(page))
470 __inc_zone_page_state(page, NR_SHMEM_THPS);
471 __mod_zone_page_state(page_zone(page), NR_FILE_PAGES, nr);
472 __mod_zone_page_state(page_zone(page), NR_SHMEM, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700473 spin_unlock_irq(&mapping->tree_lock);
474 } else {
475 page->mapping = NULL;
476 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700477 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700478 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700479 return error;
480}
481
482/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700483 * Like delete_from_page_cache, but substitutes swap for page.
484 */
485static void shmem_delete_from_page_cache(struct page *page, void *radswap)
486{
487 struct address_space *mapping = page->mapping;
488 int error;
489
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700490 VM_BUG_ON_PAGE(PageCompound(page), page);
491
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700492 spin_lock_irq(&mapping->tree_lock);
493 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
494 page->mapping = NULL;
495 mapping->nrpages--;
496 __dec_zone_page_state(page, NR_FILE_PAGES);
497 __dec_zone_page_state(page, NR_SHMEM);
498 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300499 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700500 BUG_ON(error);
501}
502
503/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700504 * Remove swap entry from radix tree, free the swap and its page cache.
505 */
506static int shmem_free_swap(struct address_space *mapping,
507 pgoff_t index, void *radswap)
508{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700509 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700510
511 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700512 old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700513 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700514 if (old != radswap)
515 return -ENOENT;
516 free_swap_and_cache(radix_to_swp_entry(radswap));
517 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700518}
519
520/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800521 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800522 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800523 *
524 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
525 * as long as the inode doesn't go away and racy results are not a problem.
526 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800527unsigned long shmem_partial_swap_usage(struct address_space *mapping,
528 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800529{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800530 struct radix_tree_iter iter;
531 void **slot;
532 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800533 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800534
535 rcu_read_lock();
536
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800537 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
538 if (iter.index >= end)
539 break;
540
541 page = radix_tree_deref_slot(slot);
542
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700543 if (radix_tree_deref_retry(page)) {
544 slot = radix_tree_iter_retry(&iter);
545 continue;
546 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800547
548 if (radix_tree_exceptional_entry(page))
549 swapped++;
550
551 if (need_resched()) {
552 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -0700553 slot = radix_tree_iter_next(&iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800554 }
555 }
556
557 rcu_read_unlock();
558
559 return swapped << PAGE_SHIFT;
560}
561
562/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800563 * Determine (in bytes) how many of the shmem object's pages mapped by the
564 * given vma is swapped out.
565 *
566 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
567 * as long as the inode doesn't go away and racy results are not a problem.
568 */
569unsigned long shmem_swap_usage(struct vm_area_struct *vma)
570{
571 struct inode *inode = file_inode(vma->vm_file);
572 struct shmem_inode_info *info = SHMEM_I(inode);
573 struct address_space *mapping = inode->i_mapping;
574 unsigned long swapped;
575
576 /* Be careful as we don't hold info->lock */
577 swapped = READ_ONCE(info->swapped);
578
579 /*
580 * The easier cases are when the shmem object has nothing in swap, or
581 * the vma maps it whole. Then we can simply use the stats that we
582 * already track.
583 */
584 if (!swapped)
585 return 0;
586
587 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
588 return swapped << PAGE_SHIFT;
589
590 /* Here comes the more involved part */
591 return shmem_partial_swap_usage(mapping,
592 linear_page_index(vma, vma->vm_start),
593 linear_page_index(vma, vma->vm_end));
594}
595
596/*
Hugh Dickins24513262012-01-20 14:34:21 -0800597 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
598 */
599void shmem_unlock_mapping(struct address_space *mapping)
600{
601 struct pagevec pvec;
602 pgoff_t indices[PAGEVEC_SIZE];
603 pgoff_t index = 0;
604
605 pagevec_init(&pvec, 0);
606 /*
607 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
608 */
609 while (!mapping_unevictable(mapping)) {
610 /*
611 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
612 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
613 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700614 pvec.nr = find_get_entries(mapping, index,
615 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800616 if (!pvec.nr)
617 break;
618 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700619 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800620 check_move_unevictable_pages(pvec.pages, pvec.nr);
621 pagevec_release(&pvec);
622 cond_resched();
623 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700624}
625
626/*
627 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700628 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700629 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700630static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
631 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700633 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300635 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
636 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
637 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
638 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700639 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700640 pgoff_t indices[PAGEVEC_SIZE];
641 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700642 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700643 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700645 if (lend == -1)
646 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700647
648 pagevec_init(&pvec, 0);
649 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700650 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700651 pvec.nr = find_get_entries(mapping, index,
652 min(end - index, (pgoff_t)PAGEVEC_SIZE),
653 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700654 if (!pvec.nr)
655 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700656 for (i = 0; i < pagevec_count(&pvec); i++) {
657 struct page *page = pvec.pages[i];
658
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700659 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700660 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700661 break;
662
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700663 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700664 if (unfalloc)
665 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700666 nr_swaps_freed += !shmem_free_swap(mapping,
667 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700668 continue;
669 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700670
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700671 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
672
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700673 if (!trylock_page(page))
674 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700675
676 if (PageTransTail(page)) {
677 /* Middle of THP: zero out the page */
678 clear_highpage(page);
679 unlock_page(page);
680 continue;
681 } else if (PageTransHuge(page)) {
682 if (index == round_down(end, HPAGE_PMD_NR)) {
683 /*
684 * Range ends in the middle of THP:
685 * zero out the page
686 */
687 clear_highpage(page);
688 unlock_page(page);
689 continue;
690 }
691 index += HPAGE_PMD_NR - 1;
692 i += HPAGE_PMD_NR - 1;
693 }
694
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700695 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700696 VM_BUG_ON_PAGE(PageTail(page), page);
697 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800698 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700699 truncate_inode_page(mapping, page);
700 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700701 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700702 unlock_page(page);
703 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700704 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800705 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700706 cond_resched();
707 index++;
708 }
709
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700710 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700711 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700712 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700713 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300714 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700715 if (start > end) {
716 top = partial_end;
717 partial_end = 0;
718 }
719 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700720 set_page_dirty(page);
721 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300722 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700723 }
724 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700725 if (partial_end) {
726 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700727 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700728 if (page) {
729 zero_user_segment(page, 0, partial_end);
730 set_page_dirty(page);
731 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300732 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700733 }
734 }
735 if (start >= end)
736 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700737
738 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700739 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700740 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700741
742 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700743 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700744 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700745 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700746 /* If all gone or hole-punch or unfalloc, we're done */
747 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700748 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700749 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700750 index = start;
751 continue;
752 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700753 for (i = 0; i < pagevec_count(&pvec); i++) {
754 struct page *page = pvec.pages[i];
755
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700756 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700757 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700758 break;
759
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700760 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700761 if (unfalloc)
762 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700763 if (shmem_free_swap(mapping, index, page)) {
764 /* Swap was replaced by page: retry */
765 index--;
766 break;
767 }
768 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700769 continue;
770 }
771
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700772 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700773
774 if (PageTransTail(page)) {
775 /* Middle of THP: zero out the page */
776 clear_highpage(page);
777 unlock_page(page);
778 /*
779 * Partial thp truncate due 'start' in middle
780 * of THP: don't need to look on these pages
781 * again on !pvec.nr restart.
782 */
783 if (index != round_down(end, HPAGE_PMD_NR))
784 start++;
785 continue;
786 } else if (PageTransHuge(page)) {
787 if (index == round_down(end, HPAGE_PMD_NR)) {
788 /*
789 * Range ends in the middle of THP:
790 * zero out the page
791 */
792 clear_highpage(page);
793 unlock_page(page);
794 continue;
795 }
796 index += HPAGE_PMD_NR - 1;
797 i += HPAGE_PMD_NR - 1;
798 }
799
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700800 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700801 VM_BUG_ON_PAGE(PageTail(page), page);
802 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800803 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700804 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700805 } else {
806 /* Page was replaced by swap: retry */
807 unlock_page(page);
808 index--;
809 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700810 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700811 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700812 unlock_page(page);
813 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700814 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800815 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700816 index++;
817 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700820 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 shmem_recalc_inode(inode);
822 spin_unlock(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700823}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700825void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
826{
827 shmem_undo_range(inode, lstart, lend, false);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700828 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700830EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Yu Zhao44a30222015-09-08 15:03:33 -0700832static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
833 struct kstat *stat)
834{
835 struct inode *inode = dentry->d_inode;
836 struct shmem_inode_info *info = SHMEM_I(inode);
837
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800838 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
839 spin_lock(&info->lock);
840 shmem_recalc_inode(inode);
841 spin_unlock(&info->lock);
842 }
Yu Zhao44a30222015-09-08 15:03:33 -0700843 generic_fillattr(inode, stat);
Yu Zhao44a30222015-09-08 15:03:33 -0700844 return 0;
845}
846
Hugh Dickins94c1e622011-06-27 16:18:03 -0700847static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
David Howells75c3cfa2015-03-17 22:26:12 +0000849 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -0700850 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 int error;
852
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200853 error = inode_change_ok(inode, attr);
854 if (error)
855 return error;
856
Hugh Dickins94c1e622011-06-27 16:18:03 -0700857 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
858 loff_t oldsize = inode->i_size;
859 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000860
David Herrmann40e041a2014-08-08 14:25:27 -0700861 /* protected by i_mutex */
862 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
863 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
864 return -EPERM;
865
Hugh Dickins94c1e622011-06-27 16:18:03 -0700866 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700867 error = shmem_reacct_size(SHMEM_I(inode)->flags,
868 oldsize, newsize);
869 if (error)
870 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -0700871 i_size_write(inode, newsize);
872 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
873 }
Josef Bacikafa2db22015-06-24 16:58:45 -0700874 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -0700875 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800876 if (oldsize > holebegin)
877 unmap_mapping_range(inode->i_mapping,
878 holebegin, 0, 1);
879 if (info->alloced)
880 shmem_truncate_range(inode,
881 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700882 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800883 if (oldsize > holebegin)
884 unmap_mapping_range(inode->i_mapping,
885 holebegin, 0, 1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200889 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200890 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800891 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return error;
893}
894
Al Viro1f895f72010-06-05 19:10:41 -0400895static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct shmem_inode_info *info = SHMEM_I(inode);
898
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000899 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 shmem_unacct_size(info->flags, inode->i_size);
901 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000902 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800904 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800906 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Al Viro3ed47db2016-01-22 18:08:52 -0500908 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700909
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400910 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -0800911 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800912 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +0200913 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700916/*
917 * If swap found in inode, free it and move page from swapcache to filecache.
918 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700919static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700920 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700922 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700923 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700924 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700925 gfp_t gfp;
926 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700928 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700929 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700930 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -0700931 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800932
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800933 /*
934 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400935 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800936 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700937 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800938 */
939 if (shmem_swaplist.next != &info->swaplist)
940 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800941
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700942 gfp = mapping_gfp_mask(mapping);
943 if (shmem_should_replace_page(*pagep, gfp)) {
944 mutex_unlock(&shmem_swaplist_mutex);
945 error = shmem_replace_page(pagep, gfp, info, index);
946 mutex_lock(&shmem_swaplist_mutex);
947 /*
948 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -0700949 * allocation, but the inode might have been freed while we
950 * dropped it: although a racing shmem_evict_inode() cannot
951 * complete without emptying the radix_tree, our page lock
952 * on this swapcache page is not enough to prevent that -
953 * free_swap_and_cache() of our swap entry will only
954 * trylock_page(), removing swap from radix_tree whatever.
955 *
956 * We must not proceed to shmem_add_to_page_cache() if the
957 * inode has been freed, but of course we cannot rely on
958 * inode or mapping or info to check that. However, we can
959 * safely check if our swap entry is still in use (and here
960 * it can't have got reused for another page): if it's still
961 * in use, then the inode cannot have been freed yet, and we
962 * can safely proceed (if it's no longer in use, that tells
963 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700964 */
965 if (!page_swapcount(*pagep))
966 error = -ENOENT;
967 }
968
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800969 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700970 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
971 * but also to hold up shmem_evict_inode(): so inode cannot be freed
972 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800973 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700974 if (!error)
975 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700976 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700977 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700978 /*
979 * Truncation and eviction use free_swap_and_cache(), which
980 * only does trylock page: if we raced, best clean up here.
981 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700982 delete_from_swap_cache(*pagep);
983 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700984 if (!error) {
985 spin_lock(&info->lock);
986 info->swapped--;
987 spin_unlock(&info->lock);
988 swap_free(swap);
989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800991 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700995 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700997int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700999 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001001 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001002 int error = 0;
1003
1004 /*
1005 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001006 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001007 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001008 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001009 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001010
1011 /*
1012 * Charge page using GFP_KERNEL while we can wait, before taking
1013 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1014 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001015 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001016 error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
1017 false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001018 if (error)
1019 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001020 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001021 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001023 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001024 list_for_each_safe(this, next, &shmem_swaplist) {
1025 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001026 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001027 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001028 else
1029 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001030 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001031 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001032 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001033 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001035 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001036
Johannes Weiner00501b52014-08-08 14:19:20 -07001037 if (error) {
1038 if (error != -ENOMEM)
1039 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001040 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001041 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001042 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001043out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001044 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001045 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001046 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
1049/*
1050 * Move the page from the page cache to the swap cache.
1051 */
1052static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1053{
1054 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001057 swp_entry_t swap;
1058 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001060 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 mapping = page->mapping;
1063 index = page->index;
1064 inode = mapping->host;
1065 info = SHMEM_I(inode);
1066 if (info->flags & VM_LOCKED)
1067 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001068 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 goto redirty;
1070
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001071 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001072 * Our capabilities prevent regular writeback or sync from ever calling
1073 * shmem_writepage; but a stacking filesystem might use ->writepage of
1074 * its underlying filesystem, in which case tmpfs should write out to
1075 * swap only in response to memory pressure, and not for the writeback
1076 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001077 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001078 if (!wbc->for_reclaim) {
1079 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1080 goto redirty;
1081 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001082
1083 /*
1084 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1085 * value into swapfile.c, the only way we can correctly account for a
1086 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001087 *
1088 * That's okay for a page already fallocated earlier, but if we have
1089 * not yet completed the fallocation, then (a) we want to keep track
1090 * of this page in case we have to undo it, and (b) it may not be a
1091 * good idea to continue anyway, once we're pushing into swap. So
1092 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001093 */
1094 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001095 if (inode->i_private) {
1096 struct shmem_falloc *shmem_falloc;
1097 spin_lock(&inode->i_lock);
1098 shmem_falloc = inode->i_private;
1099 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001100 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001101 index >= shmem_falloc->start &&
1102 index < shmem_falloc->next)
1103 shmem_falloc->nr_unswapped++;
1104 else
1105 shmem_falloc = NULL;
1106 spin_unlock(&inode->i_lock);
1107 if (shmem_falloc)
1108 goto redirty;
1109 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001110 clear_highpage(page);
1111 flush_dcache_page(page);
1112 SetPageUptodate(page);
1113 }
1114
Hugh Dickins48f170f2011-07-25 17:12:37 -07001115 swap = get_swap_page();
1116 if (!swap.val)
1117 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001118
Vladimir Davydov37e84352016-01-20 15:02:56 -08001119 if (mem_cgroup_try_charge_swap(page, swap))
1120 goto free_swap;
1121
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001122 /*
1123 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001124 * if it's not already there. Do it now before the page is
1125 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001126 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001127 * we've incremented swapped, because shmem_unuse_inode() will
1128 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001129 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001130 mutex_lock(&shmem_swaplist_mutex);
1131 if (list_empty(&info->swaplist))
1132 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001133
Hugh Dickins48f170f2011-07-25 17:12:37 -07001134 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001135 spin_lock(&info->lock);
1136 shmem_recalc_inode(inode);
1137 info->swapped++;
1138 spin_unlock(&info->lock);
1139
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001140 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001141 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1142
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001143 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001144 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001145 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147 }
1148
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001149 mutex_unlock(&shmem_swaplist_mutex);
Vladimir Davydov37e84352016-01-20 15:02:56 -08001150free_swap:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001151 swapcache_free(swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152redirty:
1153 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001154 if (wbc->for_reclaim)
1155 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1156 unlock_page(page);
1157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Hugh Dickins75edd342016-05-19 17:12:44 -07001160#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001161static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001162{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001163 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001164
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001165 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001166 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001167
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001168 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001169
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001170 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001171}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001172
1173static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1174{
1175 struct mempolicy *mpol = NULL;
1176 if (sbinfo->mpol) {
1177 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1178 mpol = sbinfo->mpol;
1179 mpol_get(mpol);
1180 spin_unlock(&sbinfo->stat_lock);
1181 }
1182 return mpol;
1183}
Hugh Dickins75edd342016-05-19 17:12:44 -07001184#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1185static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1186{
1187}
1188static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1189{
1190 return NULL;
1191}
1192#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1193#ifndef CONFIG_NUMA
1194#define vm_policy vm_private_data
1195#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001196
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001197static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1198 struct shmem_inode_info *info, pgoff_t index)
1199{
1200 /* Create a pseudo vma that just contains the policy */
1201 vma->vm_start = 0;
1202 /* Bias interleave by inode number to distribute better across nodes */
1203 vma->vm_pgoff = index + info->vfs_inode.i_ino;
1204 vma->vm_ops = NULL;
1205 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1206}
1207
1208static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1209{
1210 /* Drop reference taken by mpol_shared_policy_lookup() */
1211 mpol_cond_put(vma->vm_policy);
1212}
1213
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001214static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1215 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001218 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001220 shmem_pseudo_vma_init(&pvma, info, index);
Mel Gorman18a2f372012-12-05 14:01:41 -08001221 page = swapin_readahead(swap, gfp, &pvma, 0);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001222 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001223
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001224 return page;
1225}
Mel Gorman18a2f372012-12-05 14:01:41 -08001226
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001227static struct page *shmem_alloc_hugepage(gfp_t gfp,
1228 struct shmem_inode_info *info, pgoff_t index)
1229{
1230 struct vm_area_struct pvma;
1231 struct inode *inode = &info->vfs_inode;
1232 struct address_space *mapping = inode->i_mapping;
1233 pgoff_t idx, hindex = round_down(index, HPAGE_PMD_NR);
1234 void __rcu **results;
1235 struct page *page;
1236
1237 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1238 return NULL;
1239
1240 rcu_read_lock();
1241 if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
1242 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1243 rcu_read_unlock();
1244 return NULL;
1245 }
1246 rcu_read_unlock();
1247
1248 shmem_pseudo_vma_init(&pvma, info, hindex);
1249 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1250 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1251 shmem_pseudo_vma_destroy(&pvma);
1252 if (page)
1253 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001254 return page;
1255}
1256
1257static struct page *shmem_alloc_page(gfp_t gfp,
1258 struct shmem_inode_info *info, pgoff_t index)
1259{
1260 struct vm_area_struct pvma;
1261 struct page *page;
1262
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001263 shmem_pseudo_vma_init(&pvma, info, index);
1264 page = alloc_page_vma(gfp, &pvma, 0);
1265 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001266
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001267 return page;
1268}
1269
1270static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
1271 struct shmem_inode_info *info, struct shmem_sb_info *sbinfo,
1272 pgoff_t index, bool huge)
1273{
1274 struct page *page;
1275 int nr;
1276 int err = -ENOSPC;
1277
1278 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1279 huge = false;
1280 nr = huge ? HPAGE_PMD_NR : 1;
1281
1282 if (shmem_acct_block(info->flags, nr))
1283 goto failed;
1284 if (sbinfo->max_blocks) {
1285 if (percpu_counter_compare(&sbinfo->used_blocks,
1286 sbinfo->max_blocks - nr) > 0)
1287 goto unacct;
1288 percpu_counter_add(&sbinfo->used_blocks, nr);
1289 }
1290
1291 if (huge)
1292 page = shmem_alloc_hugepage(gfp, info, index);
1293 else
1294 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001295 if (page) {
1296 __SetPageLocked(page);
1297 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001298 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001299 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001300
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001301 err = -ENOMEM;
1302 if (sbinfo->max_blocks)
1303 percpu_counter_add(&sbinfo->used_blocks, -nr);
1304unacct:
1305 shmem_unacct_blocks(info->flags, nr);
1306failed:
1307 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001311 * When a page is moved from swapcache to shmem filecache (either by the
1312 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1313 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1314 * ignorance of the mapping it belongs to. If that mapping has special
1315 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1316 * we may need to copy to a suitable page before moving to filecache.
1317 *
1318 * In a future release, this may well be extended to respect cpuset and
1319 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1320 * but for now it is a simple matter of zone.
1321 */
1322static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1323{
1324 return page_zonenum(page) > gfp_zone(gfp);
1325}
1326
1327static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1328 struct shmem_inode_info *info, pgoff_t index)
1329{
1330 struct page *oldpage, *newpage;
1331 struct address_space *swap_mapping;
1332 pgoff_t swap_index;
1333 int error;
1334
1335 oldpage = *pagep;
1336 swap_index = page_private(oldpage);
1337 swap_mapping = page_mapping(oldpage);
1338
1339 /*
1340 * We have arrived here because our zones are constrained, so don't
1341 * limit chance of success by further cpuset and node constraints.
1342 */
1343 gfp &= ~GFP_CONSTRAINT_MASK;
1344 newpage = shmem_alloc_page(gfp, info, index);
1345 if (!newpage)
1346 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001347
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001348 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001349 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001350 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001351
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001352 SetPageUptodate(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001353 set_page_private(newpage, swap_index);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001354 SetPageSwapCache(newpage);
1355
1356 /*
1357 * Our caller will very soon move newpage out of swapcache, but it's
1358 * a nice clean interface for us to replace oldpage by newpage there.
1359 */
1360 spin_lock_irq(&swap_mapping->tree_lock);
1361 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1362 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001363 if (!error) {
1364 __inc_zone_page_state(newpage, NR_FILE_PAGES);
1365 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
1366 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001367 spin_unlock_irq(&swap_mapping->tree_lock);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001368
Hugh Dickins0142ef62012-06-07 14:21:09 -07001369 if (unlikely(error)) {
1370 /*
1371 * Is this possible? I think not, now that our callers check
1372 * both PageSwapCache and page_private after getting page lock;
1373 * but be defensive. Reverse old to newpage for clear and free.
1374 */
1375 oldpage = newpage;
1376 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001377 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001378 lru_cache_add_anon(newpage);
1379 *pagep = newpage;
1380 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001381
1382 ClearPageSwapCache(oldpage);
1383 set_page_private(oldpage, 0);
1384
1385 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001386 put_page(oldpage);
1387 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001388 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001389}
1390
1391/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001392 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 *
1394 * If we allocate a new one we do not mark it dirty. That's up to the
1395 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001396 * entry since a page cannot live in both the swap and page cache.
1397 *
1398 * fault_mm and fault_type are only supplied by shmem_fault:
1399 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001401static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001402 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
1403 struct mm_struct *fault_mm, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -07001406 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001408 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001409 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001410 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 swp_entry_t swap;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001412 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001414 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001415 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001417 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001420 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001421 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001422 if (radix_tree_exceptional_entry(page)) {
1423 swap = radix_to_swp_entry(page);
1424 page = NULL;
1425 }
1426
Hugh Dickins75edd342016-05-19 17:12:44 -07001427 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001428 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001429 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001430 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001431 }
1432
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001433 if (page && sgp == SGP_WRITE)
1434 mark_page_accessed(page);
1435
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001436 /* fallocated page? */
1437 if (page && !PageUptodate(page)) {
1438 if (sgp != SGP_READ)
1439 goto clear;
1440 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001441 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001442 page = NULL;
1443 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001444 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001445 *pagep = page;
1446 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001450 * Fast cache lookup did not find it:
1451 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001453 info = SHMEM_I(inode);
1454 sbinfo = SHMEM_SB(inode->i_sb);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001455 charge_mm = fault_mm ? : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (swap.val) {
1458 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001459 page = lookup_swap_cache(swap);
1460 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001461 /* Or update major stats only when swapin succeeds?? */
1462 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001463 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001464 count_vm_event(PGMAJFAULT);
1465 mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT);
1466 }
1467 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001468 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001469 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001470 error = -ENOMEM;
1471 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001476 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001477 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001478 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001479 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001480 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001481 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001482 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001484 goto failed;
1485 }
1486 wait_on_page_writeback(page);
1487
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001488 if (shmem_should_replace_page(page, gfp)) {
1489 error = shmem_replace_page(&page, gfp, info, index);
1490 if (error)
1491 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001494 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001495 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001496 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001497 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001498 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001499 /*
1500 * We already confirmed swap under page lock, and make
1501 * no memory allocation here, so usually no possibility
1502 * of error; but free_swap_and_cache() only trylocks a
1503 * page, so it is just possible that the entry has been
1504 * truncated or holepunched since swap was confirmed.
1505 * shmem_undo_range() will have done some of the
1506 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001507 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001508 * Reset swap.val? No, leave it so "failed" goes back to
1509 * "repeat": reading a hole and writing should succeed.
1510 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001511 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001512 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001513 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001514 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001515 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001516 if (error)
1517 goto failed;
1518
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001519 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001520
Hugh Dickins54af60422011-08-03 16:21:24 -07001521 spin_lock(&info->lock);
1522 info->swapped--;
1523 shmem_recalc_inode(inode);
1524 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001525
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001526 if (sgp == SGP_WRITE)
1527 mark_page_accessed(page);
1528
Hugh Dickins27ab7002011-07-25 17:12:36 -07001529 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001530 set_page_dirty(page);
1531 swap_free(swap);
1532
Hugh Dickins54af60422011-08-03 16:21:24 -07001533 } else {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001534 /* shmem_symlink() */
1535 if (mapping->a_ops != &shmem_aops)
1536 goto alloc_nohuge;
1537 if (shmem_huge == SHMEM_HUGE_DENY)
1538 goto alloc_nohuge;
1539 if (shmem_huge == SHMEM_HUGE_FORCE)
1540 goto alloc_huge;
1541 switch (sbinfo->huge) {
1542 loff_t i_size;
1543 pgoff_t off;
1544 case SHMEM_HUGE_NEVER:
1545 goto alloc_nohuge;
1546 case SHMEM_HUGE_WITHIN_SIZE:
1547 off = round_up(index, HPAGE_PMD_NR);
1548 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1549 if (i_size >= HPAGE_PMD_SIZE &&
1550 i_size >> PAGE_SHIFT >= off)
1551 goto alloc_huge;
1552 /* fallthrough */
1553 case SHMEM_HUGE_ADVISE:
1554 /* TODO: wire up fadvise()/madvise() */
1555 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
1599 spin_lock(&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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 spin_unlock(&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);
1635 spin_lock(&info->lock);
1636 shmem_recalc_inode(inode);
1637 spin_unlock(&info->lock);
1638 }
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);
1669 spin_lock(&info->lock);
1670 shmem_recalc_inode(inode);
1671 spin_unlock(&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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001684 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001686 /*
1687 * Trinity finds that probing a hole which tmpfs is punching can
1688 * prevent the hole-punch from ever completing: which in turn
1689 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001690 * faulting pages into the hole while it's being punched. Although
1691 * shmem_undo_range() does remove the additions, it may be unable to
1692 * keep up, as each new page needs its own unmap_mapping_range() call,
1693 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1694 *
1695 * It does not matter if we sometimes reach this check just before the
1696 * hole-punch begins, so that one fault then races with the punch:
1697 * we just need to make racing faults a rare case.
1698 *
1699 * The implementation below would be much simpler if we just used a
1700 * standard mutex or completion: but we cannot take i_mutex in fault,
1701 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001702 */
1703 if (unlikely(inode->i_private)) {
1704 struct shmem_falloc *shmem_falloc;
1705
1706 spin_lock(&inode->i_lock);
1707 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001708 if (shmem_falloc &&
1709 shmem_falloc->waitq &&
1710 vmf->pgoff >= shmem_falloc->start &&
1711 vmf->pgoff < shmem_falloc->next) {
1712 wait_queue_head_t *shmem_falloc_waitq;
1713 DEFINE_WAIT(shmem_fault_wait);
1714
1715 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001716 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1717 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001718 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001719 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001720 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001721 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001722
1723 shmem_falloc_waitq = shmem_falloc->waitq;
1724 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1725 TASK_UNINTERRUPTIBLE);
1726 spin_unlock(&inode->i_lock);
1727 schedule();
1728
1729 /*
1730 * shmem_falloc_waitq points into the shmem_fallocate()
1731 * stack of the hole-punching task: shmem_falloc_waitq
1732 * is usually invalid by the time we reach here, but
1733 * finish_wait() does not dereference it in that case;
1734 * though i_lock needed lest racing with wake_up_all().
1735 */
1736 spin_lock(&inode->i_lock);
1737 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1738 spin_unlock(&inode->i_lock);
1739 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001740 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001741 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001742 }
1743
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001744 error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, SGP_CACHE,
1745 gfp, vma->vm_mm, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (error)
1747 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001748 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}
1750
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001751unsigned long shmem_get_unmapped_area(struct file *file,
1752 unsigned long uaddr, unsigned long len,
1753 unsigned long pgoff, unsigned long flags)
1754{
1755 unsigned long (*get_area)(struct file *,
1756 unsigned long, unsigned long, unsigned long, unsigned long);
1757 unsigned long addr;
1758 unsigned long offset;
1759 unsigned long inflated_len;
1760 unsigned long inflated_addr;
1761 unsigned long inflated_offset;
1762
1763 if (len > TASK_SIZE)
1764 return -ENOMEM;
1765
1766 get_area = current->mm->get_unmapped_area;
1767 addr = get_area(file, uaddr, len, pgoff, flags);
1768
1769 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1770 return addr;
1771 if (IS_ERR_VALUE(addr))
1772 return addr;
1773 if (addr & ~PAGE_MASK)
1774 return addr;
1775 if (addr > TASK_SIZE - len)
1776 return addr;
1777
1778 if (shmem_huge == SHMEM_HUGE_DENY)
1779 return addr;
1780 if (len < HPAGE_PMD_SIZE)
1781 return addr;
1782 if (flags & MAP_FIXED)
1783 return addr;
1784 /*
1785 * Our priority is to support MAP_SHARED mapped hugely;
1786 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
1787 * But if caller specified an address hint, respect that as before.
1788 */
1789 if (uaddr)
1790 return addr;
1791
1792 if (shmem_huge != SHMEM_HUGE_FORCE) {
1793 struct super_block *sb;
1794
1795 if (file) {
1796 VM_BUG_ON(file->f_op != &shmem_file_operations);
1797 sb = file_inode(file)->i_sb;
1798 } else {
1799 /*
1800 * Called directly from mm/mmap.c, or drivers/char/mem.c
1801 * for "/dev/zero", to create a shared anonymous object.
1802 */
1803 if (IS_ERR(shm_mnt))
1804 return addr;
1805 sb = shm_mnt->mnt_sb;
1806 }
1807 if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER)
1808 return addr;
1809 }
1810
1811 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
1812 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
1813 return addr;
1814 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
1815 return addr;
1816
1817 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
1818 if (inflated_len > TASK_SIZE)
1819 return addr;
1820 if (inflated_len < len)
1821 return addr;
1822
1823 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
1824 if (IS_ERR_VALUE(inflated_addr))
1825 return addr;
1826 if (inflated_addr & ~PAGE_MASK)
1827 return addr;
1828
1829 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
1830 inflated_addr += offset - inflated_offset;
1831 if (inflated_offset > offset)
1832 inflated_addr += HPAGE_PMD_SIZE;
1833
1834 if (inflated_addr > TASK_SIZE - len)
1835 return addr;
1836 return inflated_addr;
1837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001840static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Al Viro496ad9a2013-01-23 17:07:38 -05001842 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001843 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001846static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1847 unsigned long addr)
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 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001852 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1853 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855#endif
1856
1857int shmem_lock(struct file *file, int lock, struct user_struct *user)
1858{
Al Viro496ad9a2013-01-23 17:07:38 -05001859 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct shmem_inode_info *info = SHMEM_I(inode);
1861 int retval = -ENOMEM;
1862
1863 spin_lock(&info->lock);
1864 if (lock && !(info->flags & VM_LOCKED)) {
1865 if (!user_shm_lock(inode->i_size, user))
1866 goto out_nomem;
1867 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001868 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870 if (!lock && (info->flags & VM_LOCKED) && user) {
1871 user_shm_unlock(inode->i_size, user);
1872 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001873 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877out_nomem:
1878 spin_unlock(&info->lock);
1879 return retval;
1880}
1881
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001882static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
1884 file_accessed(file);
1885 vma->vm_ops = &shmem_vm_ops;
1886 return 0;
1887}
1888
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001889static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001890 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct inode *inode;
1893 struct shmem_inode_info *info;
1894 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1895
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001896 if (shmem_reserve_inode(sb))
1897 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 inode = new_inode(sb);
1900 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001901 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001902 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001905 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 info = SHMEM_I(inode);
1907 memset(info, 0, (char *)inode - (char *)info);
1908 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07001909 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001910 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001912 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04001913 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 switch (mode & S_IFMT) {
1916 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001917 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 init_special_inode(inode, mode, dev);
1919 break;
1920 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001921 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 inode->i_op = &shmem_inode_operations;
1923 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001924 mpol_shared_policy_init(&info->policy,
1925 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001928 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* Some things misbehave if size == 0 on a directory */
1930 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1931 inode->i_op = &shmem_dir_inode_operations;
1932 inode->i_fop = &simple_dir_operations;
1933 break;
1934 case S_IFLNK:
1935 /*
1936 * Must not load anything in the rbtree,
1937 * mpol_free_shared_policy will not be called.
1938 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001939 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001942 } else
1943 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return inode;
1945}
1946
Johannes Weiner0cd61442014-04-03 14:47:46 -07001947bool shmem_mapping(struct address_space *mapping)
1948{
Sasha Levinf0774d82015-02-23 05:38:00 -05001949 if (!mapping->host)
1950 return false;
1951
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001952 return mapping->host->i_sb->s_op == &shmem_ops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001953}
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001956static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001957static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001959#ifdef CONFIG_TMPFS_XATTR
1960static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1961#else
1962#define shmem_initxattrs NULL
1963#endif
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001966shmem_write_begin(struct file *file, struct address_space *mapping,
1967 loff_t pos, unsigned len, unsigned flags,
1968 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Nick Piggin800d15a2007-10-16 01:25:03 -07001970 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07001971 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001972 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07001973
1974 /* i_mutex is held by caller */
1975 if (unlikely(info->seals)) {
1976 if (info->seals & F_SEAL_WRITE)
1977 return -EPERM;
1978 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1979 return -EPERM;
1980 }
1981
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001982 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07001983}
1984
1985static int
1986shmem_write_end(struct file *file, struct address_space *mapping,
1987 loff_t pos, unsigned len, unsigned copied,
1988 struct page *page, void *fsdata)
1989{
1990 struct inode *inode = mapping->host;
1991
Hugh Dickinsd3602442008-02-04 22:28:44 -08001992 if (pos + copied > inode->i_size)
1993 i_size_write(inode, pos + copied);
1994
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001995 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001996 struct page *head = compound_head(page);
1997 if (PageTransCompound(page)) {
1998 int i;
1999
2000 for (i = 0; i < HPAGE_PMD_NR; i++) {
2001 if (head + i == page)
2002 continue;
2003 clear_highpage(head + i);
2004 flush_dcache_page(head + i);
2005 }
2006 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002007 if (copied < PAGE_SIZE) {
2008 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002009 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002010 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002011 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002012 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002013 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002014 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002015 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002016 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002017
Nick Piggin800d15a2007-10-16 01:25:03 -07002018 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Al Viro2ba5bbe2014-04-02 20:00:02 -04002021static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Al Viro6e58e792014-02-03 17:07:03 -05002023 struct file *file = iocb->ki_filp;
2024 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002026 pgoff_t index;
2027 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002028 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002029 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002030 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002031 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002032
2033 /*
2034 * Might this read be for a stacking filesystem? Then when reading
2035 * holes of a sparse file, we actually need to allocate those pages,
2036 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2037 */
Al Viro777eda22014-12-17 04:46:46 -05002038 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002039 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002041 index = *ppos >> PAGE_SHIFT;
2042 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 for (;;) {
2045 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002046 pgoff_t end_index;
2047 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 loff_t i_size = i_size_read(inode);
2049
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002050 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (index > end_index)
2052 break;
2053 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002054 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (nr <= offset)
2056 break;
2057 }
2058
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002059 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002060 if (error) {
2061 if (error == -EINVAL)
2062 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 break;
2064 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002065 if (page) {
2066 if (sgp == SGP_CACHE)
2067 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002068 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 /*
2072 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002073 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002077 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (nr <= offset) {
2081 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
2084 }
2085 }
2086 nr -= offset;
2087
2088 if (page) {
2089 /*
2090 * If users can be writing to this page using arbitrary
2091 * virtual addresses, take care about potential aliasing
2092 * before reading the page on the kernel side.
2093 */
2094 if (mapping_writably_mapped(mapping))
2095 flush_dcache_page(page);
2096 /*
2097 * Mark the page accessed if we read the beginning.
2098 */
2099 if (!offset)
2100 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002101 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /*
2107 * Ok, we have the page, and it's up-to-date, so
2108 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002110 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002111 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 index += offset >> PAGE_SHIFT;
2114 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002116 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002117 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 break;
Al Viro6e58e792014-02-03 17:07:03 -05002119 if (ret < nr) {
2120 error = -EFAULT;
2121 break;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 cond_resched();
2124 }
2125
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002126 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002127 file_accessed(file);
2128 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Hugh Dickins708e3502011-07-25 17:12:32 -07002131static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
2132 struct pipe_inode_info *pipe, size_t len,
2133 unsigned int flags)
2134{
2135 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002136 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07002137 unsigned int loff, nr_pages, req_pages;
2138 struct page *pages[PIPE_DEF_BUFFERS];
2139 struct partial_page partial[PIPE_DEF_BUFFERS];
2140 struct page *page;
2141 pgoff_t index, end_index;
2142 loff_t isize, left;
2143 int error, page_nr;
2144 struct splice_pipe_desc spd = {
2145 .pages = pages,
2146 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002147 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07002148 .flags = flags,
2149 .ops = &page_cache_pipe_buf_ops,
2150 .spd_release = spd_release_page,
2151 };
2152
Hugh Dickins71f0e072011-07-25 17:12:33 -07002153 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07002154 if (unlikely(*ppos >= isize))
2155 return 0;
2156
2157 left = isize - *ppos;
2158 if (unlikely(left < len))
2159 len = left;
2160
2161 if (splice_grow_spd(pipe, &spd))
2162 return -ENOMEM;
2163
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002164 index = *ppos >> PAGE_SHIFT;
2165 loff = *ppos & ~PAGE_MASK;
2166 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Viroa786c062014-04-11 12:01:03 -04002167 nr_pages = min(req_pages, spd.nr_pages_max);
Hugh Dickins708e3502011-07-25 17:12:32 -07002168
Hugh Dickins708e3502011-07-25 17:12:32 -07002169 spd.nr_pages = find_get_pages_contig(mapping, index,
2170 nr_pages, spd.pages);
2171 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07002172 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002173
Hugh Dickins708e3502011-07-25 17:12:32 -07002174 while (spd.nr_pages < nr_pages) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002175 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002176 if (error)
2177 break;
2178 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07002179 spd.pages[spd.nr_pages++] = page;
2180 index++;
2181 }
2182
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002183 index = *ppos >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002184 nr_pages = spd.nr_pages;
2185 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002186
Hugh Dickins708e3502011-07-25 17:12:32 -07002187 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
2188 unsigned int this_len;
2189
2190 if (!len)
2191 break;
2192
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002193 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
Hugh Dickins708e3502011-07-25 17:12:32 -07002194 page = spd.pages[page_nr];
2195
Hugh Dickins71f0e072011-07-25 17:12:33 -07002196 if (!PageUptodate(page) || page->mapping != mapping) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002197 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002198 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07002199 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002200 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002201 put_page(spd.pages[page_nr]);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002202 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07002203 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07002204
2205 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002206 end_index = (isize - 1) >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002207 if (unlikely(!isize || index > end_index))
2208 break;
2209
Hugh Dickins708e3502011-07-25 17:12:32 -07002210 if (end_index == index) {
2211 unsigned int plen;
2212
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002213 plen = ((isize - 1) & ~PAGE_MASK) + 1;
Hugh Dickins708e3502011-07-25 17:12:32 -07002214 if (plen <= loff)
2215 break;
2216
Hugh Dickins708e3502011-07-25 17:12:32 -07002217 this_len = min(this_len, plen - loff);
2218 len = this_len;
2219 }
2220
2221 spd.partial[page_nr].offset = loff;
2222 spd.partial[page_nr].len = this_len;
2223 len -= this_len;
2224 loff = 0;
2225 spd.nr_pages++;
2226 index++;
2227 }
2228
Hugh Dickins708e3502011-07-25 17:12:32 -07002229 while (page_nr < nr_pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002230 put_page(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07002231
2232 if (spd.nr_pages)
2233 error = splice_to_pipe(pipe, &spd);
2234
Eric Dumazet047fe362012-06-12 15:24:40 +02002235 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07002236
2237 if (error > 0) {
2238 *ppos += error;
2239 file_accessed(in);
2240 }
2241 return error;
2242}
2243
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002244/*
2245 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2246 */
2247static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002248 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002249{
2250 struct page *page;
2251 struct pagevec pvec;
2252 pgoff_t indices[PAGEVEC_SIZE];
2253 bool done = false;
2254 int i;
2255
2256 pagevec_init(&pvec, 0);
2257 pvec.nr = 1; /* start small: we may be there already */
2258 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002259 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002260 pvec.nr, pvec.pages, indices);
2261 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002262 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002263 index = end;
2264 break;
2265 }
2266 for (i = 0; i < pvec.nr; i++, index++) {
2267 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002268 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002269 done = true;
2270 break;
2271 }
2272 index = indices[i];
2273 }
2274 page = pvec.pages[i];
2275 if (page && !radix_tree_exceptional_entry(page)) {
2276 if (!PageUptodate(page))
2277 page = NULL;
2278 }
2279 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002280 (page && whence == SEEK_DATA) ||
2281 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002282 done = true;
2283 break;
2284 }
2285 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002286 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002287 pagevec_release(&pvec);
2288 pvec.nr = PAGEVEC_SIZE;
2289 cond_resched();
2290 }
2291 return index;
2292}
2293
Andrew Morton965c8e52012-12-17 15:59:39 -08002294static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002295{
2296 struct address_space *mapping = file->f_mapping;
2297 struct inode *inode = mapping->host;
2298 pgoff_t start, end;
2299 loff_t new_offset;
2300
Andrew Morton965c8e52012-12-17 15:59:39 -08002301 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2302 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002303 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002304 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002305 /* We're holding i_mutex so we can access i_size directly */
2306
2307 if (offset < 0)
2308 offset = -EINVAL;
2309 else if (offset >= inode->i_size)
2310 offset = -ENXIO;
2311 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002312 start = offset >> PAGE_SHIFT;
2313 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002314 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002315 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002316 if (new_offset > offset) {
2317 if (new_offset < inode->i_size)
2318 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002319 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002320 offset = -ENXIO;
2321 else
2322 offset = inode->i_size;
2323 }
2324 }
2325
Hugh Dickins387aae62013-08-04 11:30:25 -07002326 if (offset >= 0)
2327 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002328 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002329 return offset;
2330}
2331
David Herrmann05f65b52014-08-08 14:25:36 -07002332/*
2333 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
2334 * so reuse a tag which we firmly believe is never set or cleared on shmem.
2335 */
2336#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
2337#define LAST_SCAN 4 /* about 150ms max */
2338
2339static void shmem_tag_pins(struct address_space *mapping)
2340{
2341 struct radix_tree_iter iter;
2342 void **slot;
2343 pgoff_t start;
2344 struct page *page;
2345
2346 lru_add_drain();
2347 start = 0;
2348 rcu_read_lock();
2349
David Herrmann05f65b52014-08-08 14:25:36 -07002350 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
2351 page = radix_tree_deref_slot(slot);
2352 if (!page || radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002353 if (radix_tree_deref_retry(page)) {
2354 slot = radix_tree_iter_retry(&iter);
2355 continue;
2356 }
David Herrmann05f65b52014-08-08 14:25:36 -07002357 } else if (page_count(page) - page_mapcount(page) > 1) {
2358 spin_lock_irq(&mapping->tree_lock);
2359 radix_tree_tag_set(&mapping->page_tree, iter.index,
2360 SHMEM_TAG_PINNED);
2361 spin_unlock_irq(&mapping->tree_lock);
2362 }
2363
2364 if (need_resched()) {
2365 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002366 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002367 }
2368 }
2369 rcu_read_unlock();
2370}
2371
2372/*
2373 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
2374 * via get_user_pages(), drivers might have some pending I/O without any active
2375 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
2376 * and see whether it has an elevated ref-count. If so, we tag them and wait for
2377 * them to be dropped.
2378 * The caller must guarantee that no new user will acquire writable references
2379 * to those pages to avoid races.
2380 */
David Herrmann40e041a2014-08-08 14:25:27 -07002381static int shmem_wait_for_pins(struct address_space *mapping)
2382{
David Herrmann05f65b52014-08-08 14:25:36 -07002383 struct radix_tree_iter iter;
2384 void **slot;
2385 pgoff_t start;
2386 struct page *page;
2387 int error, scan;
2388
2389 shmem_tag_pins(mapping);
2390
2391 error = 0;
2392 for (scan = 0; scan <= LAST_SCAN; scan++) {
2393 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
2394 break;
2395
2396 if (!scan)
2397 lru_add_drain_all();
2398 else if (schedule_timeout_killable((HZ << scan) / 200))
2399 scan = LAST_SCAN;
2400
2401 start = 0;
2402 rcu_read_lock();
David Herrmann05f65b52014-08-08 14:25:36 -07002403 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
2404 start, SHMEM_TAG_PINNED) {
2405
2406 page = radix_tree_deref_slot(slot);
2407 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002408 if (radix_tree_deref_retry(page)) {
2409 slot = radix_tree_iter_retry(&iter);
2410 continue;
2411 }
David Herrmann05f65b52014-08-08 14:25:36 -07002412
2413 page = NULL;
2414 }
2415
2416 if (page &&
2417 page_count(page) - page_mapcount(page) != 1) {
2418 if (scan < LAST_SCAN)
2419 goto continue_resched;
2420
2421 /*
2422 * On the last scan, we clean up all those tags
2423 * we inserted; but make a note that we still
2424 * found pages pinned.
2425 */
2426 error = -EBUSY;
2427 }
2428
2429 spin_lock_irq(&mapping->tree_lock);
2430 radix_tree_tag_clear(&mapping->page_tree,
2431 iter.index, SHMEM_TAG_PINNED);
2432 spin_unlock_irq(&mapping->tree_lock);
2433continue_resched:
2434 if (need_resched()) {
2435 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002436 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002437 }
2438 }
2439 rcu_read_unlock();
2440 }
2441
2442 return error;
David Herrmann40e041a2014-08-08 14:25:27 -07002443}
2444
2445#define F_ALL_SEALS (F_SEAL_SEAL | \
2446 F_SEAL_SHRINK | \
2447 F_SEAL_GROW | \
2448 F_SEAL_WRITE)
2449
2450int shmem_add_seals(struct file *file, unsigned int seals)
2451{
2452 struct inode *inode = file_inode(file);
2453 struct shmem_inode_info *info = SHMEM_I(inode);
2454 int error;
2455
2456 /*
2457 * SEALING
2458 * Sealing allows multiple parties to share a shmem-file but restrict
2459 * access to a specific subset of file operations. Seals can only be
2460 * added, but never removed. This way, mutually untrusted parties can
2461 * share common memory regions with a well-defined policy. A malicious
2462 * peer can thus never perform unwanted operations on a shared object.
2463 *
2464 * Seals are only supported on special shmem-files and always affect
2465 * the whole underlying inode. Once a seal is set, it may prevent some
2466 * kinds of access to the file. Currently, the following seals are
2467 * defined:
2468 * SEAL_SEAL: Prevent further seals from being set on this file
2469 * SEAL_SHRINK: Prevent the file from shrinking
2470 * SEAL_GROW: Prevent the file from growing
2471 * SEAL_WRITE: Prevent write access to the file
2472 *
2473 * As we don't require any trust relationship between two parties, we
2474 * must prevent seals from being removed. Therefore, sealing a file
2475 * only adds a given set of seals to the file, it never touches
2476 * existing seals. Furthermore, the "setting seals"-operation can be
2477 * sealed itself, which basically prevents any further seal from being
2478 * added.
2479 *
2480 * Semantics of sealing are only defined on volatile files. Only
2481 * anonymous shmem files support sealing. More importantly, seals are
2482 * never written to disk. Therefore, there's no plan to support it on
2483 * other file types.
2484 */
2485
2486 if (file->f_op != &shmem_file_operations)
2487 return -EINVAL;
2488 if (!(file->f_mode & FMODE_WRITE))
2489 return -EPERM;
2490 if (seals & ~(unsigned int)F_ALL_SEALS)
2491 return -EINVAL;
2492
Al Viro59551022016-01-22 15:40:57 -05002493 inode_lock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002494
2495 if (info->seals & F_SEAL_SEAL) {
2496 error = -EPERM;
2497 goto unlock;
2498 }
2499
2500 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2501 error = mapping_deny_writable(file->f_mapping);
2502 if (error)
2503 goto unlock;
2504
2505 error = shmem_wait_for_pins(file->f_mapping);
2506 if (error) {
2507 mapping_allow_writable(file->f_mapping);
2508 goto unlock;
2509 }
2510 }
2511
2512 info->seals |= seals;
2513 error = 0;
2514
2515unlock:
Al Viro59551022016-01-22 15:40:57 -05002516 inode_unlock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002517 return error;
2518}
2519EXPORT_SYMBOL_GPL(shmem_add_seals);
2520
2521int shmem_get_seals(struct file *file)
2522{
2523 if (file->f_op != &shmem_file_operations)
2524 return -EINVAL;
2525
2526 return SHMEM_I(file_inode(file))->seals;
2527}
2528EXPORT_SYMBOL_GPL(shmem_get_seals);
2529
2530long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2531{
2532 long error;
2533
2534 switch (cmd) {
2535 case F_ADD_SEALS:
2536 /* disallow upper 32bit */
2537 if (arg > UINT_MAX)
2538 return -EINVAL;
2539
2540 error = shmem_add_seals(file, arg);
2541 break;
2542 case F_GET_SEALS:
2543 error = shmem_get_seals(file);
2544 break;
2545 default:
2546 error = -EINVAL;
2547 break;
2548 }
2549
2550 return error;
2551}
2552
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002553static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2554 loff_t len)
2555{
Al Viro496ad9a2013-01-23 17:07:38 -05002556 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002557 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002558 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002559 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002560 pgoff_t start, index, end;
2561 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002562
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002563 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2564 return -EOPNOTSUPP;
2565
Al Viro59551022016-01-22 15:40:57 -05002566 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002567
2568 if (mode & FALLOC_FL_PUNCH_HOLE) {
2569 struct address_space *mapping = file->f_mapping;
2570 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2571 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002572 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002573
David Herrmann40e041a2014-08-08 14:25:27 -07002574 /* protected by i_mutex */
2575 if (info->seals & F_SEAL_WRITE) {
2576 error = -EPERM;
2577 goto out;
2578 }
2579
Hugh Dickins8e205f72014-07-23 14:00:10 -07002580 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002581 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2582 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2583 spin_lock(&inode->i_lock);
2584 inode->i_private = &shmem_falloc;
2585 spin_unlock(&inode->i_lock);
2586
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002587 if ((u64)unmap_end > (u64)unmap_start)
2588 unmap_mapping_range(mapping, unmap_start,
2589 1 + unmap_end - unmap_start, 0);
2590 shmem_truncate_range(inode, offset, offset + len - 1);
2591 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002592
2593 spin_lock(&inode->i_lock);
2594 inode->i_private = NULL;
2595 wake_up_all(&shmem_falloc_waitq);
2596 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002597 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002598 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002599 }
2600
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002601 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2602 error = inode_newsize_ok(inode, offset + len);
2603 if (error)
2604 goto out;
2605
David Herrmann40e041a2014-08-08 14:25:27 -07002606 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2607 error = -EPERM;
2608 goto out;
2609 }
2610
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002611 start = offset >> PAGE_SHIFT;
2612 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002613 /* Try to avoid a swapstorm if len is impossible to satisfy */
2614 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2615 error = -ENOSPC;
2616 goto out;
2617 }
2618
Hugh Dickins8e205f72014-07-23 14:00:10 -07002619 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002620 shmem_falloc.start = start;
2621 shmem_falloc.next = start;
2622 shmem_falloc.nr_falloced = 0;
2623 shmem_falloc.nr_unswapped = 0;
2624 spin_lock(&inode->i_lock);
2625 inode->i_private = &shmem_falloc;
2626 spin_unlock(&inode->i_lock);
2627
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002628 for (index = start; index < end; index++) {
2629 struct page *page;
2630
2631 /*
2632 * Good, the fallocate(2) manpage permits EINTR: we may have
2633 * been interrupted because we are using up too much memory.
2634 */
2635 if (signal_pending(current))
2636 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002637 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2638 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002639 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002640 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002641 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002642 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002643 if (index > start) {
2644 shmem_undo_range(inode,
2645 (loff_t)start << PAGE_SHIFT,
2646 ((loff_t)index << PAGE_SHIFT) - 1, true);
2647 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002648 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002649 }
2650
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002651 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002652 * Inform shmem_writepage() how far we have reached.
2653 * No need for lock or barrier: we have the page lock.
2654 */
2655 shmem_falloc.next++;
2656 if (!PageUptodate(page))
2657 shmem_falloc.nr_falloced++;
2658
2659 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002660 * If !PageUptodate, leave it that way so that freeable pages
2661 * can be recognized if we need to rollback on error later.
2662 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002663 * than free the pages we are allocating (and SGP_CACHE pages
2664 * might still be clean: we now need to mark those dirty too).
2665 */
2666 set_page_dirty(page);
2667 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002668 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002669 cond_resched();
2670 }
2671
2672 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2673 i_size_write(inode, offset + len);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002674 inode->i_ctime = CURRENT_TIME;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002675undone:
2676 spin_lock(&inode->i_lock);
2677 inode->i_private = NULL;
2678 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002679out:
Al Viro59551022016-01-22 15:40:57 -05002680 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002681 return error;
2682}
2683
David Howells726c3342006-06-23 02:02:58 -07002684static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685{
David Howells726c3342006-06-23 02:02:58 -07002686 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002689 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002691 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002693 buf->f_bavail =
2694 buf->f_bfree = sbinfo->max_blocks -
2695 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002696 }
2697 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 buf->f_files = sbinfo->max_inodes;
2699 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701 /* else leave those fields 0 like simple_statfs */
2702 return 0;
2703}
2704
2705/*
2706 * File creation. Allocate an inode, and we're done..
2707 */
2708static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002709shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002711 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 int error = -ENOSPC;
2713
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002714 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002716 error = simple_acl_create(dir, inode);
2717 if (error)
2718 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002719 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002720 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002721 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002722 if (error && error != -EOPNOTSUPP)
2723 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002724
Al Viro718deb62009-12-16 19:35:36 -05002725 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 dir->i_size += BOGO_DIRENT_SIZE;
2727 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2728 d_instantiate(dentry, inode);
2729 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002732out_iput:
2733 iput(inode);
2734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Al Viro60545d02013-06-07 01:20:27 -04002737static int
2738shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2739{
2740 struct inode *inode;
2741 int error = -ENOSPC;
2742
2743 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2744 if (inode) {
2745 error = security_inode_init_security(inode, dir,
2746 NULL,
2747 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002748 if (error && error != -EOPNOTSUPP)
2749 goto out_iput;
2750 error = simple_acl_create(dir, inode);
2751 if (error)
2752 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002753 d_tmpfile(dentry, inode);
2754 }
2755 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002756out_iput:
2757 iput(inode);
2758 return error;
Al Viro60545d02013-06-07 01:20:27 -04002759}
2760
Al Viro18bb1db2011-07-26 01:41:39 -04002761static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
2763 int error;
2764
2765 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2766 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002767 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return 0;
2769}
2770
Al Viro4acdaf22011-07-26 01:42:34 -04002771static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002772 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2775}
2776
2777/*
2778 * Link a file..
2779 */
2780static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2781{
David Howells75c3cfa2015-03-17 22:26:12 +00002782 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002783 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /*
2786 * No ordinary (disk based) filesystem counts links as inodes;
2787 * but each new link needs a new dentry, pinning lowmem, and
2788 * tmpfs dentries cannot be pruned until they are unlinked.
2789 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002790 ret = shmem_reserve_inode(inode->i_sb);
2791 if (ret)
2792 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 dir->i_size += BOGO_DIRENT_SIZE;
2795 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07002796 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002797 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 dget(dentry); /* Extra pinning count for the created dentry */
2799 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002800out:
2801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
2804static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2805{
David Howells75c3cfa2015-03-17 22:26:12 +00002806 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002808 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2809 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 dir->i_size -= BOGO_DIRENT_SIZE;
2812 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002813 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 dput(dentry); /* Undo the count from "create" - this does all the work */
2815 return 0;
2816}
2817
2818static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2819{
2820 if (!simple_empty(dentry))
2821 return -ENOTEMPTY;
2822
David Howells75c3cfa2015-03-17 22:26:12 +00002823 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002824 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return shmem_unlink(dir, dentry);
2826}
2827
Miklos Szeredi37456772014-07-23 15:15:34 +02002828static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2829{
David Howellse36cb0b2015-01-29 12:02:35 +00002830 bool old_is_dir = d_is_dir(old_dentry);
2831 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002832
2833 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2834 if (old_is_dir) {
2835 drop_nlink(old_dir);
2836 inc_nlink(new_dir);
2837 } else {
2838 drop_nlink(new_dir);
2839 inc_nlink(old_dir);
2840 }
2841 }
2842 old_dir->i_ctime = old_dir->i_mtime =
2843 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002844 d_inode(old_dentry)->i_ctime =
2845 d_inode(new_dentry)->i_ctime = CURRENT_TIME;
Miklos Szeredi37456772014-07-23 15:15:34 +02002846
2847 return 0;
2848}
2849
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002850static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2851{
2852 struct dentry *whiteout;
2853 int error;
2854
2855 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2856 if (!whiteout)
2857 return -ENOMEM;
2858
2859 error = shmem_mknod(old_dir, whiteout,
2860 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2861 dput(whiteout);
2862 if (error)
2863 return error;
2864
2865 /*
2866 * Cheat and hash the whiteout while the old dentry is still in
2867 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2868 *
2869 * d_lookup() will consistently find one of them at this point,
2870 * not sure which one, but that isn't even important.
2871 */
2872 d_rehash(whiteout);
2873 return 0;
2874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876/*
2877 * The VFS layer already does all the dentry stuff for rename,
2878 * we just have to decrement the usage count for the target if
2879 * it exists so that the VFS layer correctly free's it when it
2880 * gets overwritten.
2881 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002882static 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 -07002883{
David Howells75c3cfa2015-03-17 22:26:12 +00002884 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int they_are_dirs = S_ISDIR(inode->i_mode);
2886
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002887 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002888 return -EINVAL;
2889
Miklos Szeredi37456772014-07-23 15:15:34 +02002890 if (flags & RENAME_EXCHANGE)
2891 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (!simple_empty(new_dentry))
2894 return -ENOTEMPTY;
2895
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002896 if (flags & RENAME_WHITEOUT) {
2897 int error;
2898
2899 error = shmem_whiteout(old_dir, old_dentry);
2900 if (error)
2901 return error;
2902 }
2903
David Howells75c3cfa2015-03-17 22:26:12 +00002904 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002906 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002907 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002908 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002911 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002912 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914
2915 old_dir->i_size -= BOGO_DIRENT_SIZE;
2916 new_dir->i_size += BOGO_DIRENT_SIZE;
2917 old_dir->i_ctime = old_dir->i_mtime =
2918 new_dir->i_ctime = new_dir->i_mtime =
2919 inode->i_ctime = CURRENT_TIME;
2920 return 0;
2921}
2922
2923static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2924{
2925 int error;
2926 int len;
2927 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002928 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 struct shmem_inode_info *info;
2930
2931 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002932 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return -ENAMETOOLONG;
2934
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002935 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (!inode)
2937 return -ENOSPC;
2938
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002939 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002940 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002941 if (error) {
2942 if (error != -EOPNOTSUPP) {
2943 iput(inode);
2944 return error;
2945 }
2946 error = 0;
2947 }
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 info = SHMEM_I(inode);
2950 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002951 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05002952 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
2953 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002954 iput(inode);
2955 return -ENOMEM;
2956 }
2957 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05002959 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002960 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (error) {
2962 iput(inode);
2963 return error;
2964 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07002965 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05002967 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002968 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002970 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002971 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 dir->i_size += BOGO_DIRENT_SIZE;
2974 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2975 d_instantiate(dentry, inode);
2976 dget(dentry);
2977 return 0;
2978}
2979
Al Virofceef392015-12-29 15:58:39 -05002980static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Al Virofceef392015-12-29 15:58:39 -05002982 mark_page_accessed(arg);
2983 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
Al Viro6b255392015-11-17 10:20:54 -05002986static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002987 struct inode *inode,
2988 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05002991 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05002992 if (!dentry) {
2993 page = find_get_page(inode->i_mapping, 0);
2994 if (!page)
2995 return ERR_PTR(-ECHILD);
2996 if (!PageUptodate(page)) {
2997 put_page(page);
2998 return ERR_PTR(-ECHILD);
2999 }
3000 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003001 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003002 if (error)
3003 return ERR_PTR(error);
3004 unlock_page(page);
3005 }
Al Virofceef392015-12-29 15:58:39 -05003006 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003007 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Eric Parisb09e0fa2011-05-24 17:12:39 -07003010#ifdef CONFIG_TMPFS_XATTR
3011/*
3012 * Superblocks without xattr inode operations may get some security.* xattr
3013 * support from the LSM "for free". As soon as we have any other xattrs
3014 * like ACLs, we also need to implement the security.* handlers at
3015 * filesystem level, though.
3016 */
3017
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003018/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003019 * Callback for security_inode_init_security() for acquiring xattrs.
3020 */
3021static int shmem_initxattrs(struct inode *inode,
3022 const struct xattr *xattr_array,
3023 void *fs_info)
3024{
3025 struct shmem_inode_info *info = SHMEM_I(inode);
3026 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003027 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003028 size_t len;
3029
3030 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003031 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003032 if (!new_xattr)
3033 return -ENOMEM;
3034
3035 len = strlen(xattr->name) + 1;
3036 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3037 GFP_KERNEL);
3038 if (!new_xattr->name) {
3039 kfree(new_xattr);
3040 return -ENOMEM;
3041 }
3042
3043 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3044 XATTR_SECURITY_PREFIX_LEN);
3045 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3046 xattr->name, len);
3047
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003048 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003049 }
3050
3051 return 0;
3052}
3053
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003054static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003055 struct dentry *unused, struct inode *inode,
3056 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003057{
Al Virob2968212016-04-10 20:48:24 -04003058 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003059
3060 name = xattr_full_name(handler, name);
3061 return simple_xattr_get(&info->xattrs, name, buffer, size);
3062}
3063
3064static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003065 struct dentry *unused, struct inode *inode,
3066 const char *name, const void *value,
3067 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003068{
Al Viro59301222016-05-27 10:19:30 -04003069 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003070
3071 name = xattr_full_name(handler, name);
3072 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3073}
3074
3075static const struct xattr_handler shmem_security_xattr_handler = {
3076 .prefix = XATTR_SECURITY_PREFIX,
3077 .get = shmem_xattr_handler_get,
3078 .set = shmem_xattr_handler_set,
3079};
3080
3081static const struct xattr_handler shmem_trusted_xattr_handler = {
3082 .prefix = XATTR_TRUSTED_PREFIX,
3083 .get = shmem_xattr_handler_get,
3084 .set = shmem_xattr_handler_set,
3085};
3086
Eric Parisb09e0fa2011-05-24 17:12:39 -07003087static const struct xattr_handler *shmem_xattr_handlers[] = {
3088#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003089 &posix_acl_access_xattr_handler,
3090 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003091#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003092 &shmem_security_xattr_handler,
3093 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003094 NULL
3095};
3096
Eric Parisb09e0fa2011-05-24 17:12:39 -07003097static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3098{
David Howells75c3cfa2015-03-17 22:26:12 +00003099 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003100 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003101}
3102#endif /* CONFIG_TMPFS_XATTR */
3103
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003104static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003106 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003107#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003108 .setxattr = generic_setxattr,
3109 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003110 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003111 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003112#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113};
3114
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003115static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003117 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003118#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003119 .setxattr = generic_setxattr,
3120 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003121 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003122 .removexattr = generic_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003123#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003124};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003125
David M. Grimes91828a42006-10-17 00:09:45 -07003126static struct dentry *shmem_get_parent(struct dentry *child)
3127{
3128 return ERR_PTR(-ESTALE);
3129}
3130
3131static int shmem_match(struct inode *ino, void *vfh)
3132{
3133 __u32 *fh = vfh;
3134 __u64 inum = fh[2];
3135 inum = (inum << 32) | fh[1];
3136 return ino->i_ino == inum && fh[0] == ino->i_generation;
3137}
3138
Christoph Hellwig480b1162007-10-21 16:42:13 -07003139static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3140 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003141{
David M. Grimes91828a42006-10-17 00:09:45 -07003142 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003143 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003144 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003145
Christoph Hellwig480b1162007-10-21 16:42:13 -07003146 if (fh_len < 3)
3147 return NULL;
3148
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003149 inum = fid->raw[2];
3150 inum = (inum << 32) | fid->raw[1];
3151
Christoph Hellwig480b1162007-10-21 16:42:13 -07003152 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3153 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003154 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07003155 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003156 iput(inode);
3157 }
3158
Christoph Hellwig480b1162007-10-21 16:42:13 -07003159 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003160}
3161
Al Virob0b03822012-04-02 14:34:06 -04003162static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3163 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003164{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303165 if (*len < 3) {
3166 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003167 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303168 }
David M. Grimes91828a42006-10-17 00:09:45 -07003169
Al Viro1d3382cb2010-10-23 15:19:20 -04003170 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003171 /* Unfortunately insert_inode_hash is not idempotent,
3172 * so as we hash inodes here rather than at creation
3173 * time, we need a lock to ensure we only try
3174 * to do it once
3175 */
3176 static DEFINE_SPINLOCK(lock);
3177 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003178 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003179 __insert_inode_hash(inode,
3180 inode->i_ino + inode->i_generation);
3181 spin_unlock(&lock);
3182 }
3183
3184 fh[0] = inode->i_generation;
3185 fh[1] = inode->i_ino;
3186 fh[2] = ((__u64)inode->i_ino) >> 32;
3187
3188 *len = 3;
3189 return 1;
3190}
3191
Christoph Hellwig39655162007-10-21 16:42:17 -07003192static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003193 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003194 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003195 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003196};
3197
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003198static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3199 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
3201 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003202 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003203 uid_t uid;
3204 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003206 while (options != NULL) {
3207 this_char = options;
3208 for (;;) {
3209 /*
3210 * NUL-terminate this option: unfortunately,
3211 * mount options form a comma-separated list,
3212 * but mpol's nodelist may also contain commas.
3213 */
3214 options = strchr(options, ',');
3215 if (options == NULL)
3216 break;
3217 options++;
3218 if (!isdigit(*options)) {
3219 options[-1] = '\0';
3220 break;
3221 }
3222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 if (!*this_char)
3224 continue;
3225 if ((value = strchr(this_char,'=')) != NULL) {
3226 *value++ = 0;
3227 } else {
Joe Perches11705322016-03-17 14:19:50 -07003228 pr_err("tmpfs: No value for mount option '%s'\n",
3229 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003230 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 }
3232
3233 if (!strcmp(this_char,"size")) {
3234 unsigned long long size;
3235 size = memparse(value,&rest);
3236 if (*rest == '%') {
3237 size <<= PAGE_SHIFT;
3238 size *= totalram_pages;
3239 do_div(size, 100);
3240 rest++;
3241 }
3242 if (*rest)
3243 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003244 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003245 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003247 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (*rest)
3249 goto bad_val;
3250 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003251 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (*rest)
3253 goto bad_val;
3254 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003255 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003257 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 if (*rest)
3259 goto bad_val;
3260 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003261 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003263 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (*rest)
3265 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003266 sbinfo->uid = make_kuid(current_user_ns(), uid);
3267 if (!uid_valid(sbinfo->uid))
3268 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003270 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003272 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (*rest)
3274 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003275 sbinfo->gid = make_kgid(current_user_ns(), gid);
3276 if (!gid_valid(sbinfo->gid))
3277 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003278#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3279 } else if (!strcmp(this_char, "huge")) {
3280 int huge;
3281 huge = shmem_parse_huge(value);
3282 if (huge < 0)
3283 goto bad_val;
3284 if (!has_transparent_hugepage() &&
3285 huge != SHMEM_HUGE_NEVER)
3286 goto bad_val;
3287 sbinfo->huge = huge;
3288#endif
3289#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003290 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003291 mpol_put(mpol);
3292 mpol = NULL;
3293 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003294 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003295#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 } else {
Joe Perches11705322016-03-17 14:19:50 -07003297 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003298 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
3300 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003301 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return 0;
3303
3304bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003305 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003307error:
3308 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return 1;
3310
3311}
3312
3313static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3314{
3315 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003316 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003317 unsigned long inodes;
3318 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Greg Thelen5f001102013-02-22 16:36:01 -08003320 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003321 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003322 return error;
3323
3324 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003325 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003326 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003327 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003328 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003329 goto out;
3330 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003331 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003332 * but we must separately disallow unlimited->limited, because
3333 * in that case we have no record of how much is already in use.
3334 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003335 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003336 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003337 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003338 goto out;
3339
3340 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003341 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003342 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003343 sbinfo->max_inodes = config.max_inodes;
3344 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003345
Greg Thelen5f001102013-02-22 16:36:01 -08003346 /*
3347 * Preserve previous mempolicy unless mpol remount option was specified.
3348 */
3349 if (config.mpol) {
3350 mpol_put(sbinfo->mpol);
3351 sbinfo->mpol = config.mpol; /* transfers initial ref */
3352 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003353out:
3354 spin_unlock(&sbinfo->stat_lock);
3355 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003357
Al Viro34c80b12011-12-08 21:32:45 -05003358static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003359{
Al Viro34c80b12011-12-08 21:32:45 -05003360 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003361
3362 if (sbinfo->max_blocks != shmem_default_max_blocks())
3363 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003364 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003365 if (sbinfo->max_inodes != shmem_default_max_inodes())
3366 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
3367 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003368 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003369 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3370 seq_printf(seq, ",uid=%u",
3371 from_kuid_munged(&init_user_ns, sbinfo->uid));
3372 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3373 seq_printf(seq, ",gid=%u",
3374 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003375#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3376 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3377 if (sbinfo->huge)
3378 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3379#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003380 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003381 return 0;
3382}
David Herrmann9183df22014-08-08 14:25:29 -07003383
3384#define MFD_NAME_PREFIX "memfd:"
3385#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
3386#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
3387
3388#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
3389
3390SYSCALL_DEFINE2(memfd_create,
3391 const char __user *, uname,
3392 unsigned int, flags)
3393{
3394 struct shmem_inode_info *info;
3395 struct file *file;
3396 int fd, error;
3397 char *name;
3398 long len;
3399
3400 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
3401 return -EINVAL;
3402
3403 /* length includes terminating zero */
3404 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
3405 if (len <= 0)
3406 return -EFAULT;
3407 if (len > MFD_NAME_MAX_LEN + 1)
3408 return -EINVAL;
3409
3410 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
3411 if (!name)
3412 return -ENOMEM;
3413
3414 strcpy(name, MFD_NAME_PREFIX);
3415 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
3416 error = -EFAULT;
3417 goto err_name;
3418 }
3419
3420 /* terminating-zero may have changed after strnlen_user() returned */
3421 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
3422 error = -EFAULT;
3423 goto err_name;
3424 }
3425
3426 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
3427 if (fd < 0) {
3428 error = fd;
3429 goto err_name;
3430 }
3431
3432 file = shmem_file_setup(name, 0, VM_NORESERVE);
3433 if (IS_ERR(file)) {
3434 error = PTR_ERR(file);
3435 goto err_fd;
3436 }
3437 info = SHMEM_I(file_inode(file));
3438 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
3439 file->f_flags |= O_RDWR | O_LARGEFILE;
3440 if (flags & MFD_ALLOW_SEALING)
3441 info->seals &= ~F_SEAL_SEAL;
3442
3443 fd_install(fd, file);
3444 kfree(name);
3445 return fd;
3446
3447err_fd:
3448 put_unused_fd(fd);
3449err_name:
3450 kfree(name);
3451 return error;
3452}
3453
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003454#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456static void shmem_put_super(struct super_block *sb)
3457{
Hugh Dickins602586a2010-08-17 15:23:56 -07003458 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3459
3460 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003461 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003462 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 sb->s_fs_info = NULL;
3464}
3465
Kay Sievers2b2af542009-04-30 15:23:42 +02003466int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467{
3468 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003469 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003470 int err = -ENOMEM;
3471
3472 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003473 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003474 L1_CACHE_BYTES), GFP_KERNEL);
3475 if (!sbinfo)
3476 return -ENOMEM;
3477
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003478 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003479 sbinfo->uid = current_fsuid();
3480 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003481 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003483#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /*
3485 * Per default we only allow half of the physical ram per
3486 * tmpfs instance, limiting inodes to one per page of lowmem;
3487 * but the internal instance is left unlimited.
3488 */
Al Viroca4e0512013-08-31 12:57:10 -04003489 if (!(sb->s_flags & MS_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003490 sbinfo->max_blocks = shmem_default_max_blocks();
3491 sbinfo->max_inodes = shmem_default_max_inodes();
3492 if (shmem_parse_options(data, sbinfo, false)) {
3493 err = -EINVAL;
3494 goto failed;
3495 }
Al Viroca4e0512013-08-31 12:57:10 -04003496 } else {
3497 sb->s_flags |= MS_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
David M. Grimes91828a42006-10-17 00:09:45 -07003499 sb->s_export_op = &shmem_export_ops;
Hugh Dickins2f6e38f2012-05-29 15:06:38 -07003500 sb->s_flags |= MS_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501#else
3502 sb->s_flags |= MS_NOUSER;
3503#endif
3504
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003505 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003506 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003507 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003508 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003509
Hugh Dickins285b2c42011-08-03 16:21:20 -07003510 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003511 sb->s_blocksize = PAGE_SIZE;
3512 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 sb->s_magic = TMPFS_MAGIC;
3514 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003515 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003516#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003517 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003518#endif
3519#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003520 sb->s_flags |= MS_POSIXACL;
3521#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003522
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003523 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (!inode)
3525 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003526 inode->i_uid = sbinfo->uid;
3527 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003528 sb->s_root = d_make_root(inode);
3529 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003530 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return 0;
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533failed:
3534 shmem_put_super(sb);
3535 return err;
3536}
3537
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003538static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540static struct inode *shmem_alloc_inode(struct super_block *sb)
3541{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003542 struct shmem_inode_info *info;
3543 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3544 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003546 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003549static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003550{
3551 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003552 if (S_ISLNK(inode->i_mode))
3553 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003554 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3555}
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557static void shmem_destroy_inode(struct inode *inode)
3558{
Al Viro09208d12011-07-26 03:15:03 -04003559 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003561 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003564static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003566 struct shmem_inode_info *info = foo;
3567 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003570static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3573 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003574 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return 0;
3576}
3577
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003578static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003580 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003583static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003585 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003587 .write_begin = shmem_write_begin,
3588 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003590#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003591 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003592#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003593 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594};
3595
Helge Deller15ad7cd2006-12-06 20:40:36 -08003596static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003598 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003600 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003601 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003602 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003603 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07003604 .splice_read = shmem_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003605 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003606 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607#endif
3608};
3609
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003610static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003611 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003612 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003613#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003614 .setxattr = generic_setxattr,
3615 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003616 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003617 .removexattr = generic_removexattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003618 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620};
3621
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003622static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623#ifdef CONFIG_TMPFS
3624 .create = shmem_create,
3625 .lookup = simple_lookup,
3626 .link = shmem_link,
3627 .unlink = shmem_unlink,
3628 .symlink = shmem_symlink,
3629 .mkdir = shmem_mkdir,
3630 .rmdir = shmem_rmdir,
3631 .mknod = shmem_mknod,
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003632 .rename2 = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003633 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003635#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003636 .setxattr = generic_setxattr,
3637 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003638 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003639 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003640#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003641#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003642 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003643 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003644#endif
3645};
3646
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003647static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003648#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003649 .setxattr = generic_setxattr,
3650 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003651 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003652 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003653#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003654#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003655 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003656 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658};
3659
Hugh Dickins759b9772007-03-05 00:30:28 -08003660static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 .alloc_inode = shmem_alloc_inode,
3662 .destroy_inode = shmem_destroy_inode,
3663#ifdef CONFIG_TMPFS
3664 .statfs = shmem_statfs,
3665 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003666 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667#endif
Al Viro1f895f72010-06-05 19:10:41 -04003668 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 .drop_inode = generic_delete_inode,
3670 .put_super = shmem_put_super,
3671};
3672
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003673static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003674 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003675 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676#ifdef CONFIG_NUMA
3677 .set_policy = shmem_set_policy,
3678 .get_policy = shmem_get_policy,
3679#endif
3680};
3681
Al Viro3c26ff62010-07-25 11:46:36 +04003682static struct dentry *shmem_mount(struct file_system_type *fs_type,
3683 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Al Viro3c26ff62010-07-25 11:46:36 +04003685 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003688static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .owner = THIS_MODULE,
3690 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003691 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003693 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003696int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 int error;
3699
Rob Landley16203a72013-09-11 14:26:12 -07003700 /* If rootfs called this, don't re-init */
3701 if (shmem_inode_cachep)
3702 return 0;
3703
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003704 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (error)
3706 goto out3;
3707
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003708 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003710 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 goto out2;
3712 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003713
Al Viroca4e0512013-08-31 12:57:10 -04003714 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 if (IS_ERR(shm_mnt)) {
3716 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003717 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 goto out1;
3719 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003720
3721#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3722 if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY)
3723 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3724 else
3725 shmem_huge = 0; /* just in case it was patched */
3726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return 0;
3728
3729out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003730 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003732 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733out3:
3734 shm_mnt = ERR_PTR(error);
3735 return error;
3736}
Matt Mackall853ac432009-01-06 14:40:20 -08003737
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003738#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
3739static ssize_t shmem_enabled_show(struct kobject *kobj,
3740 struct kobj_attribute *attr, char *buf)
3741{
3742 int values[] = {
3743 SHMEM_HUGE_ALWAYS,
3744 SHMEM_HUGE_WITHIN_SIZE,
3745 SHMEM_HUGE_ADVISE,
3746 SHMEM_HUGE_NEVER,
3747 SHMEM_HUGE_DENY,
3748 SHMEM_HUGE_FORCE,
3749 };
3750 int i, count;
3751
3752 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3753 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3754
3755 count += sprintf(buf + count, fmt,
3756 shmem_format_huge(values[i]));
3757 }
3758 buf[count - 1] = '\n';
3759 return count;
3760}
3761
3762static ssize_t shmem_enabled_store(struct kobject *kobj,
3763 struct kobj_attribute *attr, const char *buf, size_t count)
3764{
3765 char tmp[16];
3766 int huge;
3767
3768 if (count + 1 > sizeof(tmp))
3769 return -EINVAL;
3770 memcpy(tmp, buf, count);
3771 tmp[count] = '\0';
3772 if (count && tmp[count - 1] == '\n')
3773 tmp[count - 1] = '\0';
3774
3775 huge = shmem_parse_huge(tmp);
3776 if (huge == -EINVAL)
3777 return -EINVAL;
3778 if (!has_transparent_hugepage() &&
3779 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3780 return -EINVAL;
3781
3782 shmem_huge = huge;
3783 if (shmem_huge < SHMEM_HUGE_DENY)
3784 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3785 return count;
3786}
3787
3788struct kobj_attribute shmem_enabled_attr =
3789 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
3790#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
3791
Matt Mackall853ac432009-01-06 14:40:20 -08003792#else /* !CONFIG_SHMEM */
3793
3794/*
3795 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3796 *
3797 * This is intended for small system where the benefits of the full
3798 * shmem code (swap-backed and resource-limited) are outweighed by
3799 * their complexity. On systems without swap this code should be
3800 * effectively equivalent, but much lighter weight.
3801 */
3802
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003803static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003804 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003805 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003806 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003807 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003808};
3809
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003810int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003811{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003812 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003813
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003814 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003815 BUG_ON(IS_ERR(shm_mnt));
3816
3817 return 0;
3818}
3819
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003820int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003821{
3822 return 0;
3823}
3824
Hugh Dickins3f96b792009-09-21 17:03:37 -07003825int shmem_lock(struct file *file, int lock, struct user_struct *user)
3826{
3827 return 0;
3828}
3829
Hugh Dickins24513262012-01-20 14:34:21 -08003830void shmem_unlock_mapping(struct address_space *mapping)
3831{
3832}
3833
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003834#ifdef CONFIG_MMU
3835unsigned long shmem_get_unmapped_area(struct file *file,
3836 unsigned long addr, unsigned long len,
3837 unsigned long pgoff, unsigned long flags)
3838{
3839 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3840}
3841#endif
3842
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003843void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003844{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003845 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003846}
3847EXPORT_SYMBOL_GPL(shmem_truncate_range);
3848
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003849#define shmem_vm_ops generic_file_vm_ops
3850#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003851#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003852#define shmem_acct_size(flags, size) 0
3853#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003854
3855#endif /* CONFIG_SHMEM */
3856
3857/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Al Viro34515382013-02-14 22:38:02 -05003859static struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04003860 .d_dname = simple_dname
Al Viro34515382013-02-14 22:38:02 -05003861};
3862
Eric Parisc7277092013-12-02 11:24:19 +00003863static struct file *__shmem_file_setup(const char *name, loff_t size,
3864 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
Al Viro6b4d0b22013-02-14 21:37:26 -05003866 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04003868 struct path path;
Al Viro34515382013-02-14 22:38:02 -05003869 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 struct qstr this;
3871
3872 if (IS_ERR(shm_mnt))
Al Viro6b4d0b22013-02-14 21:37:26 -05003873 return ERR_CAST(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Hugh Dickins285b2c42011-08-03 16:21:20 -07003875 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return ERR_PTR(-EINVAL);
3877
3878 if (shmem_acct_size(flags, size))
3879 return ERR_PTR(-ENOMEM);
3880
Al Viro6b4d0b22013-02-14 21:37:26 -05003881 res = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 this.name = name;
3883 this.len = strlen(name);
3884 this.hash = 0; /* will go */
Al Viro34515382013-02-14 22:38:02 -05003885 sb = shm_mnt->mnt_sb;
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003886 path.mnt = mntget(shm_mnt);
Al Viro34515382013-02-14 22:38:02 -05003887 path.dentry = d_alloc_pseudo(sb, &this);
Al Viro2c48b9c2009-08-09 00:52:35 +04003888 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 goto put_memory;
Al Viro34515382013-02-14 22:38:02 -05003890 d_set_d_op(path.dentry, &anon_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
Al Viro6b4d0b22013-02-14 21:37:26 -05003892 res = ERR_PTR(-ENOSPC);
Al Viro34515382013-02-14 22:38:02 -05003893 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 if (!inode)
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003895 goto put_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Eric Parisc7277092013-12-02 11:24:19 +00003897 inode->i_flags |= i_flags;
Al Viro2c48b9c2009-08-09 00:52:35 +04003898 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003900 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003901 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
3902 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003903 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003904
Al Viro6b4d0b22013-02-14 21:37:26 -05003905 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04003906 &shmem_file_operations);
Al Viro6b4d0b22013-02-14 21:37:26 -05003907 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003908 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003909
Al Viro6b4d0b22013-02-14 21:37:26 -05003910 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912put_memory:
3913 shmem_unacct_size(flags, size);
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003914put_path:
3915 path_put(&path);
Al Viro6b4d0b22013-02-14 21:37:26 -05003916 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
Eric Parisc7277092013-12-02 11:24:19 +00003918
3919/**
3920 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3921 * kernel internal. There will be NO LSM permission checks against the
3922 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003923 * higher layer. The users are the big_key and shm implementations. LSM
3924 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003925 * @name: name for dentry (to be seen in /proc/<pid>/maps
3926 * @size: size to be set for the file
3927 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3928 */
3929struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3930{
3931 return __shmem_file_setup(name, size, flags, S_PRIVATE);
3932}
3933
3934/**
3935 * shmem_file_setup - get an unlinked file living in tmpfs
3936 * @name: name for dentry (to be seen in /proc/<pid>/maps
3937 * @size: size to be set for the file
3938 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3939 */
3940struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3941{
3942 return __shmem_file_setup(name, size, flags, 0);
3943}
Keith Packard395e0dd2008-06-20 00:08:06 -07003944EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Randy Dunlap46711812008-03-19 17:00:41 -07003946/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3949 */
3950int shmem_zero_setup(struct vm_area_struct *vma)
3951{
3952 struct file *file;
3953 loff_t size = vma->vm_end - vma->vm_start;
3954
Hugh Dickins66fc1302015-06-14 09:48:09 -07003955 /*
3956 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3957 * between XFS directory reading and selinux: since this file is only
3958 * accessible to the user through its mapping, use S_PRIVATE flag to
3959 * bypass file security, in the same way as shmem_kernel_file_setup().
3960 */
3961 file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 if (IS_ERR(file))
3963 return PTR_ERR(file);
3964
3965 if (vma->vm_file)
3966 fput(vma->vm_file);
3967 vma->vm_file = file;
3968 vma->vm_ops = &shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return 0;
3970}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003971
3972/**
3973 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3974 * @mapping: the page's address_space
3975 * @index: the page index
3976 * @gfp: the page allocator flags to use if allocating
3977 *
3978 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3979 * with any new page allocations done using the specified allocation flags.
3980 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3981 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3982 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3983 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07003984 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3985 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003986 */
3987struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3988 pgoff_t index, gfp_t gfp)
3989{
Hugh Dickins68da9f02011-07-25 17:12:34 -07003990#ifdef CONFIG_SHMEM
3991 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003992 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07003993 int error;
3994
3995 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003996 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
3997 gfp, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07003998 if (error)
3999 page = ERR_PTR(error);
4000 else
4001 unlock_page(page);
4002 return page;
4003#else
4004 /*
4005 * The tiny !SHMEM case uses ramfs without swap
4006 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004007 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004008#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004009}
4010EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);