blob: 4aaa53abe3026ccd686dc541e402c9e6ec5dff3c [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) ?
130 0 : security_vm_enough_memory_kern(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) ?
148 security_vm_enough_memory_kern(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 Dickins54af60422011-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 Dickins54af60422011-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 */
382static void shmem_pagevec_release(struct pagevec *pvec)
383{
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;
392 pagevec_release(pvec);
393}
394
395/*
396 * Remove range of pages and swap entries from radix tree, and free them.
397 */
Hugh Dickins285b2c42011-08-03 16:21:20 -0700398void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700400 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700402 pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700403 unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700404 pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700405 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700406 pgoff_t indices[PAGEVEC_SIZE];
407 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700408 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700409 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700411 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
412
413 pagevec_init(&pvec, 0);
414 index = start;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700415 while (index <= end) {
416 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
417 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
418 pvec.pages, indices);
419 if (!pvec.nr)
420 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700421 mem_cgroup_uncharge_start();
422 for (i = 0; i < pagevec_count(&pvec); i++) {
423 struct page *page = pvec.pages[i];
424
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700425 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700426 if (index > end)
427 break;
428
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700429 if (radix_tree_exceptional_entry(page)) {
430 nr_swaps_freed += !shmem_free_swap(mapping,
431 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700432 continue;
433 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700434
435 if (!trylock_page(page))
436 continue;
437 if (page->mapping == mapping) {
438 VM_BUG_ON(PageWriteback(page));
439 truncate_inode_page(mapping, page);
440 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700441 unlock_page(page);
442 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700443 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700444 mem_cgroup_uncharge_end();
445 cond_resched();
446 index++;
447 }
448
449 if (partial) {
450 struct page *page = NULL;
451 shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
452 if (page) {
453 zero_user_segment(page, partial, PAGE_CACHE_SIZE);
454 set_page_dirty(page);
455 unlock_page(page);
456 page_cache_release(page);
457 }
458 }
459
460 index = start;
461 for ( ; ; ) {
462 cond_resched();
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700463 pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
464 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
465 pvec.pages, indices);
466 if (!pvec.nr) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700467 if (index == start)
468 break;
469 index = start;
470 continue;
471 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700472 if (index == start && indices[0] > end) {
473 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700474 break;
475 }
476 mem_cgroup_uncharge_start();
477 for (i = 0; i < pagevec_count(&pvec); i++) {
478 struct page *page = pvec.pages[i];
479
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700480 index = indices[i];
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700481 if (index > end)
482 break;
483
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700484 if (radix_tree_exceptional_entry(page)) {
485 nr_swaps_freed += !shmem_free_swap(mapping,
486 index, page);
487 continue;
488 }
489
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700490 lock_page(page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700491 if (page->mapping == mapping) {
492 VM_BUG_ON(PageWriteback(page));
493 truncate_inode_page(mapping, page);
494 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700495 unlock_page(page);
496 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700497 shmem_pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700498 mem_cgroup_uncharge_end();
499 index++;
500 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 spin_lock(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700503 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 shmem_recalc_inode(inode);
505 spin_unlock(&info->lock);
506
Hugh Dickins285b2c42011-08-03 16:21:20 -0700507 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700509EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Hugh Dickins94c1e622011-06-27 16:18:03 -0700511static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int error;
515
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200516 error = inode_change_ok(inode, attr);
517 if (error)
518 return error;
519
Hugh Dickins94c1e622011-06-27 16:18:03 -0700520 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
521 loff_t oldsize = inode->i_size;
522 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000523
Hugh Dickins94c1e622011-06-27 16:18:03 -0700524 if (newsize != oldsize) {
525 i_size_write(inode, newsize);
526 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
527 }
528 if (newsize < oldsize) {
529 loff_t holebegin = round_up(newsize, PAGE_SIZE);
530 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
531 shmem_truncate_range(inode, newsize, (loff_t)-1);
532 /* unmap again to remove racily COWed private pages */
533 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200537 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700538#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200539 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100540 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return error;
543}
544
Al Viro1f895f72010-06-05 19:10:41 -0400545static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700548 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000550 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 shmem_unacct_size(info->flags, inode->i_size);
552 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000553 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800555 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800557 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700559 } else
560 kfree(info->symlink);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700561
562 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
563 kfree(xattr->name);
564 kfree(xattr);
565 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700566 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800567 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400568 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700571/*
572 * If swap found in inode, free it and move page from swapcache to filecache.
573 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700574static int shmem_unuse_inode(struct shmem_inode_info *info,
575 swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700577 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700578 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700579 pgoff_t index;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800580 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700582 radswap = swp_to_radix_entry(swap);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700583 index = radix_tree_locate_item(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700584 if (index == -1)
Hugh Dickins285b2c42011-08-03 16:21:20 -0700585 return 0;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800586
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800587 /*
588 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400589 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800590 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -0700591 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800592 */
593 if (shmem_swaplist.next != &info->swaplist)
594 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800595
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800596 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700597 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
598 * but also to hold up shmem_evict_inode(): so inode cannot be freed
599 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800600 */
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700601 error = shmem_add_to_page_cache(page, mapping, index,
602 GFP_NOWAIT, radswap);
Hugh Dickins778dd892011-05-11 15:13:37 -0700603 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700604
Hugh Dickins48f170f2011-07-25 17:12:37 -0700605 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700606 /*
607 * Truncation and eviction use free_swap_and_cache(), which
608 * only does trylock page: if we raced, best clean up here.
609 */
Hugh Dickins73b12622008-02-04 22:28:50 -0800610 delete_from_swap_cache(page);
611 set_page_dirty(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700612 if (!error) {
613 spin_lock(&info->lock);
614 info->swapped--;
615 spin_unlock(&info->lock);
616 swap_free(swap);
617 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800618 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800620 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700624 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700626int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700628 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct shmem_inode_info *info;
630 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -0700631 int error;
632
633 /*
634 * Charge page using GFP_KERNEL while we can wait, before taking
635 * the shmem_swaplist_mutex which might hold up shmem_writepage().
636 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -0700637 */
638 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
639 if (error)
640 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700641 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800643 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700644 list_for_each_safe(this, next, &shmem_swaplist) {
645 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -0700646 if (info->swapped)
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700647 found = shmem_unuse_inode(info, swap, page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700648 else
649 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800650 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800651 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -0700652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800654 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -0700655
Hugh Dickins778dd892011-05-11 15:13:37 -0700656 if (!found)
657 mem_cgroup_uncharge_cache_page(page);
658 if (found < 0)
659 error = found;
660out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800661 unlock_page(page);
662 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -0700663 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
667 * Move the page from the page cache to the swap cache.
668 */
669static int shmem_writepage(struct page *page, struct writeback_control *wbc)
670{
671 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700674 swp_entry_t swap;
675 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 mapping = page->mapping;
679 index = page->index;
680 inode = mapping->host;
681 info = SHMEM_I(inode);
682 if (info->flags & VM_LOCKED)
683 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800684 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto redirty;
686
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800687 /*
688 * shmem_backing_dev_info's capabilities prevent regular writeback or
689 * sync from ever calling shmem_writepage; but a stacking filesystem
Hugh Dickins48f170f2011-07-25 17:12:37 -0700690 * might use ->writepage of its underlying filesystem, in which case
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800691 * tmpfs should write out to swap only in response to memory pressure,
Hugh Dickins48f170f2011-07-25 17:12:37 -0700692 * and not for the writeback threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800693 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700694 if (!wbc->for_reclaim) {
695 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
696 goto redirty;
697 }
698 swap = get_swap_page();
699 if (!swap.val)
700 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800701
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700702 /*
703 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700704 * if it's not already there. Do it now before the page is
705 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700706 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700707 * we've incremented swapped, because shmem_unuse_inode() will
708 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700709 */
Hugh Dickins48f170f2011-07-25 17:12:37 -0700710 mutex_lock(&shmem_swaplist_mutex);
711 if (list_empty(&info->swaplist))
712 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -0700713
Hugh Dickins48f170f2011-07-25 17:12:37 -0700714 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800715 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700716 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
717
718 spin_lock(&info->lock);
719 info->swapped++;
720 shmem_recalc_inode(inode);
Hugh Dickins826267c2011-05-28 13:14:09 -0700721 spin_unlock(&info->lock);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700722
723 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800724 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -0700725 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727 }
728
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700729 mutex_unlock(&shmem_swaplist_mutex);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700730 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731redirty:
732 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800733 if (wbc->for_reclaim)
734 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
735 unlock_page(page);
736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800740#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700741static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800742{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700743 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800744
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700745 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700746 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800747
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700748 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800749
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -0700750 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800751}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700752
753static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
754{
755 struct mempolicy *mpol = NULL;
756 if (sbinfo->mpol) {
757 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
758 mpol = sbinfo->mpol;
759 mpol_get(mpol);
760 spin_unlock(&sbinfo->stat_lock);
761 }
762 return mpol;
763}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800764#endif /* CONFIG_TMPFS */
765
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700766static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
767 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700769 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 struct vm_area_struct pvma;
771
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700772 spol = mpol_cond_copy(&mpol,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700773 mpol_shared_policy_lookup(&info->policy, index));
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800776 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700777 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800778 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700779 pvma.vm_policy = spol;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700780 return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Hugh Dickins02098fe2008-02-04 22:28:42 -0800783static struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700784 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800788 /* Create a pseudo vma that just contains the policy */
789 pvma.vm_start = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700790 pvma.vm_pgoff = index;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -0800791 pvma.vm_ops = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700792 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -0700793
794 /*
795 * alloc_page_vma() will drop the shared policy reference
796 */
797 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800799#else /* !CONFIG_NUMA */
800#ifdef CONFIG_TMPFS
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700801static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800802{
803}
804#endif /* CONFIG_TMPFS */
805
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700806static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
807 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700809 return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Hugh Dickins02098fe2008-02-04 22:28:42 -0800812static inline struct page *shmem_alloc_page(gfp_t gfp,
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700813 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Hugh Dickinse84e2e12007-11-28 18:55:10 +0000815 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800817#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Lee Schermerhorn71fe8042008-04-28 02:13:26 -0700819#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
820static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
821{
822 return NULL;
823}
824#endif
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/*
Hugh Dickins68da9f02011-07-25 17:12:34 -0700827 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *
829 * If we allocate a new one we do not mark it dirty. That's up to the
830 * vm. If we swap it in we mark it dirty since we also free the swap
831 * entry since a page cannot live in both the swap and page cache
832 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700833static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Hugh Dickins68da9f02011-07-25 17:12:34 -0700834 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct address_space *mapping = inode->i_mapping;
Hugh Dickins54af60422011-08-03 16:21:24 -0700837 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct shmem_sb_info *sbinfo;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700839 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 swp_entry_t swap;
841 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -0700842 int once = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700844 if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EFBIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -0700847 swap.val = 0;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700848 page = find_lock_page(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -0700849 if (radix_tree_exceptional_entry(page)) {
850 swap = radix_to_swp_entry(page);
851 page = NULL;
852 }
853
854 if (sgp != SGP_WRITE &&
855 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
856 error = -EINVAL;
857 goto failed;
858 }
859
860 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800861 /*
Hugh Dickins27ab7002011-07-25 17:12:36 -0700862 * Once we can get the page lock, it must be uptodate:
863 * if there were an error in reading back from swap,
864 * the page would not be inserted into the filecache.
Hugh Dickinsb409f9f2008-02-04 22:28:54 -0800865 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700866 BUG_ON(page && !PageUptodate(page));
867 *pagep = page;
868 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -0700869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /*
Hugh Dickins54af60422011-08-03 16:21:24 -0700872 * Fast cache lookup did not find it:
873 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700875 info = SHMEM_I(inode);
876 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (swap.val) {
879 /* Look it up and read it in.. */
Hugh Dickins27ab7002011-07-25 17:12:36 -0700880 page = lookup_swap_cache(swap);
881 if (!page) {
Ying Han456f9982011-05-26 16:25:38 -0700882 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700883 if (fault_type)
884 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700885 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700886 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -0700887 error = -ENOMEM;
888 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -0700893 lock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700894 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -0700896 goto failed;
897 }
898 wait_on_page_writeback(page);
899
900 /* Someone may have already done it for us */
901 if (page->mapping) {
902 if (page->mapping == mapping &&
903 page->index == index)
904 goto done;
905 error = -EEXIST;
906 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700909 error = mem_cgroup_cache_charge(page, current->mm,
910 gfp & GFP_RECLAIM_MASK);
911 if (!error)
912 error = shmem_add_to_page_cache(page, mapping, index,
913 gfp, swp_to_radix_entry(swap));
Hugh Dickins54af60422011-08-03 16:21:24 -0700914 if (error)
915 goto failed;
916
917 spin_lock(&info->lock);
918 info->swapped--;
919 shmem_recalc_inode(inode);
920 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700921
Hugh Dickins27ab7002011-07-25 17:12:36 -0700922 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -0700923 set_page_dirty(page);
924 swap_free(swap);
925
Hugh Dickins54af60422011-08-03 16:21:24 -0700926 } else {
927 if (shmem_acct_block(info->flags)) {
928 error = -ENOSPC;
929 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700931 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700932 if (percpu_counter_compare(&sbinfo->used_blocks,
Hugh Dickins54af60422011-08-03 16:21:24 -0700933 sbinfo->max_blocks) >= 0) {
934 error = -ENOSPC;
935 goto unacct;
936 }
Tim Chen7e496292010-08-09 17:19:05 -0700937 percpu_counter_inc(&sbinfo->used_blocks);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Hugh Dickins54af60422011-08-03 16:21:24 -0700940 page = shmem_alloc_page(gfp, info, index);
941 if (!page) {
942 error = -ENOMEM;
943 goto decused;
944 }
945
946 SetPageSwapBacked(page);
947 __set_page_locked(page);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -0700948 error = mem_cgroup_cache_charge(page, current->mm,
949 gfp & GFP_RECLAIM_MASK);
950 if (!error)
951 error = shmem_add_to_page_cache(page, mapping, index,
952 gfp, NULL);
Hugh Dickins54af60422011-08-03 16:21:24 -0700953 if (error)
954 goto decused;
955 lru_cache_add_anon(page);
956
957 spin_lock(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 info->alloced++;
Hugh Dickins54af60422011-08-03 16:21:24 -0700959 inode->i_blocks += BLOCKS_PER_PAGE;
960 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -0700962
Hugh Dickins27ab7002011-07-25 17:12:36 -0700963 clear_highpage(page);
964 flush_dcache_page(page);
965 SetPageUptodate(page);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800966 if (sgp == SGP_DIRTY)
Hugh Dickins27ab7002011-07-25 17:12:36 -0700967 set_page_dirty(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -0700968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969done:
Hugh Dickins54af60422011-08-03 16:21:24 -0700970 /* Perhaps the file has been truncated since we checked */
971 if (sgp != SGP_WRITE &&
972 ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
973 error = -EINVAL;
974 goto trunc;
Shaohua Liff36b8012010-08-09 17:19:06 -0700975 }
Hugh Dickins54af60422011-08-03 16:21:24 -0700976 *pagep = page;
977 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -0700978
Nick Piggind0217ac2007-07-19 01:47:03 -0700979 /*
Hugh Dickins54af60422011-08-03 16:21:24 -0700980 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Hugh Dickins54af60422011-08-03 16:21:24 -0700982trunc:
983 ClearPageDirty(page);
984 delete_from_page_cache(page);
985 spin_lock(&info->lock);
986 info->alloced--;
987 inode->i_blocks -= BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 spin_unlock(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -0700989decused:
990 if (sbinfo->max_blocks)
991 percpu_counter_add(&sbinfo->used_blocks, -1);
992unacct:
993 shmem_unacct_blocks(info->flags, 1);
994failed:
995 if (swap.val && error != -EINVAL) {
996 struct page *test = find_get_page(mapping, index);
997 if (test && !radix_tree_exceptional_entry(test))
998 page_cache_release(test);
999 /* Have another try if the entry has changed */
1000 if (test != swp_to_radix_entry(swap))
1001 error = -EEXIST;
1002 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001003 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001004 unlock_page(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001005 page_cache_release(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001006 }
1007 if (error == -ENOSPC && !once++) {
1008 info = SHMEM_I(inode);
1009 spin_lock(&info->lock);
1010 shmem_recalc_inode(inode);
1011 spin_unlock(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001012 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001013 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001014 if (error == -EEXIST)
1015 goto repeat;
1016 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1020{
1021 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1022 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001023 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1026 if (error)
1027 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001028
Ying Han456f9982011-05-26 16:25:38 -07001029 if (ret & VM_FAULT_MAJOR) {
1030 count_vm_event(PGMAJFAULT);
1031 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1032 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001037static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001039 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1040 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001043static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1044 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001046 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1047 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001049 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1050 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052#endif
1053
1054int shmem_lock(struct file *file, int lock, struct user_struct *user)
1055{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001056 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct shmem_inode_info *info = SHMEM_I(inode);
1058 int retval = -ENOMEM;
1059
1060 spin_lock(&info->lock);
1061 if (lock && !(info->flags & VM_LOCKED)) {
1062 if (!user_shm_lock(inode->i_size, user))
1063 goto out_nomem;
1064 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001065 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 if (!lock && (info->flags & VM_LOCKED) && user) {
1068 user_shm_unlock(inode->i_size, user);
1069 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001070 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074out_nomem:
1075 spin_unlock(&info->lock);
1076 return retval;
1077}
1078
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001079static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 file_accessed(file);
1082 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001083 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return 0;
1085}
1086
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001087static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04001088 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 struct inode *inode;
1091 struct shmem_inode_info *info;
1092 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1093
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001094 if (shmem_reserve_inode(sb))
1095 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 inode = new_inode(sb);
1098 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001099 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001100 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1103 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001104 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 info = SHMEM_I(inode);
1106 memset(info, 0, (char *)inode - (char *)info);
1107 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001108 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001110 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001111 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 switch (mode & S_IFMT) {
1114 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001115 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 init_special_inode(inode, mode, dev);
1117 break;
1118 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001119 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 inode->i_op = &shmem_inode_operations;
1121 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001122 mpol_shared_policy_init(&info->policy,
1123 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001126 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* Some things misbehave if size == 0 on a directory */
1128 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1129 inode->i_op = &shmem_dir_inode_operations;
1130 inode->i_fop = &simple_dir_operations;
1131 break;
1132 case S_IFLNK:
1133 /*
1134 * Must not load anything in the rbtree,
1135 * mpol_free_shared_policy will not be called.
1136 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001137 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001140 } else
1141 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return inode;
1143}
1144
1145#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001146static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001147static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001150shmem_write_begin(struct file *file, struct address_space *mapping,
1151 loff_t pos, unsigned len, unsigned flags,
1152 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Nick Piggin800d15a2007-10-16 01:25:03 -07001154 struct inode *inode = mapping->host;
1155 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
Nick Piggin800d15a2007-10-16 01:25:03 -07001156 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1157}
1158
1159static int
1160shmem_write_end(struct file *file, struct address_space *mapping,
1161 loff_t pos, unsigned len, unsigned copied,
1162 struct page *page, void *fsdata)
1163{
1164 struct inode *inode = mapping->host;
1165
Hugh Dickinsd3602442008-02-04 22:28:44 -08001166 if (pos + copied > inode->i_size)
1167 i_size_write(inode, pos + copied);
1168
Nick Piggin800d15a2007-10-16 01:25:03 -07001169 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001170 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001171 page_cache_release(page);
1172
Nick Piggin800d15a2007-10-16 01:25:03 -07001173 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1177{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001178 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001180 pgoff_t index;
1181 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001182 enum sgp_type sgp = SGP_READ;
1183
1184 /*
1185 * Might this read be for a stacking filesystem? Then when reading
1186 * holes of a sparse file, we actually need to allocate those pages,
1187 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1188 */
1189 if (segment_eq(get_fs(), KERNEL_DS))
1190 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 index = *ppos >> PAGE_CACHE_SHIFT;
1193 offset = *ppos & ~PAGE_CACHE_MASK;
1194
1195 for (;;) {
1196 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001197 pgoff_t end_index;
1198 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 loff_t i_size = i_size_read(inode);
1200
1201 end_index = i_size >> PAGE_CACHE_SHIFT;
1202 if (index > end_index)
1203 break;
1204 if (index == end_index) {
1205 nr = i_size & ~PAGE_CACHE_MASK;
1206 if (nr <= offset)
1207 break;
1208 }
1209
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001210 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (desc->error) {
1212 if (desc->error == -EINVAL)
1213 desc->error = 0;
1214 break;
1215 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001216 if (page)
1217 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001221 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 */
1223 nr = PAGE_CACHE_SIZE;
1224 i_size = i_size_read(inode);
1225 end_index = i_size >> PAGE_CACHE_SHIFT;
1226 if (index == end_index) {
1227 nr = i_size & ~PAGE_CACHE_MASK;
1228 if (nr <= offset) {
1229 if (page)
1230 page_cache_release(page);
1231 break;
1232 }
1233 }
1234 nr -= offset;
1235
1236 if (page) {
1237 /*
1238 * If users can be writing to this page using arbitrary
1239 * virtual addresses, take care about potential aliasing
1240 * before reading the page on the kernel side.
1241 */
1242 if (mapping_writably_mapped(mapping))
1243 flush_dcache_page(page);
1244 /*
1245 * Mark the page accessed if we read the beginning.
1246 */
1247 if (!offset)
1248 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001249 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001251 page_cache_get(page);
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 /*
1255 * Ok, we have the page, and it's up-to-date, so
1256 * now we can copy it to user space...
1257 *
1258 * The actor routine returns how many bytes were actually used..
1259 * NOTE! This may not be the same as how much of a user buffer
1260 * we filled up (we may be padding etc), so we can only update
1261 * "pos" here (the actor routine has to update the user buffer
1262 * pointers and the remaining count).
1263 */
1264 ret = actor(desc, page, offset, nr);
1265 offset += ret;
1266 index += offset >> PAGE_CACHE_SHIFT;
1267 offset &= ~PAGE_CACHE_MASK;
1268
1269 page_cache_release(page);
1270 if (ret != nr || !desc->count)
1271 break;
1272
1273 cond_resched();
1274 }
1275
1276 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1277 file_accessed(filp);
1278}
1279
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07001280static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1281 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07001283 struct file *filp = iocb->ki_filp;
1284 ssize_t retval;
1285 unsigned long seg;
1286 size_t count;
1287 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07001289 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1290 if (retval)
1291 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07001293 for (seg = 0; seg < nr_segs; seg++) {
1294 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07001296 desc.written = 0;
1297 desc.arg.buf = iov[seg].iov_base;
1298 desc.count = iov[seg].iov_len;
1299 if (desc.count == 0)
1300 continue;
1301 desc.error = 0;
1302 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1303 retval += desc.written;
1304 if (desc.error) {
1305 retval = retval ?: desc.error;
1306 break;
1307 }
1308 if (desc.count > 0)
1309 break;
1310 }
1311 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Hugh Dickins708e3502011-07-25 17:12:32 -07001314static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1315 struct pipe_inode_info *pipe, size_t len,
1316 unsigned int flags)
1317{
1318 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001319 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001320 unsigned int loff, nr_pages, req_pages;
1321 struct page *pages[PIPE_DEF_BUFFERS];
1322 struct partial_page partial[PIPE_DEF_BUFFERS];
1323 struct page *page;
1324 pgoff_t index, end_index;
1325 loff_t isize, left;
1326 int error, page_nr;
1327 struct splice_pipe_desc spd = {
1328 .pages = pages,
1329 .partial = partial,
1330 .flags = flags,
1331 .ops = &page_cache_pipe_buf_ops,
1332 .spd_release = spd_release_page,
1333 };
1334
Hugh Dickins71f0e072011-07-25 17:12:33 -07001335 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001336 if (unlikely(*ppos >= isize))
1337 return 0;
1338
1339 left = isize - *ppos;
1340 if (unlikely(left < len))
1341 len = left;
1342
1343 if (splice_grow_spd(pipe, &spd))
1344 return -ENOMEM;
1345
1346 index = *ppos >> PAGE_CACHE_SHIFT;
1347 loff = *ppos & ~PAGE_CACHE_MASK;
1348 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1349 nr_pages = min(req_pages, pipe->buffers);
1350
Hugh Dickins708e3502011-07-25 17:12:32 -07001351 spd.nr_pages = find_get_pages_contig(mapping, index,
1352 nr_pages, spd.pages);
1353 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001354 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001355
Hugh Dickins708e3502011-07-25 17:12:32 -07001356 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001357 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1358 if (error)
1359 break;
1360 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001361 spd.pages[spd.nr_pages++] = page;
1362 index++;
1363 }
1364
Hugh Dickins708e3502011-07-25 17:12:32 -07001365 index = *ppos >> PAGE_CACHE_SHIFT;
1366 nr_pages = spd.nr_pages;
1367 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001368
Hugh Dickins708e3502011-07-25 17:12:32 -07001369 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1370 unsigned int this_len;
1371
1372 if (!len)
1373 break;
1374
Hugh Dickins708e3502011-07-25 17:12:32 -07001375 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1376 page = spd.pages[page_nr];
1377
Hugh Dickins71f0e072011-07-25 17:12:33 -07001378 if (!PageUptodate(page) || page->mapping != mapping) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001379 error = shmem_getpage(inode, index, &page,
1380 SGP_CACHE, NULL);
1381 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001382 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001383 unlock_page(page);
1384 page_cache_release(spd.pages[page_nr]);
1385 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001386 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001387
1388 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001389 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1390 if (unlikely(!isize || index > end_index))
1391 break;
1392
Hugh Dickins708e3502011-07-25 17:12:32 -07001393 if (end_index == index) {
1394 unsigned int plen;
1395
Hugh Dickins708e3502011-07-25 17:12:32 -07001396 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1397 if (plen <= loff)
1398 break;
1399
Hugh Dickins708e3502011-07-25 17:12:32 -07001400 this_len = min(this_len, plen - loff);
1401 len = this_len;
1402 }
1403
1404 spd.partial[page_nr].offset = loff;
1405 spd.partial[page_nr].len = this_len;
1406 len -= this_len;
1407 loff = 0;
1408 spd.nr_pages++;
1409 index++;
1410 }
1411
Hugh Dickins708e3502011-07-25 17:12:32 -07001412 while (page_nr < nr_pages)
1413 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001414
1415 if (spd.nr_pages)
1416 error = splice_to_pipe(pipe, &spd);
1417
1418 splice_shrink_spd(pipe, &spd);
1419
1420 if (error > 0) {
1421 *ppos += error;
1422 file_accessed(in);
1423 }
1424 return error;
1425}
1426
David Howells726c3342006-06-23 02:02:58 -07001427static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
David Howells726c3342006-06-23 02:02:58 -07001429 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 buf->f_type = TMPFS_MAGIC;
1432 buf->f_bsize = PAGE_CACHE_SIZE;
1433 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001434 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001436 buf->f_bavail =
1437 buf->f_bfree = sbinfo->max_blocks -
1438 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001439 }
1440 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 buf->f_files = sbinfo->max_inodes;
1442 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444 /* else leave those fields 0 like simple_statfs */
1445 return 0;
1446}
1447
1448/*
1449 * File creation. Allocate an inode, and we're done..
1450 */
1451static int
Al Viro1a67aaf2011-07-26 01:52:52 -04001452shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001454 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int error = -ENOSPC;
1456
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001457 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001459 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001460 &dentry->d_name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001461 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001462 if (error) {
1463 if (error != -EOPNOTSUPP) {
1464 iput(inode);
1465 return error;
1466 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001467 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001468#ifdef CONFIG_TMPFS_POSIX_ACL
1469 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001470 if (error) {
1471 iput(inode);
1472 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001473 }
Al Viro718deb62009-12-16 19:35:36 -05001474#else
1475 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01001476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 dir->i_size += BOGO_DIRENT_SIZE;
1478 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1479 d_instantiate(dentry, inode);
1480 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 return error;
1483}
1484
Al Viro18bb1db2011-07-26 01:41:39 -04001485static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 int error;
1488
1489 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1490 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07001491 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493}
1494
Al Viro4acdaf22011-07-26 01:42:34 -04001495static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct nameidata *nd)
1497{
1498 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1499}
1500
1501/*
1502 * Link a file..
1503 */
1504static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1505{
1506 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001507 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 /*
1510 * No ordinary (disk based) filesystem counts links as inodes;
1511 * but each new link needs a new dentry, pinning lowmem, and
1512 * tmpfs dentries cannot be pruned until they are unlinked.
1513 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001514 ret = shmem_reserve_inode(inode->i_sb);
1515 if (ret)
1516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 dir->i_size += BOGO_DIRENT_SIZE;
1519 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07001520 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04001521 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dget(dentry); /* Extra pinning count for the created dentry */
1523 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001524out:
1525 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
1528static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1529{
1530 struct inode *inode = dentry->d_inode;
1531
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001532 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1533 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 dir->i_size -= BOGO_DIRENT_SIZE;
1536 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001537 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 dput(dentry); /* Undo the count from "create" - this does all the work */
1539 return 0;
1540}
1541
1542static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1543{
1544 if (!simple_empty(dentry))
1545 return -ENOTEMPTY;
1546
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001547 drop_nlink(dentry->d_inode);
1548 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return shmem_unlink(dir, dentry);
1550}
1551
1552/*
1553 * The VFS layer already does all the dentry stuff for rename,
1554 * we just have to decrement the usage count for the target if
1555 * it exists so that the VFS layer correctly free's it when it
1556 * gets overwritten.
1557 */
1558static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1559{
1560 struct inode *inode = old_dentry->d_inode;
1561 int they_are_dirs = S_ISDIR(inode->i_mode);
1562
1563 if (!simple_empty(new_dentry))
1564 return -ENOTEMPTY;
1565
1566 if (new_dentry->d_inode) {
1567 (void) shmem_unlink(new_dir, new_dentry);
1568 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001569 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001571 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07001572 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574
1575 old_dir->i_size -= BOGO_DIRENT_SIZE;
1576 new_dir->i_size += BOGO_DIRENT_SIZE;
1577 old_dir->i_ctime = old_dir->i_mtime =
1578 new_dir->i_ctime = new_dir->i_mtime =
1579 inode->i_ctime = CURRENT_TIME;
1580 return 0;
1581}
1582
1583static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1584{
1585 int error;
1586 int len;
1587 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07001588 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 char *kaddr;
1590 struct shmem_inode_info *info;
1591
1592 len = strlen(symname) + 1;
1593 if (len > PAGE_CACHE_SIZE)
1594 return -ENAMETOOLONG;
1595
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001596 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!inode)
1598 return -ENOSPC;
1599
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04001600 error = security_inode_init_security(inode, dir, &dentry->d_name,
Eric Paris2a7dba32011-02-01 11:05:39 -05001601 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07001602 if (error) {
1603 if (error != -EOPNOTSUPP) {
1604 iput(inode);
1605 return error;
1606 }
1607 error = 0;
1608 }
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 info = SHMEM_I(inode);
1611 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001612 if (len <= SHORT_SYMLINK_LEN) {
1613 info->symlink = kmemdup(symname, len, GFP_KERNEL);
1614 if (!info->symlink) {
1615 iput(inode);
1616 return -ENOMEM;
1617 }
1618 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else {
1620 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1621 if (error) {
1622 iput(inode);
1623 return error;
1624 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07001625 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 inode->i_op = &shmem_symlink_inode_operations;
1627 kaddr = kmap_atomic(page, KM_USER0);
1628 memcpy(kaddr, symname, len);
1629 kunmap_atomic(kaddr, KM_USER0);
1630 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001631 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 page_cache_release(page);
1633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 dir->i_size += BOGO_DIRENT_SIZE;
1635 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1636 d_instantiate(dentry, inode);
1637 dget(dentry);
1638 return 0;
1639}
1640
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001641static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001643 nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001644 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001647static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001650 int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1651 nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08001652 if (page)
1653 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001654 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001657static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07001660 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 kunmap(page);
1662 mark_page_accessed(page);
1663 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665}
1666
Eric Parisb09e0fa2011-05-24 17:12:39 -07001667#ifdef CONFIG_TMPFS_XATTR
1668/*
1669 * Superblocks without xattr inode operations may get some security.* xattr
1670 * support from the LSM "for free". As soon as we have any other xattrs
1671 * like ACLs, we also need to implement the security.* handlers at
1672 * filesystem level, though.
1673 */
1674
1675static int shmem_xattr_get(struct dentry *dentry, const char *name,
1676 void *buffer, size_t size)
1677{
1678 struct shmem_inode_info *info;
1679 struct shmem_xattr *xattr;
1680 int ret = -ENODATA;
1681
1682 info = SHMEM_I(dentry->d_inode);
1683
1684 spin_lock(&info->lock);
1685 list_for_each_entry(xattr, &info->xattr_list, list) {
1686 if (strcmp(name, xattr->name))
1687 continue;
1688
1689 ret = xattr->size;
1690 if (buffer) {
1691 if (size < xattr->size)
1692 ret = -ERANGE;
1693 else
1694 memcpy(buffer, xattr->value, xattr->size);
1695 }
1696 break;
1697 }
1698 spin_unlock(&info->lock);
1699 return ret;
1700}
1701
1702static int shmem_xattr_set(struct dentry *dentry, const char *name,
1703 const void *value, size_t size, int flags)
1704{
1705 struct inode *inode = dentry->d_inode;
1706 struct shmem_inode_info *info = SHMEM_I(inode);
1707 struct shmem_xattr *xattr;
1708 struct shmem_xattr *new_xattr = NULL;
1709 size_t len;
1710 int err = 0;
1711
1712 /* value == NULL means remove */
1713 if (value) {
1714 /* wrap around? */
1715 len = sizeof(*new_xattr) + size;
1716 if (len <= sizeof(*new_xattr))
1717 return -ENOMEM;
1718
1719 new_xattr = kmalloc(len, GFP_KERNEL);
1720 if (!new_xattr)
1721 return -ENOMEM;
1722
1723 new_xattr->name = kstrdup(name, GFP_KERNEL);
1724 if (!new_xattr->name) {
1725 kfree(new_xattr);
1726 return -ENOMEM;
1727 }
1728
1729 new_xattr->size = size;
1730 memcpy(new_xattr->value, value, size);
1731 }
1732
1733 spin_lock(&info->lock);
1734 list_for_each_entry(xattr, &info->xattr_list, list) {
1735 if (!strcmp(name, xattr->name)) {
1736 if (flags & XATTR_CREATE) {
1737 xattr = new_xattr;
1738 err = -EEXIST;
1739 } else if (new_xattr) {
1740 list_replace(&xattr->list, &new_xattr->list);
1741 } else {
1742 list_del(&xattr->list);
1743 }
1744 goto out;
1745 }
1746 }
1747 if (flags & XATTR_REPLACE) {
1748 xattr = new_xattr;
1749 err = -ENODATA;
1750 } else {
1751 list_add(&new_xattr->list, &info->xattr_list);
1752 xattr = NULL;
1753 }
1754out:
1755 spin_unlock(&info->lock);
1756 if (xattr)
1757 kfree(xattr->name);
1758 kfree(xattr);
1759 return err;
1760}
1761
Eric Parisb09e0fa2011-05-24 17:12:39 -07001762static const struct xattr_handler *shmem_xattr_handlers[] = {
1763#ifdef CONFIG_TMPFS_POSIX_ACL
1764 &generic_acl_access_handler,
1765 &generic_acl_default_handler,
1766#endif
1767 NULL
1768};
1769
1770static int shmem_xattr_validate(const char *name)
1771{
1772 struct { const char *prefix; size_t len; } arr[] = {
1773 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
1774 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
1775 };
1776 int i;
1777
1778 for (i = 0; i < ARRAY_SIZE(arr); i++) {
1779 size_t preflen = arr[i].len;
1780 if (strncmp(name, arr[i].prefix, preflen) == 0) {
1781 if (!name[preflen])
1782 return -EINVAL;
1783 return 0;
1784 }
1785 }
1786 return -EOPNOTSUPP;
1787}
1788
1789static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
1790 void *buffer, size_t size)
1791{
1792 int err;
1793
1794 /*
1795 * If this is a request for a synthetic attribute in the system.*
1796 * namespace use the generic infrastructure to resolve a handler
1797 * for it via sb->s_xattr.
1798 */
1799 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1800 return generic_getxattr(dentry, name, buffer, size);
1801
1802 err = shmem_xattr_validate(name);
1803 if (err)
1804 return err;
1805
1806 return shmem_xattr_get(dentry, name, buffer, size);
1807}
1808
1809static int shmem_setxattr(struct dentry *dentry, const char *name,
1810 const void *value, size_t size, int flags)
1811{
1812 int err;
1813
1814 /*
1815 * If this is a request for a synthetic attribute in the system.*
1816 * namespace use the generic infrastructure to resolve a handler
1817 * for it via sb->s_xattr.
1818 */
1819 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1820 return generic_setxattr(dentry, name, value, size, flags);
1821
1822 err = shmem_xattr_validate(name);
1823 if (err)
1824 return err;
1825
1826 if (size == 0)
1827 value = ""; /* empty EA, do not remove */
1828
1829 return shmem_xattr_set(dentry, name, value, size, flags);
1830
1831}
1832
1833static int shmem_removexattr(struct dentry *dentry, const char *name)
1834{
1835 int err;
1836
1837 /*
1838 * If this is a request for a synthetic attribute in the system.*
1839 * namespace use the generic infrastructure to resolve a handler
1840 * for it via sb->s_xattr.
1841 */
1842 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
1843 return generic_removexattr(dentry, name);
1844
1845 err = shmem_xattr_validate(name);
1846 if (err)
1847 return err;
1848
1849 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
1850}
1851
1852static bool xattr_is_trusted(const char *name)
1853{
1854 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1855}
1856
1857static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
1858{
1859 bool trusted = capable(CAP_SYS_ADMIN);
1860 struct shmem_xattr *xattr;
1861 struct shmem_inode_info *info;
1862 size_t used = 0;
1863
1864 info = SHMEM_I(dentry->d_inode);
1865
1866 spin_lock(&info->lock);
1867 list_for_each_entry(xattr, &info->xattr_list, list) {
1868 size_t len;
1869
1870 /* skip "trusted." attributes for unprivileged callers */
1871 if (!trusted && xattr_is_trusted(xattr->name))
1872 continue;
1873
1874 len = strlen(xattr->name) + 1;
1875 used += len;
1876 if (buffer) {
1877 if (size < used) {
1878 used = -ERANGE;
1879 break;
1880 }
1881 memcpy(buffer, xattr->name, len);
1882 buffer += len;
1883 }
1884 }
1885 spin_unlock(&info->lock);
1886
1887 return used;
1888}
1889#endif /* CONFIG_TMPFS_XATTR */
1890
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001891static const struct inode_operations shmem_short_symlink_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .readlink = generic_readlink,
Hugh Dickins69f07ec2011-08-03 16:21:26 -07001893 .follow_link = shmem_follow_short_symlink,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001894#ifdef CONFIG_TMPFS_XATTR
1895 .setxattr = shmem_setxattr,
1896 .getxattr = shmem_getxattr,
1897 .listxattr = shmem_listxattr,
1898 .removexattr = shmem_removexattr,
1899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900};
1901
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001902static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 .readlink = generic_readlink,
1904 .follow_link = shmem_follow_link,
1905 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07001906#ifdef CONFIG_TMPFS_XATTR
1907 .setxattr = shmem_setxattr,
1908 .getxattr = shmem_getxattr,
1909 .listxattr = shmem_listxattr,
1910 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001911#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07001912};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001913
David M. Grimes91828a42006-10-17 00:09:45 -07001914static struct dentry *shmem_get_parent(struct dentry *child)
1915{
1916 return ERR_PTR(-ESTALE);
1917}
1918
1919static int shmem_match(struct inode *ino, void *vfh)
1920{
1921 __u32 *fh = vfh;
1922 __u64 inum = fh[2];
1923 inum = (inum << 32) | fh[1];
1924 return ino->i_ino == inum && fh[0] == ino->i_generation;
1925}
1926
Christoph Hellwig480b1162007-10-21 16:42:13 -07001927static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
1928 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07001929{
David M. Grimes91828a42006-10-17 00:09:45 -07001930 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07001931 struct dentry *dentry = NULL;
1932 u64 inum = fid->raw[2];
1933 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07001934
Christoph Hellwig480b1162007-10-21 16:42:13 -07001935 if (fh_len < 3)
1936 return NULL;
1937
1938 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
1939 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07001940 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07001941 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07001942 iput(inode);
1943 }
1944
Christoph Hellwig480b1162007-10-21 16:42:13 -07001945 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07001946}
1947
1948static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
1949 int connectable)
1950{
1951 struct inode *inode = dentry->d_inode;
1952
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301953 if (*len < 3) {
1954 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07001955 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05301956 }
David M. Grimes91828a42006-10-17 00:09:45 -07001957
Al Viro1d3382cb2010-10-23 15:19:20 -04001958 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07001959 /* Unfortunately insert_inode_hash is not idempotent,
1960 * so as we hash inodes here rather than at creation
1961 * time, we need a lock to ensure we only try
1962 * to do it once
1963 */
1964 static DEFINE_SPINLOCK(lock);
1965 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04001966 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07001967 __insert_inode_hash(inode,
1968 inode->i_ino + inode->i_generation);
1969 spin_unlock(&lock);
1970 }
1971
1972 fh[0] = inode->i_generation;
1973 fh[1] = inode->i_ino;
1974 fh[2] = ((__u64)inode->i_ino) >> 32;
1975
1976 *len = 3;
1977 return 1;
1978}
1979
Christoph Hellwig39655162007-10-21 16:42:17 -07001980static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07001981 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07001982 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07001983 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07001984};
1985
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001986static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
1987 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 char *this_char, *value, *rest;
1990
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00001991 while (options != NULL) {
1992 this_char = options;
1993 for (;;) {
1994 /*
1995 * NUL-terminate this option: unfortunately,
1996 * mount options form a comma-separated list,
1997 * but mpol's nodelist may also contain commas.
1998 */
1999 options = strchr(options, ',');
2000 if (options == NULL)
2001 break;
2002 options++;
2003 if (!isdigit(*options)) {
2004 options[-1] = '\0';
2005 break;
2006 }
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (!*this_char)
2009 continue;
2010 if ((value = strchr(this_char,'=')) != NULL) {
2011 *value++ = 0;
2012 } else {
2013 printk(KERN_ERR
2014 "tmpfs: No value for mount option '%s'\n",
2015 this_char);
2016 return 1;
2017 }
2018
2019 if (!strcmp(this_char,"size")) {
2020 unsigned long long size;
2021 size = memparse(value,&rest);
2022 if (*rest == '%') {
2023 size <<= PAGE_SHIFT;
2024 size *= totalram_pages;
2025 do_div(size, 100);
2026 rest++;
2027 }
2028 if (*rest)
2029 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002030 sbinfo->max_blocks =
2031 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002033 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (*rest)
2035 goto bad_val;
2036 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002037 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (*rest)
2039 goto bad_val;
2040 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002041 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002043 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (*rest)
2045 goto bad_val;
2046 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002047 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002049 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (*rest)
2051 goto bad_val;
2052 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002053 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002055 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (*rest)
2057 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002058 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002059 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002060 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 } else {
2062 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2063 this_char);
2064 return 1;
2065 }
2066 }
2067 return 0;
2068
2069bad_val:
2070 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2071 value, this_char);
2072 return 1;
2073
2074}
2075
2076static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2077{
2078 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002079 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002080 unsigned long inodes;
2081 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002083 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002084 return error;
2085
2086 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002087 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002088 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002089 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002090 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002091 goto out;
2092 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002093 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002094 * but we must separately disallow unlimited->limited, because
2095 * in that case we have no record of how much is already in use.
2096 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002097 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002098 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002099 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002100 goto out;
2101
2102 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002103 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002104 sbinfo->max_inodes = config.max_inodes;
2105 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002106
2107 mpol_put(sbinfo->mpol);
2108 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002109out:
2110 spin_unlock(&sbinfo->stat_lock);
2111 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002113
Al Viro34c80b12011-12-08 21:32:45 -05002114static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002115{
Al Viro34c80b12011-12-08 21:32:45 -05002116 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002117
2118 if (sbinfo->max_blocks != shmem_default_max_blocks())
2119 seq_printf(seq, ",size=%luk",
2120 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2121 if (sbinfo->max_inodes != shmem_default_max_inodes())
2122 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2123 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04002124 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002125 if (sbinfo->uid != 0)
2126 seq_printf(seq, ",uid=%u", sbinfo->uid);
2127 if (sbinfo->gid != 0)
2128 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002129 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002130 return 0;
2131}
2132#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134static void shmem_put_super(struct super_block *sb)
2135{
Hugh Dickins602586a2010-08-17 15:23:56 -07002136 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2137
2138 percpu_counter_destroy(&sbinfo->used_blocks);
2139 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 sb->s_fs_info = NULL;
2141}
2142
Kay Sievers2b2af542009-04-30 15:23:42 +02002143int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct inode *inode;
2146 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002147 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002148 int err = -ENOMEM;
2149
2150 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002151 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002152 L1_CACHE_BYTES), GFP_KERNEL);
2153 if (!sbinfo)
2154 return -ENOMEM;
2155
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002156 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002157 sbinfo->uid = current_fsuid();
2158 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002159 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002161#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /*
2163 * Per default we only allow half of the physical ram per
2164 * tmpfs instance, limiting inodes to one per page of lowmem;
2165 * but the internal instance is left unlimited.
2166 */
2167 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002168 sbinfo->max_blocks = shmem_default_max_blocks();
2169 sbinfo->max_inodes = shmem_default_max_inodes();
2170 if (shmem_parse_options(data, sbinfo, false)) {
2171 err = -EINVAL;
2172 goto failed;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
David M. Grimes91828a42006-10-17 00:09:45 -07002175 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176#else
2177 sb->s_flags |= MS_NOUSER;
2178#endif
2179
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002180 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002181 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2182 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002183 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002184
Hugh Dickins285b2c42011-08-03 16:21:20 -07002185 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 sb->s_blocksize = PAGE_CACHE_SIZE;
2187 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2188 sb->s_magic = TMPFS_MAGIC;
2189 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002190 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002191#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002192 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002193#endif
2194#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002195 sb->s_flags |= MS_POSIXACL;
2196#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002197
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002198 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!inode)
2200 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002201 inode->i_uid = sbinfo->uid;
2202 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 root = d_alloc_root(inode);
2204 if (!root)
2205 goto failed_iput;
2206 sb->s_root = root;
2207 return 0;
2208
2209failed_iput:
2210 iput(inode);
2211failed:
2212 shmem_put_super(sb);
2213 return err;
2214}
2215
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002216static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218static struct inode *shmem_alloc_inode(struct super_block *sb)
2219{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002220 struct shmem_inode_info *info;
2221 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2222 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002224 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002227static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002228{
2229 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002230 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233static void shmem_destroy_inode(struct inode *inode)
2234{
Al Viro09208d12011-07-26 03:15:03 -04002235 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002237 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002240static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002242 struct shmem_inode_info *info = foo;
2243 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002246static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2249 sizeof(struct shmem_inode_info),
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002250 0, SLAB_PANIC, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
2252}
2253
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002254static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002256 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002259static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002261 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07002263 .write_begin = shmem_write_begin,
2264 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002266 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002267 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268};
2269
Helge Deller15ad7cd2006-12-06 20:40:36 -08002270static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .mmap = shmem_mmap,
2272#ifdef CONFIG_TMPFS
2273 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07002274 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002275 .write = do_sync_write,
Hugh Dickinsbcd78e492008-07-23 21:27:35 -07002276 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002277 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002278 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002279 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002280 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281#endif
2282};
2283
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002284static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002285 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002286 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002287#ifdef CONFIG_TMPFS_XATTR
2288 .setxattr = shmem_setxattr,
2289 .getxattr = shmem_getxattr,
2290 .listxattr = shmem_listxattr,
2291 .removexattr = shmem_removexattr,
2292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293};
2294
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002295static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296#ifdef CONFIG_TMPFS
2297 .create = shmem_create,
2298 .lookup = simple_lookup,
2299 .link = shmem_link,
2300 .unlink = shmem_unlink,
2301 .symlink = shmem_symlink,
2302 .mkdir = shmem_mkdir,
2303 .rmdir = shmem_rmdir,
2304 .mknod = shmem_mknod,
2305 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002307#ifdef CONFIG_TMPFS_XATTR
2308 .setxattr = shmem_setxattr,
2309 .getxattr = shmem_getxattr,
2310 .listxattr = shmem_listxattr,
2311 .removexattr = shmem_removexattr,
2312#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002313#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002314 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002315#endif
2316};
2317
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002318static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002319#ifdef CONFIG_TMPFS_XATTR
2320 .setxattr = shmem_setxattr,
2321 .getxattr = shmem_getxattr,
2322 .listxattr = shmem_listxattr,
2323 .removexattr = shmem_removexattr,
2324#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002325#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002326 .setattr = shmem_setattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328};
2329
Hugh Dickins759b9772007-03-05 00:30:28 -08002330static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 .alloc_inode = shmem_alloc_inode,
2332 .destroy_inode = shmem_destroy_inode,
2333#ifdef CONFIG_TMPFS
2334 .statfs = shmem_statfs,
2335 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002336 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#endif
Al Viro1f895f72010-06-05 19:10:41 -04002338 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 .drop_inode = generic_delete_inode,
2340 .put_super = shmem_put_super,
2341};
2342
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002343static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002344 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345#ifdef CONFIG_NUMA
2346 .set_policy = shmem_set_policy,
2347 .get_policy = shmem_get_policy,
2348#endif
2349};
2350
Al Viro3c26ff62010-07-25 11:46:36 +04002351static struct dentry *shmem_mount(struct file_system_type *fs_type,
2352 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Al Viro3c26ff62010-07-25 11:46:36 +04002354 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002357static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 .owner = THIS_MODULE,
2359 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002360 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .kill_sb = kill_litter_super,
2362};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002364int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 int error;
2367
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002368 error = bdi_init(&shmem_backing_dev_info);
2369 if (error)
2370 goto out4;
2371
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002372 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (error)
2374 goto out3;
2375
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002376 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (error) {
2378 printk(KERN_ERR "Could not register tmpfs\n");
2379 goto out2;
2380 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002381
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002382 shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
2383 shmem_fs_type.name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (IS_ERR(shm_mnt)) {
2385 error = PTR_ERR(shm_mnt);
2386 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2387 goto out1;
2388 }
2389 return 0;
2390
2391out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002392 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002394 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002396 bdi_destroy(&shmem_backing_dev_info);
2397out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 shm_mnt = ERR_PTR(error);
2399 return error;
2400}
Matt Mackall853ac432009-01-06 14:40:20 -08002401
2402#else /* !CONFIG_SHMEM */
2403
2404/*
2405 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2406 *
2407 * This is intended for small system where the benefits of the full
2408 * shmem code (swap-backed and resource-limited) are outweighed by
2409 * their complexity. On systems without swap this code should be
2410 * effectively equivalent, but much lighter weight.
2411 */
2412
2413#include <linux/ramfs.h>
2414
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002415static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08002416 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002417 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08002418 .kill_sb = kill_litter_super,
2419};
2420
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002421int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08002422{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002423 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08002424
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002425 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08002426 BUG_ON(IS_ERR(shm_mnt));
2427
2428 return 0;
2429}
2430
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002431int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08002432{
2433 return 0;
2434}
2435
Hugh Dickins3f96b792009-09-21 17:03:37 -07002436int shmem_lock(struct file *file, int lock, struct user_struct *user)
2437{
2438 return 0;
2439}
2440
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002441void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07002442{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002443 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07002444}
2445EXPORT_SYMBOL_GPL(shmem_truncate_range);
2446
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002447#define shmem_vm_ops generic_file_vm_ops
2448#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002449#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002450#define shmem_acct_size(flags, size) 0
2451#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08002452
2453#endif /* CONFIG_SHMEM */
2454
2455/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Randy Dunlap46711812008-03-19 17:00:41 -07002457/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * @name: name for dentry (to be seen in /proc/<pid>/maps
2460 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002461 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07002463struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 int error;
2466 struct file *file;
2467 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04002468 struct path path;
2469 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct qstr this;
2471
2472 if (IS_ERR(shm_mnt))
2473 return (void *)shm_mnt;
2474
Hugh Dickins285b2c42011-08-03 16:21:20 -07002475 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 return ERR_PTR(-EINVAL);
2477
2478 if (shmem_acct_size(flags, size))
2479 return ERR_PTR(-ENOMEM);
2480
2481 error = -ENOMEM;
2482 this.name = name;
2483 this.len = strlen(name);
2484 this.hash = 0; /* will go */
2485 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04002486 path.dentry = d_alloc(root, &this);
2487 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04002489 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002492 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04002494 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Al Viro2c48b9c2009-08-09 00:52:35 +04002496 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02002498 clear_nlink(inode); /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08002499#ifndef CONFIG_MMU
2500 error = ramfs_nommu_expand_for_mapping(inode, size);
2501 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04002502 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08002503#endif
Al Viro4b42af82009-08-05 18:25:56 +04002504
2505 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04002506 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04002507 &shmem_file_operations);
2508 if (!file)
2509 goto put_dentry;
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return file;
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04002514 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515put_memory:
2516 shmem_unacct_size(flags, size);
2517 return ERR_PTR(error);
2518}
Keith Packard395e0dd2008-06-20 00:08:06 -07002519EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Randy Dunlap46711812008-03-19 17:00:41 -07002521/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2524 */
2525int shmem_zero_setup(struct vm_area_struct *vma)
2526{
2527 struct file *file;
2528 loff_t size = vma->vm_end - vma->vm_start;
2529
2530 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2531 if (IS_ERR(file))
2532 return PTR_ERR(file);
2533
2534 if (vma->vm_file)
2535 fput(vma->vm_file);
2536 vma->vm_file = file;
2537 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07002538 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return 0;
2540}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002541
2542/**
2543 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
2544 * @mapping: the page's address_space
2545 * @index: the page index
2546 * @gfp: the page allocator flags to use if allocating
2547 *
2548 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
2549 * with any new page allocations done using the specified allocation flags.
2550 * But read_cache_page_gfp() uses the ->readpage() method: which does not
2551 * suit tmpfs, since it may have pages in swapcache, and needs to find those
2552 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
2553 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07002554 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
2555 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002556 */
2557struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
2558 pgoff_t index, gfp_t gfp)
2559{
Hugh Dickins68da9f02011-07-25 17:12:34 -07002560#ifdef CONFIG_SHMEM
2561 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07002562 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07002563 int error;
2564
2565 BUG_ON(mapping->a_ops != &shmem_aops);
2566 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
2567 if (error)
2568 page = ERR_PTR(error);
2569 else
2570 unlock_page(page);
2571 return page;
2572#else
2573 /*
2574 * The tiny !SHMEM case uses ramfs without swap
2575 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002576 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002577#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07002578}
2579EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);