blob: f19b6b44ae4661fe68c01afe3c2be78440352ffc [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 */
Kirill A. Shutemov657e3032016-07-26 15:26:21 -0700104 SGP_NOHUGE, /* like SGP_CACHE, but no huge pages */
105 SGP_HUGE, /* like SGP_CACHE, huge pages preferred */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700106 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
107 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Andrew Mortonb76db732008-02-08 04:21:49 -0800110#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800111static unsigned long shmem_default_max_blocks(void)
112{
113 return totalram_pages / 2;
114}
115
116static unsigned long shmem_default_max_inodes(void)
117{
118 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
119}
Andrew Mortonb76db732008-02-08 04:21:49 -0800120#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800121
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700122static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
123static int shmem_replace_page(struct page **pagep, gfp_t gfp,
124 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700125static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700126 struct page **pagep, enum sgp_type sgp,
127 gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700128
129static inline int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700130 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700131{
132 return shmem_getpage_gfp(inode, index, pagep, sgp,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700133 mapping_gfp_mask(inode->i_mapping), NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
137{
138 return sb->s_fs_info;
139}
140
141/*
142 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
143 * for shared memory and for shared anonymous (/dev/zero) mappings
144 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
145 * consistent with the pre-accounting of private mappings ...
146 */
147static inline int shmem_acct_size(unsigned long flags, loff_t size)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000150 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void shmem_unacct_size(unsigned long flags, loff_t size)
154{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000155 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vm_unacct_memory(VM_ACCT(size));
157}
158
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700159static inline int shmem_reacct_size(unsigned long flags,
160 loff_t oldsize, loff_t newsize)
161{
162 if (!(flags & VM_NORESERVE)) {
163 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
164 return security_vm_enough_memory_mm(current->mm,
165 VM_ACCT(newsize) - VM_ACCT(oldsize));
166 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
167 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
168 }
169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700174 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
176 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
177 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700178static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700180 if (!(flags & VM_NORESERVE))
181 return 0;
182
183 return security_vm_enough_memory_mm(current->mm,
184 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static inline void shmem_unacct_blocks(unsigned long flags, long pages)
188{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000189 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300190 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Hugh Dickins759b9772007-03-05 00:30:28 -0800193static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700194static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800195static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800196static const struct inode_operations shmem_inode_operations;
197static const struct inode_operations shmem_dir_inode_operations;
198static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400199static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800202static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800204static int shmem_reserve_inode(struct super_block *sb)
205{
206 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
207 if (sbinfo->max_inodes) {
208 spin_lock(&sbinfo->stat_lock);
209 if (!sbinfo->free_inodes) {
210 spin_unlock(&sbinfo->stat_lock);
211 return -ENOSPC;
212 }
213 sbinfo->free_inodes--;
214 spin_unlock(&sbinfo->stat_lock);
215 }
216 return 0;
217}
218
219static void shmem_free_inode(struct super_block *sb)
220{
221 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
222 if (sbinfo->max_inodes) {
223 spin_lock(&sbinfo->stat_lock);
224 sbinfo->free_inodes++;
225 spin_unlock(&sbinfo->stat_lock);
226 }
227}
228
Randy Dunlap46711812008-03-19 17:00:41 -0700229/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700230 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * @inode: inode to recalc
232 *
233 * We have to calculate the free blocks since the mm can drop
234 * undirtied hole pages behind our back.
235 *
236 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
237 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
238 *
239 * It has to be called with the spinlock held.
240 */
241static void shmem_recalc_inode(struct inode *inode)
242{
243 struct shmem_inode_info *info = SHMEM_I(inode);
244 long freed;
245
246 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
247 if (freed > 0) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700248 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
249 if (sbinfo->max_blocks)
250 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700252 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255}
256
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700257bool shmem_charge(struct inode *inode, long pages)
258{
259 struct shmem_inode_info *info = SHMEM_I(inode);
260 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
261
262 if (shmem_acct_block(info->flags, pages))
263 return false;
264 spin_lock(&info->lock);
265 info->alloced += pages;
266 inode->i_blocks += pages * BLOCKS_PER_PAGE;
267 shmem_recalc_inode(inode);
268 spin_unlock(&info->lock);
269 inode->i_mapping->nrpages += pages;
270
271 if (!sbinfo->max_blocks)
272 return true;
273 if (percpu_counter_compare(&sbinfo->used_blocks,
274 sbinfo->max_blocks - pages) > 0) {
275 inode->i_mapping->nrpages -= pages;
276 spin_lock(&info->lock);
277 info->alloced -= pages;
278 shmem_recalc_inode(inode);
279 spin_unlock(&info->lock);
280
281 return false;
282 }
283 percpu_counter_add(&sbinfo->used_blocks, pages);
284 return true;
285}
286
287void shmem_uncharge(struct inode *inode, long pages)
288{
289 struct shmem_inode_info *info = SHMEM_I(inode);
290 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
291
292 spin_lock(&info->lock);
293 info->alloced -= pages;
294 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
295 shmem_recalc_inode(inode);
296 spin_unlock(&info->lock);
297
298 if (sbinfo->max_blocks)
299 percpu_counter_sub(&sbinfo->used_blocks, pages);
300}
301
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700302/*
303 * Replace item expected in radix tree by a new item, while holding tree lock.
304 */
305static int shmem_radix_tree_replace(struct address_space *mapping,
306 pgoff_t index, void *expected, void *replacement)
307{
308 void **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700309 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700310
311 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700312 VM_BUG_ON(!replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700313 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700314 if (!pslot)
315 return -ENOENT;
316 item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700317 if (item != expected)
318 return -ENOENT;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700319 radix_tree_replace_slot(pslot, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700320 return 0;
321}
322
323/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700324 * Sometimes, before we decide whether to proceed or to fail, we must check
325 * that an entry was not already brought back from swap by a racing thread.
326 *
327 * Checking page is not enough: by the time a SwapCache page is locked, it
328 * might be reused, and again be SwapCache, using the same swap as before.
329 */
330static bool shmem_confirm_swap(struct address_space *mapping,
331 pgoff_t index, swp_entry_t swap)
332{
333 void *item;
334
335 rcu_read_lock();
336 item = radix_tree_lookup(&mapping->page_tree, index);
337 rcu_read_unlock();
338 return item == swp_to_radix_entry(swap);
339}
340
341/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700342 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
343 *
344 * SHMEM_HUGE_NEVER:
345 * disables huge pages for the mount;
346 * SHMEM_HUGE_ALWAYS:
347 * enables huge pages for the mount;
348 * SHMEM_HUGE_WITHIN_SIZE:
349 * only allocate huge pages if the page will be fully within i_size,
350 * also respect fadvise()/madvise() hints;
351 * SHMEM_HUGE_ADVISE:
352 * only allocate huge pages if requested with fadvise()/madvise();
353 */
354
355#define SHMEM_HUGE_NEVER 0
356#define SHMEM_HUGE_ALWAYS 1
357#define SHMEM_HUGE_WITHIN_SIZE 2
358#define SHMEM_HUGE_ADVISE 3
359
360/*
361 * Special values.
362 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
363 *
364 * SHMEM_HUGE_DENY:
365 * disables huge on shm_mnt and all mounts, for emergency use;
366 * SHMEM_HUGE_FORCE:
367 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
368 *
369 */
370#define SHMEM_HUGE_DENY (-1)
371#define SHMEM_HUGE_FORCE (-2)
372
373#ifdef CONFIG_TRANSPARENT_HUGEPAGE
374/* ifdef here to avoid bloating shmem.o when not necessary */
375
376int shmem_huge __read_mostly;
377
378static int shmem_parse_huge(const char *str)
379{
380 if (!strcmp(str, "never"))
381 return SHMEM_HUGE_NEVER;
382 if (!strcmp(str, "always"))
383 return SHMEM_HUGE_ALWAYS;
384 if (!strcmp(str, "within_size"))
385 return SHMEM_HUGE_WITHIN_SIZE;
386 if (!strcmp(str, "advise"))
387 return SHMEM_HUGE_ADVISE;
388 if (!strcmp(str, "deny"))
389 return SHMEM_HUGE_DENY;
390 if (!strcmp(str, "force"))
391 return SHMEM_HUGE_FORCE;
392 return -EINVAL;
393}
394
395static const char *shmem_format_huge(int huge)
396{
397 switch (huge) {
398 case SHMEM_HUGE_NEVER:
399 return "never";
400 case SHMEM_HUGE_ALWAYS:
401 return "always";
402 case SHMEM_HUGE_WITHIN_SIZE:
403 return "within_size";
404 case SHMEM_HUGE_ADVISE:
405 return "advise";
406 case SHMEM_HUGE_DENY:
407 return "deny";
408 case SHMEM_HUGE_FORCE:
409 return "force";
410 default:
411 VM_BUG_ON(1);
412 return "bad_val";
413 }
414}
415
416#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
417
418#define shmem_huge SHMEM_HUGE_DENY
419
420#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
421
422/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700423 * Like add_to_page_cache_locked, but error if expected item has gone.
424 */
425static int shmem_add_to_page_cache(struct page *page,
426 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700427 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700428{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700429 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700430
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700431 VM_BUG_ON_PAGE(PageTail(page), page);
432 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800433 VM_BUG_ON_PAGE(!PageLocked(page), page);
434 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700435 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700436
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700437 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700438 page->mapping = mapping;
439 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700440
Hugh Dickinsb065b432012-07-11 14:02:48 -0700441 spin_lock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700442 if (PageTransHuge(page)) {
443 void __rcu **results;
444 pgoff_t idx;
445 int i;
446
447 error = 0;
448 if (radix_tree_gang_lookup_slot(&mapping->page_tree,
449 &results, &idx, index, 1) &&
450 idx < index + HPAGE_PMD_NR) {
451 error = -EEXIST;
452 }
453
454 if (!error) {
455 for (i = 0; i < HPAGE_PMD_NR; i++) {
456 error = radix_tree_insert(&mapping->page_tree,
457 index + i, page + i);
458 VM_BUG_ON(error);
459 }
460 count_vm_event(THP_FILE_ALLOC);
461 }
462 } else if (!expected) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700463 error = radix_tree_insert(&mapping->page_tree, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700464 } else {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700465 error = shmem_radix_tree_replace(mapping, index, expected,
466 page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700467 }
468
Hugh Dickinsb065b432012-07-11 14:02:48 -0700469 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700470 mapping->nrpages += nr;
471 if (PageTransHuge(page))
472 __inc_zone_page_state(page, NR_SHMEM_THPS);
473 __mod_zone_page_state(page_zone(page), NR_FILE_PAGES, nr);
474 __mod_zone_page_state(page_zone(page), NR_SHMEM, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700475 spin_unlock_irq(&mapping->tree_lock);
476 } else {
477 page->mapping = NULL;
478 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700479 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700480 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700481 return error;
482}
483
484/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700485 * Like delete_from_page_cache, but substitutes swap for page.
486 */
487static void shmem_delete_from_page_cache(struct page *page, void *radswap)
488{
489 struct address_space *mapping = page->mapping;
490 int error;
491
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700492 VM_BUG_ON_PAGE(PageCompound(page), page);
493
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700494 spin_lock_irq(&mapping->tree_lock);
495 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
496 page->mapping = NULL;
497 mapping->nrpages--;
498 __dec_zone_page_state(page, NR_FILE_PAGES);
499 __dec_zone_page_state(page, NR_SHMEM);
500 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300501 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700502 BUG_ON(error);
503}
504
505/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700506 * Remove swap entry from radix tree, free the swap and its page cache.
507 */
508static int shmem_free_swap(struct address_space *mapping,
509 pgoff_t index, void *radswap)
510{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700511 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700512
513 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700514 old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700515 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700516 if (old != radswap)
517 return -ENOENT;
518 free_swap_and_cache(radix_to_swp_entry(radswap));
519 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700520}
521
522/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800523 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800524 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800525 *
526 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
527 * as long as the inode doesn't go away and racy results are not a problem.
528 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800529unsigned long shmem_partial_swap_usage(struct address_space *mapping,
530 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800531{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800532 struct radix_tree_iter iter;
533 void **slot;
534 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800535 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800536
537 rcu_read_lock();
538
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800539 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
540 if (iter.index >= end)
541 break;
542
543 page = radix_tree_deref_slot(slot);
544
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700545 if (radix_tree_deref_retry(page)) {
546 slot = radix_tree_iter_retry(&iter);
547 continue;
548 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800549
550 if (radix_tree_exceptional_entry(page))
551 swapped++;
552
553 if (need_resched()) {
554 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -0700555 slot = radix_tree_iter_next(&iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800556 }
557 }
558
559 rcu_read_unlock();
560
561 return swapped << PAGE_SHIFT;
562}
563
564/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800565 * Determine (in bytes) how many of the shmem object's pages mapped by the
566 * given vma is swapped out.
567 *
568 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
569 * as long as the inode doesn't go away and racy results are not a problem.
570 */
571unsigned long shmem_swap_usage(struct vm_area_struct *vma)
572{
573 struct inode *inode = file_inode(vma->vm_file);
574 struct shmem_inode_info *info = SHMEM_I(inode);
575 struct address_space *mapping = inode->i_mapping;
576 unsigned long swapped;
577
578 /* Be careful as we don't hold info->lock */
579 swapped = READ_ONCE(info->swapped);
580
581 /*
582 * The easier cases are when the shmem object has nothing in swap, or
583 * the vma maps it whole. Then we can simply use the stats that we
584 * already track.
585 */
586 if (!swapped)
587 return 0;
588
589 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
590 return swapped << PAGE_SHIFT;
591
592 /* Here comes the more involved part */
593 return shmem_partial_swap_usage(mapping,
594 linear_page_index(vma, vma->vm_start),
595 linear_page_index(vma, vma->vm_end));
596}
597
598/*
Hugh Dickins24513262012-01-20 14:34:21 -0800599 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
600 */
601void shmem_unlock_mapping(struct address_space *mapping)
602{
603 struct pagevec pvec;
604 pgoff_t indices[PAGEVEC_SIZE];
605 pgoff_t index = 0;
606
607 pagevec_init(&pvec, 0);
608 /*
609 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
610 */
611 while (!mapping_unevictable(mapping)) {
612 /*
613 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
614 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
615 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700616 pvec.nr = find_get_entries(mapping, index,
617 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800618 if (!pvec.nr)
619 break;
620 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700621 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800622 check_move_unevictable_pages(pvec.pages, pvec.nr);
623 pagevec_release(&pvec);
624 cond_resched();
625 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700626}
627
628/*
629 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700630 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700631 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700632static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
633 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700635 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300637 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
638 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
639 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
640 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700641 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700642 pgoff_t indices[PAGEVEC_SIZE];
643 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700644 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700645 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700647 if (lend == -1)
648 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700649
650 pagevec_init(&pvec, 0);
651 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700652 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700653 pvec.nr = find_get_entries(mapping, index,
654 min(end - index, (pgoff_t)PAGEVEC_SIZE),
655 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700656 if (!pvec.nr)
657 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700658 for (i = 0; i < pagevec_count(&pvec); i++) {
659 struct page *page = pvec.pages[i];
660
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700661 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700662 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700663 break;
664
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700665 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700666 if (unfalloc)
667 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700668 nr_swaps_freed += !shmem_free_swap(mapping,
669 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700670 continue;
671 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700672
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700673 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
674
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700675 if (!trylock_page(page))
676 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700677
678 if (PageTransTail(page)) {
679 /* Middle of THP: zero out the page */
680 clear_highpage(page);
681 unlock_page(page);
682 continue;
683 } else if (PageTransHuge(page)) {
684 if (index == round_down(end, HPAGE_PMD_NR)) {
685 /*
686 * Range ends in the middle of THP:
687 * zero out the page
688 */
689 clear_highpage(page);
690 unlock_page(page);
691 continue;
692 }
693 index += HPAGE_PMD_NR - 1;
694 i += HPAGE_PMD_NR - 1;
695 }
696
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700697 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700698 VM_BUG_ON_PAGE(PageTail(page), page);
699 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800700 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700701 truncate_inode_page(mapping, page);
702 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700703 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700704 unlock_page(page);
705 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700706 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800707 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700708 cond_resched();
709 index++;
710 }
711
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700712 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700713 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700714 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700715 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300716 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700717 if (start > end) {
718 top = partial_end;
719 partial_end = 0;
720 }
721 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700722 set_page_dirty(page);
723 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300724 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700725 }
726 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700727 if (partial_end) {
728 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700729 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700730 if (page) {
731 zero_user_segment(page, 0, partial_end);
732 set_page_dirty(page);
733 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300734 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700735 }
736 }
737 if (start >= end)
738 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700739
740 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700741 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700742 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700743
744 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700745 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700746 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700747 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700748 /* If all gone or hole-punch or unfalloc, we're done */
749 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700750 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700751 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700752 index = start;
753 continue;
754 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700755 for (i = 0; i < pagevec_count(&pvec); i++) {
756 struct page *page = pvec.pages[i];
757
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700758 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700759 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700760 break;
761
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700762 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700763 if (unfalloc)
764 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700765 if (shmem_free_swap(mapping, index, page)) {
766 /* Swap was replaced by page: retry */
767 index--;
768 break;
769 }
770 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700771 continue;
772 }
773
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700774 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700775
776 if (PageTransTail(page)) {
777 /* Middle of THP: zero out the page */
778 clear_highpage(page);
779 unlock_page(page);
780 /*
781 * Partial thp truncate due 'start' in middle
782 * of THP: don't need to look on these pages
783 * again on !pvec.nr restart.
784 */
785 if (index != round_down(end, HPAGE_PMD_NR))
786 start++;
787 continue;
788 } else if (PageTransHuge(page)) {
789 if (index == round_down(end, HPAGE_PMD_NR)) {
790 /*
791 * Range ends in the middle of THP:
792 * zero out the page
793 */
794 clear_highpage(page);
795 unlock_page(page);
796 continue;
797 }
798 index += HPAGE_PMD_NR - 1;
799 i += HPAGE_PMD_NR - 1;
800 }
801
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700802 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700803 VM_BUG_ON_PAGE(PageTail(page), page);
804 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800805 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700806 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700807 } else {
808 /* Page was replaced by swap: retry */
809 unlock_page(page);
810 index--;
811 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700812 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700813 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700814 unlock_page(page);
815 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700816 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800817 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700818 index++;
819 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700822 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 shmem_recalc_inode(inode);
824 spin_unlock(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700827void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
828{
829 shmem_undo_range(inode, lstart, lend, false);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700830 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700832EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Yu Zhao44a30222015-09-08 15:03:33 -0700834static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
835 struct kstat *stat)
836{
837 struct inode *inode = dentry->d_inode;
838 struct shmem_inode_info *info = SHMEM_I(inode);
839
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800840 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
841 spin_lock(&info->lock);
842 shmem_recalc_inode(inode);
843 spin_unlock(&info->lock);
844 }
Yu Zhao44a30222015-09-08 15:03:33 -0700845 generic_fillattr(inode, stat);
Yu Zhao44a30222015-09-08 15:03:33 -0700846 return 0;
847}
848
Hugh Dickins94c1e622011-06-27 16:18:03 -0700849static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
David Howells75c3cfa2015-03-17 22:26:12 +0000851 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -0700852 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int error;
854
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200855 error = inode_change_ok(inode, attr);
856 if (error)
857 return error;
858
Hugh Dickins94c1e622011-06-27 16:18:03 -0700859 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
860 loff_t oldsize = inode->i_size;
861 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000862
David Herrmann40e041a2014-08-08 14:25:27 -0700863 /* protected by i_mutex */
864 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
865 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
866 return -EPERM;
867
Hugh Dickins94c1e622011-06-27 16:18:03 -0700868 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700869 error = shmem_reacct_size(SHMEM_I(inode)->flags,
870 oldsize, newsize);
871 if (error)
872 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -0700873 i_size_write(inode, newsize);
874 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
875 }
Josef Bacikafa2db22015-06-24 16:58:45 -0700876 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -0700877 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800878 if (oldsize > holebegin)
879 unmap_mapping_range(inode->i_mapping,
880 holebegin, 0, 1);
881 if (info->alloced)
882 shmem_truncate_range(inode,
883 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700884 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800885 if (oldsize > holebegin)
886 unmap_mapping_range(inode->i_mapping,
887 holebegin, 0, 1);
Hugh Dickins94c1e622011-06-27 16:18:03 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200891 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200892 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -0800893 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return error;
895}
896
Al Viro1f895f72010-06-05 19:10:41 -0400897static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct shmem_inode_info *info = SHMEM_I(inode);
900
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000901 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 shmem_unacct_size(info->flags, inode->i_size);
903 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000904 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800906 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800908 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Al Viro3ed47db2016-01-22 18:08:52 -0500910 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700911
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400912 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -0800913 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800914 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +0200915 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700918/*
919 * If swap found in inode, free it and move page from swapcache to filecache.
920 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700921static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700922 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700924 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700925 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700926 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700927 gfp_t gfp;
928 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700930 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700931 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700932 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -0700933 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800934
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800935 /*
936 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400937 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800938 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700939 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800940 */
941 if (shmem_swaplist.next != &info->swaplist)
942 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800943
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700944 gfp = mapping_gfp_mask(mapping);
945 if (shmem_should_replace_page(*pagep, gfp)) {
946 mutex_unlock(&shmem_swaplist_mutex);
947 error = shmem_replace_page(pagep, gfp, info, index);
948 mutex_lock(&shmem_swaplist_mutex);
949 /*
950 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -0700951 * allocation, but the inode might have been freed while we
952 * dropped it: although a racing shmem_evict_inode() cannot
953 * complete without emptying the radix_tree, our page lock
954 * on this swapcache page is not enough to prevent that -
955 * free_swap_and_cache() of our swap entry will only
956 * trylock_page(), removing swap from radix_tree whatever.
957 *
958 * We must not proceed to shmem_add_to_page_cache() if the
959 * inode has been freed, but of course we cannot rely on
960 * inode or mapping or info to check that. However, we can
961 * safely check if our swap entry is still in use (and here
962 * it can't have got reused for another page): if it's still
963 * in use, then the inode cannot have been freed yet, and we
964 * can safely proceed (if it's no longer in use, that tells
965 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700966 */
967 if (!page_swapcount(*pagep))
968 error = -ENOENT;
969 }
970
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800971 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700972 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
973 * but also to hold up shmem_evict_inode(): so inode cannot be freed
974 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800975 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700976 if (!error)
977 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700978 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -0700979 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700980 /*
981 * Truncation and eviction use free_swap_and_cache(), which
982 * only does trylock page: if we raced, best clean up here.
983 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700984 delete_from_swap_cache(*pagep);
985 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700986 if (!error) {
987 spin_lock(&info->lock);
988 info->swapped--;
989 spin_unlock(&info->lock);
990 swap_free(swap);
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800993 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700997 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700999int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001001 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001003 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001004 int error = 0;
1005
1006 /*
1007 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001008 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001009 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001010 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001011 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001012
1013 /*
1014 * Charge page using GFP_KERNEL while we can wait, before taking
1015 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1016 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001017 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001018 error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
1019 false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001020 if (error)
1021 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001022 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001023 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001025 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001026 list_for_each_safe(this, next, &shmem_swaplist) {
1027 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001028 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001029 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001030 else
1031 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001032 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001033 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001034 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001035 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001037 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001038
Johannes Weiner00501b52014-08-08 14:19:20 -07001039 if (error) {
1040 if (error != -ENOMEM)
1041 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001042 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001043 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001044 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001045out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001046 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001047 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/*
1052 * Move the page from the page cache to the swap cache.
1053 */
1054static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1055{
1056 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001059 swp_entry_t swap;
1060 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001062 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 mapping = page->mapping;
1065 index = page->index;
1066 inode = mapping->host;
1067 info = SHMEM_I(inode);
1068 if (info->flags & VM_LOCKED)
1069 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001070 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto redirty;
1072
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001073 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001074 * Our capabilities prevent regular writeback or sync from ever calling
1075 * shmem_writepage; but a stacking filesystem might use ->writepage of
1076 * its underlying filesystem, in which case tmpfs should write out to
1077 * swap only in response to memory pressure, and not for the writeback
1078 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001079 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001080 if (!wbc->for_reclaim) {
1081 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1082 goto redirty;
1083 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001084
1085 /*
1086 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1087 * value into swapfile.c, the only way we can correctly account for a
1088 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001089 *
1090 * That's okay for a page already fallocated earlier, but if we have
1091 * not yet completed the fallocation, then (a) we want to keep track
1092 * of this page in case we have to undo it, and (b) it may not be a
1093 * good idea to continue anyway, once we're pushing into swap. So
1094 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001095 */
1096 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001097 if (inode->i_private) {
1098 struct shmem_falloc *shmem_falloc;
1099 spin_lock(&inode->i_lock);
1100 shmem_falloc = inode->i_private;
1101 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001102 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001103 index >= shmem_falloc->start &&
1104 index < shmem_falloc->next)
1105 shmem_falloc->nr_unswapped++;
1106 else
1107 shmem_falloc = NULL;
1108 spin_unlock(&inode->i_lock);
1109 if (shmem_falloc)
1110 goto redirty;
1111 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001112 clear_highpage(page);
1113 flush_dcache_page(page);
1114 SetPageUptodate(page);
1115 }
1116
Hugh Dickins48f170f2011-07-25 17:12:37 -07001117 swap = get_swap_page();
1118 if (!swap.val)
1119 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001120
Vladimir Davydov37e84352016-01-20 15:02:56 -08001121 if (mem_cgroup_try_charge_swap(page, swap))
1122 goto free_swap;
1123
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001124 /*
1125 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001126 * if it's not already there. Do it now before the page is
1127 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001128 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001129 * we've incremented swapped, because shmem_unuse_inode() will
1130 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001131 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001132 mutex_lock(&shmem_swaplist_mutex);
1133 if (list_empty(&info->swaplist))
1134 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001135
Hugh Dickins48f170f2011-07-25 17:12:37 -07001136 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001137 spin_lock(&info->lock);
1138 shmem_recalc_inode(inode);
1139 info->swapped++;
1140 spin_unlock(&info->lock);
1141
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001142 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001143 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1144
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001145 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001146 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001147 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149 }
1150
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001151 mutex_unlock(&shmem_swaplist_mutex);
Vladimir Davydov37e84352016-01-20 15:02:56 -08001152free_swap:
Johannes Weiner0a31bc92014-08-08 14:19:22 -07001153 swapcache_free(swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154redirty:
1155 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001156 if (wbc->for_reclaim)
1157 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1158 unlock_page(page);
1159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Hugh Dickins75edd342016-05-19 17:12:44 -07001162#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001163static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001164{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001165 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001166
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001167 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001168 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001169
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001170 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001171
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001172 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001173}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001174
1175static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1176{
1177 struct mempolicy *mpol = NULL;
1178 if (sbinfo->mpol) {
1179 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1180 mpol = sbinfo->mpol;
1181 mpol_get(mpol);
1182 spin_unlock(&sbinfo->stat_lock);
1183 }
1184 return mpol;
1185}
Hugh Dickins75edd342016-05-19 17:12:44 -07001186#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1187static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1188{
1189}
1190static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1191{
1192 return NULL;
1193}
1194#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1195#ifndef CONFIG_NUMA
1196#define vm_policy vm_private_data
1197#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001198
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001199static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1200 struct shmem_inode_info *info, pgoff_t index)
1201{
1202 /* Create a pseudo vma that just contains the policy */
1203 vma->vm_start = 0;
1204 /* Bias interleave by inode number to distribute better across nodes */
1205 vma->vm_pgoff = index + info->vfs_inode.i_ino;
1206 vma->vm_ops = NULL;
1207 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1208}
1209
1210static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1211{
1212 /* Drop reference taken by mpol_shared_policy_lookup() */
1213 mpol_cond_put(vma->vm_policy);
1214}
1215
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001216static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1217 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001220 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001222 shmem_pseudo_vma_init(&pvma, info, index);
Mel Gorman18a2f372012-12-05 14:01:41 -08001223 page = swapin_readahead(swap, gfp, &pvma, 0);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001224 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001225
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001226 return page;
1227}
Mel Gorman18a2f372012-12-05 14:01:41 -08001228
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001229static struct page *shmem_alloc_hugepage(gfp_t gfp,
1230 struct shmem_inode_info *info, pgoff_t index)
1231{
1232 struct vm_area_struct pvma;
1233 struct inode *inode = &info->vfs_inode;
1234 struct address_space *mapping = inode->i_mapping;
1235 pgoff_t idx, hindex = round_down(index, HPAGE_PMD_NR);
1236 void __rcu **results;
1237 struct page *page;
1238
1239 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1240 return NULL;
1241
1242 rcu_read_lock();
1243 if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
1244 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1245 rcu_read_unlock();
1246 return NULL;
1247 }
1248 rcu_read_unlock();
1249
1250 shmem_pseudo_vma_init(&pvma, info, hindex);
1251 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1252 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1253 shmem_pseudo_vma_destroy(&pvma);
1254 if (page)
1255 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001256 return page;
1257}
1258
1259static struct page *shmem_alloc_page(gfp_t gfp,
1260 struct shmem_inode_info *info, pgoff_t index)
1261{
1262 struct vm_area_struct pvma;
1263 struct page *page;
1264
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001265 shmem_pseudo_vma_init(&pvma, info, index);
1266 page = alloc_page_vma(gfp, &pvma, 0);
1267 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001268
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001269 return page;
1270}
1271
1272static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
1273 struct shmem_inode_info *info, struct shmem_sb_info *sbinfo,
1274 pgoff_t index, bool huge)
1275{
1276 struct page *page;
1277 int nr;
1278 int err = -ENOSPC;
1279
1280 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1281 huge = false;
1282 nr = huge ? HPAGE_PMD_NR : 1;
1283
1284 if (shmem_acct_block(info->flags, nr))
1285 goto failed;
1286 if (sbinfo->max_blocks) {
1287 if (percpu_counter_compare(&sbinfo->used_blocks,
1288 sbinfo->max_blocks - nr) > 0)
1289 goto unacct;
1290 percpu_counter_add(&sbinfo->used_blocks, nr);
1291 }
1292
1293 if (huge)
1294 page = shmem_alloc_hugepage(gfp, info, index);
1295 else
1296 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001297 if (page) {
1298 __SetPageLocked(page);
1299 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001300 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001301 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001302
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001303 err = -ENOMEM;
1304 if (sbinfo->max_blocks)
1305 percpu_counter_add(&sbinfo->used_blocks, -nr);
1306unacct:
1307 shmem_unacct_blocks(info->flags, nr);
1308failed:
1309 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001313 * When a page is moved from swapcache to shmem filecache (either by the
1314 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1315 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1316 * ignorance of the mapping it belongs to. If that mapping has special
1317 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1318 * we may need to copy to a suitable page before moving to filecache.
1319 *
1320 * In a future release, this may well be extended to respect cpuset and
1321 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1322 * but for now it is a simple matter of zone.
1323 */
1324static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1325{
1326 return page_zonenum(page) > gfp_zone(gfp);
1327}
1328
1329static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1330 struct shmem_inode_info *info, pgoff_t index)
1331{
1332 struct page *oldpage, *newpage;
1333 struct address_space *swap_mapping;
1334 pgoff_t swap_index;
1335 int error;
1336
1337 oldpage = *pagep;
1338 swap_index = page_private(oldpage);
1339 swap_mapping = page_mapping(oldpage);
1340
1341 /*
1342 * We have arrived here because our zones are constrained, so don't
1343 * limit chance of success by further cpuset and node constraints.
1344 */
1345 gfp &= ~GFP_CONSTRAINT_MASK;
1346 newpage = shmem_alloc_page(gfp, info, index);
1347 if (!newpage)
1348 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001349
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001350 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001351 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001352 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001353
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001354 SetPageUptodate(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001355 set_page_private(newpage, swap_index);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001356 SetPageSwapCache(newpage);
1357
1358 /*
1359 * Our caller will very soon move newpage out of swapcache, but it's
1360 * a nice clean interface for us to replace oldpage by newpage there.
1361 */
1362 spin_lock_irq(&swap_mapping->tree_lock);
1363 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1364 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001365 if (!error) {
1366 __inc_zone_page_state(newpage, NR_FILE_PAGES);
1367 __dec_zone_page_state(oldpage, NR_FILE_PAGES);
1368 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001369 spin_unlock_irq(&swap_mapping->tree_lock);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001370
Hugh Dickins0142ef62012-06-07 14:21:09 -07001371 if (unlikely(error)) {
1372 /*
1373 * Is this possible? I think not, now that our callers check
1374 * both PageSwapCache and page_private after getting page lock;
1375 * but be defensive. Reverse old to newpage for clear and free.
1376 */
1377 oldpage = newpage;
1378 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001379 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001380 lru_cache_add_anon(newpage);
1381 *pagep = newpage;
1382 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001383
1384 ClearPageSwapCache(oldpage);
1385 set_page_private(oldpage, 0);
1386
1387 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001388 put_page(oldpage);
1389 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001390 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001391}
1392
1393/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001394 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *
1396 * If we allocate a new one we do not mark it dirty. That's up to the
1397 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001398 * entry since a page cannot live in both the swap and page cache.
1399 *
1400 * fault_mm and fault_type are only supplied by shmem_fault:
1401 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001403static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001404 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
1405 struct mm_struct *fault_mm, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -07001408 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001410 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001411 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001412 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001414 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001415 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001417 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001418 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001420 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001422 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1423 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001425 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001426 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001427 if (radix_tree_exceptional_entry(page)) {
1428 swap = radix_to_swp_entry(page);
1429 page = NULL;
1430 }
1431
Hugh Dickins75edd342016-05-19 17:12:44 -07001432 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001433 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001434 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001435 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001436 }
1437
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001438 if (page && sgp == SGP_WRITE)
1439 mark_page_accessed(page);
1440
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001441 /* fallocated page? */
1442 if (page && !PageUptodate(page)) {
1443 if (sgp != SGP_READ)
1444 goto clear;
1445 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001446 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001447 page = NULL;
1448 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001449 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001450 *pagep = page;
1451 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001455 * Fast cache lookup did not find it:
1456 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001458 info = SHMEM_I(inode);
1459 sbinfo = SHMEM_SB(inode->i_sb);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001460 charge_mm = fault_mm ? : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (swap.val) {
1463 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001464 page = lookup_swap_cache(swap);
1465 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001466 /* Or update major stats only when swapin succeeds?? */
1467 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001468 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001469 count_vm_event(PGMAJFAULT);
1470 mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT);
1471 }
1472 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001473 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001474 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001475 error = -ENOMEM;
1476 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479
1480 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001481 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001482 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001483 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001484 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001485 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001486 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001487 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001489 goto failed;
1490 }
1491 wait_on_page_writeback(page);
1492
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001493 if (shmem_should_replace_page(page, gfp)) {
1494 error = shmem_replace_page(&page, gfp, info, index);
1495 if (error)
1496 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001499 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001500 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001501 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001502 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001503 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001504 /*
1505 * We already confirmed swap under page lock, and make
1506 * no memory allocation here, so usually no possibility
1507 * of error; but free_swap_and_cache() only trylocks a
1508 * page, so it is just possible that the entry has been
1509 * truncated or holepunched since swap was confirmed.
1510 * shmem_undo_range() will have done some of the
1511 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001512 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001513 * Reset swap.val? No, leave it so "failed" goes back to
1514 * "repeat": reading a hole and writing should succeed.
1515 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001516 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001517 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001518 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001519 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001520 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001521 if (error)
1522 goto failed;
1523
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001524 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001525
Hugh Dickins54af60422011-08-03 16:21:24 -07001526 spin_lock(&info->lock);
1527 info->swapped--;
1528 shmem_recalc_inode(inode);
1529 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001530
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001531 if (sgp == SGP_WRITE)
1532 mark_page_accessed(page);
1533
Hugh Dickins27ab7002011-07-25 17:12:36 -07001534 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001535 set_page_dirty(page);
1536 swap_free(swap);
1537
Hugh Dickins54af60422011-08-03 16:21:24 -07001538 } else {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001539 /* shmem_symlink() */
1540 if (mapping->a_ops != &shmem_aops)
1541 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001542 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001543 goto alloc_nohuge;
1544 if (shmem_huge == SHMEM_HUGE_FORCE)
1545 goto alloc_huge;
1546 switch (sbinfo->huge) {
1547 loff_t i_size;
1548 pgoff_t off;
1549 case SHMEM_HUGE_NEVER:
1550 goto alloc_nohuge;
1551 case SHMEM_HUGE_WITHIN_SIZE:
1552 off = round_up(index, HPAGE_PMD_NR);
1553 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1554 if (i_size >= HPAGE_PMD_SIZE &&
1555 i_size >> PAGE_SHIFT >= off)
1556 goto alloc_huge;
1557 /* fallthrough */
1558 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001559 if (sgp_huge == SGP_HUGE)
1560 goto alloc_huge;
1561 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001562 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001565alloc_huge:
1566 page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1567 index, true);
1568 if (IS_ERR(page)) {
1569alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo,
1570 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001571 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001572 if (IS_ERR(page)) {
1573 error = PTR_ERR(page);
1574 page = NULL;
1575 goto failed;
1576 }
1577
1578 if (PageTransHuge(page))
1579 hindex = round_down(index, HPAGE_PMD_NR);
1580 else
1581 hindex = index;
1582
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001583 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001584 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001585
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001586 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001587 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001588 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001589 goto unacct;
1590 error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1591 compound_order(page));
Hugh Dickinsb065b432012-07-11 14:02:48 -07001592 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001593 error = shmem_add_to_page_cache(page, mapping, hindex,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001594 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001595 radix_tree_preload_end();
1596 }
1597 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001598 mem_cgroup_cancel_charge(page, memcg,
1599 PageTransHuge(page));
1600 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001601 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001602 mem_cgroup_commit_charge(page, memcg, false,
1603 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001604 lru_cache_add_anon(page);
1605
1606 spin_lock(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001607 info->alloced += 1 << compound_order(page);
1608 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001609 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 spin_unlock(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001611 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001612
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001613 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001614 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1615 */
1616 if (sgp == SGP_FALLOC)
1617 sgp = SGP_WRITE;
1618clear:
1619 /*
1620 * Let SGP_WRITE caller clear ends if write does not fill page;
1621 * but SGP_FALLOC on a page fallocated earlier must initialize
1622 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001623 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001624 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1625 struct page *head = compound_head(page);
1626 int i;
1627
1628 for (i = 0; i < (1 << compound_order(head)); i++) {
1629 clear_highpage(head + i);
1630 flush_dcache_page(head + i);
1631 }
1632 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001633 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001634 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001635
Hugh Dickins54af60422011-08-03 16:21:24 -07001636 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001637 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001638 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001639 if (alloced) {
1640 ClearPageDirty(page);
1641 delete_from_page_cache(page);
1642 spin_lock(&info->lock);
1643 shmem_recalc_inode(inode);
1644 spin_unlock(&info->lock);
1645 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001646 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001647 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001648 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001649 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001650 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001651
Nick Piggind0217ac2007-07-19 01:47:03 -07001652 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001653 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001655unacct:
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001656 if (sbinfo->max_blocks)
1657 percpu_counter_sub(&sbinfo->used_blocks,
1658 1 << compound_order(page));
1659 shmem_unacct_blocks(info->flags, 1 << compound_order(page));
1660
1661 if (PageTransHuge(page)) {
1662 unlock_page(page);
1663 put_page(page);
1664 goto alloc_nohuge;
1665 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001666failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001667 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001668 error = -EEXIST;
1669unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001670 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001671 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001672 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001673 }
1674 if (error == -ENOSPC && !once++) {
1675 info = SHMEM_I(inode);
1676 spin_lock(&info->lock);
1677 shmem_recalc_inode(inode);
1678 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001679 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001680 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001681 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001682 goto repeat;
1683 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1687{
Al Viro496ad9a2013-01-23 17:07:38 -05001688 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001689 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001690 enum sgp_type sgp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001692 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001694 /*
1695 * Trinity finds that probing a hole which tmpfs is punching can
1696 * prevent the hole-punch from ever completing: which in turn
1697 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001698 * faulting pages into the hole while it's being punched. Although
1699 * shmem_undo_range() does remove the additions, it may be unable to
1700 * keep up, as each new page needs its own unmap_mapping_range() call,
1701 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1702 *
1703 * It does not matter if we sometimes reach this check just before the
1704 * hole-punch begins, so that one fault then races with the punch:
1705 * we just need to make racing faults a rare case.
1706 *
1707 * The implementation below would be much simpler if we just used a
1708 * standard mutex or completion: but we cannot take i_mutex in fault,
1709 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001710 */
1711 if (unlikely(inode->i_private)) {
1712 struct shmem_falloc *shmem_falloc;
1713
1714 spin_lock(&inode->i_lock);
1715 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001716 if (shmem_falloc &&
1717 shmem_falloc->waitq &&
1718 vmf->pgoff >= shmem_falloc->start &&
1719 vmf->pgoff < shmem_falloc->next) {
1720 wait_queue_head_t *shmem_falloc_waitq;
1721 DEFINE_WAIT(shmem_fault_wait);
1722
1723 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001724 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1725 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001726 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001727 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001728 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001729 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001730
1731 shmem_falloc_waitq = shmem_falloc->waitq;
1732 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1733 TASK_UNINTERRUPTIBLE);
1734 spin_unlock(&inode->i_lock);
1735 schedule();
1736
1737 /*
1738 * shmem_falloc_waitq points into the shmem_fallocate()
1739 * stack of the hole-punching task: shmem_falloc_waitq
1740 * is usually invalid by the time we reach here, but
1741 * finish_wait() does not dereference it in that case;
1742 * though i_lock needed lest racing with wake_up_all().
1743 */
1744 spin_lock(&inode->i_lock);
1745 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
1746 spin_unlock(&inode->i_lock);
1747 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001748 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001749 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001750 }
1751
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001752 sgp = SGP_CACHE;
1753 if (vma->vm_flags & VM_HUGEPAGE)
1754 sgp = SGP_HUGE;
1755 else if (vma->vm_flags & VM_NOHUGEPAGE)
1756 sgp = SGP_NOHUGE;
1757
1758 error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001759 gfp, vma->vm_mm, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (error)
1761 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07001765unsigned long shmem_get_unmapped_area(struct file *file,
1766 unsigned long uaddr, unsigned long len,
1767 unsigned long pgoff, unsigned long flags)
1768{
1769 unsigned long (*get_area)(struct file *,
1770 unsigned long, unsigned long, unsigned long, unsigned long);
1771 unsigned long addr;
1772 unsigned long offset;
1773 unsigned long inflated_len;
1774 unsigned long inflated_addr;
1775 unsigned long inflated_offset;
1776
1777 if (len > TASK_SIZE)
1778 return -ENOMEM;
1779
1780 get_area = current->mm->get_unmapped_area;
1781 addr = get_area(file, uaddr, len, pgoff, flags);
1782
1783 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1784 return addr;
1785 if (IS_ERR_VALUE(addr))
1786 return addr;
1787 if (addr & ~PAGE_MASK)
1788 return addr;
1789 if (addr > TASK_SIZE - len)
1790 return addr;
1791
1792 if (shmem_huge == SHMEM_HUGE_DENY)
1793 return addr;
1794 if (len < HPAGE_PMD_SIZE)
1795 return addr;
1796 if (flags & MAP_FIXED)
1797 return addr;
1798 /*
1799 * Our priority is to support MAP_SHARED mapped hugely;
1800 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
1801 * But if caller specified an address hint, respect that as before.
1802 */
1803 if (uaddr)
1804 return addr;
1805
1806 if (shmem_huge != SHMEM_HUGE_FORCE) {
1807 struct super_block *sb;
1808
1809 if (file) {
1810 VM_BUG_ON(file->f_op != &shmem_file_operations);
1811 sb = file_inode(file)->i_sb;
1812 } else {
1813 /*
1814 * Called directly from mm/mmap.c, or drivers/char/mem.c
1815 * for "/dev/zero", to create a shared anonymous object.
1816 */
1817 if (IS_ERR(shm_mnt))
1818 return addr;
1819 sb = shm_mnt->mnt_sb;
1820 }
1821 if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER)
1822 return addr;
1823 }
1824
1825 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
1826 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
1827 return addr;
1828 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
1829 return addr;
1830
1831 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
1832 if (inflated_len > TASK_SIZE)
1833 return addr;
1834 if (inflated_len < len)
1835 return addr;
1836
1837 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
1838 if (IS_ERR_VALUE(inflated_addr))
1839 return addr;
1840 if (inflated_addr & ~PAGE_MASK)
1841 return addr;
1842
1843 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
1844 inflated_addr += offset - inflated_offset;
1845 if (inflated_offset > offset)
1846 inflated_addr += HPAGE_PMD_SIZE;
1847
1848 if (inflated_addr > TASK_SIZE - len)
1849 return addr;
1850 return inflated_addr;
1851}
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001854static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Al Viro496ad9a2013-01-23 17:07:38 -05001856 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001857 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001860static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1861 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Al Viro496ad9a2013-01-23 17:07:38 -05001863 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001864 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001866 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1867 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869#endif
1870
1871int shmem_lock(struct file *file, int lock, struct user_struct *user)
1872{
Al Viro496ad9a2013-01-23 17:07:38 -05001873 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct shmem_inode_info *info = SHMEM_I(inode);
1875 int retval = -ENOMEM;
1876
1877 spin_lock(&info->lock);
1878 if (lock && !(info->flags & VM_LOCKED)) {
1879 if (!user_shm_lock(inode->i_size, user))
1880 goto out_nomem;
1881 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001882 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 if (!lock && (info->flags & VM_LOCKED) && user) {
1885 user_shm_unlock(inode->i_size, user);
1886 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001887 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891out_nomem:
1892 spin_unlock(&info->lock);
1893 return retval;
1894}
1895
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001896static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 file_accessed(file);
1899 vma->vm_ops = &shmem_vm_ops;
1900 return 0;
1901}
1902
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001903static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001904 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct inode *inode;
1907 struct shmem_inode_info *info;
1908 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1909
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001910 if (shmem_reserve_inode(sb))
1911 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 inode = new_inode(sb);
1914 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001915 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001916 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001919 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 info = SHMEM_I(inode);
1921 memset(info, 0, (char *)inode - (char *)info);
1922 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07001923 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001924 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001926 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04001927 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 switch (mode & S_IFMT) {
1930 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001931 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 init_special_inode(inode, mode, dev);
1933 break;
1934 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001935 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 inode->i_op = &shmem_inode_operations;
1937 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001938 mpol_shared_policy_init(&info->policy,
1939 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001942 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* Some things misbehave if size == 0 on a directory */
1944 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1945 inode->i_op = &shmem_dir_inode_operations;
1946 inode->i_fop = &simple_dir_operations;
1947 break;
1948 case S_IFLNK:
1949 /*
1950 * Must not load anything in the rbtree,
1951 * mpol_free_shared_policy will not be called.
1952 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001953 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 break;
1955 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001956 } else
1957 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return inode;
1959}
1960
Johannes Weiner0cd61442014-04-03 14:47:46 -07001961bool shmem_mapping(struct address_space *mapping)
1962{
Sasha Levinf0774d82015-02-23 05:38:00 -05001963 if (!mapping->host)
1964 return false;
1965
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001966 return mapping->host->i_sb->s_op == &shmem_ops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001970static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001971static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001973#ifdef CONFIG_TMPFS_XATTR
1974static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1975#else
1976#define shmem_initxattrs NULL
1977#endif
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001980shmem_write_begin(struct file *file, struct address_space *mapping,
1981 loff_t pos, unsigned len, unsigned flags,
1982 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Nick Piggin800d15a2007-10-16 01:25:03 -07001984 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07001985 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001986 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07001987
1988 /* i_mutex is held by caller */
1989 if (unlikely(info->seals)) {
1990 if (info->seals & F_SEAL_WRITE)
1991 return -EPERM;
1992 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
1993 return -EPERM;
1994 }
1995
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001996 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07001997}
1998
1999static int
2000shmem_write_end(struct file *file, struct address_space *mapping,
2001 loff_t pos, unsigned len, unsigned copied,
2002 struct page *page, void *fsdata)
2003{
2004 struct inode *inode = mapping->host;
2005
Hugh Dickinsd3602442008-02-04 22:28:44 -08002006 if (pos + copied > inode->i_size)
2007 i_size_write(inode, pos + copied);
2008
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002009 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002010 struct page *head = compound_head(page);
2011 if (PageTransCompound(page)) {
2012 int i;
2013
2014 for (i = 0; i < HPAGE_PMD_NR; i++) {
2015 if (head + i == page)
2016 continue;
2017 clear_highpage(head + i);
2018 flush_dcache_page(head + i);
2019 }
2020 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002021 if (copied < PAGE_SIZE) {
2022 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002023 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002024 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002025 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002026 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002027 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002028 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002029 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002030 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002031
Nick Piggin800d15a2007-10-16 01:25:03 -07002032 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Al Viro2ba5bbe2014-04-02 20:00:02 -04002035static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Al Viro6e58e792014-02-03 17:07:03 -05002037 struct file *file = iocb->ki_filp;
2038 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002040 pgoff_t index;
2041 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002042 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002043 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002044 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002045 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002046
2047 /*
2048 * Might this read be for a stacking filesystem? Then when reading
2049 * holes of a sparse file, we actually need to allocate those pages,
2050 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2051 */
Al Viro777eda22014-12-17 04:46:46 -05002052 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002053 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002055 index = *ppos >> PAGE_SHIFT;
2056 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 for (;;) {
2059 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002060 pgoff_t end_index;
2061 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 loff_t i_size = i_size_read(inode);
2063
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002064 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (index > end_index)
2066 break;
2067 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002068 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (nr <= offset)
2070 break;
2071 }
2072
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002073 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002074 if (error) {
2075 if (error == -EINVAL)
2076 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 break;
2078 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002079 if (page) {
2080 if (sgp == SGP_CACHE)
2081 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002082 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 /*
2086 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002087 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002091 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002093 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (nr <= offset) {
2095 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 break;
2098 }
2099 }
2100 nr -= offset;
2101
2102 if (page) {
2103 /*
2104 * If users can be writing to this page using arbitrary
2105 * virtual addresses, take care about potential aliasing
2106 * before reading the page on the kernel side.
2107 */
2108 if (mapping_writably_mapped(mapping))
2109 flush_dcache_page(page);
2110 /*
2111 * Mark the page accessed if we read the beginning.
2112 */
2113 if (!offset)
2114 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002115 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002117 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /*
2121 * Ok, we have the page, and it's up-to-date, so
2122 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002124 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002125 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002127 index += offset >> PAGE_SHIFT;
2128 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002130 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002131 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 break;
Al Viro6e58e792014-02-03 17:07:03 -05002133 if (ret < nr) {
2134 error = -EFAULT;
2135 break;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 cond_resched();
2138 }
2139
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002140 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002141 file_accessed(file);
2142 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Hugh Dickins708e3502011-07-25 17:12:32 -07002145static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
2146 struct pipe_inode_info *pipe, size_t len,
2147 unsigned int flags)
2148{
2149 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002150 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07002151 unsigned int loff, nr_pages, req_pages;
2152 struct page *pages[PIPE_DEF_BUFFERS];
2153 struct partial_page partial[PIPE_DEF_BUFFERS];
2154 struct page *page;
2155 pgoff_t index, end_index;
2156 loff_t isize, left;
2157 int error, page_nr;
2158 struct splice_pipe_desc spd = {
2159 .pages = pages,
2160 .partial = partial,
Eric Dumazet047fe362012-06-12 15:24:40 +02002161 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07002162 .flags = flags,
2163 .ops = &page_cache_pipe_buf_ops,
2164 .spd_release = spd_release_page,
2165 };
2166
Hugh Dickins71f0e072011-07-25 17:12:33 -07002167 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07002168 if (unlikely(*ppos >= isize))
2169 return 0;
2170
2171 left = isize - *ppos;
2172 if (unlikely(left < len))
2173 len = left;
2174
2175 if (splice_grow_spd(pipe, &spd))
2176 return -ENOMEM;
2177
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002178 index = *ppos >> PAGE_SHIFT;
2179 loff = *ppos & ~PAGE_MASK;
2180 req_pages = (len + loff + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Viroa786c062014-04-11 12:01:03 -04002181 nr_pages = min(req_pages, spd.nr_pages_max);
Hugh Dickins708e3502011-07-25 17:12:32 -07002182
Hugh Dickins708e3502011-07-25 17:12:32 -07002183 spd.nr_pages = find_get_pages_contig(mapping, index,
2184 nr_pages, spd.pages);
2185 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07002186 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002187
Hugh Dickins708e3502011-07-25 17:12:32 -07002188 while (spd.nr_pages < nr_pages) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002189 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002190 if (error)
2191 break;
2192 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07002193 spd.pages[spd.nr_pages++] = page;
2194 index++;
2195 }
2196
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002197 index = *ppos >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002198 nr_pages = spd.nr_pages;
2199 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002200
Hugh Dickins708e3502011-07-25 17:12:32 -07002201 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
2202 unsigned int this_len;
2203
2204 if (!len)
2205 break;
2206
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002207 this_len = min_t(unsigned long, len, PAGE_SIZE - loff);
Hugh Dickins708e3502011-07-25 17:12:32 -07002208 page = spd.pages[page_nr];
2209
Hugh Dickins71f0e072011-07-25 17:12:33 -07002210 if (!PageUptodate(page) || page->mapping != mapping) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002211 error = shmem_getpage(inode, index, &page, SGP_CACHE);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002212 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07002213 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07002214 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002215 put_page(spd.pages[page_nr]);
Hugh Dickins71f0e072011-07-25 17:12:33 -07002216 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07002217 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07002218
2219 isize = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002220 end_index = (isize - 1) >> PAGE_SHIFT;
Hugh Dickins708e3502011-07-25 17:12:32 -07002221 if (unlikely(!isize || index > end_index))
2222 break;
2223
Hugh Dickins708e3502011-07-25 17:12:32 -07002224 if (end_index == index) {
2225 unsigned int plen;
2226
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002227 plen = ((isize - 1) & ~PAGE_MASK) + 1;
Hugh Dickins708e3502011-07-25 17:12:32 -07002228 if (plen <= loff)
2229 break;
2230
Hugh Dickins708e3502011-07-25 17:12:32 -07002231 this_len = min(this_len, plen - loff);
2232 len = this_len;
2233 }
2234
2235 spd.partial[page_nr].offset = loff;
2236 spd.partial[page_nr].len = this_len;
2237 len -= this_len;
2238 loff = 0;
2239 spd.nr_pages++;
2240 index++;
2241 }
2242
Hugh Dickins708e3502011-07-25 17:12:32 -07002243 while (page_nr < nr_pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002244 put_page(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07002245
2246 if (spd.nr_pages)
2247 error = splice_to_pipe(pipe, &spd);
2248
Eric Dumazet047fe362012-06-12 15:24:40 +02002249 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07002250
2251 if (error > 0) {
2252 *ppos += error;
2253 file_accessed(in);
2254 }
2255 return error;
2256}
2257
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002258/*
2259 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2260 */
2261static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002262 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002263{
2264 struct page *page;
2265 struct pagevec pvec;
2266 pgoff_t indices[PAGEVEC_SIZE];
2267 bool done = false;
2268 int i;
2269
2270 pagevec_init(&pvec, 0);
2271 pvec.nr = 1; /* start small: we may be there already */
2272 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002273 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002274 pvec.nr, pvec.pages, indices);
2275 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002276 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002277 index = end;
2278 break;
2279 }
2280 for (i = 0; i < pvec.nr; i++, index++) {
2281 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002282 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002283 done = true;
2284 break;
2285 }
2286 index = indices[i];
2287 }
2288 page = pvec.pages[i];
2289 if (page && !radix_tree_exceptional_entry(page)) {
2290 if (!PageUptodate(page))
2291 page = NULL;
2292 }
2293 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002294 (page && whence == SEEK_DATA) ||
2295 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002296 done = true;
2297 break;
2298 }
2299 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002300 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002301 pagevec_release(&pvec);
2302 pvec.nr = PAGEVEC_SIZE;
2303 cond_resched();
2304 }
2305 return index;
2306}
2307
Andrew Morton965c8e52012-12-17 15:59:39 -08002308static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002309{
2310 struct address_space *mapping = file->f_mapping;
2311 struct inode *inode = mapping->host;
2312 pgoff_t start, end;
2313 loff_t new_offset;
2314
Andrew Morton965c8e52012-12-17 15:59:39 -08002315 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2316 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002317 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002318 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002319 /* We're holding i_mutex so we can access i_size directly */
2320
2321 if (offset < 0)
2322 offset = -EINVAL;
2323 else if (offset >= inode->i_size)
2324 offset = -ENXIO;
2325 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002326 start = offset >> PAGE_SHIFT;
2327 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002328 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002329 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002330 if (new_offset > offset) {
2331 if (new_offset < inode->i_size)
2332 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002333 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002334 offset = -ENXIO;
2335 else
2336 offset = inode->i_size;
2337 }
2338 }
2339
Hugh Dickins387aae62013-08-04 11:30:25 -07002340 if (offset >= 0)
2341 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002342 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002343 return offset;
2344}
2345
David Herrmann05f65b52014-08-08 14:25:36 -07002346/*
2347 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
2348 * so reuse a tag which we firmly believe is never set or cleared on shmem.
2349 */
2350#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
2351#define LAST_SCAN 4 /* about 150ms max */
2352
2353static void shmem_tag_pins(struct address_space *mapping)
2354{
2355 struct radix_tree_iter iter;
2356 void **slot;
2357 pgoff_t start;
2358 struct page *page;
2359
2360 lru_add_drain();
2361 start = 0;
2362 rcu_read_lock();
2363
David Herrmann05f65b52014-08-08 14:25:36 -07002364 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
2365 page = radix_tree_deref_slot(slot);
2366 if (!page || radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002367 if (radix_tree_deref_retry(page)) {
2368 slot = radix_tree_iter_retry(&iter);
2369 continue;
2370 }
David Herrmann05f65b52014-08-08 14:25:36 -07002371 } else if (page_count(page) - page_mapcount(page) > 1) {
2372 spin_lock_irq(&mapping->tree_lock);
2373 radix_tree_tag_set(&mapping->page_tree, iter.index,
2374 SHMEM_TAG_PINNED);
2375 spin_unlock_irq(&mapping->tree_lock);
2376 }
2377
2378 if (need_resched()) {
2379 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002380 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002381 }
2382 }
2383 rcu_read_unlock();
2384}
2385
2386/*
2387 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
2388 * via get_user_pages(), drivers might have some pending I/O without any active
2389 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
2390 * and see whether it has an elevated ref-count. If so, we tag them and wait for
2391 * them to be dropped.
2392 * The caller must guarantee that no new user will acquire writable references
2393 * to those pages to avoid races.
2394 */
David Herrmann40e041a2014-08-08 14:25:27 -07002395static int shmem_wait_for_pins(struct address_space *mapping)
2396{
David Herrmann05f65b52014-08-08 14:25:36 -07002397 struct radix_tree_iter iter;
2398 void **slot;
2399 pgoff_t start;
2400 struct page *page;
2401 int error, scan;
2402
2403 shmem_tag_pins(mapping);
2404
2405 error = 0;
2406 for (scan = 0; scan <= LAST_SCAN; scan++) {
2407 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
2408 break;
2409
2410 if (!scan)
2411 lru_add_drain_all();
2412 else if (schedule_timeout_killable((HZ << scan) / 200))
2413 scan = LAST_SCAN;
2414
2415 start = 0;
2416 rcu_read_lock();
David Herrmann05f65b52014-08-08 14:25:36 -07002417 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
2418 start, SHMEM_TAG_PINNED) {
2419
2420 page = radix_tree_deref_slot(slot);
2421 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002422 if (radix_tree_deref_retry(page)) {
2423 slot = radix_tree_iter_retry(&iter);
2424 continue;
2425 }
David Herrmann05f65b52014-08-08 14:25:36 -07002426
2427 page = NULL;
2428 }
2429
2430 if (page &&
2431 page_count(page) - page_mapcount(page) != 1) {
2432 if (scan < LAST_SCAN)
2433 goto continue_resched;
2434
2435 /*
2436 * On the last scan, we clean up all those tags
2437 * we inserted; but make a note that we still
2438 * found pages pinned.
2439 */
2440 error = -EBUSY;
2441 }
2442
2443 spin_lock_irq(&mapping->tree_lock);
2444 radix_tree_tag_clear(&mapping->page_tree,
2445 iter.index, SHMEM_TAG_PINNED);
2446 spin_unlock_irq(&mapping->tree_lock);
2447continue_resched:
2448 if (need_resched()) {
2449 cond_resched_rcu();
Matthew Wilcox71650922016-03-17 14:22:06 -07002450 slot = radix_tree_iter_next(&iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002451 }
2452 }
2453 rcu_read_unlock();
2454 }
2455
2456 return error;
David Herrmann40e041a2014-08-08 14:25:27 -07002457}
2458
2459#define F_ALL_SEALS (F_SEAL_SEAL | \
2460 F_SEAL_SHRINK | \
2461 F_SEAL_GROW | \
2462 F_SEAL_WRITE)
2463
2464int shmem_add_seals(struct file *file, unsigned int seals)
2465{
2466 struct inode *inode = file_inode(file);
2467 struct shmem_inode_info *info = SHMEM_I(inode);
2468 int error;
2469
2470 /*
2471 * SEALING
2472 * Sealing allows multiple parties to share a shmem-file but restrict
2473 * access to a specific subset of file operations. Seals can only be
2474 * added, but never removed. This way, mutually untrusted parties can
2475 * share common memory regions with a well-defined policy. A malicious
2476 * peer can thus never perform unwanted operations on a shared object.
2477 *
2478 * Seals are only supported on special shmem-files and always affect
2479 * the whole underlying inode. Once a seal is set, it may prevent some
2480 * kinds of access to the file. Currently, the following seals are
2481 * defined:
2482 * SEAL_SEAL: Prevent further seals from being set on this file
2483 * SEAL_SHRINK: Prevent the file from shrinking
2484 * SEAL_GROW: Prevent the file from growing
2485 * SEAL_WRITE: Prevent write access to the file
2486 *
2487 * As we don't require any trust relationship between two parties, we
2488 * must prevent seals from being removed. Therefore, sealing a file
2489 * only adds a given set of seals to the file, it never touches
2490 * existing seals. Furthermore, the "setting seals"-operation can be
2491 * sealed itself, which basically prevents any further seal from being
2492 * added.
2493 *
2494 * Semantics of sealing are only defined on volatile files. Only
2495 * anonymous shmem files support sealing. More importantly, seals are
2496 * never written to disk. Therefore, there's no plan to support it on
2497 * other file types.
2498 */
2499
2500 if (file->f_op != &shmem_file_operations)
2501 return -EINVAL;
2502 if (!(file->f_mode & FMODE_WRITE))
2503 return -EPERM;
2504 if (seals & ~(unsigned int)F_ALL_SEALS)
2505 return -EINVAL;
2506
Al Viro59551022016-01-22 15:40:57 -05002507 inode_lock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002508
2509 if (info->seals & F_SEAL_SEAL) {
2510 error = -EPERM;
2511 goto unlock;
2512 }
2513
2514 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2515 error = mapping_deny_writable(file->f_mapping);
2516 if (error)
2517 goto unlock;
2518
2519 error = shmem_wait_for_pins(file->f_mapping);
2520 if (error) {
2521 mapping_allow_writable(file->f_mapping);
2522 goto unlock;
2523 }
2524 }
2525
2526 info->seals |= seals;
2527 error = 0;
2528
2529unlock:
Al Viro59551022016-01-22 15:40:57 -05002530 inode_unlock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002531 return error;
2532}
2533EXPORT_SYMBOL_GPL(shmem_add_seals);
2534
2535int shmem_get_seals(struct file *file)
2536{
2537 if (file->f_op != &shmem_file_operations)
2538 return -EINVAL;
2539
2540 return SHMEM_I(file_inode(file))->seals;
2541}
2542EXPORT_SYMBOL_GPL(shmem_get_seals);
2543
2544long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2545{
2546 long error;
2547
2548 switch (cmd) {
2549 case F_ADD_SEALS:
2550 /* disallow upper 32bit */
2551 if (arg > UINT_MAX)
2552 return -EINVAL;
2553
2554 error = shmem_add_seals(file, arg);
2555 break;
2556 case F_GET_SEALS:
2557 error = shmem_get_seals(file);
2558 break;
2559 default:
2560 error = -EINVAL;
2561 break;
2562 }
2563
2564 return error;
2565}
2566
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002567static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2568 loff_t len)
2569{
Al Viro496ad9a2013-01-23 17:07:38 -05002570 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002571 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002572 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002573 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002574 pgoff_t start, index, end;
2575 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002576
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002577 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2578 return -EOPNOTSUPP;
2579
Al Viro59551022016-01-22 15:40:57 -05002580 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002581
2582 if (mode & FALLOC_FL_PUNCH_HOLE) {
2583 struct address_space *mapping = file->f_mapping;
2584 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2585 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002586 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002587
David Herrmann40e041a2014-08-08 14:25:27 -07002588 /* protected by i_mutex */
2589 if (info->seals & F_SEAL_WRITE) {
2590 error = -EPERM;
2591 goto out;
2592 }
2593
Hugh Dickins8e205f72014-07-23 14:00:10 -07002594 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002595 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2596 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2597 spin_lock(&inode->i_lock);
2598 inode->i_private = &shmem_falloc;
2599 spin_unlock(&inode->i_lock);
2600
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002601 if ((u64)unmap_end > (u64)unmap_start)
2602 unmap_mapping_range(mapping, unmap_start,
2603 1 + unmap_end - unmap_start, 0);
2604 shmem_truncate_range(inode, offset, offset + len - 1);
2605 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002606
2607 spin_lock(&inode->i_lock);
2608 inode->i_private = NULL;
2609 wake_up_all(&shmem_falloc_waitq);
2610 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002611 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002612 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002613 }
2614
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002615 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2616 error = inode_newsize_ok(inode, offset + len);
2617 if (error)
2618 goto out;
2619
David Herrmann40e041a2014-08-08 14:25:27 -07002620 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2621 error = -EPERM;
2622 goto out;
2623 }
2624
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002625 start = offset >> PAGE_SHIFT;
2626 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002627 /* Try to avoid a swapstorm if len is impossible to satisfy */
2628 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2629 error = -ENOSPC;
2630 goto out;
2631 }
2632
Hugh Dickins8e205f72014-07-23 14:00:10 -07002633 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002634 shmem_falloc.start = start;
2635 shmem_falloc.next = start;
2636 shmem_falloc.nr_falloced = 0;
2637 shmem_falloc.nr_unswapped = 0;
2638 spin_lock(&inode->i_lock);
2639 inode->i_private = &shmem_falloc;
2640 spin_unlock(&inode->i_lock);
2641
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002642 for (index = start; index < end; index++) {
2643 struct page *page;
2644
2645 /*
2646 * Good, the fallocate(2) manpage permits EINTR: we may have
2647 * been interrupted because we are using up too much memory.
2648 */
2649 if (signal_pending(current))
2650 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002651 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2652 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002653 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002654 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002655 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002656 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002657 if (index > start) {
2658 shmem_undo_range(inode,
2659 (loff_t)start << PAGE_SHIFT,
2660 ((loff_t)index << PAGE_SHIFT) - 1, true);
2661 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002662 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002663 }
2664
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002665 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002666 * Inform shmem_writepage() how far we have reached.
2667 * No need for lock or barrier: we have the page lock.
2668 */
2669 shmem_falloc.next++;
2670 if (!PageUptodate(page))
2671 shmem_falloc.nr_falloced++;
2672
2673 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002674 * If !PageUptodate, leave it that way so that freeable pages
2675 * can be recognized if we need to rollback on error later.
2676 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002677 * than free the pages we are allocating (and SGP_CACHE pages
2678 * might still be clean: we now need to mark those dirty too).
2679 */
2680 set_page_dirty(page);
2681 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002682 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002683 cond_resched();
2684 }
2685
2686 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2687 i_size_write(inode, offset + len);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002688 inode->i_ctime = CURRENT_TIME;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002689undone:
2690 spin_lock(&inode->i_lock);
2691 inode->i_private = NULL;
2692 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002693out:
Al Viro59551022016-01-22 15:40:57 -05002694 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002695 return error;
2696}
2697
David Howells726c3342006-06-23 02:02:58 -07002698static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
David Howells726c3342006-06-23 02:02:58 -07002700 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002703 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002705 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002707 buf->f_bavail =
2708 buf->f_bfree = sbinfo->max_blocks -
2709 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002710 }
2711 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 buf->f_files = sbinfo->max_inodes;
2713 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715 /* else leave those fields 0 like simple_statfs */
2716 return 0;
2717}
2718
2719/*
2720 * File creation. Allocate an inode, and we're done..
2721 */
2722static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002723shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002725 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 int error = -ENOSPC;
2727
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002728 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002730 error = simple_acl_create(dir, inode);
2731 if (error)
2732 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002733 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002734 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002735 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002736 if (error && error != -EOPNOTSUPP)
2737 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002738
Al Viro718deb62009-12-16 19:35:36 -05002739 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 dir->i_size += BOGO_DIRENT_SIZE;
2741 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2742 d_instantiate(dentry, inode);
2743 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002746out_iput:
2747 iput(inode);
2748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
Al Viro60545d02013-06-07 01:20:27 -04002751static int
2752shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2753{
2754 struct inode *inode;
2755 int error = -ENOSPC;
2756
2757 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2758 if (inode) {
2759 error = security_inode_init_security(inode, dir,
2760 NULL,
2761 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002762 if (error && error != -EOPNOTSUPP)
2763 goto out_iput;
2764 error = simple_acl_create(dir, inode);
2765 if (error)
2766 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002767 d_tmpfile(dentry, inode);
2768 }
2769 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002770out_iput:
2771 iput(inode);
2772 return error;
Al Viro60545d02013-06-07 01:20:27 -04002773}
2774
Al Viro18bb1db2011-07-26 01:41:39 -04002775static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 int error;
2778
2779 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2780 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002781 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return 0;
2783}
2784
Al Viro4acdaf22011-07-26 01:42:34 -04002785static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002786 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2789}
2790
2791/*
2792 * Link a file..
2793 */
2794static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2795{
David Howells75c3cfa2015-03-17 22:26:12 +00002796 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002797 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 /*
2800 * No ordinary (disk based) filesystem counts links as inodes;
2801 * but each new link needs a new dentry, pinning lowmem, and
2802 * tmpfs dentries cannot be pruned until they are unlinked.
2803 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002804 ret = shmem_reserve_inode(inode->i_sb);
2805 if (ret)
2806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 dir->i_size += BOGO_DIRENT_SIZE;
2809 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07002810 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002811 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 dget(dentry); /* Extra pinning count for the created dentry */
2813 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002814out:
2815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2819{
David Howells75c3cfa2015-03-17 22:26:12 +00002820 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002822 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2823 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 dir->i_size -= BOGO_DIRENT_SIZE;
2826 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002827 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 dput(dentry); /* Undo the count from "create" - this does all the work */
2829 return 0;
2830}
2831
2832static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2833{
2834 if (!simple_empty(dentry))
2835 return -ENOTEMPTY;
2836
David Howells75c3cfa2015-03-17 22:26:12 +00002837 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002838 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return shmem_unlink(dir, dentry);
2840}
2841
Miklos Szeredi37456772014-07-23 15:15:34 +02002842static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2843{
David Howellse36cb0b2015-01-29 12:02:35 +00002844 bool old_is_dir = d_is_dir(old_dentry);
2845 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002846
2847 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2848 if (old_is_dir) {
2849 drop_nlink(old_dir);
2850 inc_nlink(new_dir);
2851 } else {
2852 drop_nlink(new_dir);
2853 inc_nlink(old_dir);
2854 }
2855 }
2856 old_dir->i_ctime = old_dir->i_mtime =
2857 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002858 d_inode(old_dentry)->i_ctime =
2859 d_inode(new_dentry)->i_ctime = CURRENT_TIME;
Miklos Szeredi37456772014-07-23 15:15:34 +02002860
2861 return 0;
2862}
2863
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002864static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2865{
2866 struct dentry *whiteout;
2867 int error;
2868
2869 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2870 if (!whiteout)
2871 return -ENOMEM;
2872
2873 error = shmem_mknod(old_dir, whiteout,
2874 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2875 dput(whiteout);
2876 if (error)
2877 return error;
2878
2879 /*
2880 * Cheat and hash the whiteout while the old dentry is still in
2881 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2882 *
2883 * d_lookup() will consistently find one of them at this point,
2884 * not sure which one, but that isn't even important.
2885 */
2886 d_rehash(whiteout);
2887 return 0;
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890/*
2891 * The VFS layer already does all the dentry stuff for rename,
2892 * we just have to decrement the usage count for the target if
2893 * it exists so that the VFS layer correctly free's it when it
2894 * gets overwritten.
2895 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002896static 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 -07002897{
David Howells75c3cfa2015-03-17 22:26:12 +00002898 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 int they_are_dirs = S_ISDIR(inode->i_mode);
2900
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002901 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002902 return -EINVAL;
2903
Miklos Szeredi37456772014-07-23 15:15:34 +02002904 if (flags & RENAME_EXCHANGE)
2905 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (!simple_empty(new_dentry))
2908 return -ENOTEMPTY;
2909
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002910 if (flags & RENAME_WHITEOUT) {
2911 int error;
2912
2913 error = shmem_whiteout(old_dir, old_dentry);
2914 if (error)
2915 return error;
2916 }
2917
David Howells75c3cfa2015-03-17 22:26:12 +00002918 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002920 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002921 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002922 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002925 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002926 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
2929 old_dir->i_size -= BOGO_DIRENT_SIZE;
2930 new_dir->i_size += BOGO_DIRENT_SIZE;
2931 old_dir->i_ctime = old_dir->i_mtime =
2932 new_dir->i_ctime = new_dir->i_mtime =
2933 inode->i_ctime = CURRENT_TIME;
2934 return 0;
2935}
2936
2937static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2938{
2939 int error;
2940 int len;
2941 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002942 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct shmem_inode_info *info;
2944
2945 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002946 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return -ENAMETOOLONG;
2948
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002949 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (!inode)
2951 return -ENOSPC;
2952
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002953 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002954 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002955 if (error) {
2956 if (error != -EOPNOTSUPP) {
2957 iput(inode);
2958 return error;
2959 }
2960 error = 0;
2961 }
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 info = SHMEM_I(inode);
2964 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002965 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05002966 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
2967 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002968 iput(inode);
2969 return -ENOMEM;
2970 }
2971 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05002973 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002974 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (error) {
2976 iput(inode);
2977 return error;
2978 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07002979 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05002981 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002982 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002984 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002985 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 dir->i_size += BOGO_DIRENT_SIZE;
2988 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2989 d_instantiate(dentry, inode);
2990 dget(dentry);
2991 return 0;
2992}
2993
Al Virofceef392015-12-29 15:58:39 -05002994static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Al Virofceef392015-12-29 15:58:39 -05002996 mark_page_accessed(arg);
2997 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
Al Viro6b255392015-11-17 10:20:54 -05003000static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003001 struct inode *inode,
3002 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003005 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003006 if (!dentry) {
3007 page = find_get_page(inode->i_mapping, 0);
3008 if (!page)
3009 return ERR_PTR(-ECHILD);
3010 if (!PageUptodate(page)) {
3011 put_page(page);
3012 return ERR_PTR(-ECHILD);
3013 }
3014 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003015 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003016 if (error)
3017 return ERR_PTR(error);
3018 unlock_page(page);
3019 }
Al Virofceef392015-12-29 15:58:39 -05003020 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003021 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Eric Parisb09e0fa2011-05-24 17:12:39 -07003024#ifdef CONFIG_TMPFS_XATTR
3025/*
3026 * Superblocks without xattr inode operations may get some security.* xattr
3027 * support from the LSM "for free". As soon as we have any other xattrs
3028 * like ACLs, we also need to implement the security.* handlers at
3029 * filesystem level, though.
3030 */
3031
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003032/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003033 * Callback for security_inode_init_security() for acquiring xattrs.
3034 */
3035static int shmem_initxattrs(struct inode *inode,
3036 const struct xattr *xattr_array,
3037 void *fs_info)
3038{
3039 struct shmem_inode_info *info = SHMEM_I(inode);
3040 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003041 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003042 size_t len;
3043
3044 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003045 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003046 if (!new_xattr)
3047 return -ENOMEM;
3048
3049 len = strlen(xattr->name) + 1;
3050 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3051 GFP_KERNEL);
3052 if (!new_xattr->name) {
3053 kfree(new_xattr);
3054 return -ENOMEM;
3055 }
3056
3057 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3058 XATTR_SECURITY_PREFIX_LEN);
3059 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3060 xattr->name, len);
3061
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003062 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003063 }
3064
3065 return 0;
3066}
3067
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003068static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003069 struct dentry *unused, struct inode *inode,
3070 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003071{
Al Virob2968212016-04-10 20:48:24 -04003072 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003073
3074 name = xattr_full_name(handler, name);
3075 return simple_xattr_get(&info->xattrs, name, buffer, size);
3076}
3077
3078static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003079 struct dentry *unused, struct inode *inode,
3080 const char *name, const void *value,
3081 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003082{
Al Viro59301222016-05-27 10:19:30 -04003083 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003084
3085 name = xattr_full_name(handler, name);
3086 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3087}
3088
3089static const struct xattr_handler shmem_security_xattr_handler = {
3090 .prefix = XATTR_SECURITY_PREFIX,
3091 .get = shmem_xattr_handler_get,
3092 .set = shmem_xattr_handler_set,
3093};
3094
3095static const struct xattr_handler shmem_trusted_xattr_handler = {
3096 .prefix = XATTR_TRUSTED_PREFIX,
3097 .get = shmem_xattr_handler_get,
3098 .set = shmem_xattr_handler_set,
3099};
3100
Eric Parisb09e0fa2011-05-24 17:12:39 -07003101static const struct xattr_handler *shmem_xattr_handlers[] = {
3102#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003103 &posix_acl_access_xattr_handler,
3104 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003105#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003106 &shmem_security_xattr_handler,
3107 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003108 NULL
3109};
3110
Eric Parisb09e0fa2011-05-24 17:12:39 -07003111static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3112{
David Howells75c3cfa2015-03-17 22:26:12 +00003113 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003114 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003115}
3116#endif /* CONFIG_TMPFS_XATTR */
3117
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003118static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003120 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003121#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003122 .setxattr = generic_setxattr,
3123 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003124 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003125 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127};
3128
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003129static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05003131 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003132#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003133 .setxattr = generic_setxattr,
3134 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003135 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003136 .removexattr = generic_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003137#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003138};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003139
David M. Grimes91828a42006-10-17 00:09:45 -07003140static struct dentry *shmem_get_parent(struct dentry *child)
3141{
3142 return ERR_PTR(-ESTALE);
3143}
3144
3145static int shmem_match(struct inode *ino, void *vfh)
3146{
3147 __u32 *fh = vfh;
3148 __u64 inum = fh[2];
3149 inum = (inum << 32) | fh[1];
3150 return ino->i_ino == inum && fh[0] == ino->i_generation;
3151}
3152
Christoph Hellwig480b1162007-10-21 16:42:13 -07003153static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3154 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003155{
David M. Grimes91828a42006-10-17 00:09:45 -07003156 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003157 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003158 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003159
Christoph Hellwig480b1162007-10-21 16:42:13 -07003160 if (fh_len < 3)
3161 return NULL;
3162
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003163 inum = fid->raw[2];
3164 inum = (inum << 32) | fid->raw[1];
3165
Christoph Hellwig480b1162007-10-21 16:42:13 -07003166 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3167 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003168 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07003169 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003170 iput(inode);
3171 }
3172
Christoph Hellwig480b1162007-10-21 16:42:13 -07003173 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003174}
3175
Al Virob0b03822012-04-02 14:34:06 -04003176static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3177 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003178{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303179 if (*len < 3) {
3180 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003181 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303182 }
David M. Grimes91828a42006-10-17 00:09:45 -07003183
Al Viro1d3382cb2010-10-23 15:19:20 -04003184 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003185 /* Unfortunately insert_inode_hash is not idempotent,
3186 * so as we hash inodes here rather than at creation
3187 * time, we need a lock to ensure we only try
3188 * to do it once
3189 */
3190 static DEFINE_SPINLOCK(lock);
3191 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003192 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003193 __insert_inode_hash(inode,
3194 inode->i_ino + inode->i_generation);
3195 spin_unlock(&lock);
3196 }
3197
3198 fh[0] = inode->i_generation;
3199 fh[1] = inode->i_ino;
3200 fh[2] = ((__u64)inode->i_ino) >> 32;
3201
3202 *len = 3;
3203 return 1;
3204}
3205
Christoph Hellwig39655162007-10-21 16:42:17 -07003206static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003207 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003208 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003209 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003210};
3211
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003212static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3213 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003216 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003217 uid_t uid;
3218 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003220 while (options != NULL) {
3221 this_char = options;
3222 for (;;) {
3223 /*
3224 * NUL-terminate this option: unfortunately,
3225 * mount options form a comma-separated list,
3226 * but mpol's nodelist may also contain commas.
3227 */
3228 options = strchr(options, ',');
3229 if (options == NULL)
3230 break;
3231 options++;
3232 if (!isdigit(*options)) {
3233 options[-1] = '\0';
3234 break;
3235 }
3236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 if (!*this_char)
3238 continue;
3239 if ((value = strchr(this_char,'=')) != NULL) {
3240 *value++ = 0;
3241 } else {
Joe Perches11705322016-03-17 14:19:50 -07003242 pr_err("tmpfs: No value for mount option '%s'\n",
3243 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003244 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246
3247 if (!strcmp(this_char,"size")) {
3248 unsigned long long size;
3249 size = memparse(value,&rest);
3250 if (*rest == '%') {
3251 size <<= PAGE_SHIFT;
3252 size *= totalram_pages;
3253 do_div(size, 100);
3254 rest++;
3255 }
3256 if (*rest)
3257 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003258 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003259 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003261 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (*rest)
3263 goto bad_val;
3264 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003265 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (*rest)
3267 goto bad_val;
3268 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003269 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003271 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (*rest)
3273 goto bad_val;
3274 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003275 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003277 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (*rest)
3279 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003280 sbinfo->uid = make_kuid(current_user_ns(), uid);
3281 if (!uid_valid(sbinfo->uid))
3282 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003284 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003286 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 if (*rest)
3288 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003289 sbinfo->gid = make_kgid(current_user_ns(), gid);
3290 if (!gid_valid(sbinfo->gid))
3291 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003292#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3293 } else if (!strcmp(this_char, "huge")) {
3294 int huge;
3295 huge = shmem_parse_huge(value);
3296 if (huge < 0)
3297 goto bad_val;
3298 if (!has_transparent_hugepage() &&
3299 huge != SHMEM_HUGE_NEVER)
3300 goto bad_val;
3301 sbinfo->huge = huge;
3302#endif
3303#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003304 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003305 mpol_put(mpol);
3306 mpol = NULL;
3307 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003308 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003309#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 } else {
Joe Perches11705322016-03-17 14:19:50 -07003311 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003312 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003315 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return 0;
3317
3318bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003319 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003321error:
3322 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return 1;
3324
3325}
3326
3327static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3328{
3329 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003330 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003331 unsigned long inodes;
3332 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Greg Thelen5f001102013-02-22 16:36:01 -08003334 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003335 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003336 return error;
3337
3338 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003339 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003340 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003341 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003342 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003343 goto out;
3344 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003345 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003346 * but we must separately disallow unlimited->limited, because
3347 * in that case we have no record of how much is already in use.
3348 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003349 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003350 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003351 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003352 goto out;
3353
3354 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003355 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003356 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003357 sbinfo->max_inodes = config.max_inodes;
3358 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003359
Greg Thelen5f001102013-02-22 16:36:01 -08003360 /*
3361 * Preserve previous mempolicy unless mpol remount option was specified.
3362 */
3363 if (config.mpol) {
3364 mpol_put(sbinfo->mpol);
3365 sbinfo->mpol = config.mpol; /* transfers initial ref */
3366 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003367out:
3368 spin_unlock(&sbinfo->stat_lock);
3369 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003371
Al Viro34c80b12011-12-08 21:32:45 -05003372static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003373{
Al Viro34c80b12011-12-08 21:32:45 -05003374 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003375
3376 if (sbinfo->max_blocks != shmem_default_max_blocks())
3377 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003378 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003379 if (sbinfo->max_inodes != shmem_default_max_inodes())
3380 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
3381 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003382 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003383 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3384 seq_printf(seq, ",uid=%u",
3385 from_kuid_munged(&init_user_ns, sbinfo->uid));
3386 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3387 seq_printf(seq, ",gid=%u",
3388 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003389#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3390 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3391 if (sbinfo->huge)
3392 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3393#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003394 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003395 return 0;
3396}
David Herrmann9183df22014-08-08 14:25:29 -07003397
3398#define MFD_NAME_PREFIX "memfd:"
3399#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
3400#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
3401
3402#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
3403
3404SYSCALL_DEFINE2(memfd_create,
3405 const char __user *, uname,
3406 unsigned int, flags)
3407{
3408 struct shmem_inode_info *info;
3409 struct file *file;
3410 int fd, error;
3411 char *name;
3412 long len;
3413
3414 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
3415 return -EINVAL;
3416
3417 /* length includes terminating zero */
3418 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
3419 if (len <= 0)
3420 return -EFAULT;
3421 if (len > MFD_NAME_MAX_LEN + 1)
3422 return -EINVAL;
3423
3424 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
3425 if (!name)
3426 return -ENOMEM;
3427
3428 strcpy(name, MFD_NAME_PREFIX);
3429 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
3430 error = -EFAULT;
3431 goto err_name;
3432 }
3433
3434 /* terminating-zero may have changed after strnlen_user() returned */
3435 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
3436 error = -EFAULT;
3437 goto err_name;
3438 }
3439
3440 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
3441 if (fd < 0) {
3442 error = fd;
3443 goto err_name;
3444 }
3445
3446 file = shmem_file_setup(name, 0, VM_NORESERVE);
3447 if (IS_ERR(file)) {
3448 error = PTR_ERR(file);
3449 goto err_fd;
3450 }
3451 info = SHMEM_I(file_inode(file));
3452 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
3453 file->f_flags |= O_RDWR | O_LARGEFILE;
3454 if (flags & MFD_ALLOW_SEALING)
3455 info->seals &= ~F_SEAL_SEAL;
3456
3457 fd_install(fd, file);
3458 kfree(name);
3459 return fd;
3460
3461err_fd:
3462 put_unused_fd(fd);
3463err_name:
3464 kfree(name);
3465 return error;
3466}
3467
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003468#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470static void shmem_put_super(struct super_block *sb)
3471{
Hugh Dickins602586a2010-08-17 15:23:56 -07003472 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3473
3474 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003475 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003476 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 sb->s_fs_info = NULL;
3478}
3479
Kay Sievers2b2af542009-04-30 15:23:42 +02003480int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003483 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003484 int err = -ENOMEM;
3485
3486 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003487 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003488 L1_CACHE_BYTES), GFP_KERNEL);
3489 if (!sbinfo)
3490 return -ENOMEM;
3491
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003492 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003493 sbinfo->uid = current_fsuid();
3494 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003495 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003497#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /*
3499 * Per default we only allow half of the physical ram per
3500 * tmpfs instance, limiting inodes to one per page of lowmem;
3501 * but the internal instance is left unlimited.
3502 */
Al Viroca4e0512013-08-31 12:57:10 -04003503 if (!(sb->s_flags & MS_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003504 sbinfo->max_blocks = shmem_default_max_blocks();
3505 sbinfo->max_inodes = shmem_default_max_inodes();
3506 if (shmem_parse_options(data, sbinfo, false)) {
3507 err = -EINVAL;
3508 goto failed;
3509 }
Al Viroca4e0512013-08-31 12:57:10 -04003510 } else {
3511 sb->s_flags |= MS_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 }
David M. Grimes91828a42006-10-17 00:09:45 -07003513 sb->s_export_op = &shmem_export_ops;
Hugh Dickins2f6e38f2012-05-29 15:06:38 -07003514 sb->s_flags |= MS_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515#else
3516 sb->s_flags |= MS_NOUSER;
3517#endif
3518
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003519 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003520 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003521 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003522 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003523
Hugh Dickins285b2c42011-08-03 16:21:20 -07003524 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003525 sb->s_blocksize = PAGE_SIZE;
3526 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 sb->s_magic = TMPFS_MAGIC;
3528 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003529 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003530#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003531 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003532#endif
3533#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003534 sb->s_flags |= MS_POSIXACL;
3535#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003536
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003537 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (!inode)
3539 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003540 inode->i_uid = sbinfo->uid;
3541 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003542 sb->s_root = d_make_root(inode);
3543 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003544 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return 0;
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547failed:
3548 shmem_put_super(sb);
3549 return err;
3550}
3551
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003552static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554static struct inode *shmem_alloc_inode(struct super_block *sb)
3555{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003556 struct shmem_inode_info *info;
3557 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3558 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003560 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003563static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003564{
3565 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003566 if (S_ISLNK(inode->i_mode))
3567 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003568 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3569}
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571static void shmem_destroy_inode(struct inode *inode)
3572{
Al Viro09208d12011-07-26 03:15:03 -04003573 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003575 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003578static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003580 struct shmem_inode_info *info = foo;
3581 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003584static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585{
3586 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3587 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003588 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return 0;
3590}
3591
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003592static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003594 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003597static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003599 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003601 .write_begin = shmem_write_begin,
3602 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003604#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003605 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003606#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003607 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608};
3609
Helge Deller15ad7cd2006-12-06 20:40:36 -08003610static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003612 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003614 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003615 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003616 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003617 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07003618 .splice_read = shmem_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003619 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003620 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621#endif
3622};
3623
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003624static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003625 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003626 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003627#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003628 .setxattr = generic_setxattr,
3629 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003630 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003631 .removexattr = generic_removexattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003632 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003633#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634};
3635
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003636static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637#ifdef CONFIG_TMPFS
3638 .create = shmem_create,
3639 .lookup = simple_lookup,
3640 .link = shmem_link,
3641 .unlink = shmem_unlink,
3642 .symlink = shmem_symlink,
3643 .mkdir = shmem_mkdir,
3644 .rmdir = shmem_rmdir,
3645 .mknod = shmem_mknod,
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003646 .rename2 = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003647 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003649#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003650 .setxattr = generic_setxattr,
3651 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003652 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003653 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003654#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003655#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003656 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003657 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003658#endif
3659};
3660
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003661static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003662#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003663 .setxattr = generic_setxattr,
3664 .getxattr = generic_getxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003665 .listxattr = shmem_listxattr,
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003666 .removexattr = generic_removexattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003667#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003668#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003669 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003670 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672};
3673
Hugh Dickins759b9772007-03-05 00:30:28 -08003674static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 .alloc_inode = shmem_alloc_inode,
3676 .destroy_inode = shmem_destroy_inode,
3677#ifdef CONFIG_TMPFS
3678 .statfs = shmem_statfs,
3679 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003680 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681#endif
Al Viro1f895f72010-06-05 19:10:41 -04003682 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 .drop_inode = generic_delete_inode,
3684 .put_super = shmem_put_super,
3685};
3686
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003687static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003688 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003689 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690#ifdef CONFIG_NUMA
3691 .set_policy = shmem_set_policy,
3692 .get_policy = shmem_get_policy,
3693#endif
3694};
3695
Al Viro3c26ff62010-07-25 11:46:36 +04003696static struct dentry *shmem_mount(struct file_system_type *fs_type,
3697 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Al Viro3c26ff62010-07-25 11:46:36 +04003699 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003702static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 .owner = THIS_MODULE,
3704 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003705 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003707 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003710int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 int error;
3713
Rob Landley16203a72013-09-11 14:26:12 -07003714 /* If rootfs called this, don't re-init */
3715 if (shmem_inode_cachep)
3716 return 0;
3717
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003718 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (error)
3720 goto out3;
3721
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003722 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003724 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 goto out2;
3726 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003727
Al Viroca4e0512013-08-31 12:57:10 -04003728 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (IS_ERR(shm_mnt)) {
3730 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003731 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 goto out1;
3733 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003734
3735#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3736 if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY)
3737 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3738 else
3739 shmem_huge = 0; /* just in case it was patched */
3740#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return 0;
3742
3743out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003744 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003746 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747out3:
3748 shm_mnt = ERR_PTR(error);
3749 return error;
3750}
Matt Mackall853ac432009-01-06 14:40:20 -08003751
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003752#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
3753static ssize_t shmem_enabled_show(struct kobject *kobj,
3754 struct kobj_attribute *attr, char *buf)
3755{
3756 int values[] = {
3757 SHMEM_HUGE_ALWAYS,
3758 SHMEM_HUGE_WITHIN_SIZE,
3759 SHMEM_HUGE_ADVISE,
3760 SHMEM_HUGE_NEVER,
3761 SHMEM_HUGE_DENY,
3762 SHMEM_HUGE_FORCE,
3763 };
3764 int i, count;
3765
3766 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3767 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3768
3769 count += sprintf(buf + count, fmt,
3770 shmem_format_huge(values[i]));
3771 }
3772 buf[count - 1] = '\n';
3773 return count;
3774}
3775
3776static ssize_t shmem_enabled_store(struct kobject *kobj,
3777 struct kobj_attribute *attr, const char *buf, size_t count)
3778{
3779 char tmp[16];
3780 int huge;
3781
3782 if (count + 1 > sizeof(tmp))
3783 return -EINVAL;
3784 memcpy(tmp, buf, count);
3785 tmp[count] = '\0';
3786 if (count && tmp[count - 1] == '\n')
3787 tmp[count - 1] = '\0';
3788
3789 huge = shmem_parse_huge(tmp);
3790 if (huge == -EINVAL)
3791 return -EINVAL;
3792 if (!has_transparent_hugepage() &&
3793 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3794 return -EINVAL;
3795
3796 shmem_huge = huge;
3797 if (shmem_huge < SHMEM_HUGE_DENY)
3798 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3799 return count;
3800}
3801
3802struct kobj_attribute shmem_enabled_attr =
3803 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
3804#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
3805
Matt Mackall853ac432009-01-06 14:40:20 -08003806#else /* !CONFIG_SHMEM */
3807
3808/*
3809 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3810 *
3811 * This is intended for small system where the benefits of the full
3812 * shmem code (swap-backed and resource-limited) are outweighed by
3813 * their complexity. On systems without swap this code should be
3814 * effectively equivalent, but much lighter weight.
3815 */
3816
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003817static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003818 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003819 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003820 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003821 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003822};
3823
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003824int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003825{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003826 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003827
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003828 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003829 BUG_ON(IS_ERR(shm_mnt));
3830
3831 return 0;
3832}
3833
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003834int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003835{
3836 return 0;
3837}
3838
Hugh Dickins3f96b792009-09-21 17:03:37 -07003839int shmem_lock(struct file *file, int lock, struct user_struct *user)
3840{
3841 return 0;
3842}
3843
Hugh Dickins24513262012-01-20 14:34:21 -08003844void shmem_unlock_mapping(struct address_space *mapping)
3845{
3846}
3847
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003848#ifdef CONFIG_MMU
3849unsigned long shmem_get_unmapped_area(struct file *file,
3850 unsigned long addr, unsigned long len,
3851 unsigned long pgoff, unsigned long flags)
3852{
3853 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3854}
3855#endif
3856
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003857void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003858{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003859 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003860}
3861EXPORT_SYMBOL_GPL(shmem_truncate_range);
3862
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003863#define shmem_vm_ops generic_file_vm_ops
3864#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003865#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003866#define shmem_acct_size(flags, size) 0
3867#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003868
3869#endif /* CONFIG_SHMEM */
3870
3871/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Al Viro34515382013-02-14 22:38:02 -05003873static struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04003874 .d_dname = simple_dname
Al Viro34515382013-02-14 22:38:02 -05003875};
3876
Eric Parisc7277092013-12-02 11:24:19 +00003877static struct file *__shmem_file_setup(const char *name, loff_t size,
3878 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
Al Viro6b4d0b22013-02-14 21:37:26 -05003880 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04003882 struct path path;
Al Viro34515382013-02-14 22:38:02 -05003883 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 struct qstr this;
3885
3886 if (IS_ERR(shm_mnt))
Al Viro6b4d0b22013-02-14 21:37:26 -05003887 return ERR_CAST(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Hugh Dickins285b2c42011-08-03 16:21:20 -07003889 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return ERR_PTR(-EINVAL);
3891
3892 if (shmem_acct_size(flags, size))
3893 return ERR_PTR(-ENOMEM);
3894
Al Viro6b4d0b22013-02-14 21:37:26 -05003895 res = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 this.name = name;
3897 this.len = strlen(name);
3898 this.hash = 0; /* will go */
Al Viro34515382013-02-14 22:38:02 -05003899 sb = shm_mnt->mnt_sb;
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003900 path.mnt = mntget(shm_mnt);
Al Viro34515382013-02-14 22:38:02 -05003901 path.dentry = d_alloc_pseudo(sb, &this);
Al Viro2c48b9c2009-08-09 00:52:35 +04003902 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 goto put_memory;
Al Viro34515382013-02-14 22:38:02 -05003904 d_set_d_op(path.dentry, &anon_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Al Viro6b4d0b22013-02-14 21:37:26 -05003906 res = ERR_PTR(-ENOSPC);
Al Viro34515382013-02-14 22:38:02 -05003907 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if (!inode)
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003909 goto put_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Eric Parisc7277092013-12-02 11:24:19 +00003911 inode->i_flags |= i_flags;
Al Viro2c48b9c2009-08-09 00:52:35 +04003912 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003914 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003915 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
3916 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003917 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003918
Al Viro6b4d0b22013-02-14 21:37:26 -05003919 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04003920 &shmem_file_operations);
Al Viro6b4d0b22013-02-14 21:37:26 -05003921 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003922 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04003923
Al Viro6b4d0b22013-02-14 21:37:26 -05003924 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926put_memory:
3927 shmem_unacct_size(flags, size);
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07003928put_path:
3929 path_put(&path);
Al Viro6b4d0b22013-02-14 21:37:26 -05003930 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
Eric Parisc7277092013-12-02 11:24:19 +00003932
3933/**
3934 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3935 * kernel internal. There will be NO LSM permission checks against the
3936 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003937 * higher layer. The users are the big_key and shm implementations. LSM
3938 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003939 * @name: name for dentry (to be seen in /proc/<pid>/maps
3940 * @size: size to be set for the file
3941 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3942 */
3943struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3944{
3945 return __shmem_file_setup(name, size, flags, S_PRIVATE);
3946}
3947
3948/**
3949 * shmem_file_setup - get an unlinked file living in tmpfs
3950 * @name: name for dentry (to be seen in /proc/<pid>/maps
3951 * @size: size to be set for the file
3952 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3953 */
3954struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3955{
3956 return __shmem_file_setup(name, size, flags, 0);
3957}
Keith Packard395e0dd2008-06-20 00:08:06 -07003958EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Randy Dunlap46711812008-03-19 17:00:41 -07003960/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3963 */
3964int shmem_zero_setup(struct vm_area_struct *vma)
3965{
3966 struct file *file;
3967 loff_t size = vma->vm_end - vma->vm_start;
3968
Hugh Dickins66fc1302015-06-14 09:48:09 -07003969 /*
3970 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3971 * between XFS directory reading and selinux: since this file is only
3972 * accessible to the user through its mapping, use S_PRIVATE flag to
3973 * bypass file security, in the same way as shmem_kernel_file_setup().
3974 */
3975 file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 if (IS_ERR(file))
3977 return PTR_ERR(file);
3978
3979 if (vma->vm_file)
3980 fput(vma->vm_file);
3981 vma->vm_file = file;
3982 vma->vm_ops = &shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return 0;
3984}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003985
3986/**
3987 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3988 * @mapping: the page's address_space
3989 * @index: the page index
3990 * @gfp: the page allocator flags to use if allocating
3991 *
3992 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3993 * with any new page allocations done using the specified allocation flags.
3994 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3995 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3996 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3997 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07003998 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3999 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004000 */
4001struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4002 pgoff_t index, gfp_t gfp)
4003{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004004#ifdef CONFIG_SHMEM
4005 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004006 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004007 int error;
4008
4009 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004010 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
4011 gfp, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004012 if (error)
4013 page = ERR_PTR(error);
4014 else
4015 unlock_page(page);
4016 return page;
4017#else
4018 /*
4019 * The tiny !SHMEM case uses ramfs without swap
4020 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004021 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004022#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004023}
4024EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);