blob: a409bd817819d4e984cc62ec7782f417df3e48f3 [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>
Hugh Dickinscaefba12009-04-13 14:40:12 -070028#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080029#include <linux/file.h>
30#include <linux/mm.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040031#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080032#include <linux/swap.h>
33
34static struct vfsmount *shm_mnt;
35
36#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * This virtual memory filesystem is heavily based on the ramfs. It
39 * extends ramfs by the ability to use swap and honor resource limits
40 * which makes it a completely usable filesystem.
41 */
42
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070043#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070044#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010045#include <linux/posix_acl.h>
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070046#include <linux/generic_acl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/backing-dev.h>
51#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070054#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070055#include <linux/percpu_counter.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070056#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/security.h>
58#include <linux/swapops.h>
59#include <linux/mempolicy.h>
60#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000061#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070062#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070063#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080064#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040065#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <asm/pgtable.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* Pretend that each entry is of this size in directory's i_size */
74#define BOGO_DIRENT_SIZE 20
75
Hugh Dickins69f07ec2011-08-03 16:21:26 -070076/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
77#define SHORT_SYMLINK_LEN 128
78
Eric Parisb09e0fa2011-05-24 17:12:39 -070079struct shmem_xattr {
80 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
81 char *name; /* xattr name */
82 size_t size;
83 char value[0];
84};
85
Hugh Dickins285b2c42011-08-03 16:21:20 -070086/* Flag allocation requirements to shmem_getpage */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 SGP_READ, /* don't exceed i_size, don't allocate page */
89 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -080090 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 SGP_WRITE, /* may exceed i_size, may allocate page */
92};
93
Andrew Mortonb76db732008-02-08 04:21:49 -080094#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080095static unsigned long shmem_default_max_blocks(void)
96{
97 return totalram_pages / 2;
98}
99
100static unsigned long shmem_default_max_inodes(void)
101{
102 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
103}
Andrew Mortonb76db732008-02-08 04:21:49 -0800104#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800105
Hugh Dickins68da9f02011-07-25 17:12:34 -0700106static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
107 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
108
109static inline int shmem_getpage(struct inode *inode, pgoff_t index,
110 struct page **pagep, enum sgp_type sgp, int *fault_type)
111{
112 return shmem_getpage_gfp(inode, index, pagep, sgp,
113 mapping_gfp_mask(inode->i_mapping), fault_type);
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
117{
118 return sb->s_fs_info;
119}
120
121/*
122 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
123 * for shared memory and for shared anonymous (/dev/zero) mappings
124 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
125 * consistent with the pre-accounting of private mappings ...
126 */
127static inline int shmem_acct_size(unsigned long flags, loff_t size)
128{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000129 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000130 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static inline void shmem_unacct_size(unsigned long flags, loff_t size)
134{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000135 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vm_unacct_memory(VM_ACCT(size));
137}
138
139/*
140 * ... whereas tmpfs objects are accounted incrementally as
141 * pages are allocated, in order to allow huge sparse files.
142 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
143 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
144 */
145static inline int shmem_acct_block(unsigned long flags)
146{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000147 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000148 security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static inline void shmem_unacct_blocks(unsigned long flags, long pages)
152{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000153 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
155}
156
Hugh Dickins759b9772007-03-05 00:30:28 -0800157static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700158static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800159static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800160static const struct inode_operations shmem_inode_operations;
161static const struct inode_operations shmem_dir_inode_operations;
162static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400163static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700165static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700167 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
170static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800171static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800173static int shmem_reserve_inode(struct super_block *sb)
174{
175 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
176 if (sbinfo->max_inodes) {
177 spin_lock(&sbinfo->stat_lock);
178 if (!sbinfo->free_inodes) {
179 spin_unlock(&sbinfo->stat_lock);
180 return -ENOSPC;
181 }
182 sbinfo->free_inodes--;
183 spin_unlock(&sbinfo->stat_lock);
184 }
185 return 0;
186}
187
188static void shmem_free_inode(struct super_block *sb)
189{
190 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
191 if (sbinfo->max_inodes) {
192 spin_lock(&sbinfo->stat_lock);
193 sbinfo->free_inodes++;
194 spin_unlock(&sbinfo->stat_lock);
195 }
196}
197
Randy Dunlap46711812008-03-19 17:00:41 -0700198/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700199 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @inode: inode to recalc
201 *
202 * We have to calculate the free blocks since the mm can drop
203 * undirtied hole pages behind our back.
204 *
205 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
206 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
207 *
208 * It has to be called with the spinlock held.
209 */
210static void shmem_recalc_inode(struct inode *inode)
211{
212 struct shmem_inode_info *info = SHMEM_I(inode);
213 long freed;
214
215 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
216 if (freed > 0) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218 if (sbinfo->max_blocks)
219 percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 info->alloced -= freed;
Hugh Dickins54af6042011-08-03 16:21:24 -0700221 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 shmem_unacct_blocks(info->flags, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224}
225
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700226/*
227 * Replace item expected in radix tree by a new item, while holding tree lock.
228 */
229static int shmem_radix_tree_replace(struct address_space *mapping,
230 pgoff_t index, void *expected, void *replacement)
231{
232 void **pslot;
233 void *item = NULL;
234
235 VM_BUG_ON(!expected);
236 pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
237 if (pslot)
238 item = radix_tree_deref_slot_protected(pslot,
239 &mapping->tree_lock);
240 if (item != expected)
241 return -ENOENT;
242 if (replacement)
243 radix_tree_replace_slot(pslot, replacement);
244 else
245 radix_tree_delete(&mapping->page_tree, index);
246 return 0;
247}
248
249/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700250 * Like add_to_page_cache_locked, but error if expected item has gone.
251 */
252static int shmem_add_to_page_cache(struct page *page,
253 struct address_space *mapping,
254 pgoff_t index, gfp_t gfp, void *expected)
255{
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700256 int error = 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700257
258 VM_BUG_ON(!PageLocked(page));
259 VM_BUG_ON(!PageSwapBacked(page));
260
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700261 if (!expected)
262 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
263 if (!error) {
264 page_cache_get(page);
265 page->mapping = mapping;
266 page->index = index;
267
268 spin_lock_irq(&mapping->tree_lock);
269 if (!expected)
270 error = radix_tree_insert(&mapping->page_tree,
271 index, page);
272 else
273 error = shmem_radix_tree_replace(mapping, index,
274 expected, page);
275 if (!error) {
276 mapping->nrpages++;
277 __inc_zone_page_state(page, NR_FILE_PAGES);
278 __inc_zone_page_state(page, NR_SHMEM);
279 spin_unlock_irq(&mapping->tree_lock);
280 } else {
281 page->mapping = NULL;
282 spin_unlock_irq(&mapping->tree_lock);
283 page_cache_release(page);
284 }
285 if (!expected)
286 radix_tree_preload_end();
287 }
288 if (error)
289 mem_cgroup_uncharge_cache_page(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700290 return error;
291}
292
293/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700294 * Like delete_from_page_cache, but substitutes swap for page.
295 */
296static void shmem_delete_from_page_cache(struct page *page, void *radswap)
297{
298 struct address_space *mapping = page->mapping;
299 int error;
300
301 spin_lock_irq(&mapping->tree_lock);
302 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
303 page->mapping = NULL;
304 mapping->nrpages--;
305 __dec_zone_page_state(page, NR_FILE_PAGES);
306 __dec_zone_page_state(page, NR_SHMEM);
307 spin_unlock_irq(&mapping->tree_lock);
308 page_cache_release(page);
309 BUG_ON(error);
310}
311
312/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700313 * Like find_get_pages, but collecting swap entries as well as pages.
314 */
315static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
316 pgoff_t start, unsigned int nr_pages,
317 struct page **pages, pgoff_t *indices)
318{
319 unsigned int i;
320 unsigned int ret;
321 unsigned int nr_found;
322
323 rcu_read_lock();
324restart:
325 nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
326 (void ***)pages, indices, start, nr_pages);
327 ret = 0;
328 for (i = 0; i < nr_found; i++) {
329 struct page *page;
330repeat:
331 page = radix_tree_deref_slot((void **)pages[i]);
332 if (unlikely(!page))
333 continue;
334 if (radix_tree_exception(page)) {
Hugh Dickins8079b1c2011-08-03 16:21:28 -0700335 if (radix_tree_deref_retry(page))
336 goto restart;
337 /*
338 * Otherwise, we must be storing a swap entry
339 * here as an exceptional entry: so return it
340 * without attempting to raise page count.
341 */
342 goto export;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700343 }
344 if (!page_cache_get_speculative(page))
345 goto repeat;
346
347 /* Has the page moved? */
348 if (unlikely(page != *((void **)pages[i]))) {
349 page_cache_release(page);
350 goto repeat;
351 }
352export:
353 indices[ret] = indices[i];
354 pages[ret] = page;
355 ret++;
356 }
357 if (unlikely(!ret && nr_found))
358 goto restart;
359 rcu_read_unlock();
360 return ret;
361}
362
363/*
364 * Remove swap entry from radix tree, free the swap and its page cache.
365 */
366static int shmem_free_swap(struct address_space *mapping,
367 pgoff_t index, void *radswap)
368{
369 int error;
370
371 spin_lock_irq(&mapping->tree_lock);
372 error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
373 spin_unlock_irq(&mapping->tree_lock);
374 if (!error)
375 free_swap_and_cache(radix_to_swp_entry(radswap));
376 return error;
377}
378
379/*
380 * Pagevec may contain swap entries, so shuffle up pages before releasing.
381 */
Hugh Dickins24513262012-01-20 14:34:21 -0800382static void shmem_deswap_pagevec(struct pagevec *pvec)
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700383{
384 int i, j;
385
386 for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
387 struct page *page = pvec->pages[i];
388 if (!radix_tree_exceptional_entry(page))
389 pvec->pages[j++] = page;
390 }
391 pvec->nr = j;
Hugh Dickins24513262012-01-20 14:34:21 -0800392}
393
394/*
395 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
396 */
397void shmem_unlock_mapping(struct address_space *mapping)
398{
399 struct pagevec pvec;
400 pgoff_t indices[PAGEVEC_SIZE];
401 pgoff_t index = 0;
402
403 pagevec_init(&pvec, 0);
404 /*
405 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
406 */
407 while (!mapping_unevictable(mapping)) {
408 /*
409 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
410 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
411 */
412 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
413 PAGEVEC_SIZE, pvec.pages, indices);
414 if (!pvec.nr)
415 break;
416 index = indices[pvec.nr - 1] + 1;
417 shmem_deswap_pagevec(&pvec);
418 check_move_unevictable_pages(pvec.pages, pvec.nr);
419 pagevec_release(&pvec);
420 cond_resched();
421 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700422}
423
424/*
425 * Remove range of pages and swap entries from radix tree, and free them.
426 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700427void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700429 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700431 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700432 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700433 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700434 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700435 pgoff_t indices[PAGEVEC_SIZE];
436 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700437 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700438 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700440 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
441
442 pagevec_init(&pvec, 0);
443 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700444 while (index <= end) {
445 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
446 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
447 pvec.pages, indices);
448 if (!pvec.nr)
449 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700450 mem_cgroup_uncharge_start();
451 for (i = 0; i < pagevec_count(&pvec); i++) {
452 struct page *page = pvec.pages[i];
453
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700454 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700455 if (index > end)
456 break;
457
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700458 if (radix_tree_exceptional_entry(page)) {
459 nr_swaps_freed += !shmem_free_swap(mapping,
460 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700461 continue;
462 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700463
464 if (!trylock_page(page))
465 continue;
466 if (page->mapping == mapping) {
467 VM_BUG_ON(PageWriteback(page));
468 truncate_inode_page(mapping, page);
469 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700470 unlock_page(page);
471 }
Hugh Dickins24513262012-01-20 14:34:21 -0800472 shmem_deswap_pagevec(&pvec);
473 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700474 mem_cgroup_uncharge_end();
475 cond_resched();
476 index++;
477 }
478
479 if (partial) {
480 struct page *page = NULL;
481 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
482 if (page) {
483 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
484 set_page_dirty(page);
485 unlock_page(page);
486 page_cache_release(page);
487 }
488 }
489
490 index = start;
491 for ( ; ; ) {
492 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700493 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
494 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
495 pvec.pages, indices);
496 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700497 if (index == start)
498 break;
499 index = start;
500 continue;
501 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700502 if (index == start && indices[0] > end) {
Hugh Dickins24513262012-01-20 14:34:21 -0800503 shmem_deswap_pagevec(&pvec);
504 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700505 break;
506 }
507 mem_cgroup_uncharge_start();
508 for (i = 0; i < pagevec_count(&pvec); i++) {
509 struct page *page = pvec.pages[i];
510
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700511 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700512 if (index > end)
513 break;
514
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700515 if (radix_tree_exceptional_entry(page)) {
516 nr_swaps_freed += !shmem_free_swap(mapping,
517 index, page);
518 continue;
519 }
520
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700521 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700522 if (page->mapping == mapping) {
523 VM_BUG_ON(PageWriteback(page));
524 truncate_inode_page(mapping, page);
525 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700526 unlock_page(page);
527 }
Hugh Dickins24513262012-01-20 14:34:21 -0800528 shmem_deswap_pagevec(&pvec);
529 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700530 mem_cgroup_uncharge_end();
531 index++;
532 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700535 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 shmem_recalc_inode(inode);
537 spin_unlock(&info->lock);
538
Hugh Dickins285b2c42011-08-03 16:21:20 -0700539 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700541EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Hugh Dickins94c1e622011-06-27 16:18:03 -0700543static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int error;
547
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200548 error = inode_change_ok(inode, attr);
549 if (error)
550 return error;
551
Hugh Dickins94c1e622011-06-27 16:18:03 -0700552 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
553 loff_t oldsize = inode->i_size;
554 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000555
Hugh Dickins94c1e622011-06-27 16:18:03 -0700556 if (newsize != oldsize) {
557 i_size_write(inode, newsize);
558 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
559 }
560 if (newsize < oldsize) {
561 loff_t holebegin = round_up(newsize, PAGE_SIZE);
562 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
563 shmem_truncate_range(inode, newsize, (loff_t)-1);
564 /* unmap again to remove racily COWed private pages */
565 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200569 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700570#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200571 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100572 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return error;
575}
576
Al Viro1f895f72010-06-05 19:10:41 -0400577static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700580 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000582 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 shmem_unacct_size(info->flags, inode->i_size);
584 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000585 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800587 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800589 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700591 } else
592 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700593
594 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
595 kfree(xattr->name);
596 kfree(xattr);
597 }
Hugh Dickinsec592422012-11-16 14:15:04 -0800598 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800599 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400600 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700603/*
604 * If swap found in inode, free it and move page from swapcache to filecache.
605 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700606static int shmem_unuse_inode(struct shmem_inode_info *info,
607 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700609 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700610 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700611 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800612 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700614 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700615 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700616 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700617 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800618
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800619 /*
620 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400621 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800622 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700623 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800624 */
625 if (shmem_swaplist.next != &info->swaplist)
626 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800627
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800628 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700629 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
630 * but also to hold up shmem_evict_inode(): so inode cannot be freed
631 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800632 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700633 error = shmem_add_to_page_cache(page, mapping, index,
634 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700635 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700636
Hugh Dickins48f170f2011-07-25 17:12:37 -0700637 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700638 /*
639 * Truncation and eviction use free_swap_and_cache(), which
640 * only does trylock page: if we raced, best clean up here.
641 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800642 delete_from_swap_cache(page);
643 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700644 if (!error) {
645 spin_lock(&info->lock);
646 info->swapped--;
647 spin_unlock(&info->lock);
648 swap_free(swap);
649 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800650 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800652 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700656 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700658int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700660 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct shmem_inode_info *info;
662 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700663 int error;
664
665 /*
666 * Charge page using GFP_KERNEL while we can wait, before taking
667 * the shmem_swaplist_mutex which might hold up shmem_writepage().
668 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700669 */
670 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
671 if (error)
672 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700673 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800675 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700676 list_for_each_safe(this, next, &shmem_swaplist) {
677 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700678 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700679 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700680 else
681 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800682 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800683 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800686 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700687
Hugh Dickins778dd892011-05-11 15:13:37 -0700688 if (!found)
689 mem_cgroup_uncharge_cache_page(page);
690 if (found < 0)
691 error = found;
692out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800693 unlock_page(page);
694 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698/*
699 * Move the page from the page cache to the swap cache.
700 */
701static int shmem_writepage(struct page *page, struct writeback_control *wbc)
702{
703 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700706 swp_entry_t swap;
707 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 mapping = page->mapping;
711 index = page->index;
712 inode = mapping->host;
713 info = SHMEM_I(inode);
714 if (info->flags & VM_LOCKED)
715 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800716 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto redirty;
718
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800719 /*
720 * shmem_backing_dev_info's capabilities prevent regular writeback or
721 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700722 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800723 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700724 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800725 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700726 if (!wbc->for_reclaim) {
727 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
728 goto redirty;
729 }
730 swap = get_swap_page();
731 if (!swap.val)
732 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800733
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700734 /*
735 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700736 * if it's not already there. Do it now before the page is
737 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700738 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700739 * we've incremented swapped, because shmem_unuse_inode() will
740 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700741 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700742 mutex_lock(&shmem_swaplist_mutex);
743 if (list_empty(&info->swaplist))
744 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700745
Hugh Dickins48f170f2011-07-25 17:12:37 -0700746 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800747 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700748 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
749
750 spin_lock(&info->lock);
751 info->swapped++;
752 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700753 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700754
755 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800756 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700757 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759 }
760
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700761 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700762 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763redirty:
764 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800765 if (wbc->for_reclaim)
766 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
767 unlock_page(page);
768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800772#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700773static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800774{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700775 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800776
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700777 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700778 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800779
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700780 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800781
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700782 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800783}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700784
785static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
786{
787 struct mempolicy *mpol = NULL;
788 if (sbinfo->mpol) {
789 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
790 mpol = sbinfo->mpol;
791 mpol_get(mpol);
792 spin_unlock(&sbinfo->stat_lock);
793 }
794 return mpol;
795}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800796#endif /* CONFIG_TMPFS */
797
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700798static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
799 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct vm_area_struct pvma;
Mel Gorman78a685b2012-12-05 14:01:41 -0800802 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800804 /* Create a pseudo vma that just contains the policy */
805 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700806 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800807 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700808 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700809
Mel Gorman78a685b2012-12-05 14:01:41 -0800810 page = swapin_readahead(swap, gfp, &pvma, 0);
811
812 /* Drop reference taken by mpol_shared_policy_lookup() */
813 mpol_cond_put(pvma.vm_policy);
814
815 return page;
816}
817
818static struct page *shmem_alloc_page(gfp_t gfp,
819 struct shmem_inode_info *info, pgoff_t index)
820{
821 struct vm_area_struct pvma;
822 struct page *page;
823
824 /* Create a pseudo vma that just contains the policy */
825 pvma.vm_start = 0;
826 pvma.vm_pgoff = index;
827 pvma.vm_ops = NULL;
828 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
829
830 page = alloc_page_vma(gfp, &pvma, 0);
831
832 /* Drop reference taken by mpol_shared_policy_lookup() */
833 mpol_cond_put(pvma.vm_policy);
834
835 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800837#else /* !CONFIG_NUMA */
838#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700839static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800840{
841}
842#endif /* CONFIG_TMPFS */
843
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700844static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
845 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700847 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Hugh Dickins02098fe2008-02-04 22:28:42 -0800850static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700851 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000853 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800855#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700857#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
858static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
859{
860 return NULL;
861}
862#endif
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700865 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *
867 * If we allocate a new one we do not mark it dirty. That's up to the
868 * vm. If we swap it in we mark it dirty since we also free the swap
869 * entry since a page cannot live in both the swap and page cache
870 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700871static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700872 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af6042011-08-03 16:21:24 -0700875 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700877 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 swp_entry_t swap;
879 int error;
Hugh Dickins54af6042011-08-03 16:21:24 -0700880 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700882 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884repeat:
Hugh Dickins54af6042011-08-03 16:21:24 -0700885 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700886 page = find_lock_page(mapping, index);
Hugh Dickins54af6042011-08-03 16:21:24 -0700887 if (radix_tree_exceptional_entry(page)) {
888 swap = radix_to_swp_entry(page);
889 page = NULL;
890 }
891
892 if (sgp != SGP_WRITE &&
893 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
894 error = -EINVAL;
895 goto failed;
896 }
897
898 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800899 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700900 * Once we can get the page lock, it must be uptodate:
901 * if there were an error in reading back from swap,
902 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800903 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700904 BUG_ON(page && !PageUptodate(page));
905 *pagep = page;
906 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /*
Hugh Dickins54af6042011-08-03 16:21:24 -0700910 * Fast cache lookup did not find it:
911 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
Hugh Dickins54af6042011-08-03 16:21:24 -0700913 info = SHMEM_I(inode);
914 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (swap.val) {
917 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700918 page = lookup_swap_cache(swap);
919 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700920 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700921 if (fault_type)
922 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700923 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700924 if (!page) {
Hugh Dickins54af6042011-08-03 16:21:24 -0700925 error = -ENOMEM;
926 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
930 /* We have to do this with page locked to prevent races */
Hugh Dickins54af6042011-08-03 16:21:24 -0700931 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700932 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 error = -EIO;
Hugh Dickins54af6042011-08-03 16:21:24 -0700934 goto failed;
935 }
936 wait_on_page_writeback(page);
937
938 /* Someone may have already done it for us */
939 if (page->mapping) {
940 if (page->mapping == mapping &&
941 page->index == index)
942 goto done;
943 error = -EEXIST;
944 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700947 error = mem_cgroup_cache_charge(page, current->mm,
948 gfp & GFP_RECLAIM_MASK);
949 if (!error)
950 error = shmem_add_to_page_cache(page, mapping, index,
951 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af6042011-08-03 16:21:24 -0700952 if (error)
953 goto failed;
954
955 spin_lock(&info->lock);
956 info->swapped--;
957 shmem_recalc_inode(inode);
958 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700959
Hugh Dickins27ab7002011-07-25 17:12:36 -0700960 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700961 set_page_dirty(page);
962 swap_free(swap);
963
Hugh Dickins54af6042011-08-03 16:21:24 -0700964 } else {
965 if (shmem_acct_block(info->flags)) {
966 error = -ENOSPC;
967 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700969 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700970 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af6042011-08-03 16:21:24 -0700971 sbinfo->max_blocks) >= 0) {
972 error = -ENOSPC;
973 goto unacct;
974 }
Tim Chen7e496292010-08-09 17:19:05 -0700975 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Hugh Dickins54af6042011-08-03 16:21:24 -0700978 page = shmem_alloc_page(gfp, info, index);
979 if (!page) {
980 error = -ENOMEM;
981 goto decused;
982 }
983
984 SetPageSwapBacked(page);
985 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700986 error = mem_cgroup_cache_charge(page, current->mm,
987 gfp & GFP_RECLAIM_MASK);
988 if (!error)
989 error = shmem_add_to_page_cache(page, mapping, index,
990 gfp, NULL);
Hugh Dickins54af6042011-08-03 16:21:24 -0700991 if (error)
992 goto decused;
993 lru_cache_add_anon(page);
994
995 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 info->alloced++;
Hugh Dickins54af6042011-08-03 16:21:24 -0700997 inode->i_blocks += BLOCKS_PER_PAGE;
998 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001000
Hugh Dickins27ab7002011-07-25 17:12:36 -07001001 clear_highpage(page);
1002 flush_dcache_page(page);
1003 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001004 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -07001005 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007done:
Hugh Dickins54af6042011-08-03 16:21:24 -07001008 /* Perhaps the file has been truncated since we checked */
1009 if (sgp != SGP_WRITE &&
1010 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
1011 error = -EINVAL;
1012 goto trunc;
Shaohua Liff36b802010-08-09 17:19:06 -07001013 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001014 *pagep = page;
1015 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001016
Nick Piggind0217ac2007-07-19 01:47:03 -07001017 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07001018 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Hugh Dickins54af6042011-08-03 16:21:24 -07001020trunc:
1021 ClearPageDirty(page);
1022 delete_from_page_cache(page);
1023 spin_lock(&info->lock);
1024 info->alloced--;
1025 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_unlock(&info->lock);
Hugh Dickins54af6042011-08-03 16:21:24 -07001027decused:
1028 if (sbinfo->max_blocks)
1029 percpu_counter_add(&sbinfo->used_blocks, -1);
1030unacct:
1031 shmem_unacct_blocks(info->flags, 1);
1032failed:
1033 if (swap.val && error != -EINVAL) {
1034 struct page *test = find_get_page(mapping, index);
1035 if (test && !radix_tree_exceptional_entry(test))
1036 page_cache_release(test);
1037 /* Have another try if the entry has changed */
1038 if (test != swp_to_radix_entry(swap))
1039 error = -EEXIST;
1040 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001041 if (page) {
Hugh Dickins54af6042011-08-03 16:21:24 -07001042 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001043 page_cache_release(page);
Hugh Dickins54af6042011-08-03 16:21:24 -07001044 }
1045 if (error == -ENOSPC && !once++) {
1046 info = SHMEM_I(inode);
1047 spin_lock(&info->lock);
1048 shmem_recalc_inode(inode);
1049 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001050 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001051 }
Hugh Dickins54af6042011-08-03 16:21:24 -07001052 if (error == -EEXIST)
1053 goto repeat;
1054 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1058{
1059 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1060 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001061 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1064 if (error)
1065 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001066
Ying Han456f9982011-05-26 16:25:38 -07001067 if (ret & VM_FAULT_MAJOR) {
1068 count_vm_event(PGMAJFAULT);
1069 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1070 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001071 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001075static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001077 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1078 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001081static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1082 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001084 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1085 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001087 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1088 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090#endif
1091
1092int shmem_lock(struct file *file, int lock, struct user_struct *user)
1093{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001094 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 struct shmem_inode_info *info = SHMEM_I(inode);
1096 int retval = -ENOMEM;
1097
1098 spin_lock(&info->lock);
1099 if (lock && !(info->flags & VM_LOCKED)) {
1100 if (!user_shm_lock(inode->i_size, user))
1101 goto out_nomem;
1102 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001103 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 if (!lock && (info->flags & VM_LOCKED) && user) {
1106 user_shm_unlock(inode->i_size, user);
1107 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001108 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112out_nomem:
1113 spin_unlock(&info->lock);
1114 return retval;
1115}
1116
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001117static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 file_accessed(file);
1120 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001121 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
1123}
1124
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001125static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001126 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct inode *inode;
1129 struct shmem_inode_info *info;
1130 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1131
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001132 if (shmem_reserve_inode(sb))
1133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 inode = new_inode(sb);
1136 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001137 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001138 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1141 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001142 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 info = SHMEM_I(inode);
1144 memset(info, 0, (char *)inode - (char *)info);
1145 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001146 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001148 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001149 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 switch (mode & S_IFMT) {
1152 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001153 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 init_special_inode(inode, mode, dev);
1155 break;
1156 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001157 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 inode->i_op = &shmem_inode_operations;
1159 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001160 mpol_shared_policy_init(&info->policy,
1161 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 break;
1163 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001164 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* Some things misbehave if size == 0 on a directory */
1166 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1167 inode->i_op = &shmem_dir_inode_operations;
1168 inode->i_fop = &simple_dir_operations;
1169 break;
1170 case S_IFLNK:
1171 /*
1172 * Must not load anything in the rbtree,
1173 * mpol_free_shared_policy will not be called.
1174 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001175 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001178 } else
1179 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return inode;
1181}
1182
1183#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001184static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001185static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001187#ifdef CONFIG_TMPFS_XATTR
1188static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
1189#else
1190#define shmem_initxattrs NULL
1191#endif
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001194shmem_write_begin(struct file *file, struct address_space *mapping,
1195 loff_t pos, unsigned len, unsigned flags,
1196 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Nick Piggin800d15a2007-10-16 01:25:03 -07001198 struct inode *inode = mapping->host;
1199 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001200 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1201}
1202
1203static int
1204shmem_write_end(struct file *file, struct address_space *mapping,
1205 loff_t pos, unsigned len, unsigned copied,
1206 struct page *page, void *fsdata)
1207{
1208 struct inode *inode = mapping->host;
1209
Hugh Dickinsd3602442008-02-04 22:28:44 -08001210 if (pos + copied > inode->i_size)
1211 i_size_write(inode, pos + copied);
1212
Nick Piggin800d15a2007-10-16 01:25:03 -07001213 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001214 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001215 page_cache_release(page);
1216
Nick Piggin800d15a2007-10-16 01:25:03 -07001217 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1221{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001222 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001224 pgoff_t index;
1225 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001226 enum sgp_type sgp = SGP_READ;
1227
1228 /*
1229 * Might this read be for a stacking filesystem? Then when reading
1230 * holes of a sparse file, we actually need to allocate those pages,
1231 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1232 */
1233 if (segment_eq(get_fs(), KERNEL_DS))
1234 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 index = *ppos >> PAGE_CACHE_SHIFT;
1237 offset = *ppos & ~PAGE_CACHE_MASK;
1238
1239 for (;;) {
1240 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001241 pgoff_t end_index;
1242 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 loff_t i_size = i_size_read(inode);
1244
1245 end_index = i_size >> PAGE_CACHE_SHIFT;
1246 if (index > end_index)
1247 break;
1248 if (index == end_index) {
1249 nr = i_size & ~PAGE_CACHE_MASK;
1250 if (nr <= offset)
1251 break;
1252 }
1253
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001254 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (desc->error) {
1256 if (desc->error == -EINVAL)
1257 desc->error = 0;
1258 break;
1259 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001260 if (page)
1261 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /*
1264 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001265 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
1267 nr = PAGE_CACHE_SIZE;
1268 i_size = i_size_read(inode);
1269 end_index = i_size >> PAGE_CACHE_SHIFT;
1270 if (index == end_index) {
1271 nr = i_size & ~PAGE_CACHE_MASK;
1272 if (nr <= offset) {
1273 if (page)
1274 page_cache_release(page);
1275 break;
1276 }
1277 }
1278 nr -= offset;
1279
1280 if (page) {
1281 /*
1282 * If users can be writing to this page using arbitrary
1283 * virtual addresses, take care about potential aliasing
1284 * before reading the page on the kernel side.
1285 */
1286 if (mapping_writably_mapped(mapping))
1287 flush_dcache_page(page);
1288 /*
1289 * Mark the page accessed if we read the beginning.
1290 */
1291 if (!offset)
1292 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001293 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001295 page_cache_get(page);
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /*
1299 * Ok, we have the page, and it's up-to-date, so
1300 * now we can copy it to user space...
1301 *
1302 * The actor routine returns how many bytes were actually used..
1303 * NOTE! This may not be the same as how much of a user buffer
1304 * we filled up (we may be padding etc), so we can only update
1305 * "pos" here (the actor routine has to update the user buffer
1306 * pointers and the remaining count).
1307 */
1308 ret = actor(desc, page, offset, nr);
1309 offset += ret;
1310 index += offset >> PAGE_CACHE_SHIFT;
1311 offset &= ~PAGE_CACHE_MASK;
1312
1313 page_cache_release(page);
1314 if (ret != nr || !desc->count)
1315 break;
1316
1317 cond_resched();
1318 }
1319
1320 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1321 file_accessed(filp);
1322}
1323
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001324static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1325 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001327 struct file *filp = iocb->ki_filp;
1328 ssize_t retval;
1329 unsigned long seg;
1330 size_t count;
1331 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001333 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1334 if (retval)
1335 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001337 for (seg = 0; seg < nr_segs; seg++) {
1338 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001340 desc.written = 0;
1341 desc.arg.buf = iov[seg].iov_base;
1342 desc.count = iov[seg].iov_len;
1343 if (desc.count == 0)
1344 continue;
1345 desc.error = 0;
1346 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1347 retval += desc.written;
1348 if (desc.error) {
1349 retval = retval ?: desc.error;
1350 break;
1351 }
1352 if (desc.count > 0)
1353 break;
1354 }
1355 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Hugh Dickins708e3502011-07-25 17:12:32 -07001358static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1359 struct pipe_inode_info *pipe, size_t len,
1360 unsigned int flags)
1361{
1362 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001363 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001364 unsigned int loff, nr_pages, req_pages;
1365 struct page *pages[PIPE_DEF_BUFFERS];
1366 struct partial_page partial[PIPE_DEF_BUFFERS];
1367 struct page *page;
1368 pgoff_t index, end_index;
1369 loff_t isize, left;
1370 int error, page_nr;
1371 struct splice_pipe_desc spd = {
1372 .pages = pages,
1373 .partial = partial,
Eric Dumazet2c07f252012-06-12 15:24:40 +02001374 .nr_pages_max = PIPE_DEF_BUFFERS,
Hugh Dickins708e3502011-07-25 17:12:32 -07001375 .flags = flags,
1376 .ops = &page_cache_pipe_buf_ops,
1377 .spd_release = spd_release_page,
1378 };
1379
Hugh Dickins71f0e072011-07-25 17:12:33 -07001380 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001381 if (unlikely(*ppos >= isize))
1382 return 0;
1383
1384 left = isize - *ppos;
1385 if (unlikely(left < len))
1386 len = left;
1387
1388 if (splice_grow_spd(pipe, &spd))
1389 return -ENOMEM;
1390
1391 index = *ppos >> PAGE_CACHE_SHIFT;
1392 loff = *ppos & ~PAGE_CACHE_MASK;
1393 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1394 nr_pages = min(req_pages, pipe->buffers);
1395
Hugh Dickins708e3502011-07-25 17:12:32 -07001396 spd.nr_pages = find_get_pages_contig(mapping, index,
1397 nr_pages, spd.pages);
1398 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001399 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001400
Hugh Dickins708e3502011-07-25 17:12:32 -07001401 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001402 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1403 if (error)
1404 break;
1405 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001406 spd.pages[spd.nr_pages++] = page;
1407 index++;
1408 }
1409
Hugh Dickins708e3502011-07-25 17:12:32 -07001410 index = *ppos >> PAGE_CACHE_SHIFT;
1411 nr_pages = spd.nr_pages;
1412 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001413
Hugh Dickins708e3502011-07-25 17:12:32 -07001414 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1415 unsigned int this_len;
1416
1417 if (!len)
1418 break;
1419
Hugh Dickins708e3502011-07-25 17:12:32 -07001420 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1421 page = spd.pages[page_nr];
1422
Hugh Dickins71f0e072011-07-25 17:12:33 -07001423 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001424 error = shmem_getpage(inode, index, &page,
1425 SGP_CACHE, NULL);
1426 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001427 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001428 unlock_page(page);
1429 page_cache_release(spd.pages[page_nr]);
1430 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001431 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001432
1433 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001434 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1435 if (unlikely(!isize || index > end_index))
1436 break;
1437
Hugh Dickins708e3502011-07-25 17:12:32 -07001438 if (end_index == index) {
1439 unsigned int plen;
1440
Hugh Dickins708e3502011-07-25 17:12:32 -07001441 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1442 if (plen <= loff)
1443 break;
1444
Hugh Dickins708e3502011-07-25 17:12:32 -07001445 this_len = min(this_len, plen - loff);
1446 len = this_len;
1447 }
1448
1449 spd.partial[page_nr].offset = loff;
1450 spd.partial[page_nr].len = this_len;
1451 len -= this_len;
1452 loff = 0;
1453 spd.nr_pages++;
1454 index++;
1455 }
1456
Hugh Dickins708e3502011-07-25 17:12:32 -07001457 while (page_nr < nr_pages)
1458 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001459
1460 if (spd.nr_pages)
1461 error = splice_to_pipe(pipe, &spd);
1462
Eric Dumazet2c07f252012-06-12 15:24:40 +02001463 splice_shrink_spd(&spd);
Hugh Dickins708e3502011-07-25 17:12:32 -07001464
1465 if (error > 0) {
1466 *ppos += error;
1467 file_accessed(in);
1468 }
1469 return error;
1470}
1471
David Howells726c3342006-06-23 02:02:58 -07001472static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
David Howells726c3342006-06-23 02:02:58 -07001474 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 buf->f_type = TMPFS_MAGIC;
1477 buf->f_bsize = PAGE_CACHE_SIZE;
1478 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001479 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001481 buf->f_bavail =
1482 buf->f_bfree = sbinfo->max_blocks -
1483 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001484 }
1485 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 buf->f_files = sbinfo->max_inodes;
1487 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489 /* else leave those fields 0 like simple_statfs */
1490 return 0;
1491}
1492
1493/*
1494 * File creation. Allocate an inode, and we're done..
1495 */
1496static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001497shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001499 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 int error = -ENOSPC;
1501
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001502 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001504 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001505 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001506 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001507 if (error) {
1508 if (error != -EOPNOTSUPP) {
1509 iput(inode);
1510 return error;
1511 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001512 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001513#ifdef CONFIG_TMPFS_POSIX_ACL
1514 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001515 if (error) {
1516 iput(inode);
1517 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001518 }
Al Viro718deb62009-12-16 19:35:36 -05001519#else
1520 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001521#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dir->i_size += BOGO_DIRENT_SIZE;
1523 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1524 d_instantiate(dentry, inode);
1525 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 return error;
1528}
1529
Al Viro18bb1db2011-07-26 01:41:39 -04001530static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 int error;
1533
1534 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1535 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001536 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538}
1539
Al Viro4acdaf22011-07-26 01:42:34 -04001540static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 struct nameidata *nd)
1542{
1543 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1544}
1545
1546/*
1547 * Link a file..
1548 */
1549static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1550{
1551 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001552 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /*
1555 * No ordinary (disk based) filesystem counts links as inodes;
1556 * but each new link needs a new dentry, pinning lowmem, and
1557 * tmpfs dentries cannot be pruned until they are unlinked.
1558 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001559 ret = shmem_reserve_inode(inode->i_sb);
1560 if (ret)
1561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 dir->i_size += BOGO_DIRENT_SIZE;
1564 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001565 inc_nlink(inode);
Al Viro7de9c6e2010-10-23 11:11:40 -04001566 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 dget(dentry); /* Extra pinning count for the created dentry */
1568 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001569out:
1570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1574{
1575 struct inode *inode = dentry->d_inode;
1576
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001577 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1578 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 dir->i_size -= BOGO_DIRENT_SIZE;
1581 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001582 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 dput(dentry); /* Undo the count from "create" - this does all the work */
1584 return 0;
1585}
1586
1587static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1588{
1589 if (!simple_empty(dentry))
1590 return -ENOTEMPTY;
1591
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001592 drop_nlink(dentry->d_inode);
1593 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return shmem_unlink(dir, dentry);
1595}
1596
1597/*
1598 * The VFS layer already does all the dentry stuff for rename,
1599 * we just have to decrement the usage count for the target if
1600 * it exists so that the VFS layer correctly free's it when it
1601 * gets overwritten.
1602 */
1603static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1604{
1605 struct inode *inode = old_dentry->d_inode;
1606 int they_are_dirs = S_ISDIR(inode->i_mode);
1607
1608 if (!simple_empty(new_dentry))
1609 return -ENOTEMPTY;
1610
1611 if (new_dentry->d_inode) {
1612 (void) shmem_unlink(new_dir, new_dentry);
1613 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001614 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001616 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001617 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
1619
1620 old_dir->i_size -= BOGO_DIRENT_SIZE;
1621 new_dir->i_size += BOGO_DIRENT_SIZE;
1622 old_dir->i_ctime = old_dir->i_mtime =
1623 new_dir->i_ctime = new_dir->i_mtime =
1624 inode->i_ctime = CURRENT_TIME;
1625 return 0;
1626}
1627
1628static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1629{
1630 int error;
1631 int len;
1632 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001633 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 char *kaddr;
1635 struct shmem_inode_info *info;
1636
1637 len = strlen(symname) + 1;
1638 if (len > PAGE_CACHE_SIZE)
1639 return -ENAMETOOLONG;
1640
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001641 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (!inode)
1643 return -ENOSPC;
1644
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001645 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001646 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001647 if (error) {
1648 if (error != -EOPNOTSUPP) {
1649 iput(inode);
1650 return error;
1651 }
1652 error = 0;
1653 }
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 info = SHMEM_I(inode);
1656 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001657 if (len <= SHORT_SYMLINK_LEN) {
1658 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1659 if (!info->symlink) {
1660 iput(inode);
1661 return -ENOMEM;
1662 }
1663 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 } else {
1665 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1666 if (error) {
1667 iput(inode);
1668 return error;
1669 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001670 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 inode->i_op = &shmem_symlink_inode_operations;
Cong Wang9b04c5f2011-11-25 23:14:39 +08001672 kaddr = kmap_atomic(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 memcpy(kaddr, symname, len);
Cong Wang9b04c5f2011-11-25 23:14:39 +08001674 kunmap_atomic(kaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001676 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 page_cache_release(page);
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 dir->i_size += BOGO_DIRENT_SIZE;
1680 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1681 d_instantiate(dentry, inode);
1682 dget(dentry);
1683 return 0;
1684}
1685
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001686static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001688 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001689 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001692static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001695 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1696 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001697 if (page)
1698 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001699 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001702static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001705 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 kunmap(page);
1707 mark_page_accessed(page);
1708 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710}
1711
Eric Parisb09e0fa2011-05-24 17:12:39 -07001712#ifdef CONFIG_TMPFS_XATTR
1713/*
1714 * Superblocks without xattr inode operations may get some security.* xattr
1715 * support from the LSM "for free". As soon as we have any other xattrs
1716 * like ACLs, we also need to implement the security.* handlers at
1717 * filesystem level, though.
1718 */
1719
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001720/*
1721 * Allocate new xattr and copy in the value; but leave the name to callers.
1722 */
1723static struct shmem_xattr *shmem_xattr_alloc(const void *value, size_t size)
1724{
1725 struct shmem_xattr *new_xattr;
1726 size_t len;
1727
1728 /* wrap around? */
1729 len = sizeof(*new_xattr) + size;
1730 if (len <= sizeof(*new_xattr))
1731 return NULL;
1732
1733 new_xattr = kmalloc(len, GFP_KERNEL);
1734 if (!new_xattr)
1735 return NULL;
1736
1737 new_xattr->size = size;
1738 memcpy(new_xattr->value, value, size);
1739 return new_xattr;
1740}
1741
1742/*
1743 * Callback for security_inode_init_security() for acquiring xattrs.
1744 */
1745static int shmem_initxattrs(struct inode *inode,
1746 const struct xattr *xattr_array,
1747 void *fs_info)
1748{
1749 struct shmem_inode_info *info = SHMEM_I(inode);
1750 const struct xattr *xattr;
1751 struct shmem_xattr *new_xattr;
1752 size_t len;
1753
1754 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
1755 new_xattr = shmem_xattr_alloc(xattr->value, xattr->value_len);
1756 if (!new_xattr)
1757 return -ENOMEM;
1758
1759 len = strlen(xattr->name) + 1;
1760 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
1761 GFP_KERNEL);
1762 if (!new_xattr->name) {
1763 kfree(new_xattr);
1764 return -ENOMEM;
1765 }
1766
1767 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
1768 XATTR_SECURITY_PREFIX_LEN);
1769 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
1770 xattr->name, len);
1771
1772 spin_lock(&info->lock);
1773 list_add(&new_xattr->list, &info->xattr_list);
1774 spin_unlock(&info->lock);
1775 }
1776
1777 return 0;
1778}
1779
Eric Parisb09e0fa2011-05-24 17:12:39 -07001780static int shmem_xattr_get(struct dentry *dentry, const char *name,
1781 void *buffer, size_t size)
1782{
1783 struct shmem_inode_info *info;
1784 struct shmem_xattr *xattr;
1785 int ret = -ENODATA;
1786
1787 info = SHMEM_I(dentry->d_inode);
1788
1789 spin_lock(&info->lock);
1790 list_for_each_entry(xattr, &info->xattr_list, list) {
1791 if (strcmp(name, xattr->name))
1792 continue;
1793
1794 ret = xattr->size;
1795 if (buffer) {
1796 if (size < xattr->size)
1797 ret = -ERANGE;
1798 else
1799 memcpy(buffer, xattr->value, xattr->size);
1800 }
1801 break;
1802 }
1803 spin_unlock(&info->lock);
1804 return ret;
1805}
1806
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001807static int shmem_xattr_set(struct inode *inode, const char *name,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001808 const void *value, size_t size, int flags)
1809{
Eric Parisb09e0fa2011-05-24 17:12:39 -07001810 struct shmem_inode_info *info = SHMEM_I(inode);
1811 struct shmem_xattr *xattr;
1812 struct shmem_xattr *new_xattr = NULL;
Eric Parisb09e0fa2011-05-24 17:12:39 -07001813 int err = 0;
1814
1815 /* value == NULL means remove */
1816 if (value) {
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001817 new_xattr = shmem_xattr_alloc(value, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001818 if (!new_xattr)
1819 return -ENOMEM;
1820
1821 new_xattr->name = kstrdup(name, GFP_KERNEL);
1822 if (!new_xattr->name) {
1823 kfree(new_xattr);
1824 return -ENOMEM;
1825 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001826 }
1827
1828 spin_lock(&info->lock);
1829 list_for_each_entry(xattr, &info->xattr_list, list) {
1830 if (!strcmp(name, xattr->name)) {
1831 if (flags & XATTR_CREATE) {
1832 xattr = new_xattr;
1833 err = -EEXIST;
1834 } else if (new_xattr) {
1835 list_replace(&xattr->list, &new_xattr->list);
1836 } else {
1837 list_del(&xattr->list);
1838 }
1839 goto out;
1840 }
1841 }
1842 if (flags & XATTR_REPLACE) {
1843 xattr = new_xattr;
1844 err = -ENODATA;
1845 } else {
1846 list_add(&new_xattr->list, &info->xattr_list);
1847 xattr = NULL;
1848 }
1849out:
1850 spin_unlock(&info->lock);
1851 if (xattr)
1852 kfree(xattr->name);
1853 kfree(xattr);
1854 return err;
1855}
1856
Eric Parisb09e0fa2011-05-24 17:12:39 -07001857static const struct xattr_handler *shmem_xattr_handlers[] = {
1858#ifdef CONFIG_TMPFS_POSIX_ACL
1859 &generic_acl_access_handler,
1860 &generic_acl_default_handler,
1861#endif
1862 NULL
1863};
1864
1865static int shmem_xattr_validate(const char *name)
1866{
1867 struct { const char *prefix; size_t len; } arr[] = {
1868 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1869 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1870 };
1871 int i;
1872
1873 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1874 size_t preflen = arr[i].len;
1875 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1876 if (!name[preflen])
1877 return -EINVAL;
1878 return 0;
1879 }
1880 }
1881 return -EOPNOTSUPP;
1882}
1883
1884static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1885 void *buffer, size_t size)
1886{
1887 int err;
1888
1889 /*
1890 * If this is a request for a synthetic attribute in the system.*
1891 * namespace use the generic infrastructure to resolve a handler
1892 * for it via sb->s_xattr.
1893 */
1894 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1895 return generic_getxattr(dentry, name, buffer, size);
1896
1897 err = shmem_xattr_validate(name);
1898 if (err)
1899 return err;
1900
1901 return shmem_xattr_get(dentry, name, buffer, size);
1902}
1903
1904static int shmem_setxattr(struct dentry *dentry, const char *name,
1905 const void *value, size_t size, int flags)
1906{
1907 int err;
1908
1909 /*
1910 * If this is a request for a synthetic attribute in the system.*
1911 * namespace use the generic infrastructure to resolve a handler
1912 * for it via sb->s_xattr.
1913 */
1914 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1915 return generic_setxattr(dentry, name, value, size, flags);
1916
1917 err = shmem_xattr_validate(name);
1918 if (err)
1919 return err;
1920
1921 if (size == 0)
1922 value = ""; /* empty EA, do not remove */
1923
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001924 return shmem_xattr_set(dentry->d_inode, name, value, size, flags);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001925
1926}
1927
1928static int shmem_removexattr(struct dentry *dentry, const char *name)
1929{
1930 int err;
1931
1932 /*
1933 * If this is a request for a synthetic attribute in the system.*
1934 * namespace use the generic infrastructure to resolve a handler
1935 * for it via sb->s_xattr.
1936 */
1937 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1938 return generic_removexattr(dentry, name);
1939
1940 err = shmem_xattr_validate(name);
1941 if (err)
1942 return err;
1943
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07001944 return shmem_xattr_set(dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001945}
1946
1947static bool xattr_is_trusted(const char *name)
1948{
1949 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1950}
1951
1952static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1953{
1954 bool trusted = capable(CAP_SYS_ADMIN);
1955 struct shmem_xattr *xattr;
1956 struct shmem_inode_info *info;
1957 size_t used = 0;
1958
1959 info = SHMEM_I(dentry->d_inode);
1960
1961 spin_lock(&info->lock);
1962 list_for_each_entry(xattr, &info->xattr_list, list) {
1963 size_t len;
1964
1965 /* skip "trusted." attributes for unprivileged callers */
1966 if (!trusted && xattr_is_trusted(xattr->name))
1967 continue;
1968
1969 len = strlen(xattr->name) + 1;
1970 used += len;
1971 if (buffer) {
1972 if (size < used) {
1973 used = -ERANGE;
1974 break;
1975 }
1976 memcpy(buffer, xattr->name, len);
1977 buffer += len;
1978 }
1979 }
1980 spin_unlock(&info->lock);
1981
1982 return used;
1983}
1984#endif /* CONFIG_TMPFS_XATTR */
1985
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001986static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001988 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001989#ifdef CONFIG_TMPFS_XATTR
1990 .setxattr = shmem_setxattr,
1991 .getxattr = shmem_getxattr,
1992 .listxattr = shmem_listxattr,
1993 .removexattr = shmem_removexattr,
1994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995};
1996
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001997static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 .readlink = generic_readlink,
1999 .follow_link = shmem_follow_link,
2000 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002001#ifdef CONFIG_TMPFS_XATTR
2002 .setxattr = shmem_setxattr,
2003 .getxattr = shmem_getxattr,
2004 .listxattr = shmem_listxattr,
2005 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002006#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002007};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002008
David M. Grimes91828a42006-10-17 00:09:45 -07002009static struct dentry *shmem_get_parent(struct dentry *child)
2010{
2011 return ERR_PTR(-ESTALE);
2012}
2013
2014static int shmem_match(struct inode *ino, void *vfh)
2015{
2016 __u32 *fh = vfh;
2017 __u64 inum = fh[2];
2018 inum = (inum << 32) | fh[1];
2019 return ino->i_ino == inum && fh[0] == ino->i_generation;
2020}
2021
Christoph Hellwig480b1162007-10-21 16:42:13 -07002022static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2023 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002024{
David M. Grimes91828a42006-10-17 00:09:45 -07002025 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002026 struct dentry *dentry = NULL;
Hugh Dickins530258f2012-10-07 20:32:51 -07002027 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07002028
Christoph Hellwig480b1162007-10-21 16:42:13 -07002029 if (fh_len < 3)
2030 return NULL;
2031
Hugh Dickins530258f2012-10-07 20:32:51 -07002032 inum = fid->raw[2];
2033 inum = (inum << 32) | fid->raw[1];
2034
Christoph Hellwig480b1162007-10-21 16:42:13 -07002035 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2036 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002037 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002038 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002039 iput(inode);
2040 }
2041
Christoph Hellwig480b1162007-10-21 16:42:13 -07002042 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002043}
2044
2045static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2046 int connectable)
2047{
2048 struct inode *inode = dentry->d_inode;
2049
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302050 if (*len < 3) {
2051 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002052 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302053 }
David M. Grimes91828a42006-10-17 00:09:45 -07002054
Al Viro1d3382c2010-10-23 15:19:20 -04002055 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002056 /* Unfortunately insert_inode_hash is not idempotent,
2057 * so as we hash inodes here rather than at creation
2058 * time, we need a lock to ensure we only try
2059 * to do it once
2060 */
2061 static DEFINE_SPINLOCK(lock);
2062 spin_lock(&lock);
Al Viro1d3382c2010-10-23 15:19:20 -04002063 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002064 __insert_inode_hash(inode,
2065 inode->i_ino + inode->i_generation);
2066 spin_unlock(&lock);
2067 }
2068
2069 fh[0] = inode->i_generation;
2070 fh[1] = inode->i_ino;
2071 fh[2] = ((__u64)inode->i_ino) >> 32;
2072
2073 *len = 3;
2074 return 1;
2075}
2076
Christoph Hellwig39655162007-10-21 16:42:17 -07002077static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002078 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002079 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002080 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002081};
2082
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002083static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2084 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
2086 char *this_char, *value, *rest;
2087
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002088 while (options != NULL) {
2089 this_char = options;
2090 for (;;) {
2091 /*
2092 * NUL-terminate this option: unfortunately,
2093 * mount options form a comma-separated list,
2094 * but mpol's nodelist may also contain commas.
2095 */
2096 options = strchr(options, ',');
2097 if (options == NULL)
2098 break;
2099 options++;
2100 if (!isdigit(*options)) {
2101 options[-1] = '\0';
2102 break;
2103 }
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (!*this_char)
2106 continue;
2107 if ((value = strchr(this_char,'=')) != NULL) {
2108 *value++ = 0;
2109 } else {
2110 printk(KERN_ERR
2111 "tmpfs: No value for mount option '%s'\n",
2112 this_char);
2113 return 1;
2114 }
2115
2116 if (!strcmp(this_char,"size")) {
2117 unsigned long long size;
2118 size = memparse(value,&rest);
2119 if (*rest == '%') {
2120 size <<= PAGE_SHIFT;
2121 size *= totalram_pages;
2122 do_div(size, 100);
2123 rest++;
2124 }
2125 if (*rest)
2126 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002127 sbinfo->max_blocks =
2128 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002130 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (*rest)
2132 goto bad_val;
2133 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002134 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (*rest)
2136 goto bad_val;
2137 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002138 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002140 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (*rest)
2142 goto bad_val;
2143 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002144 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002146 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (*rest)
2148 goto bad_val;
2149 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002150 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002152 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (*rest)
2154 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002155 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002156 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002157 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } else {
2159 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2160 this_char);
2161 return 1;
2162 }
2163 }
2164 return 0;
2165
2166bad_val:
2167 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2168 value, this_char);
2169 return 1;
2170
2171}
2172
2173static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2174{
2175 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002176 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002177 unsigned long inodes;
2178 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002180 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002181 return error;
2182
2183 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002184 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002185 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002186 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002187 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002188 goto out;
2189 /*
Hugh Dickins54af6042011-08-03 16:21:24 -07002190 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002191 * but we must separately disallow unlimited->limited, because
2192 * in that case we have no record of how much is already in use.
2193 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002194 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002195 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002196 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002197 goto out;
2198
2199 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002200 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002201 sbinfo->max_inodes = config.max_inodes;
2202 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002203
2204 mpol_put(sbinfo->mpol);
2205 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002206out:
2207 spin_unlock(&sbinfo->stat_lock);
2208 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002210
Al Viro34c80b12011-12-08 21:32:45 -05002211static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002212{
Al Viro34c80b12011-12-08 21:32:45 -05002213 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002214
2215 if (sbinfo->max_blocks != shmem_default_max_blocks())
2216 seq_printf(seq, ",size=%luk",
2217 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2218 if (sbinfo->max_inodes != shmem_default_max_inodes())
2219 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2220 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002221 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002222 if (sbinfo->uid != 0)
2223 seq_printf(seq, ",uid=%u", sbinfo->uid);
2224 if (sbinfo->gid != 0)
2225 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002226 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002227 return 0;
2228}
2229#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231static void shmem_put_super(struct super_block *sb)
2232{
Hugh Dickins602586a2010-08-17 15:23:56 -07002233 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2234
2235 percpu_counter_destroy(&sbinfo->used_blocks);
2236 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 sb->s_fs_info = NULL;
2238}
2239
Kay Sievers2b2af542009-04-30 15:23:42 +02002240int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002243 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002244 int err = -ENOMEM;
2245
2246 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002247 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002248 L1_CACHE_BYTES), GFP_KERNEL);
2249 if (!sbinfo)
2250 return -ENOMEM;
2251
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002252 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002253 sbinfo->uid = current_fsuid();
2254 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002255 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002257#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /*
2259 * Per default we only allow half of the physical ram per
2260 * tmpfs instance, limiting inodes to one per page of lowmem;
2261 * but the internal instance is left unlimited.
2262 */
2263 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002264 sbinfo->max_blocks = shmem_default_max_blocks();
2265 sbinfo->max_inodes = shmem_default_max_inodes();
2266 if (shmem_parse_options(data, sbinfo, false)) {
2267 err = -EINVAL;
2268 goto failed;
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
David M. Grimes91828a42006-10-17 00:09:45 -07002271 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272#else
2273 sb->s_flags |= MS_NOUSER;
2274#endif
2275
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002276 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002277 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2278 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002279 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002280
Hugh Dickins285b2c42011-08-03 16:21:20 -07002281 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 sb->s_blocksize = PAGE_CACHE_SIZE;
2283 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2284 sb->s_magic = TMPFS_MAGIC;
2285 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002286 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002287#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002288 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002289#endif
2290#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002291 sb->s_flags |= MS_POSIXACL;
2292#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002293
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002294 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (!inode)
2296 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002297 inode->i_uid = sbinfo->uid;
2298 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05002299 sb->s_root = d_make_root(inode);
2300 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05002301 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return 0;
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304failed:
2305 shmem_put_super(sb);
2306 return err;
2307}
2308
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002309static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311static struct inode *shmem_alloc_inode(struct super_block *sb)
2312{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002313 struct shmem_inode_info *info;
2314 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2315 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002317 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002320static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002321{
2322 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e32011-01-07 17:49:49 +11002323 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2324}
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326static void shmem_destroy_inode(struct inode *inode)
2327{
Al Viro09208d12011-07-26 03:15:03 -04002328 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002330 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002333static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002335 struct shmem_inode_info *info = foo;
2336 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002339static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2342 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002343 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 return 0;
2345}
2346
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002347static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002349 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002352static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002354 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002356 .write_begin = shmem_write_begin,
2357 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002359 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002360 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361};
2362
Helge Deller15ad7cd2006-12-06 20:40:36 -08002363static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .mmap = shmem_mmap,
2365#ifdef CONFIG_TMPFS
2366 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002367 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002368 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002369 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002370 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002371 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002372 .splice_read = shmem_file_splice_read,
Hugh Dickinsae976412007-06-04 10:00:39 +02002373 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374#endif
2375};
2376
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002377static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002378 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002379 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002380#ifdef CONFIG_TMPFS_XATTR
2381 .setxattr = shmem_setxattr,
2382 .getxattr = shmem_getxattr,
2383 .listxattr = shmem_listxattr,
2384 .removexattr = shmem_removexattr,
2385#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386};
2387
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002388static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#ifdef CONFIG_TMPFS
2390 .create = shmem_create,
2391 .lookup = simple_lookup,
2392 .link = shmem_link,
2393 .unlink = shmem_unlink,
2394 .symlink = shmem_symlink,
2395 .mkdir = shmem_mkdir,
2396 .rmdir = shmem_rmdir,
2397 .mknod = shmem_mknod,
2398 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002400#ifdef CONFIG_TMPFS_XATTR
2401 .setxattr = shmem_setxattr,
2402 .getxattr = shmem_getxattr,
2403 .listxattr = shmem_listxattr,
2404 .removexattr = shmem_removexattr,
2405#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002406#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002407 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002408#endif
2409};
2410
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002411static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002412#ifdef CONFIG_TMPFS_XATTR
2413 .setxattr = shmem_setxattr,
2414 .getxattr = shmem_getxattr,
2415 .listxattr = shmem_listxattr,
2416 .removexattr = shmem_removexattr,
2417#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002418#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002419 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002420#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421};
2422
Hugh Dickins759b9772007-03-05 00:30:28 -08002423static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 .alloc_inode = shmem_alloc_inode,
2425 .destroy_inode = shmem_destroy_inode,
2426#ifdef CONFIG_TMPFS
2427 .statfs = shmem_statfs,
2428 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002429 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430#endif
Al Viro1f895f72010-06-05 19:10:41 -04002431 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 .drop_inode = generic_delete_inode,
2433 .put_super = shmem_put_super,
2434};
2435
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002436static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002437 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438#ifdef CONFIG_NUMA
2439 .set_policy = shmem_set_policy,
2440 .get_policy = shmem_get_policy,
2441#endif
2442};
2443
Al Viro3c26ff62010-07-25 11:46:36 +04002444static struct dentry *shmem_mount(struct file_system_type *fs_type,
2445 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Al Viro3c26ff62010-07-25 11:46:36 +04002447 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002450static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 .owner = THIS_MODULE,
2452 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002453 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 .kill_sb = kill_litter_super,
2455};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002457int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 int error;
2460
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002461 error = bdi_init(&shmem_backing_dev_info);
2462 if (error)
2463 goto out4;
2464
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002465 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (error)
2467 goto out3;
2468
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002469 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (error) {
2471 printk(KERN_ERR "Could not register tmpfs\n");
2472 goto out2;
2473 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002474
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002475 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2476 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (IS_ERR(shm_mnt)) {
2478 error = PTR_ERR(shm_mnt);
2479 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2480 goto out1;
2481 }
2482 return 0;
2483
2484out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002485 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002487 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002489 bdi_destroy(&shmem_backing_dev_info);
2490out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 shm_mnt = ERR_PTR(error);
2492 return error;
2493}
Matt Mackall853ac432009-01-06 14:40:20 -08002494
2495#else /* !CONFIG_SHMEM */
2496
2497/*
2498 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2499 *
2500 * This is intended for small system where the benefits of the full
2501 * shmem code (swap-backed and resource-limited) are outweighed by
2502 * their complexity. On systems without swap this code should be
2503 * effectively equivalent, but much lighter weight.
2504 */
2505
2506#include <linux/ramfs.h>
2507
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002508static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002509 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002510 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002511 .kill_sb = kill_litter_super,
2512};
2513
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002514int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002515{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002516 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002517
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002518 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002519 BUG_ON(IS_ERR(shm_mnt));
2520
2521 return 0;
2522}
2523
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002524int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002525{
2526 return 0;
2527}
2528
Hugh Dickins3f96b792009-09-21 17:03:37 -07002529int shmem_lock(struct file *file, int lock, struct user_struct *user)
2530{
2531 return 0;
2532}
2533
Hugh Dickins24513262012-01-20 14:34:21 -08002534void shmem_unlock_mapping(struct address_space *mapping)
2535{
2536}
2537
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002538void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002539{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002540 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002541}
2542EXPORT_SYMBOL_GPL(shmem_truncate_range);
2543
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002544#define shmem_vm_ops generic_file_vm_ops
2545#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002546#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002547#define shmem_acct_size(flags, size) 0
2548#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002549
2550#endif /* CONFIG_SHMEM */
2551
2552/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Randy Dunlap46711812008-03-19 17:00:41 -07002554/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 * @name: name for dentry (to be seen in /proc/<pid>/maps
2557 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002558 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002560struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 int error;
2563 struct file *file;
2564 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002565 struct path path;
2566 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 struct qstr this;
2568
2569 if (IS_ERR(shm_mnt))
2570 return (void *)shm_mnt;
2571
Hugh Dickins285b2c42011-08-03 16:21:20 -07002572 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return ERR_PTR(-EINVAL);
2574
2575 if (shmem_acct_size(flags, size))
2576 return ERR_PTR(-ENOMEM);
2577
2578 error = -ENOMEM;
2579 this.name = name;
2580 this.len = strlen(name);
2581 this.hash = 0; /* will go */
2582 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002583 path.dentry = d_alloc(root, &this);
2584 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002586 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002589 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002591 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Al Viro2c48b9c2009-08-09 00:52:35 +04002593 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002595 clear_nlink(inode); /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002596#ifndef CONFIG_MMU
2597 error = ramfs_nommu_expand_for_mapping(inode, size);
2598 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002599 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002600#endif
Al Viro4b42af82009-08-05 18:25:56 +04002601
2602 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002603 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002604 &shmem_file_operations);
2605 if (!file)
2606 goto put_dentry;
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return file;
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002611 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612put_memory:
2613 shmem_unacct_size(flags, size);
2614 return ERR_PTR(error);
2615}
Keith Packard395e0dd2008-06-20 00:08:06 -07002616EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
Randy Dunlap46711812008-03-19 17:00:41 -07002618/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2621 */
2622int shmem_zero_setup(struct vm_area_struct *vma)
2623{
2624 struct file *file;
2625 loff_t size = vma->vm_end - vma->vm_start;
2626
2627 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2628 if (IS_ERR(file))
2629 return PTR_ERR(file);
2630
2631 if (vma->vm_file)
2632 fput(vma->vm_file);
2633 vma->vm_file = file;
2634 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c362011-03-22 16:33:43 -07002635 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return 0;
2637}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002638
2639/**
2640 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2641 * @mapping: the page's address_space
2642 * @index: the page index
2643 * @gfp: the page allocator flags to use if allocating
2644 *
2645 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2646 * with any new page allocations done using the specified allocation flags.
2647 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2648 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2649 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2650 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002651 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2652 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002653 */
2654struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2655 pgoff_t index, gfp_t gfp)
2656{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002657#ifdef CONFIG_SHMEM
2658 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002659 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002660 int error;
2661
2662 BUG_ON(mapping->a_ops != &shmem_aops);
2663 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2664 if (error)
2665 page = ERR_PTR(error);
2666 else
2667 unlock_page(page);
2668 return page;
2669#else
2670 /*
2671 * The tiny !SHMEM case uses ramfs without swap
2672 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002673 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002674#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002675}
2676EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);