blob: f6c94ba87808794eef9bae656659c66ab542c066 [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 Dickins0edd73b2005-06-21 17:15:04 -07009 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright (C) 2004 Andi Kleen, SuSE Labs
12 *
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
16 *
Matt Mackall853ac432009-01-06 14:40:20 -080017 * tiny-shmem:
18 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * This file is released under the GPL.
21 */
22
Matt Mackall853ac432009-01-06 14:40:20 -080023#include <linux/fs.h>
24#include <linux/init.h>
25#include <linux/vfs.h>
26#include <linux/mount.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070027#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080028#include <linux/file.h>
29#include <linux/mm.h>
30#include <linux/module.h>
Tim Chen7e496292010-08-09 17:19:05 -070031#include <linux/percpu_counter.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 Dickins708e3502011-07-25 17:12:32 -070054#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/security.h>
56#include <linux/swapops.h>
57#include <linux/mempolicy.h>
58#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000059#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070060#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070061#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080062#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040063#include <linux/magic.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <asm/uaccess.h>
66#include <asm/div64.h>
67#include <asm/pgtable.h>
68
Hugh Dickinscaefba12009-04-13 14:40:12 -070069/*
70 * The maximum size of a shmem/tmpfs file is limited by the maximum size of
71 * its triple-indirect swap vector - see illustration at shmem_swp_entry().
72 *
73 * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
74 * but one eighth of that on a 64-bit kernel. With 8kB page size, maximum
75 * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
76 * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
77 *
78 * We use / and * instead of shifts in the definitions below, so that the swap
79 * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
Yuri Tikhonov61609d02009-04-13 14:40:11 -070082#define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
Hugh Dickinscaefba12009-04-13 14:40:12 -070083
84#define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
85#define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)
86
87#define SHMEM_MAX_BYTES min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
88#define SHMEM_MAX_INDEX ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
92
93/* info->flags needs VM_flags to handle pagein/truncate races efficiently */
94#define SHMEM_PAGEIN VM_READ
95#define SHMEM_TRUNCATE VM_WRITE
96
97/* Definition to limit shmem_truncate's steps between cond_rescheds */
98#define LATENCY_LIMIT 64
99
100/* Pretend that each entry is of this size in directory's i_size */
101#define BOGO_DIRENT_SIZE 20
102
Eric Parisb09e0fa2011-05-24 17:12:39 -0700103struct shmem_xattr {
104 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
105 char *name; /* xattr name */
106 size_t size;
107 char value[0];
108};
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
111enum sgp_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 SGP_READ, /* don't exceed i_size, don't allocate page */
113 SGP_CACHE, /* don't exceed i_size, may allocate page */
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -0800114 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 SGP_WRITE, /* may exceed i_size, may allocate page */
116};
117
Andrew Mortonb76db7352008-02-08 04:21:49 -0800118#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800119static unsigned long shmem_default_max_blocks(void)
120{
121 return totalram_pages / 2;
122}
123
124static unsigned long shmem_default_max_inodes(void)
125{
126 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
127}
Andrew Mortonb76db7352008-02-08 04:21:49 -0800128#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800129
Hugh Dickins68da9f02011-07-25 17:12:34 -0700130static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
131 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
132
133static inline int shmem_getpage(struct inode *inode, pgoff_t index,
134 struct page **pagep, enum sgp_type sgp, int *fault_type)
135{
136 return shmem_getpage_gfp(inode, index, pagep, sgp,
137 mapping_gfp_mask(inode->i_mapping), fault_type);
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Al Viro6daa0e22005-10-21 03:18:50 -0400140static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 /*
143 * The above definition of ENTRIES_PER_PAGE, and the use of
144 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
145 * might be reconsidered if it ever diverges from PAGE_SIZE.
Mel Gorman769848c2007-07-17 04:03:05 -0700146 *
Mel Gormane12ba742007-10-16 01:25:52 -0700147 * Mobility flags are masked out as swap vectors cannot move
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 */
Mel Gormane12ba742007-10-16 01:25:52 -0700149 return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
Mel Gorman769848c2007-07-17 04:03:05 -0700150 PAGE_CACHE_SHIFT-PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void shmem_dir_free(struct page *page)
154{
155 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
156}
157
158static struct page **shmem_dir_map(struct page *page)
159{
160 return (struct page **)kmap_atomic(page, KM_USER0);
161}
162
163static inline void shmem_dir_unmap(struct page **dir)
164{
165 kunmap_atomic(dir, KM_USER0);
166}
167
168static swp_entry_t *shmem_swp_map(struct page *page)
169{
170 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
171}
172
173static inline void shmem_swp_balance_unmap(void)
174{
175 /*
176 * When passing a pointer to an i_direct entry, to code which
177 * also handles indirect entries and so will shmem_swp_unmap,
178 * we must arrange for the preempt count to remain in balance.
179 * What kmap_atomic of a lowmem page does depends on config
180 * and architecture, so pretend to kmap_atomic some lowmem page.
181 */
182 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
183}
184
185static inline void shmem_swp_unmap(swp_entry_t *entry)
186{
187 kunmap_atomic(entry, KM_USER1);
188}
189
190static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
191{
192 return sb->s_fs_info;
193}
194
195/*
196 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
197 * for shared memory and for shared anonymous (/dev/zero) mappings
198 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
199 * consistent with the pre-accounting of private mappings ...
200 */
201static inline int shmem_acct_size(unsigned long flags, loff_t size)
202{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000203 return (flags & VM_NORESERVE) ?
204 0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static inline void shmem_unacct_size(unsigned long flags, loff_t size)
208{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000209 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 vm_unacct_memory(VM_ACCT(size));
211}
212
213/*
214 * ... whereas tmpfs objects are accounted incrementally as
215 * pages are allocated, in order to allow huge sparse files.
216 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
217 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
218 */
219static inline int shmem_acct_block(unsigned long flags)
220{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000221 return (flags & VM_NORESERVE) ?
222 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static inline void shmem_unacct_blocks(unsigned long flags, long pages)
226{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000227 if (flags & VM_NORESERVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
229}
230
Hugh Dickins759b9772007-03-05 00:30:28 -0800231static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700232static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800233static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800234static const struct inode_operations shmem_inode_operations;
235static const struct inode_operations shmem_dir_inode_operations;
236static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400237static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -0700239static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 .ra_pages = 0, /* No readahead */
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700241 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
244static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800245static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247static void shmem_free_blocks(struct inode *inode, long pages)
248{
249 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700250 if (sbinfo->max_blocks) {
Tim Chen7e496292010-08-09 17:19:05 -0700251 percpu_counter_add(&sbinfo->used_blocks, -pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800256static int shmem_reserve_inode(struct super_block *sb)
257{
258 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
259 if (sbinfo->max_inodes) {
260 spin_lock(&sbinfo->stat_lock);
261 if (!sbinfo->free_inodes) {
262 spin_unlock(&sbinfo->stat_lock);
263 return -ENOSPC;
264 }
265 sbinfo->free_inodes--;
266 spin_unlock(&sbinfo->stat_lock);
267 }
268 return 0;
269}
270
271static void shmem_free_inode(struct super_block *sb)
272{
273 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
274 if (sbinfo->max_inodes) {
275 spin_lock(&sbinfo->stat_lock);
276 sbinfo->free_inodes++;
277 spin_unlock(&sbinfo->stat_lock);
278 }
279}
280
Randy Dunlap46711812008-03-19 17:00:41 -0700281/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * shmem_recalc_inode - recalculate the size of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * @inode: inode to recalc
284 *
285 * We have to calculate the free blocks since the mm can drop
286 * undirtied hole pages behind our back.
287 *
288 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
289 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
290 *
291 * It has to be called with the spinlock held.
292 */
293static void shmem_recalc_inode(struct inode *inode)
294{
295 struct shmem_inode_info *info = SHMEM_I(inode);
296 long freed;
297
298 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
299 if (freed > 0) {
300 info->alloced -= freed;
301 shmem_unacct_blocks(info->flags, freed);
302 shmem_free_blocks(inode, freed);
303 }
304}
305
Randy Dunlap46711812008-03-19 17:00:41 -0700306/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * shmem_swp_entry - find the swap vector position in the info structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * @info: info structure for the inode
309 * @index: index of the page to find
310 * @page: optional page to add to the structure. Has to be preset to
311 * all zeros
312 *
313 * If there is no space allocated yet it will return NULL when
314 * page is NULL, else it will use the page for the needed block,
315 * setting it to NULL on return to indicate that it has been used.
316 *
317 * The swap vector is organized the following way:
318 *
319 * There are SHMEM_NR_DIRECT entries directly stored in the
320 * shmem_inode_info structure. So small files do not need an addional
321 * allocation.
322 *
323 * For pages with index > SHMEM_NR_DIRECT there is the pointer
324 * i_indirect which points to a page which holds in the first half
325 * doubly indirect blocks, in the second half triple indirect blocks:
326 *
327 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
328 * following layout (for SHMEM_NR_DIRECT == 16):
329 *
330 * i_indirect -> dir --> 16-19
331 * | +-> 20-23
332 * |
333 * +-->dir2 --> 24-27
334 * | +-> 28-31
335 * | +-> 32-35
336 * | +-> 36-39
337 * |
338 * +-->dir3 --> 40-43
339 * +-> 44-47
340 * +-> 48-51
341 * +-> 52-55
342 */
343static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
344{
345 unsigned long offset;
346 struct page **dir;
347 struct page *subdir;
348
349 if (index < SHMEM_NR_DIRECT) {
350 shmem_swp_balance_unmap();
351 return info->i_direct+index;
352 }
353 if (!info->i_indirect) {
354 if (page) {
355 info->i_indirect = *page;
356 *page = NULL;
357 }
358 return NULL; /* need another page */
359 }
360
361 index -= SHMEM_NR_DIRECT;
362 offset = index % ENTRIES_PER_PAGE;
363 index /= ENTRIES_PER_PAGE;
364 dir = shmem_dir_map(info->i_indirect);
365
366 if (index >= ENTRIES_PER_PAGE/2) {
367 index -= ENTRIES_PER_PAGE/2;
368 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
369 index %= ENTRIES_PER_PAGE;
370 subdir = *dir;
371 if (!subdir) {
372 if (page) {
373 *dir = *page;
374 *page = NULL;
375 }
376 shmem_dir_unmap(dir);
377 return NULL; /* need another page */
378 }
379 shmem_dir_unmap(dir);
380 dir = shmem_dir_map(subdir);
381 }
382
383 dir += index;
384 subdir = *dir;
385 if (!subdir) {
386 if (!page || !(subdir = *page)) {
387 shmem_dir_unmap(dir);
388 return NULL; /* need a page */
389 }
390 *dir = subdir;
391 *page = NULL;
392 }
393 shmem_dir_unmap(dir);
394 return shmem_swp_map(subdir) + offset;
395}
396
397static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
398{
399 long incdec = value? 1: -1;
400
401 entry->val = value;
402 info->swapped += incdec;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700403 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
404 struct page *page = kmap_atomic_to_page(entry);
405 set_page_private(page, page_private(page) + incdec);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Randy Dunlap46711812008-03-19 17:00:41 -0700409/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * shmem_swp_alloc - get the position of the swap entry for the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 * @info: info structure for the inode
412 * @index: index of the page to find
413 * @sgp: check and recheck i_size? skip allocation?
Hugh Dickins68da9f02011-07-25 17:12:34 -0700414 * @gfp: gfp mask to use for any page allocation
Randy Dunlap46711812008-03-19 17:00:41 -0700415 *
416 * If the entry does not exist, allocate it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Hugh Dickins68da9f02011-07-25 17:12:34 -0700418static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info,
419 unsigned long index, enum sgp_type sgp, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct inode *inode = &info->vfs_inode;
422 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
423 struct page *page = NULL;
424 swp_entry_t *entry;
425
426 if (sgp != SGP_WRITE &&
427 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
428 return ERR_PTR(-EINVAL);
429
430 while (!(entry = shmem_swp_entry(info, index, &page))) {
431 if (sgp == SGP_READ)
432 return shmem_swp_map(ZERO_PAGE(0));
433 /*
Tim Chen7e496292010-08-09 17:19:05 -0700434 * Test used_blocks against 1 less max_blocks, since we have 1 data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * page (and perhaps indirect index pages) yet to allocate:
436 * a waste to allocate index if we cannot allocate data.
437 */
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700438 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -0700439 if (percpu_counter_compare(&sbinfo->used_blocks,
440 sbinfo->max_blocks - 1) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return ERR_PTR(-ENOSPC);
Tim Chen7e496292010-08-09 17:19:05 -0700442 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 inode->i_blocks += BLOCKS_PER_PAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 spin_unlock(&info->lock);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700447 page = shmem_dir_alloc(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_lock(&info->lock);
449
450 if (!page) {
451 shmem_free_blocks(inode, 1);
452 return ERR_PTR(-ENOMEM);
453 }
454 if (sgp != SGP_WRITE &&
455 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
456 entry = ERR_PTR(-EINVAL);
457 break;
458 }
459 if (info->next_index <= index)
460 info->next_index = index + 1;
461 }
462 if (page) {
463 /* another task gave its page, or truncated the file */
464 shmem_free_blocks(inode, 1);
465 shmem_dir_free(page);
466 }
467 if (info->next_index <= index && !IS_ERR(entry))
468 info->next_index = index + 1;
469 return entry;
470}
471
Randy Dunlap46711812008-03-19 17:00:41 -0700472/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * shmem_free_swp - free some swap entries in a directory
Hugh Dickins1ae70002007-03-29 01:20:36 -0700474 * @dir: pointer to the directory
475 * @edir: pointer after last entry of the directory
476 * @punch_lock: pointer to spinlock when needed for the holepunch case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Hugh Dickins1ae70002007-03-29 01:20:36 -0700478static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
479 spinlock_t *punch_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Hugh Dickins1ae70002007-03-29 01:20:36 -0700481 spinlock_t *punch_unlock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 swp_entry_t *ptr;
483 int freed = 0;
484
485 for (ptr = dir; ptr < edir; ptr++) {
486 if (ptr->val) {
Hugh Dickins1ae70002007-03-29 01:20:36 -0700487 if (unlikely(punch_lock)) {
488 punch_unlock = punch_lock;
489 punch_lock = NULL;
490 spin_lock(punch_unlock);
491 if (!ptr->val)
492 continue;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 free_swap_and_cache(*ptr);
495 *ptr = (swp_entry_t){0};
496 freed++;
497 }
498 }
Hugh Dickins1ae70002007-03-29 01:20:36 -0700499 if (punch_unlock)
500 spin_unlock(punch_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return freed;
502}
503
Hugh Dickins1ae70002007-03-29 01:20:36 -0700504static int shmem_map_and_free_swp(struct page *subdir, int offset,
505 int limit, struct page ***dir, spinlock_t *punch_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 swp_entry_t *ptr;
508 int freed = 0;
509
510 ptr = shmem_swp_map(subdir);
511 for (; offset < limit; offset += LATENCY_LIMIT) {
512 int size = limit - offset;
513 if (size > LATENCY_LIMIT)
514 size = LATENCY_LIMIT;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700515 freed += shmem_free_swp(ptr+offset, ptr+offset+size,
516 punch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (need_resched()) {
518 shmem_swp_unmap(ptr);
519 if (*dir) {
520 shmem_dir_unmap(*dir);
521 *dir = NULL;
522 }
523 cond_resched();
524 ptr = shmem_swp_map(subdir);
525 }
526 }
527 shmem_swp_unmap(ptr);
528 return freed;
529}
530
531static void shmem_free_pages(struct list_head *next)
532{
533 struct page *page;
534 int freed = 0;
535
536 do {
537 page = container_of(next, struct page, lru);
538 next = next->next;
539 shmem_dir_free(page);
540 freed++;
541 if (freed >= LATENCY_LIMIT) {
542 cond_resched();
543 freed = 0;
544 }
545 } while (next);
546}
547
Hugh Dickins94c1e622011-06-27 16:18:03 -0700548void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct shmem_inode_info *info = SHMEM_I(inode);
551 unsigned long idx;
552 unsigned long size;
553 unsigned long limit;
554 unsigned long stage;
555 unsigned long diroff;
556 struct page **dir;
557 struct page *topdir;
558 struct page *middir;
559 struct page *subdir;
560 swp_entry_t *ptr;
561 LIST_HEAD(pages_to_free);
562 long nr_pages_to_free = 0;
563 long nr_swaps_freed = 0;
564 int offset;
565 int freed;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700566 int punch_hole;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700567 spinlock_t *needs_lock;
568 spinlock_t *punch_lock;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700569 unsigned long upper_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Hugh Dickins94c1e622011-06-27 16:18:03 -0700571 truncate_inode_pages_range(inode->i_mapping, start, end);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800574 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (idx >= info->next_index)
576 return;
577
578 spin_lock(&info->lock);
579 info->flags |= SHMEM_TRUNCATE;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800580 if (likely(end == (loff_t) -1)) {
581 limit = info->next_index;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700582 upper_limit = SHMEM_MAX_INDEX;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800583 info->next_index = idx;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700584 needs_lock = NULL;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700585 punch_hole = 0;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800586 } else {
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700587 if (end + 1 >= inode->i_size) { /* we may free a little more */
588 limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
589 PAGE_CACHE_SHIFT;
590 upper_limit = SHMEM_MAX_INDEX;
591 } else {
592 limit = (end + 1) >> PAGE_CACHE_SHIFT;
593 upper_limit = limit;
594 }
Hugh Dickins1ae70002007-03-29 01:20:36 -0700595 needs_lock = &info->lock;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800596 punch_hole = 1;
597 }
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 topdir = info->i_indirect;
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800600 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 info->i_indirect = NULL;
602 nr_pages_to_free++;
603 list_add(&topdir->lru, &pages_to_free);
604 }
605 spin_unlock(&info->lock);
606
607 if (info->swapped && idx < SHMEM_NR_DIRECT) {
608 ptr = info->i_direct;
609 size = limit;
610 if (size > SHMEM_NR_DIRECT)
611 size = SHMEM_NR_DIRECT;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700612 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Badari Pulavarty92a3d032006-12-22 01:06:23 -0800614
615 /*
616 * If there are no indirect blocks or we are punching a hole
617 * below indirect blocks, nothing to be done.
618 */
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700619 if (!topdir || limit <= SHMEM_NR_DIRECT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto done2;
621
Hugh Dickins1ae70002007-03-29 01:20:36 -0700622 /*
623 * The truncation case has already dropped info->lock, and we're safe
624 * because i_size and next_index have already been lowered, preventing
625 * access beyond. But in the punch_hole case, we still need to take
626 * the lock when updating the swap directory, because there might be
627 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
628 * shmem_writepage. However, whenever we find we can remove a whole
629 * directory page (not at the misaligned start or end of the range),
630 * we first NULLify its pointer in the level above, and then have no
631 * need to take the lock when updating its contents: needs_lock and
632 * punch_lock (either pointing to info->lock or NULL) manage this.
633 */
634
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700635 upper_limit -= SHMEM_NR_DIRECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 limit -= SHMEM_NR_DIRECT;
637 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
638 offset = idx % ENTRIES_PER_PAGE;
639 idx -= offset;
640
641 dir = shmem_dir_map(topdir);
642 stage = ENTRIES_PER_PAGEPAGE/2;
643 if (idx < ENTRIES_PER_PAGEPAGE/2) {
644 middir = topdir;
645 diroff = idx/ENTRIES_PER_PAGE;
646 } else {
647 dir += ENTRIES_PER_PAGE/2;
648 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
649 while (stage <= idx)
650 stage += ENTRIES_PER_PAGEPAGE;
651 middir = *dir;
652 if (*dir) {
653 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
654 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700655 if (!diroff && !offset && upper_limit >= stage) {
Hugh Dickins1ae70002007-03-29 01:20:36 -0700656 if (needs_lock) {
657 spin_lock(needs_lock);
658 *dir = NULL;
659 spin_unlock(needs_lock);
660 needs_lock = NULL;
661 } else
662 *dir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 nr_pages_to_free++;
664 list_add(&middir->lru, &pages_to_free);
665 }
666 shmem_dir_unmap(dir);
667 dir = shmem_dir_map(middir);
668 } else {
669 diroff = 0;
670 offset = 0;
671 idx = stage;
672 }
673 }
674
675 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
676 if (unlikely(idx == stage)) {
677 shmem_dir_unmap(dir);
678 dir = shmem_dir_map(topdir) +
679 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
680 while (!*dir) {
681 dir++;
682 idx += ENTRIES_PER_PAGEPAGE;
683 if (idx >= limit)
684 goto done1;
685 }
686 stage = idx + ENTRIES_PER_PAGEPAGE;
687 middir = *dir;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700688 if (punch_hole)
689 needs_lock = &info->lock;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700690 if (upper_limit >= stage) {
Hugh Dickins1ae70002007-03-29 01:20:36 -0700691 if (needs_lock) {
692 spin_lock(needs_lock);
693 *dir = NULL;
694 spin_unlock(needs_lock);
695 needs_lock = NULL;
696 } else
697 *dir = NULL;
Hugh Dickinsa2646d12007-03-29 01:20:35 -0700698 nr_pages_to_free++;
699 list_add(&middir->lru, &pages_to_free);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 shmem_dir_unmap(dir);
702 cond_resched();
703 dir = shmem_dir_map(middir);
704 diroff = 0;
705 }
Hugh Dickins1ae70002007-03-29 01:20:36 -0700706 punch_lock = needs_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 subdir = dir[diroff];
Hugh Dickins1ae70002007-03-29 01:20:36 -0700708 if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
709 if (needs_lock) {
710 spin_lock(needs_lock);
711 dir[diroff] = NULL;
712 spin_unlock(needs_lock);
713 punch_lock = NULL;
714 } else
715 dir[diroff] = NULL;
716 nr_pages_to_free++;
717 list_add(&subdir->lru, &pages_to_free);
718 }
719 if (subdir && page_private(subdir) /* has swap entries */) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 size = limit - idx;
721 if (size > ENTRIES_PER_PAGE)
722 size = ENTRIES_PER_PAGE;
723 freed = shmem_map_and_free_swp(subdir,
Hugh Dickins1ae70002007-03-29 01:20:36 -0700724 offset, size, &dir, punch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!dir)
726 dir = shmem_dir_map(middir);
727 nr_swaps_freed += freed;
Hugh Dickins1ae70002007-03-29 01:20:36 -0700728 if (offset || punch_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_lock(&info->lock);
Hugh Dickins1ae70002007-03-29 01:20:36 -0700730 set_page_private(subdir,
731 page_private(subdir) - freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 spin_unlock(&info->lock);
Hugh Dickins1ae70002007-03-29 01:20:36 -0700733 } else
734 BUG_ON(page_private(subdir) != freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Hugh Dickins1ae70002007-03-29 01:20:36 -0700736 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738done1:
739 shmem_dir_unmap(dir);
740done2:
741 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
742 /*
743 * Call truncate_inode_pages again: racing shmem_unuse_inode
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000744 * may have swizzled a page in from swap since
745 * truncate_pagecache or generic_delete_inode did it, before we
746 * lowered next_index. Also, though shmem_getpage checks
747 * i_size before adding to cache, no recheck after: so fix the
748 * narrow window there too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -0800750 truncate_inode_pages_range(inode->i_mapping, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 spin_lock(&info->lock);
754 info->flags &= ~SHMEM_TRUNCATE;
755 info->swapped -= nr_swaps_freed;
756 if (nr_pages_to_free)
757 shmem_free_blocks(inode, nr_pages_to_free);
758 shmem_recalc_inode(inode);
759 spin_unlock(&info->lock);
760
761 /*
762 * Empty swap vector directory pages to be freed?
763 */
764 if (!list_empty(&pages_to_free)) {
765 pages_to_free.prev->next = NULL;
766 shmem_free_pages(pages_to_free.next);
767 }
768}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700769EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Hugh Dickins94c1e622011-06-27 16:18:03 -0700771static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int error;
775
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200776 error = inode_change_ok(inode, attr);
777 if (error)
778 return error;
779
Hugh Dickins94c1e622011-06-27 16:18:03 -0700780 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
781 loff_t oldsize = inode->i_size;
782 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000783 struct page *page = NULL;
784
Hugh Dickins94c1e622011-06-27 16:18:03 -0700785 if (newsize < oldsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 /*
787 * If truncating down to a partial page, then
788 * if that page is already allocated, hold it
789 * in memory until the truncation is over, so
Justin P. Mattockae0e47f2011-03-01 15:06:02 +0100790 * truncate_partial_page cannot miss it were
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 * it assigned to swap.
792 */
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000793 if (newsize & (PAGE_CACHE_SIZE-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 (void) shmem_getpage(inode,
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000795 newsize >> PAGE_CACHE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 &page, SGP_READ, NULL);
Hugh Dickinsd3602442008-02-04 22:28:44 -0800797 if (page)
798 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800 /*
801 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
802 * detect if any pages might have been added to cache
803 * after truncate_inode_pages. But we needn't bother
804 * if it's being fully truncated to zero-length: the
805 * nrpages check is efficient enough in that case.
806 */
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000807 if (newsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct shmem_inode_info *info = SHMEM_I(inode);
809 spin_lock(&info->lock);
810 info->flags &= ~SHMEM_PAGEIN;
811 spin_unlock(&info->lock);
812 }
813 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700814 if (newsize != oldsize) {
815 i_size_write(inode, newsize);
816 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
817 }
818 if (newsize < oldsize) {
819 loff_t holebegin = round_up(newsize, PAGE_SIZE);
820 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
821 shmem_truncate_range(inode, newsize, (loff_t)-1);
822 /* unmap again to remove racily COWed private pages */
823 unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
824 }
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000825 if (page)
826 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200829 setattr_copy(inode, attr);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700830#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigdb78b872010-06-04 11:30:03 +0200831 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig1c7c4742009-11-03 16:44:44 +0100832 error = generic_acl_chmod(inode);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -0700833#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return error;
835}
836
Al Viro1f895f72010-06-05 19:10:41 -0400837static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct shmem_inode_info *info = SHMEM_I(inode);
Eric Parisb09e0fa2011-05-24 17:12:39 -0700840 struct shmem_xattr *xattr, *nxattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000842 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 shmem_unacct_size(info->flags, inode->i_size);
844 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +1000845 shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800847 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800849 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 }
Eric Parisb09e0fa2011-05-24 17:12:39 -0700852
853 list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
854 kfree(xattr->name);
855 kfree(xattr);
856 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -0700857 BUG_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800858 shmem_free_inode(inode->i_sb);
Al Viro1f895f72010-06-05 19:10:41 -0400859 end_writeback(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
863{
864 swp_entry_t *ptr;
865
866 for (ptr = dir; ptr < edir; ptr++) {
867 if (ptr->val == entry.val)
868 return ptr - dir;
869 }
870 return -1;
871}
872
873static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
874{
Hugh Dickins778dd892011-05-11 15:13:37 -0700875 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned long idx;
877 unsigned long size;
878 unsigned long limit;
879 unsigned long stage;
880 struct page **dir;
881 struct page *subdir;
882 swp_entry_t *ptr;
883 int offset;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800884 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 idx = 0;
887 ptr = info->i_direct;
888 spin_lock(&info->lock);
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800889 if (!info->swapped) {
890 list_del_init(&info->swaplist);
891 goto lost2;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 limit = info->next_index;
894 size = limit;
895 if (size > SHMEM_NR_DIRECT)
896 size = SHMEM_NR_DIRECT;
897 offset = shmem_find_swp(entry, ptr, ptr+size);
Hugh Dickins778dd892011-05-11 15:13:37 -0700898 if (offset >= 0) {
899 shmem_swp_balance_unmap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto found;
Hugh Dickins778dd892011-05-11 15:13:37 -0700901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (!info->i_indirect)
903 goto lost2;
904
905 dir = shmem_dir_map(info->i_indirect);
906 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
907
908 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
909 if (unlikely(idx == stage)) {
910 shmem_dir_unmap(dir-1);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800911 if (cond_resched_lock(&info->lock)) {
912 /* check it has not been truncated */
913 if (limit > info->next_index) {
914 limit = info->next_index;
915 if (idx >= limit)
916 goto lost2;
917 }
918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 dir = shmem_dir_map(info->i_indirect) +
920 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
921 while (!*dir) {
922 dir++;
923 idx += ENTRIES_PER_PAGEPAGE;
924 if (idx >= limit)
925 goto lost1;
926 }
927 stage = idx + ENTRIES_PER_PAGEPAGE;
928 subdir = *dir;
929 shmem_dir_unmap(dir);
930 dir = shmem_dir_map(subdir);
931 }
932 subdir = *dir;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700933 if (subdir && page_private(subdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ptr = shmem_swp_map(subdir);
935 size = limit - idx;
936 if (size > ENTRIES_PER_PAGE)
937 size = ENTRIES_PER_PAGE;
938 offset = shmem_find_swp(entry, ptr, ptr+size);
Hugh Dickinse6c93662011-05-20 15:47:33 -0700939 shmem_swp_unmap(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (offset >= 0) {
941 shmem_dir_unmap(dir);
Hugh Dickinse6c93662011-05-20 15:47:33 -0700942 ptr = shmem_swp_map(subdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto found;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 }
947lost1:
948 shmem_dir_unmap(dir-1);
949lost2:
950 spin_unlock(&info->lock);
951 return 0;
952found:
953 idx += offset;
Hugh Dickins778dd892011-05-11 15:13:37 -0700954 ptr += offset;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800955
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800956 /*
957 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -0400958 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -0800959 * mutex, and there's an instant in list_move_tail when info->swaplist
960 * would appear empty, if it were the only one on shmem_swaplist. We
961 * could avoid doing it if inode NULL; or use this minor optimization.
962 */
963 if (shmem_swaplist.next != &info->swaplist)
964 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800965
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800966 /*
Hugh Dickins778dd892011-05-11 15:13:37 -0700967 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
968 * but also to hold up shmem_evict_inode(): so inode cannot be freed
969 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800970 */
Hugh Dickins778dd892011-05-11 15:13:37 -0700971 mapping = info->vfs_inode.i_mapping;
972 error = add_to_page_cache_locked(page, mapping, idx, GFP_NOWAIT);
973 /* which does mem_cgroup_uncharge_cache_page on error */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700974
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800975 if (error == -EEXIST) {
Hugh Dickins778dd892011-05-11 15:13:37 -0700976 struct page *filepage = find_get_page(mapping, idx);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800977 error = 1;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -0800978 if (filepage) {
979 /*
980 * There might be a more uptodate page coming down
981 * from a stacked writepage: forget our swappage if so.
982 */
983 if (PageUptodate(filepage))
984 error = 0;
985 page_cache_release(filepage);
986 }
987 }
988 if (!error) {
Hugh Dickins73b12622008-02-04 22:28:50 -0800989 delete_from_swap_cache(page);
990 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 info->flags |= SHMEM_PAGEIN;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800992 shmem_swp_set(info, ptr, 0);
993 swap_free(entry);
994 error = 1; /* not an error, but entry was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Hugh Dickins778dd892011-05-11 15:13:37 -0700996 shmem_swp_unmap(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_unlock(&info->lock);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -0800998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
1002 * shmem_unuse() search for an eventually swapped out shmem page.
1003 */
1004int shmem_unuse(swp_entry_t entry, struct page *page)
1005{
1006 struct list_head *p, *next;
1007 struct shmem_inode_info *info;
1008 int found = 0;
Hugh Dickins778dd892011-05-11 15:13:37 -07001009 int error;
1010
1011 /*
1012 * Charge page using GFP_KERNEL while we can wait, before taking
1013 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1014 * Charged back to the user (not to caller) when swap account is used.
1015 * add_to_page_cache() will be called with GFP_NOWAIT.
1016 */
1017 error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
1018 if (error)
1019 goto out;
1020 /*
1021 * Try to preload while we can wait, to not make a habit of
1022 * draining atomic reserves; but don't latch on to this cpu,
1023 * it's okay if sometimes we get rescheduled after this.
1024 */
1025 error = radix_tree_preload(GFP_KERNEL);
1026 if (error)
1027 goto uncharge;
1028 radix_tree_preload_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001030 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 list_for_each_safe(p, next, &shmem_swaplist) {
1032 info = list_entry(p, struct shmem_inode_info, swaplist);
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001033 found = shmem_unuse_inode(info, entry, page);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001034 cond_resched();
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001035 if (found)
Hugh Dickins778dd892011-05-11 15:13:37 -07001036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001038 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001039
1040uncharge:
1041 if (!found)
1042 mem_cgroup_uncharge_cache_page(page);
1043 if (found < 0)
1044 error = found;
1045out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001046 unlock_page(page);
1047 page_cache_release(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/*
1052 * Move the page from the page cache to the swap cache.
1053 */
1054static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1055{
1056 struct shmem_inode_info *info;
1057 swp_entry_t *entry, swap;
1058 struct address_space *mapping;
1059 unsigned long index;
1060 struct inode *inode;
1061
1062 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 mapping = page->mapping;
1064 index = page->index;
1065 inode = mapping->host;
1066 info = SHMEM_I(inode);
1067 if (info->flags & VM_LOCKED)
1068 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001069 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto redirty;
1071
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001072 /*
1073 * shmem_backing_dev_info's capabilities prevent regular writeback or
1074 * sync from ever calling shmem_writepage; but a stacking filesystem
1075 * may use the ->writepage of its underlying filesystem, in which case
1076 * tmpfs should write out to swap only in response to memory pressure,
Jens Axboe5b0830c2009-09-23 19:37:09 +02001077 * and not for the writeback threads or sync. However, in those cases,
1078 * we do still want to check if there's a redundant swappage to be
1079 * discarded.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001080 */
1081 if (wbc->for_reclaim)
1082 swap = get_swap_page();
1083 else
1084 swap.val = 0;
1085
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001086 /*
1087 * Add inode to shmem_unuse()'s list of swapped-out inodes,
1088 * if it's not already there. Do it now because we cannot take
1089 * mutex while holding spinlock, and must do so before the page
1090 * is moved to swap cache, when its pagelock no longer protects
1091 * the inode from eviction. But don't unlock the mutex until
1092 * we've taken the spinlock, because shmem_unuse_inode() will
1093 * prune a !swapped inode from the swaplist under both locks.
1094 */
Hugh Dickins05bf86b2011-05-14 12:06:42 -07001095 if (swap.val) {
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001096 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins05bf86b2011-05-14 12:06:42 -07001097 if (list_empty(&info->swaplist))
1098 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001099 }
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 spin_lock(&info->lock);
Hugh Dickins05bf86b2011-05-14 12:06:42 -07001102 if (swap.val)
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001103 mutex_unlock(&shmem_swaplist_mutex);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (index >= info->next_index) {
1106 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
1107 goto unlock;
1108 }
1109 entry = shmem_swp_entry(info, index, NULL);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001110 if (entry->val) {
1111 /*
1112 * The more uptodate page coming down from a stacked
1113 * writepage should replace our old swappage.
1114 */
1115 free_swap_and_cache(*entry);
1116 shmem_swp_set(info, entry, 0);
1117 }
1118 shmem_recalc_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001120 if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Minchan Kim4c73b1b2011-03-22 16:32:40 -07001121 delete_from_page_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 shmem_swp_set(info, entry, swap.val);
1123 shmem_swp_unmap(entry);
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001124 swap_shmem_alloc(swap);
Hugh Dickins826267c2011-05-28 13:14:09 -07001125 spin_unlock(&info->lock);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001126 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001127 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return 0;
1129 }
1130
1131 shmem_swp_unmap(entry);
1132unlock:
1133 spin_unlock(&info->lock);
Daisuke Nishimura2ca45322009-09-21 17:02:52 -07001134 /*
1135 * add_to_swap_cache() doesn't return -EEXIST, so we can safely
1136 * clear SWAP_HAS_CACHE flag.
1137 */
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07001138 swapcache_free(swap, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139redirty:
1140 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001141 if (wbc->for_reclaim)
1142 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1143 unlock_page(page);
1144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147#ifdef CONFIG_NUMA
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001148#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001149static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001150{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001151 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001152
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001153 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001154 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001155
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001156 mpol_to_str(buffer, sizeof(buffer), mpol, 1);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001157
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001158 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001159}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001160
1161static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1162{
1163 struct mempolicy *mpol = NULL;
1164 if (sbinfo->mpol) {
1165 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1166 mpol = sbinfo->mpol;
1167 mpol_get(mpol);
1168 spin_unlock(&sbinfo->stat_lock);
1169 }
1170 return mpol;
1171}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001172#endif /* CONFIG_TMPFS */
1173
Hugh Dickins02098fe2008-02-04 22:28:42 -08001174static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1175 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -07001177 struct mempolicy mpol, *spol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct vm_area_struct pvma;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -08001179 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -07001181 spol = mpol_cond_copy(&mpol,
1182 mpol_shared_policy_lookup(&info->policy, idx));
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Create a pseudo vma that just contains the policy */
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -08001185 pvma.vm_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 pvma.vm_pgoff = idx;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -08001187 pvma.vm_ops = NULL;
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -07001188 pvma.vm_policy = spol;
Hugh Dickins02098fe2008-02-04 22:28:42 -08001189 page = swapin_readahead(entry, gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return page;
1191}
1192
Hugh Dickins02098fe2008-02-04 22:28:42 -08001193static struct page *shmem_alloc_page(gfp_t gfp,
1194 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct vm_area_struct pvma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -08001198 /* Create a pseudo vma that just contains the policy */
1199 pvma.vm_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 pvma.vm_pgoff = idx;
Hugh Dickinsc4cc6d02008-02-04 22:28:40 -08001201 pvma.vm_ops = NULL;
1202 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
Lee Schermerhorn52cd3b02008-04-28 02:13:16 -07001203
1204 /*
1205 * alloc_page_vma() will drop the shared policy reference
1206 */
1207 return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001209#else /* !CONFIG_NUMA */
1210#ifdef CONFIG_TMPFS
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001211static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001212{
1213}
1214#endif /* CONFIG_TMPFS */
1215
Hugh Dickins02098fe2008-02-04 22:28:42 -08001216static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1217 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Hugh Dickins02098fe2008-02-04 22:28:42 -08001219 return swapin_readahead(entry, gfp, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Hugh Dickins02098fe2008-02-04 22:28:42 -08001222static inline struct page *shmem_alloc_page(gfp_t gfp,
1223 struct shmem_inode_info *info, unsigned long idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Hugh Dickinse84e2e12007-11-28 18:55:10 +00001225 return alloc_page(gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001227#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001229#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1230static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1231{
1232 return NULL;
1233}
1234#endif
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001237 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 *
1239 * If we allocate a new one we do not mark it dirty. That's up to the
1240 * vm. If we swap it in we mark it dirty since we also free the swap
1241 * entry since a page cannot live in both the swap and page cache
1242 */
Hugh Dickins68da9f02011-07-25 17:12:34 -07001243static int shmem_getpage_gfp(struct inode *inode, pgoff_t idx,
1244 struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct address_space *mapping = inode->i_mapping;
1247 struct shmem_inode_info *info = SHMEM_I(inode);
1248 struct shmem_sb_info *sbinfo;
1249 struct page *filepage = *pagep;
1250 struct page *swappage;
Shaohua Liff36b8012010-08-09 17:19:06 -07001251 struct page *prealloc_page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 swp_entry_t *entry;
1253 swp_entry_t swap;
1254 int error;
1255
1256 if (idx >= SHMEM_MAX_INDEX)
1257 return -EFBIG;
Nick Piggin54cb8822007-07-19 01:46:59 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Normally, filepage is NULL on entry, and either found
1261 * uptodate immediately, or allocated and zeroed, or read
1262 * in under swappage, which is then assigned to filepage.
Hugh Dickins5402b972008-02-04 22:28:44 -08001263 * But shmem_readpage (required for splice) passes in a locked
Hugh Dickinsae9764162007-06-04 10:00:39 +02001264 * filepage, which may be found not uptodate by other callers
1265 * too, and may need to be copied from the swappage read in.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 */
1267repeat:
1268 if (!filepage)
1269 filepage = find_lock_page(mapping, idx);
1270 if (filepage && PageUptodate(filepage))
1271 goto done;
Hugh Dickinsb409f9f2008-02-04 22:28:54 -08001272 if (!filepage) {
1273 /*
1274 * Try to preload while we can wait, to not make a habit of
1275 * draining atomic reserves; but don't latch on to this cpu.
1276 */
Hugh Dickins68da9f02011-07-25 17:12:34 -07001277 error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
Hugh Dickinsb409f9f2008-02-04 22:28:54 -08001278 if (error)
1279 goto failed;
1280 radix_tree_preload_end();
Shaohua Liff36b8012010-08-09 17:19:06 -07001281 if (sgp != SGP_READ && !prealloc_page) {
1282 /* We don't care if this fails */
1283 prealloc_page = shmem_alloc_page(gfp, info, idx);
1284 if (prealloc_page) {
1285 if (mem_cgroup_cache_charge(prealloc_page,
1286 current->mm, GFP_KERNEL)) {
1287 page_cache_release(prealloc_page);
1288 prealloc_page = NULL;
1289 }
1290 }
1291 }
Hugh Dickinsb409f9f2008-02-04 22:28:54 -08001292 }
Shaohua Liff36b8012010-08-09 17:19:06 -07001293 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 spin_lock(&info->lock);
1296 shmem_recalc_inode(inode);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001297 entry = shmem_swp_alloc(info, idx, sgp, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (IS_ERR(entry)) {
1299 spin_unlock(&info->lock);
1300 error = PTR_ERR(entry);
1301 goto failed;
1302 }
1303 swap = *entry;
1304
1305 if (swap.val) {
1306 /* Look it up and read it in.. */
1307 swappage = lookup_swap_cache(swap);
1308 if (!swappage) {
1309 shmem_swp_unmap(entry);
Christoph Lameterf8891e52006-06-30 01:55:45 -07001310 spin_unlock(&info->lock);
Ying Han456f9982011-05-26 16:25:38 -07001311 /* here we actually do the io */
Hugh Dickins68da9f02011-07-25 17:12:34 -07001312 if (fault_type)
1313 *fault_type |= VM_FAULT_MAJOR;
Hugh Dickins02098fe2008-02-04 22:28:42 -08001314 swappage = shmem_swapin(swap, gfp, info, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!swappage) {
1316 spin_lock(&info->lock);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001317 entry = shmem_swp_alloc(info, idx, sgp, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (IS_ERR(entry))
1319 error = PTR_ERR(entry);
1320 else {
1321 if (entry->val == swap.val)
1322 error = -ENOMEM;
1323 shmem_swp_unmap(entry);
1324 }
1325 spin_unlock(&info->lock);
1326 if (error)
1327 goto failed;
1328 goto repeat;
1329 }
1330 wait_on_page_locked(swappage);
1331 page_cache_release(swappage);
1332 goto repeat;
1333 }
1334
1335 /* We have to do this with page locked to prevent races */
Nick Piggin529ae9a2008-08-02 12:01:03 +02001336 if (!trylock_page(swappage)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 shmem_swp_unmap(entry);
1338 spin_unlock(&info->lock);
1339 wait_on_page_locked(swappage);
1340 page_cache_release(swappage);
1341 goto repeat;
1342 }
1343 if (PageWriteback(swappage)) {
1344 shmem_swp_unmap(entry);
1345 spin_unlock(&info->lock);
1346 wait_on_page_writeback(swappage);
1347 unlock_page(swappage);
1348 page_cache_release(swappage);
1349 goto repeat;
1350 }
1351 if (!PageUptodate(swappage)) {
1352 shmem_swp_unmap(entry);
1353 spin_unlock(&info->lock);
1354 unlock_page(swappage);
1355 page_cache_release(swappage);
1356 error = -EIO;
1357 goto failed;
1358 }
1359
1360 if (filepage) {
1361 shmem_swp_set(info, entry, 0);
1362 shmem_swp_unmap(entry);
1363 delete_from_swap_cache(swappage);
1364 spin_unlock(&info->lock);
1365 copy_highpage(filepage, swappage);
1366 unlock_page(swappage);
1367 page_cache_release(swappage);
1368 flush_dcache_page(filepage);
1369 SetPageUptodate(filepage);
1370 set_page_dirty(filepage);
1371 swap_free(swap);
Nick Piggine2867812008-07-25 19:45:30 -07001372 } else if (!(error = add_to_page_cache_locked(swappage, mapping,
1373 idx, GFP_NOWAIT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 info->flags |= SHMEM_PAGEIN;
1375 shmem_swp_set(info, entry, 0);
1376 shmem_swp_unmap(entry);
Hugh Dickins73b12622008-02-04 22:28:50 -08001377 delete_from_swap_cache(swappage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_unlock(&info->lock);
1379 filepage = swappage;
Hugh Dickins73b12622008-02-04 22:28:50 -08001380 set_page_dirty(filepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 swap_free(swap);
1382 } else {
1383 shmem_swp_unmap(entry);
1384 spin_unlock(&info->lock);
Hugh Dickins82369552008-02-07 00:14:22 -08001385 if (error == -ENOMEM) {
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07001386 /*
1387 * reclaim from proper memory cgroup and
1388 * call memcg's OOM if needed.
1389 */
1390 error = mem_cgroup_shmem_charge_fallback(
1391 swappage,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001392 current->mm,
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001393 gfp);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001394 if (error) {
1395 unlock_page(swappage);
1396 page_cache_release(swappage);
Hugh Dickins82369552008-02-07 00:14:22 -08001397 goto failed;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001398 }
Hugh Dickins82369552008-02-07 00:14:22 -08001399 }
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001400 unlock_page(swappage);
1401 page_cache_release(swappage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto repeat;
1403 }
1404 } else if (sgp == SGP_READ && !filepage) {
1405 shmem_swp_unmap(entry);
1406 filepage = find_get_page(mapping, idx);
1407 if (filepage &&
Nick Piggin529ae9a2008-08-02 12:01:03 +02001408 (!PageUptodate(filepage) || !trylock_page(filepage))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 spin_unlock(&info->lock);
1410 wait_on_page_locked(filepage);
1411 page_cache_release(filepage);
1412 filepage = NULL;
1413 goto repeat;
1414 }
1415 spin_unlock(&info->lock);
1416 } else {
1417 shmem_swp_unmap(entry);
1418 sbinfo = SHMEM_SB(inode->i_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001419 if (sbinfo->max_blocks) {
Hugh Dickinsfc5da222011-04-14 15:22:07 -07001420 if (percpu_counter_compare(&sbinfo->used_blocks,
1421 sbinfo->max_blocks) >= 0 ||
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001422 shmem_acct_block(info->flags))
1423 goto nospace;
Tim Chen7e496292010-08-09 17:19:05 -07001424 percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 inode->i_blocks += BLOCKS_PER_PAGE;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001426 } else if (shmem_acct_block(info->flags))
1427 goto nospace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 if (!filepage) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001430 int ret;
1431
Shaohua Liff36b8012010-08-09 17:19:06 -07001432 if (!prealloc_page) {
1433 spin_unlock(&info->lock);
1434 filepage = shmem_alloc_page(gfp, info, idx);
1435 if (!filepage) {
Hugh Dickinsd515afe2011-07-25 17:12:26 -07001436 spin_lock(&info->lock);
Shaohua Liff36b8012010-08-09 17:19:06 -07001437 shmem_unacct_blocks(info->flags, 1);
1438 shmem_free_blocks(inode, 1);
Hugh Dickinsd515afe2011-07-25 17:12:26 -07001439 spin_unlock(&info->lock);
Shaohua Liff36b8012010-08-09 17:19:06 -07001440 error = -ENOMEM;
1441 goto failed;
1442 }
1443 SetPageSwapBacked(filepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Shaohua Liff36b8012010-08-09 17:19:06 -07001445 /*
1446 * Precharge page while we can wait, compensate
1447 * after
1448 */
1449 error = mem_cgroup_cache_charge(filepage,
1450 current->mm, GFP_KERNEL);
1451 if (error) {
1452 page_cache_release(filepage);
Hugh Dickinsd515afe2011-07-25 17:12:26 -07001453 spin_lock(&info->lock);
Shaohua Liff36b8012010-08-09 17:19:06 -07001454 shmem_unacct_blocks(info->flags, 1);
1455 shmem_free_blocks(inode, 1);
Hugh Dickinsd515afe2011-07-25 17:12:26 -07001456 spin_unlock(&info->lock);
Shaohua Liff36b8012010-08-09 17:19:06 -07001457 filepage = NULL;
1458 goto failed;
1459 }
1460
1461 spin_lock(&info->lock);
1462 } else {
1463 filepage = prealloc_page;
1464 prealloc_page = NULL;
1465 SetPageSwapBacked(filepage);
Hugh Dickins82369552008-02-07 00:14:22 -08001466 }
1467
Hugh Dickins68da9f02011-07-25 17:12:34 -07001468 entry = shmem_swp_alloc(info, idx, sgp, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 if (IS_ERR(entry))
1470 error = PTR_ERR(entry);
1471 else {
1472 swap = *entry;
1473 shmem_swp_unmap(entry);
1474 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001475 ret = error || swap.val;
1476 if (ret)
1477 mem_cgroup_uncharge_cache_page(filepage);
1478 else
1479 ret = add_to_page_cache_lru(filepage, mapping,
1480 idx, GFP_NOWAIT);
1481 /*
1482 * At add_to_page_cache_lru() failure, uncharge will
1483 * be done automatically.
1484 */
1485 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 shmem_unacct_blocks(info->flags, 1);
1487 shmem_free_blocks(inode, 1);
Hugh Dickinsd515afe2011-07-25 17:12:26 -07001488 spin_unlock(&info->lock);
1489 page_cache_release(filepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 filepage = NULL;
1491 if (error)
1492 goto failed;
1493 goto repeat;
1494 }
1495 info->flags |= SHMEM_PAGEIN;
1496 }
1497
1498 info->alloced++;
1499 spin_unlock(&info->lock);
Hugh Dickinse84e2e12007-11-28 18:55:10 +00001500 clear_highpage(filepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 flush_dcache_page(filepage);
1502 SetPageUptodate(filepage);
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001503 if (sgp == SGP_DIRTY)
1504 set_page_dirty(filepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506done:
Hugh Dickinsd3602442008-02-04 22:28:44 -08001507 *pagep = filepage;
Shaohua Liff36b8012010-08-09 17:19:06 -07001508 error = 0;
1509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001511nospace:
1512 /*
1513 * Perhaps the page was brought in from swap between find_lock_page
1514 * and taking info->lock? We allow for that at add_to_page_cache_lru,
1515 * but must also avoid reporting a spurious ENOSPC while working on a
1516 * full tmpfs. (When filepage has been passed in to shmem_getpage, it
1517 * is already in page cache, which prevents this race from occurring.)
1518 */
1519 if (!filepage) {
1520 struct page *page = find_get_page(mapping, idx);
1521 if (page) {
1522 spin_unlock(&info->lock);
1523 page_cache_release(page);
1524 goto repeat;
1525 }
1526 }
1527 spin_unlock(&info->lock);
1528 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529failed:
1530 if (*pagep != filepage) {
1531 unlock_page(filepage);
1532 page_cache_release(filepage);
1533 }
Shaohua Liff36b8012010-08-09 17:19:06 -07001534out:
1535 if (prealloc_page) {
1536 mem_cgroup_uncharge_cache_page(prealloc_page);
1537 page_cache_release(prealloc_page);
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return error;
1540}
1541
Nick Piggind0217ac2007-07-19 01:47:03 -07001542static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001544 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001546 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Nick Piggind0217ac2007-07-19 01:47:03 -07001548 if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1549 return VM_FAULT_SIGBUS;
Nick Piggind00806b2007-07-19 01:46:57 -07001550
Hugh Dickins27d54b32008-02-04 22:28:43 -08001551 error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
Nick Piggind0217ac2007-07-19 01:47:03 -07001552 if (error)
1553 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07001554
Ying Han456f9982011-05-26 16:25:38 -07001555 if (ret & VM_FAULT_MAJOR) {
1556 count_vm_event(PGMAJFAULT);
1557 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1558 }
Hugh Dickins68da9f02011-07-25 17:12:34 -07001559 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#ifdef CONFIG_NUMA
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001563static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001565 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1567}
1568
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07001569static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1570 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001572 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 unsigned long idx;
1574
1575 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1576 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1577}
1578#endif
1579
1580int shmem_lock(struct file *file, int lock, struct user_struct *user)
1581{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001582 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct shmem_inode_info *info = SHMEM_I(inode);
1584 int retval = -ENOMEM;
1585
1586 spin_lock(&info->lock);
1587 if (lock && !(info->flags & VM_LOCKED)) {
1588 if (!user_shm_lock(inode->i_size, user))
1589 goto out_nomem;
1590 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001591 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 if (!lock && (info->flags & VM_LOCKED) && user) {
1594 user_shm_unlock(inode->i_size, user);
1595 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001596 mapping_clear_unevictable(file->f_mapping);
1597 scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601out_nomem:
1602 spin_unlock(&info->lock);
1603 return retval;
1604}
1605
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08001606static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 file_accessed(file);
1609 vma->vm_ops = &shmem_vm_ops;
Nick Piggind0217ac2007-07-19 01:47:03 -07001610 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
1612}
1613
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001614static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
1615 int mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct inode *inode;
1618 struct shmem_inode_info *info;
1619 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1620
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001621 if (shmem_reserve_inode(sb))
1622 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 inode = new_inode(sb);
1625 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001626 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001627 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1630 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
David M. Grimes91828a42006-10-17 00:09:45 -07001631 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 info = SHMEM_I(inode);
1633 memset(info, 0, (char *)inode - (char *)info);
1634 spin_lock_init(&info->lock);
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001635 info->flags = flags & VM_NORESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 INIT_LIST_HEAD(&info->swaplist);
Eric Parisb09e0fa2011-05-24 17:12:39 -07001637 INIT_LIST_HEAD(&info->xattr_list);
Al Viro72c04902009-06-24 16:58:48 -04001638 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 switch (mode & S_IFMT) {
1641 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07001642 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 init_special_inode(inode, mode, dev);
1644 break;
1645 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07001646 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 inode->i_op = &shmem_inode_operations;
1648 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001649 mpol_shared_policy_init(&info->policy,
1650 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07001653 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /* Some things misbehave if size == 0 on a directory */
1655 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1656 inode->i_op = &shmem_dir_inode_operations;
1657 inode->i_fop = &simple_dir_operations;
1658 break;
1659 case S_IFLNK:
1660 /*
1661 * Must not load anything in the rbtree,
1662 * mpol_free_shared_policy will not be called.
1663 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001664 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001667 } else
1668 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return inode;
1670}
1671
1672#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08001673static const struct inode_operations shmem_symlink_inode_operations;
1674static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676/*
Nick Piggin800d15a2007-10-16 01:25:03 -07001677 * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
Hugh Dickinsae9764162007-06-04 10:00:39 +02001678 * but providing them allows a tmpfs file to be used for splice, sendfile, and
1679 * below the loop driver, in the generic fashion that many filesystems support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
Hugh Dickinsae9764162007-06-04 10:00:39 +02001681static int shmem_readpage(struct file *file, struct page *page)
1682{
1683 struct inode *inode = page->mapping->host;
1684 int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1685 unlock_page(page);
1686 return error;
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689static int
Nick Piggin800d15a2007-10-16 01:25:03 -07001690shmem_write_begin(struct file *file, struct address_space *mapping,
1691 loff_t pos, unsigned len, unsigned flags,
1692 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Nick Piggin800d15a2007-10-16 01:25:03 -07001694 struct inode *inode = mapping->host;
1695 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1696 *pagep = NULL;
1697 return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1698}
1699
1700static int
1701shmem_write_end(struct file *file, struct address_space *mapping,
1702 loff_t pos, unsigned len, unsigned copied,
1703 struct page *page, void *fsdata)
1704{
1705 struct inode *inode = mapping->host;
1706
Hugh Dickinsd3602442008-02-04 22:28:44 -08001707 if (pos + copied > inode->i_size)
1708 i_size_write(inode, pos + copied);
1709
Nick Piggin800d15a2007-10-16 01:25:03 -07001710 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02001711 unlock_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07001712 page_cache_release(page);
1713
Nick Piggin800d15a2007-10-16 01:25:03 -07001714 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1718{
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001719 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 struct address_space *mapping = inode->i_mapping;
1721 unsigned long index, offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001722 enum sgp_type sgp = SGP_READ;
1723
1724 /*
1725 * Might this read be for a stacking filesystem? Then when reading
1726 * holes of a sparse file, we actually need to allocate those pages,
1727 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1728 */
1729 if (segment_eq(get_fs(), KERNEL_DS))
1730 sgp = SGP_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 index = *ppos >> PAGE_CACHE_SHIFT;
1733 offset = *ppos & ~PAGE_CACHE_MASK;
1734
1735 for (;;) {
1736 struct page *page = NULL;
1737 unsigned long end_index, nr, ret;
1738 loff_t i_size = i_size_read(inode);
1739
1740 end_index = i_size >> PAGE_CACHE_SHIFT;
1741 if (index > end_index)
1742 break;
1743 if (index == end_index) {
1744 nr = i_size & ~PAGE_CACHE_MASK;
1745 if (nr <= offset)
1746 break;
1747 }
1748
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08001749 desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (desc->error) {
1751 if (desc->error == -EINVAL)
1752 desc->error = 0;
1753 break;
1754 }
Hugh Dickinsd3602442008-02-04 22:28:44 -08001755 if (page)
1756 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 /*
1759 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001760 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
1762 nr = PAGE_CACHE_SIZE;
1763 i_size = i_size_read(inode);
1764 end_index = i_size >> PAGE_CACHE_SHIFT;
1765 if (index == end_index) {
1766 nr = i_size & ~PAGE_CACHE_MASK;
1767 if (nr <= offset) {
1768 if (page)
1769 page_cache_release(page);
1770 break;
1771 }
1772 }
1773 nr -= offset;
1774
1775 if (page) {
1776 /*
1777 * If users can be writing to this page using arbitrary
1778 * virtual addresses, take care about potential aliasing
1779 * before reading the page on the kernel side.
1780 */
1781 if (mapping_writably_mapped(mapping))
1782 flush_dcache_page(page);
1783 /*
1784 * Mark the page accessed if we read the beginning.
1785 */
1786 if (!offset)
1787 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07001788 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 page = ZERO_PAGE(0);
Nick Pigginb5810032005-10-29 18:16:12 -07001790 page_cache_get(page);
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 /*
1794 * Ok, we have the page, and it's up-to-date, so
1795 * now we can copy it to user space...
1796 *
1797 * The actor routine returns how many bytes were actually used..
1798 * NOTE! This may not be the same as how much of a user buffer
1799 * we filled up (we may be padding etc), so we can only update
1800 * "pos" here (the actor routine has to update the user buffer
1801 * pointers and the remaining count).
1802 */
1803 ret = actor(desc, page, offset, nr);
1804 offset += ret;
1805 index += offset >> PAGE_CACHE_SHIFT;
1806 offset &= ~PAGE_CACHE_MASK;
1807
1808 page_cache_release(page);
1809 if (ret != nr || !desc->count)
1810 break;
1811
1812 cond_resched();
1813 }
1814
1815 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1816 file_accessed(filp);
1817}
1818
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001819static ssize_t shmem_file_aio_read(struct kiocb *iocb,
1820 const struct iovec *iov, unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001822 struct file *filp = iocb->ki_filp;
1823 ssize_t retval;
1824 unsigned long seg;
1825 size_t count;
1826 loff_t *ppos = &iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001828 retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
1829 if (retval)
1830 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001832 for (seg = 0; seg < nr_segs; seg++) {
1833 read_descriptor_t desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07001835 desc.written = 0;
1836 desc.arg.buf = iov[seg].iov_base;
1837 desc.count = iov[seg].iov_len;
1838 if (desc.count == 0)
1839 continue;
1840 desc.error = 0;
1841 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1842 retval += desc.written;
1843 if (desc.error) {
1844 retval = retval ?: desc.error;
1845 break;
1846 }
1847 if (desc.count > 0)
1848 break;
1849 }
1850 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Hugh Dickins708e3502011-07-25 17:12:32 -07001853static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
1854 struct pipe_inode_info *pipe, size_t len,
1855 unsigned int flags)
1856{
1857 struct address_space *mapping = in->f_mapping;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001858 struct inode *inode = mapping->host;
Hugh Dickins708e3502011-07-25 17:12:32 -07001859 unsigned int loff, nr_pages, req_pages;
1860 struct page *pages[PIPE_DEF_BUFFERS];
1861 struct partial_page partial[PIPE_DEF_BUFFERS];
1862 struct page *page;
1863 pgoff_t index, end_index;
1864 loff_t isize, left;
1865 int error, page_nr;
1866 struct splice_pipe_desc spd = {
1867 .pages = pages,
1868 .partial = partial,
1869 .flags = flags,
1870 .ops = &page_cache_pipe_buf_ops,
1871 .spd_release = spd_release_page,
1872 };
1873
Hugh Dickins71f0e072011-07-25 17:12:33 -07001874 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001875 if (unlikely(*ppos >= isize))
1876 return 0;
1877
1878 left = isize - *ppos;
1879 if (unlikely(left < len))
1880 len = left;
1881
1882 if (splice_grow_spd(pipe, &spd))
1883 return -ENOMEM;
1884
1885 index = *ppos >> PAGE_CACHE_SHIFT;
1886 loff = *ppos & ~PAGE_CACHE_MASK;
1887 req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1888 nr_pages = min(req_pages, pipe->buffers);
1889
Hugh Dickins708e3502011-07-25 17:12:32 -07001890 spd.nr_pages = find_get_pages_contig(mapping, index,
1891 nr_pages, spd.pages);
1892 index += spd.nr_pages;
Hugh Dickins708e3502011-07-25 17:12:32 -07001893 error = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001894
Hugh Dickins708e3502011-07-25 17:12:32 -07001895 while (spd.nr_pages < nr_pages) {
Hugh Dickins71f0e072011-07-25 17:12:33 -07001896 page = NULL;
1897 error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
1898 if (error)
1899 break;
1900 unlock_page(page);
Hugh Dickins708e3502011-07-25 17:12:32 -07001901 spd.pages[spd.nr_pages++] = page;
1902 index++;
1903 }
1904
Hugh Dickins708e3502011-07-25 17:12:32 -07001905 index = *ppos >> PAGE_CACHE_SHIFT;
1906 nr_pages = spd.nr_pages;
1907 spd.nr_pages = 0;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001908
Hugh Dickins708e3502011-07-25 17:12:32 -07001909 for (page_nr = 0; page_nr < nr_pages; page_nr++) {
1910 unsigned int this_len;
1911
1912 if (!len)
1913 break;
1914
Hugh Dickins708e3502011-07-25 17:12:32 -07001915 this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
1916 page = spd.pages[page_nr];
1917
Hugh Dickins71f0e072011-07-25 17:12:33 -07001918 if (!PageUptodate(page) || page->mapping != mapping) {
1919 page = NULL;
1920 error = shmem_getpage(inode, index, &page,
1921 SGP_CACHE, NULL);
1922 if (error)
Hugh Dickins708e3502011-07-25 17:12:32 -07001923 break;
Hugh Dickins71f0e072011-07-25 17:12:33 -07001924 unlock_page(page);
1925 page_cache_release(spd.pages[page_nr]);
1926 spd.pages[page_nr] = page;
Hugh Dickins708e3502011-07-25 17:12:32 -07001927 }
Hugh Dickins71f0e072011-07-25 17:12:33 -07001928
1929 isize = i_size_read(inode);
Hugh Dickins708e3502011-07-25 17:12:32 -07001930 end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
1931 if (unlikely(!isize || index > end_index))
1932 break;
1933
Hugh Dickins708e3502011-07-25 17:12:32 -07001934 if (end_index == index) {
1935 unsigned int plen;
1936
Hugh Dickins708e3502011-07-25 17:12:32 -07001937 plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
1938 if (plen <= loff)
1939 break;
1940
Hugh Dickins708e3502011-07-25 17:12:32 -07001941 this_len = min(this_len, plen - loff);
1942 len = this_len;
1943 }
1944
1945 spd.partial[page_nr].offset = loff;
1946 spd.partial[page_nr].len = this_len;
1947 len -= this_len;
1948 loff = 0;
1949 spd.nr_pages++;
1950 index++;
1951 }
1952
Hugh Dickins708e3502011-07-25 17:12:32 -07001953 while (page_nr < nr_pages)
1954 page_cache_release(spd.pages[page_nr++]);
Hugh Dickins708e3502011-07-25 17:12:32 -07001955
1956 if (spd.nr_pages)
1957 error = splice_to_pipe(pipe, &spd);
1958
1959 splice_shrink_spd(pipe, &spd);
1960
1961 if (error > 0) {
1962 *ppos += error;
1963 file_accessed(in);
1964 }
1965 return error;
1966}
1967
David Howells726c3342006-06-23 02:02:58 -07001968static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
David Howells726c3342006-06-23 02:02:58 -07001970 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 buf->f_type = TMPFS_MAGIC;
1973 buf->f_bsize = PAGE_CACHE_SIZE;
1974 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001975 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 buf->f_blocks = sbinfo->max_blocks;
Tim Chen7e496292010-08-09 17:19:05 -07001977 buf->f_bavail = buf->f_bfree =
1978 sbinfo->max_blocks - percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07001979 }
1980 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 buf->f_files = sbinfo->max_inodes;
1982 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 /* else leave those fields 0 like simple_statfs */
1985 return 0;
1986}
1987
1988/*
1989 * File creation. Allocate an inode, and we're done..
1990 */
1991static int
1992shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1993{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00001994 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int error = -ENOSPC;
1996
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03001997 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if (inode) {
Eric Paris2a7dba32011-02-01 11:05:39 -05001999 error = security_inode_init_security(inode, dir,
2000 &dentry->d_name, NULL,
2001 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002002 if (error) {
2003 if (error != -EOPNOTSUPP) {
2004 iput(inode);
2005 return error;
2006 }
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002007 }
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01002008#ifdef CONFIG_TMPFS_POSIX_ACL
2009 error = generic_acl_init(inode, dir);
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002010 if (error) {
2011 iput(inode);
2012 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002013 }
Al Viro718deb62009-12-16 19:35:36 -05002014#else
2015 error = 0;
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01002016#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 dir->i_size += BOGO_DIRENT_SIZE;
2018 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2019 d_instantiate(dentry, inode);
2020 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 return error;
2023}
2024
2025static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2026{
2027 int error;
2028
2029 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2030 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002031 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return 0;
2033}
2034
2035static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
2036 struct nameidata *nd)
2037{
2038 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2039}
2040
2041/*
2042 * Link a file..
2043 */
2044static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2045{
2046 struct inode *inode = old_dentry->d_inode;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002047 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /*
2050 * No ordinary (disk based) filesystem counts links as inodes;
2051 * but each new link needs a new dentry, pinning lowmem, and
2052 * tmpfs dentries cannot be pruned until they are unlinked.
2053 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002054 ret = shmem_reserve_inode(inode->i_sb);
2055 if (ret)
2056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 dir->i_size += BOGO_DIRENT_SIZE;
2059 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansend8c76e62006-09-30 23:29:04 -07002060 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002061 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 dget(dentry); /* Extra pinning count for the created dentry */
2063 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002064out:
2065 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2069{
2070 struct inode *inode = dentry->d_inode;
2071
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002072 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2073 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 dir->i_size -= BOGO_DIRENT_SIZE;
2076 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002077 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 dput(dentry); /* Undo the count from "create" - this does all the work */
2079 return 0;
2080}
2081
2082static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2083{
2084 if (!simple_empty(dentry))
2085 return -ENOTEMPTY;
2086
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002087 drop_nlink(dentry->d_inode);
2088 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return shmem_unlink(dir, dentry);
2090}
2091
2092/*
2093 * The VFS layer already does all the dentry stuff for rename,
2094 * we just have to decrement the usage count for the target if
2095 * it exists so that the VFS layer correctly free's it when it
2096 * gets overwritten.
2097 */
2098static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2099{
2100 struct inode *inode = old_dentry->d_inode;
2101 int they_are_dirs = S_ISDIR(inode->i_mode);
2102
2103 if (!simple_empty(new_dentry))
2104 return -ENOTEMPTY;
2105
2106 if (new_dentry->d_inode) {
2107 (void) shmem_unlink(new_dir, new_dentry);
2108 if (they_are_dirs)
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002109 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002111 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002112 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114
2115 old_dir->i_size -= BOGO_DIRENT_SIZE;
2116 new_dir->i_size += BOGO_DIRENT_SIZE;
2117 old_dir->i_ctime = old_dir->i_mtime =
2118 new_dir->i_ctime = new_dir->i_mtime =
2119 inode->i_ctime = CURRENT_TIME;
2120 return 0;
2121}
2122
2123static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
2124{
2125 int error;
2126 int len;
2127 struct inode *inode;
2128 struct page *page = NULL;
2129 char *kaddr;
2130 struct shmem_inode_info *info;
2131
2132 len = strlen(symname) + 1;
2133 if (len > PAGE_CACHE_SIZE)
2134 return -ENAMETOOLONG;
2135
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002136 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (!inode)
2138 return -ENOSPC;
2139
Eric Paris2a7dba32011-02-01 11:05:39 -05002140 error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
2141 NULL, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002142 if (error) {
2143 if (error != -EOPNOTSUPP) {
2144 iput(inode);
2145 return error;
2146 }
2147 error = 0;
2148 }
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 info = SHMEM_I(inode);
2151 inode->i_size = len-1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002152 if (len <= SHMEM_SYMLINK_INLINE_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /* do it inline */
Eric Parisb09e0fa2011-05-24 17:12:39 -07002154 memcpy(info->inline_symlink, symname, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 inode->i_op = &shmem_symlink_inline_operations;
2156 } else {
2157 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
2158 if (error) {
2159 iput(inode);
2160 return error;
2161 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07002162 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 inode->i_op = &shmem_symlink_inode_operations;
2164 kaddr = kmap_atomic(page, KM_USER0);
2165 memcpy(kaddr, symname, len);
2166 kunmap_atomic(kaddr, KM_USER0);
2167 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002168 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 page_cache_release(page);
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 dir->i_size += BOGO_DIRENT_SIZE;
2172 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
2173 d_instantiate(dentry, inode);
2174 dget(dentry);
2175 return 0;
2176}
2177
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002178static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Eric Parisb09e0fa2011-05-24 17:12:39 -07002180 nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002181 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002184static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct page *page = NULL;
2187 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
2188 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
Hugh Dickinsd3602442008-02-04 22:28:44 -08002189 if (page)
2190 unlock_page(page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002191 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002194static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 if (!IS_ERR(nd_get_link(nd))) {
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002197 struct page *page = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 kunmap(page);
2199 mark_page_accessed(page);
2200 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202}
2203
Eric Parisb09e0fa2011-05-24 17:12:39 -07002204#ifdef CONFIG_TMPFS_XATTR
2205/*
2206 * Superblocks without xattr inode operations may get some security.* xattr
2207 * support from the LSM "for free". As soon as we have any other xattrs
2208 * like ACLs, we also need to implement the security.* handlers at
2209 * filesystem level, though.
2210 */
2211
2212static int shmem_xattr_get(struct dentry *dentry, const char *name,
2213 void *buffer, size_t size)
2214{
2215 struct shmem_inode_info *info;
2216 struct shmem_xattr *xattr;
2217 int ret = -ENODATA;
2218
2219 info = SHMEM_I(dentry->d_inode);
2220
2221 spin_lock(&info->lock);
2222 list_for_each_entry(xattr, &info->xattr_list, list) {
2223 if (strcmp(name, xattr->name))
2224 continue;
2225
2226 ret = xattr->size;
2227 if (buffer) {
2228 if (size < xattr->size)
2229 ret = -ERANGE;
2230 else
2231 memcpy(buffer, xattr->value, xattr->size);
2232 }
2233 break;
2234 }
2235 spin_unlock(&info->lock);
2236 return ret;
2237}
2238
2239static int shmem_xattr_set(struct dentry *dentry, const char *name,
2240 const void *value, size_t size, int flags)
2241{
2242 struct inode *inode = dentry->d_inode;
2243 struct shmem_inode_info *info = SHMEM_I(inode);
2244 struct shmem_xattr *xattr;
2245 struct shmem_xattr *new_xattr = NULL;
2246 size_t len;
2247 int err = 0;
2248
2249 /* value == NULL means remove */
2250 if (value) {
2251 /* wrap around? */
2252 len = sizeof(*new_xattr) + size;
2253 if (len <= sizeof(*new_xattr))
2254 return -ENOMEM;
2255
2256 new_xattr = kmalloc(len, GFP_KERNEL);
2257 if (!new_xattr)
2258 return -ENOMEM;
2259
2260 new_xattr->name = kstrdup(name, GFP_KERNEL);
2261 if (!new_xattr->name) {
2262 kfree(new_xattr);
2263 return -ENOMEM;
2264 }
2265
2266 new_xattr->size = size;
2267 memcpy(new_xattr->value, value, size);
2268 }
2269
2270 spin_lock(&info->lock);
2271 list_for_each_entry(xattr, &info->xattr_list, list) {
2272 if (!strcmp(name, xattr->name)) {
2273 if (flags & XATTR_CREATE) {
2274 xattr = new_xattr;
2275 err = -EEXIST;
2276 } else if (new_xattr) {
2277 list_replace(&xattr->list, &new_xattr->list);
2278 } else {
2279 list_del(&xattr->list);
2280 }
2281 goto out;
2282 }
2283 }
2284 if (flags & XATTR_REPLACE) {
2285 xattr = new_xattr;
2286 err = -ENODATA;
2287 } else {
2288 list_add(&new_xattr->list, &info->xattr_list);
2289 xattr = NULL;
2290 }
2291out:
2292 spin_unlock(&info->lock);
2293 if (xattr)
2294 kfree(xattr->name);
2295 kfree(xattr);
2296 return err;
2297}
2298
2299
2300static const struct xattr_handler *shmem_xattr_handlers[] = {
2301#ifdef CONFIG_TMPFS_POSIX_ACL
2302 &generic_acl_access_handler,
2303 &generic_acl_default_handler,
2304#endif
2305 NULL
2306};
2307
2308static int shmem_xattr_validate(const char *name)
2309{
2310 struct { const char *prefix; size_t len; } arr[] = {
2311 { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
2312 { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
2313 };
2314 int i;
2315
2316 for (i = 0; i < ARRAY_SIZE(arr); i++) {
2317 size_t preflen = arr[i].len;
2318 if (strncmp(name, arr[i].prefix, preflen) == 0) {
2319 if (!name[preflen])
2320 return -EINVAL;
2321 return 0;
2322 }
2323 }
2324 return -EOPNOTSUPP;
2325}
2326
2327static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
2328 void *buffer, size_t size)
2329{
2330 int err;
2331
2332 /*
2333 * If this is a request for a synthetic attribute in the system.*
2334 * namespace use the generic infrastructure to resolve a handler
2335 * for it via sb->s_xattr.
2336 */
2337 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2338 return generic_getxattr(dentry, name, buffer, size);
2339
2340 err = shmem_xattr_validate(name);
2341 if (err)
2342 return err;
2343
2344 return shmem_xattr_get(dentry, name, buffer, size);
2345}
2346
2347static int shmem_setxattr(struct dentry *dentry, const char *name,
2348 const void *value, size_t size, int flags)
2349{
2350 int err;
2351
2352 /*
2353 * If this is a request for a synthetic attribute in the system.*
2354 * namespace use the generic infrastructure to resolve a handler
2355 * for it via sb->s_xattr.
2356 */
2357 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2358 return generic_setxattr(dentry, name, value, size, flags);
2359
2360 err = shmem_xattr_validate(name);
2361 if (err)
2362 return err;
2363
2364 if (size == 0)
2365 value = ""; /* empty EA, do not remove */
2366
2367 return shmem_xattr_set(dentry, name, value, size, flags);
2368
2369}
2370
2371static int shmem_removexattr(struct dentry *dentry, const char *name)
2372{
2373 int err;
2374
2375 /*
2376 * If this is a request for a synthetic attribute in the system.*
2377 * namespace use the generic infrastructure to resolve a handler
2378 * for it via sb->s_xattr.
2379 */
2380 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
2381 return generic_removexattr(dentry, name);
2382
2383 err = shmem_xattr_validate(name);
2384 if (err)
2385 return err;
2386
2387 return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
2388}
2389
2390static bool xattr_is_trusted(const char *name)
2391{
2392 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
2393}
2394
2395static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
2396{
2397 bool trusted = capable(CAP_SYS_ADMIN);
2398 struct shmem_xattr *xattr;
2399 struct shmem_inode_info *info;
2400 size_t used = 0;
2401
2402 info = SHMEM_I(dentry->d_inode);
2403
2404 spin_lock(&info->lock);
2405 list_for_each_entry(xattr, &info->xattr_list, list) {
2406 size_t len;
2407
2408 /* skip "trusted." attributes for unprivileged callers */
2409 if (!trusted && xattr_is_trusted(xattr->name))
2410 continue;
2411
2412 len = strlen(xattr->name) + 1;
2413 used += len;
2414 if (buffer) {
2415 if (size < used) {
2416 used = -ERANGE;
2417 break;
2418 }
2419 memcpy(buffer, xattr->name, len);
2420 buffer += len;
2421 }
2422 }
2423 spin_unlock(&info->lock);
2424
2425 return used;
2426}
2427#endif /* CONFIG_TMPFS_XATTR */
2428
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002429static const struct inode_operations shmem_symlink_inline_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 .readlink = generic_readlink,
2431 .follow_link = shmem_follow_link_inline,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002432#ifdef CONFIG_TMPFS_XATTR
2433 .setxattr = shmem_setxattr,
2434 .getxattr = shmem_getxattr,
2435 .listxattr = shmem_listxattr,
2436 .removexattr = shmem_removexattr,
2437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438};
2439
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002440static const struct inode_operations shmem_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 .readlink = generic_readlink,
2442 .follow_link = shmem_follow_link,
2443 .put_link = shmem_put_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002444#ifdef CONFIG_TMPFS_XATTR
2445 .setxattr = shmem_setxattr,
2446 .getxattr = shmem_getxattr,
2447 .listxattr = shmem_listxattr,
2448 .removexattr = shmem_removexattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002449#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002450};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002451
David M. Grimes91828a42006-10-17 00:09:45 -07002452static struct dentry *shmem_get_parent(struct dentry *child)
2453{
2454 return ERR_PTR(-ESTALE);
2455}
2456
2457static int shmem_match(struct inode *ino, void *vfh)
2458{
2459 __u32 *fh = vfh;
2460 __u64 inum = fh[2];
2461 inum = (inum << 32) | fh[1];
2462 return ino->i_ino == inum && fh[0] == ino->i_generation;
2463}
2464
Christoph Hellwig480b1162007-10-21 16:42:13 -07002465static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2466 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07002467{
David M. Grimes91828a42006-10-17 00:09:45 -07002468 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07002469 struct dentry *dentry = NULL;
2470 u64 inum = fid->raw[2];
2471 inum = (inum << 32) | fid->raw[1];
David M. Grimes91828a42006-10-17 00:09:45 -07002472
Christoph Hellwig480b1162007-10-21 16:42:13 -07002473 if (fh_len < 3)
2474 return NULL;
2475
2476 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2477 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07002478 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07002479 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002480 iput(inode);
2481 }
2482
Christoph Hellwig480b1162007-10-21 16:42:13 -07002483 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07002484}
2485
2486static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2487 int connectable)
2488{
2489 struct inode *inode = dentry->d_inode;
2490
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302491 if (*len < 3) {
2492 *len = 3;
David M. Grimes91828a42006-10-17 00:09:45 -07002493 return 255;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05302494 }
David M. Grimes91828a42006-10-17 00:09:45 -07002495
Al Viro1d3382cb2010-10-23 15:19:20 -04002496 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07002497 /* Unfortunately insert_inode_hash is not idempotent,
2498 * so as we hash inodes here rather than at creation
2499 * time, we need a lock to ensure we only try
2500 * to do it once
2501 */
2502 static DEFINE_SPINLOCK(lock);
2503 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04002504 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07002505 __insert_inode_hash(inode,
2506 inode->i_ino + inode->i_generation);
2507 spin_unlock(&lock);
2508 }
2509
2510 fh[0] = inode->i_generation;
2511 fh[1] = inode->i_ino;
2512 fh[2] = ((__u64)inode->i_ino) >> 32;
2513
2514 *len = 3;
2515 return 1;
2516}
2517
Christoph Hellwig39655162007-10-21 16:42:17 -07002518static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07002519 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07002520 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07002521 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07002522};
2523
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002524static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2525 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 char *this_char, *value, *rest;
2528
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00002529 while (options != NULL) {
2530 this_char = options;
2531 for (;;) {
2532 /*
2533 * NUL-terminate this option: unfortunately,
2534 * mount options form a comma-separated list,
2535 * but mpol's nodelist may also contain commas.
2536 */
2537 options = strchr(options, ',');
2538 if (options == NULL)
2539 break;
2540 options++;
2541 if (!isdigit(*options)) {
2542 options[-1] = '\0';
2543 break;
2544 }
2545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (!*this_char)
2547 continue;
2548 if ((value = strchr(this_char,'=')) != NULL) {
2549 *value++ = 0;
2550 } else {
2551 printk(KERN_ERR
2552 "tmpfs: No value for mount option '%s'\n",
2553 this_char);
2554 return 1;
2555 }
2556
2557 if (!strcmp(this_char,"size")) {
2558 unsigned long long size;
2559 size = memparse(value,&rest);
2560 if (*rest == '%') {
2561 size <<= PAGE_SHIFT;
2562 size *= totalram_pages;
2563 do_div(size, 100);
2564 rest++;
2565 }
2566 if (*rest)
2567 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002568 sbinfo->max_blocks =
2569 DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002571 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (*rest)
2573 goto bad_val;
2574 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002575 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (*rest)
2577 goto bad_val;
2578 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002579 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002581 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (*rest)
2583 goto bad_val;
2584 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002585 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002587 sbinfo->uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (*rest)
2589 goto bad_val;
2590 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002591 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002593 sbinfo->gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (*rest)
2595 goto bad_val;
Robin Holt7339ff82006-01-14 13:20:48 -08002596 } else if (!strcmp(this_char,"mpol")) {
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002597 if (mpol_parse_str(value, &sbinfo->mpol, 1))
Robin Holt7339ff82006-01-14 13:20:48 -08002598 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 } else {
2600 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2601 this_char);
2602 return 1;
2603 }
2604 }
2605 return 0;
2606
2607bad_val:
2608 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2609 value, this_char);
2610 return 1;
2611
2612}
2613
2614static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2615{
2616 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002617 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002618 unsigned long inodes;
2619 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002621 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002622 return error;
2623
2624 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002625 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07002626 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002627 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002628 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002629 goto out;
2630 /*
2631 * Those tests also disallow limited->unlimited while any are in
2632 * use, so i_blocks will always be zero when max_blocks is zero;
2633 * but we must separately disallow unlimited->limited, because
2634 * in that case we have no record of how much is already in use.
2635 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002636 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002637 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002638 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002639 goto out;
2640
2641 error = 0;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002642 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002643 sbinfo->max_inodes = config.max_inodes;
2644 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002645
2646 mpol_put(sbinfo->mpol);
2647 sbinfo->mpol = config.mpol; /* transfers initial ref */
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002648out:
2649 spin_unlock(&sbinfo->stat_lock);
2650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002652
2653static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2654{
2655 struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2656
2657 if (sbinfo->max_blocks != shmem_default_max_blocks())
2658 seq_printf(seq, ",size=%luk",
2659 sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2660 if (sbinfo->max_inodes != shmem_default_max_inodes())
2661 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2662 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2663 seq_printf(seq, ",mode=%03o", sbinfo->mode);
2664 if (sbinfo->uid != 0)
2665 seq_printf(seq, ",uid=%u", sbinfo->uid);
2666 if (sbinfo->gid != 0)
2667 seq_printf(seq, ",gid=%u", sbinfo->gid);
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002668 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002669 return 0;
2670}
2671#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673static void shmem_put_super(struct super_block *sb)
2674{
Hugh Dickins602586a2010-08-17 15:23:56 -07002675 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2676
2677 percpu_counter_destroy(&sbinfo->used_blocks);
2678 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 sb->s_fs_info = NULL;
2680}
2681
Kay Sievers2b2af542009-04-30 15:23:42 +02002682int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 struct inode *inode;
2685 struct dentry *root;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002686 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002687 int err = -ENOMEM;
2688
2689 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07002690 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002691 L1_CACHE_BYTES), GFP_KERNEL);
2692 if (!sbinfo)
2693 return -ENOMEM;
2694
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002695 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11002696 sbinfo->uid = current_fsuid();
2697 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002698 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002700#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /*
2702 * Per default we only allow half of the physical ram per
2703 * tmpfs instance, limiting inodes to one per page of lowmem;
2704 * but the internal instance is left unlimited.
2705 */
2706 if (!(sb->s_flags & MS_NOUSER)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002707 sbinfo->max_blocks = shmem_default_max_blocks();
2708 sbinfo->max_inodes = shmem_default_max_inodes();
2709 if (shmem_parse_options(data, sbinfo, false)) {
2710 err = -EINVAL;
2711 goto failed;
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
David M. Grimes91828a42006-10-17 00:09:45 -07002714 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715#else
2716 sb->s_flags |= MS_NOUSER;
2717#endif
2718
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002719 spin_lock_init(&sbinfo->stat_lock);
Hugh Dickins602586a2010-08-17 15:23:56 -07002720 if (percpu_counter_init(&sbinfo->used_blocks, 0))
2721 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002722 sbinfo->free_inodes = sbinfo->max_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 sb->s_maxbytes = SHMEM_MAX_BYTES;
2725 sb->s_blocksize = PAGE_CACHE_SIZE;
2726 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2727 sb->s_magic = TMPFS_MAGIC;
2728 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01002729 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002730#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002731 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07002732#endif
2733#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002734 sb->s_flags |= MS_POSIXACL;
2735#endif
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002736
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002737 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (!inode)
2739 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002740 inode->i_uid = sbinfo->uid;
2741 inode->i_gid = sbinfo->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 root = d_alloc_root(inode);
2743 if (!root)
2744 goto failed_iput;
2745 sb->s_root = root;
2746 return 0;
2747
2748failed_iput:
2749 iput(inode);
2750failed:
2751 shmem_put_super(sb);
2752 return err;
2753}
2754
Pekka Enbergfcc234f2006-03-22 00:08:13 -08002755static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757static struct inode *shmem_alloc_inode(struct super_block *sb)
2758{
2759 struct shmem_inode_info *p;
Christoph Lametere94b1762006-12-06 20:33:17 -08002760 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (!p)
2762 return NULL;
2763 return &p->vfs_inode;
2764}
2765
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002766static void shmem_i_callback(struct rcu_head *head)
2767{
2768 struct inode *inode = container_of(head, struct inode, i_rcu);
2769 INIT_LIST_HEAD(&inode->i_dentry);
2770 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2771}
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773static void shmem_destroy_inode(struct inode *inode)
2774{
2775 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2776 /* only struct inode is valid if it's an inline symlink */
2777 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2778 }
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11002779 call_rcu(&inode->i_rcu, shmem_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002782static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2785
Christoph Lametera35afb82007-05-16 22:10:57 -07002786 inode_init_once(&p->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789static int init_inodecache(void)
2790{
2791 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2792 sizeof(struct shmem_inode_info),
Alexey Dobriyan040b5c62007-10-16 23:26:10 -07002793 0, SLAB_PANIC, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return 0;
2795}
2796
2797static void destroy_inodecache(void)
2798{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07002799 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07002802static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08002804 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805#ifdef CONFIG_TMPFS
Hugh Dickinsae9764162007-06-04 10:00:39 +02002806 .readpage = shmem_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -07002807 .write_begin = shmem_write_begin,
2808 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809#endif
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07002810 .migratepage = migrate_page,
Andi Kleenaa261f52009-09-16 11:50:16 +02002811 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812};
2813
Helge Deller15ad7cd2006-12-06 20:40:36 -08002814static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 .mmap = shmem_mmap,
2816#ifdef CONFIG_TMPFS
2817 .llseek = generic_file_llseek,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002818 .read = do_sync_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002819 .write = do_sync_write,
Hugh Dickinsbcd78e42008-07-23 21:27:35 -07002820 .aio_read = shmem_file_aio_read,
Hugh Dickins5402b972008-02-04 22:28:44 -08002821 .aio_write = generic_file_aio_write,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02002822 .fsync = noop_fsync,
Hugh Dickins708e3502011-07-25 17:12:32 -07002823 .splice_read = shmem_file_splice_read,
Hugh Dickinsae9764162007-06-04 10:00:39 +02002824 .splice_write = generic_file_splice_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825#endif
2826};
2827
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002828static const struct inode_operations shmem_inode_operations = {
Hugh Dickins94c1e622011-06-27 16:18:03 -07002829 .setattr = shmem_setattr,
Badari Pulavartyf6b3ec22006-01-06 00:10:38 -08002830 .truncate_range = shmem_truncate_range,
Eric Parisb09e0fa2011-05-24 17:12:39 -07002831#ifdef CONFIG_TMPFS_XATTR
2832 .setxattr = shmem_setxattr,
2833 .getxattr = shmem_getxattr,
2834 .listxattr = shmem_listxattr,
2835 .removexattr = shmem_removexattr,
2836#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002837#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01002838 .check_acl = generic_check_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002839#endif
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841};
2842
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002843static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844#ifdef CONFIG_TMPFS
2845 .create = shmem_create,
2846 .lookup = simple_lookup,
2847 .link = shmem_link,
2848 .unlink = shmem_unlink,
2849 .symlink = shmem_symlink,
2850 .mkdir = shmem_mkdir,
2851 .rmdir = shmem_rmdir,
2852 .mknod = shmem_mknod,
2853 .rename = shmem_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07002855#ifdef CONFIG_TMPFS_XATTR
2856 .setxattr = shmem_setxattr,
2857 .getxattr = shmem_getxattr,
2858 .listxattr = shmem_listxattr,
2859 .removexattr = shmem_removexattr,
2860#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002861#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002862 .setattr = shmem_setattr,
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01002863 .check_acl = generic_check_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002864#endif
2865};
2866
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002867static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07002868#ifdef CONFIG_TMPFS_XATTR
2869 .setxattr = shmem_setxattr,
2870 .getxattr = shmem_getxattr,
2871 .listxattr = shmem_listxattr,
2872 .removexattr = shmem_removexattr,
2873#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002874#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07002875 .setattr = shmem_setattr,
Christoph Hellwig1c7c4742009-11-03 16:44:44 +01002876 .check_acl = generic_check_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878};
2879
Hugh Dickins759b9772007-03-05 00:30:28 -08002880static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 .alloc_inode = shmem_alloc_inode,
2882 .destroy_inode = shmem_destroy_inode,
2883#ifdef CONFIG_TMPFS
2884 .statfs = shmem_statfs,
2885 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08002886 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887#endif
Al Viro1f895f72010-06-05 19:10:41 -04002888 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 .drop_inode = generic_delete_inode,
2890 .put_super = shmem_put_super,
2891};
2892
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002893static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07002894 .fault = shmem_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895#ifdef CONFIG_NUMA
2896 .set_policy = shmem_set_policy,
2897 .get_policy = shmem_get_policy,
2898#endif
2899};
2900
2901
Al Viro3c26ff62010-07-25 11:46:36 +04002902static struct dentry *shmem_mount(struct file_system_type *fs_type,
2903 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Al Viro3c26ff62010-07-25 11:46:36 +04002905 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908static struct file_system_type tmpfs_fs_type = {
2909 .owner = THIS_MODULE,
2910 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04002911 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 .kill_sb = kill_litter_super,
2913};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Kay Sievers2b2af542009-04-30 15:23:42 +02002915int __init init_tmpfs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 int error;
2918
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002919 error = bdi_init(&shmem_backing_dev_info);
2920 if (error)
2921 goto out4;
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 error = init_inodecache();
2924 if (error)
2925 goto out3;
2926
2927 error = register_filesystem(&tmpfs_fs_type);
2928 if (error) {
2929 printk(KERN_ERR "Could not register tmpfs\n");
2930 goto out2;
2931 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07002932
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -04002933 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 tmpfs_fs_type.name, NULL);
2935 if (IS_ERR(shm_mnt)) {
2936 error = PTR_ERR(shm_mnt);
2937 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2938 goto out1;
2939 }
2940 return 0;
2941
2942out1:
2943 unregister_filesystem(&tmpfs_fs_type);
2944out2:
2945 destroy_inodecache();
2946out3:
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07002947 bdi_destroy(&shmem_backing_dev_info);
2948out4:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 shm_mnt = ERR_PTR(error);
2950 return error;
2951}
Matt Mackall853ac432009-01-06 14:40:20 -08002952
Daisuke Nishimura87946a72010-05-26 14:42:39 -07002953#ifdef CONFIG_CGROUP_MEM_RES_CTLR
2954/**
2955 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
2956 * @inode: the inode to be searched
2957 * @pgoff: the offset to be searched
2958 * @pagep: the pointer for the found page to be stored
2959 * @ent: the pointer for the found swap entry to be stored
2960 *
2961 * If a page is found, refcount of it is incremented. Callers should handle
2962 * these refcount.
2963 */
2964void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
2965 struct page **pagep, swp_entry_t *ent)
2966{
2967 swp_entry_t entry = { .val = 0 }, *ptr;
2968 struct page *page = NULL;
2969 struct shmem_inode_info *info = SHMEM_I(inode);
2970
2971 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
2972 goto out;
2973
2974 spin_lock(&info->lock);
2975 ptr = shmem_swp_entry(info, pgoff, NULL);
2976#ifdef CONFIG_SWAP
2977 if (ptr && ptr->val) {
2978 entry.val = ptr->val;
2979 page = find_get_page(&swapper_space, entry.val);
2980 } else
2981#endif
2982 page = find_get_page(inode->i_mapping, pgoff);
2983 if (ptr)
2984 shmem_swp_unmap(ptr);
2985 spin_unlock(&info->lock);
2986out:
2987 *pagep = page;
2988 *ent = entry;
2989}
2990#endif
2991
Matt Mackall853ac432009-01-06 14:40:20 -08002992#else /* !CONFIG_SHMEM */
2993
2994/*
2995 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
2996 *
2997 * This is intended for small system where the benefits of the full
2998 * shmem code (swap-backed and resource-limited) are outweighed by
2999 * their complexity. On systems without swap this code should be
3000 * effectively equivalent, but much lighter weight.
3001 */
3002
3003#include <linux/ramfs.h>
3004
3005static struct file_system_type tmpfs_fs_type = {
3006 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003007 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003008 .kill_sb = kill_litter_super,
3009};
3010
Kay Sievers2b2af542009-04-30 15:23:42 +02003011int __init init_tmpfs(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003012{
3013 BUG_ON(register_filesystem(&tmpfs_fs_type) != 0);
3014
3015 shm_mnt = kern_mount(&tmpfs_fs_type);
3016 BUG_ON(IS_ERR(shm_mnt));
3017
3018 return 0;
3019}
3020
3021int shmem_unuse(swp_entry_t entry, struct page *page)
3022{
3023 return 0;
3024}
3025
Hugh Dickins3f96b792009-09-21 17:03:37 -07003026int shmem_lock(struct file *file, int lock, struct user_struct *user)
3027{
3028 return 0;
3029}
3030
Hugh Dickins94c1e622011-06-27 16:18:03 -07003031void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
3032{
3033 truncate_inode_pages_range(inode->i_mapping, start, end);
3034}
3035EXPORT_SYMBOL_GPL(shmem_truncate_range);
3036
Daisuke Nishimura87946a72010-05-26 14:42:39 -07003037#ifdef CONFIG_CGROUP_MEM_RES_CTLR
3038/**
3039 * mem_cgroup_get_shmem_target - find a page or entry assigned to the shmem file
3040 * @inode: the inode to be searched
3041 * @pgoff: the offset to be searched
3042 * @pagep: the pointer for the found page to be stored
3043 * @ent: the pointer for the found swap entry to be stored
3044 *
3045 * If a page is found, refcount of it is incremented. Callers should handle
3046 * these refcount.
3047 */
3048void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t pgoff,
3049 struct page **pagep, swp_entry_t *ent)
3050{
3051 struct page *page = NULL;
3052
3053 if ((pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
3054 goto out;
3055 page = find_get_page(inode->i_mapping, pgoff);
3056out:
3057 *pagep = page;
3058 *ent = (swp_entry_t){ .val = 0 };
3059}
3060#endif
3061
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003062#define shmem_vm_ops generic_file_vm_ops
3063#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003064#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003065#define shmem_acct_size(flags, size) 0
3066#define shmem_unacct_size(flags, size) do {} while (0)
Hugh Dickinscaefba12009-04-13 14:40:12 -07003067#define SHMEM_MAX_BYTES MAX_LFS_FILESIZE
Matt Mackall853ac432009-01-06 14:40:20 -08003068
3069#endif /* CONFIG_SHMEM */
3070
3071/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Randy Dunlap46711812008-03-19 17:00:41 -07003073/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 * shmem_file_setup - get an unlinked file living in tmpfs
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 * @name: name for dentry (to be seen in /proc/<pid>/maps
3076 * @size: size to be set for the file
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003077 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Sergei Trofimovich168f5ac2009-06-16 15:33:02 -07003079struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 int error;
3082 struct file *file;
3083 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04003084 struct path path;
3085 struct dentry *root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 struct qstr this;
3087
3088 if (IS_ERR(shm_mnt))
3089 return (void *)shm_mnt;
3090
3091 if (size < 0 || size > SHMEM_MAX_BYTES)
3092 return ERR_PTR(-EINVAL);
3093
3094 if (shmem_acct_size(flags, size))
3095 return ERR_PTR(-ENOMEM);
3096
3097 error = -ENOMEM;
3098 this.name = name;
3099 this.len = strlen(name);
3100 this.hash = 0; /* will go */
3101 root = shm_mnt->mnt_root;
Al Viro2c48b9c2009-08-09 00:52:35 +04003102 path.dentry = d_alloc(root, &this);
3103 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 goto put_memory;
Al Viro2c48b9c2009-08-09 00:52:35 +04003105 path.mnt = mntget(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 error = -ENOSPC;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003108 inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (!inode)
Al Viro4b42af82009-08-05 18:25:56 +04003110 goto put_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Al Viro2c48b9c2009-08-09 00:52:35 +04003112 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 inode->i_size = size;
3114 inode->i_nlink = 0; /* It is unlinked */
Matt Mackall853ac432009-01-06 14:40:20 -08003115#ifndef CONFIG_MMU
3116 error = ramfs_nommu_expand_for_mapping(inode, size);
3117 if (error)
Al Viro4b42af82009-08-05 18:25:56 +04003118 goto put_dentry;
Matt Mackall853ac432009-01-06 14:40:20 -08003119#endif
Al Viro4b42af82009-08-05 18:25:56 +04003120
3121 error = -ENFILE;
Al Viro2c48b9c2009-08-09 00:52:35 +04003122 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04003123 &shmem_file_operations);
3124 if (!file)
3125 goto put_dentry;
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return file;
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129put_dentry:
Al Viro2c48b9c2009-08-09 00:52:35 +04003130 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131put_memory:
3132 shmem_unacct_size(flags, size);
3133 return ERR_PTR(error);
3134}
Keith Packard395e0dd2008-06-20 00:08:06 -07003135EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Randy Dunlap46711812008-03-19 17:00:41 -07003137/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3140 */
3141int shmem_zero_setup(struct vm_area_struct *vma)
3142{
3143 struct file *file;
3144 loff_t size = vma->vm_end - vma->vm_start;
3145
3146 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
3147 if (IS_ERR(file))
3148 return PTR_ERR(file);
3149
3150 if (vma->vm_file)
3151 fput(vma->vm_file);
3152 vma->vm_file = file;
3153 vma->vm_ops = &shmem_vm_ops;
Hugh Dickinsbee4c36a2011-03-22 16:33:43 -07003154 vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
3156}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003157
3158/**
3159 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
3160 * @mapping: the page's address_space
3161 * @index: the page index
3162 * @gfp: the page allocator flags to use if allocating
3163 *
3164 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
3165 * with any new page allocations done using the specified allocation flags.
3166 * But read_cache_page_gfp() uses the ->readpage() method: which does not
3167 * suit tmpfs, since it may have pages in swapcache, and needs to find those
3168 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
3169 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07003170 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
3171 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003172 */
3173struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
3174 pgoff_t index, gfp_t gfp)
3175{
Hugh Dickins68da9f02011-07-25 17:12:34 -07003176#ifdef CONFIG_SHMEM
3177 struct inode *inode = mapping->host;
3178 struct page *page = NULL;
3179 int error;
3180
3181 BUG_ON(mapping->a_ops != &shmem_aops);
3182 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
3183 if (error)
3184 page = ERR_PTR(error);
3185 else
3186 unlock_page(page);
3187 return page;
3188#else
3189 /*
3190 * The tiny !SHMEM case uses ramfs without swap
3191 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003192 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07003193#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07003194}
3195EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);