shmem.c revision 71fe804b6d56d6a7aed680e096901434cef6a2c3
1/*
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.
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
11 * 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 *
17 * This file is released under the GPL.
18 */
19
20/*
21 * This virtual memory filesystem is heavily based on the ramfs. It
22 * extends ramfs by the ability to use swap and honor resource limits
23 * which makes it a completely usable filesystem.
24 */
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/xattr.h>
30#include <linux/exportfs.h>
31#include <linux/generic_acl.h>
32#include <linux/mm.h>
33#include <linux/mman.h>
34#include <linux/file.h>
35#include <linux/swap.h>
36#include <linux/pagemap.h>
37#include <linux/string.h>
38#include <linux/slab.h>
39#include <linux/backing-dev.h>
40#include <linux/shmem_fs.h>
41#include <linux/mount.h>
42#include <linux/writeback.h>
43#include <linux/vfs.h>
44#include <linux/blkdev.h>
45#include <linux/security.h>
46#include <linux/swapops.h>
47#include <linux/mempolicy.h>
48#include <linux/namei.h>
49#include <linux/ctype.h>
50#include <linux/migrate.h>
51#include <linux/highmem.h>
52#include <linux/seq_file.h>
53
54#include <asm/uaccess.h>
55#include <asm/div64.h>
56#include <asm/pgtable.h>
57
58/* This magic number is used in glibc for posix shared memory */
59#define TMPFS_MAGIC	0x01021994
60
61#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
62#define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
63#define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
64
65#define SHMEM_MAX_INDEX  (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
66#define SHMEM_MAX_BYTES  ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
67
68#define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
69
70/* info->flags needs VM_flags to handle pagein/truncate races efficiently */
71#define SHMEM_PAGEIN	 VM_READ
72#define SHMEM_TRUNCATE	 VM_WRITE
73
74/* Definition to limit shmem_truncate's steps between cond_rescheds */
75#define LATENCY_LIMIT	 64
76
77/* Pretend that each entry is of this size in directory's i_size */
78#define BOGO_DIRENT_SIZE 20
79
80/* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
81enum sgp_type {
82	SGP_READ,	/* don't exceed i_size, don't allocate page */
83	SGP_CACHE,	/* don't exceed i_size, may allocate page */
84	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
85	SGP_WRITE,	/* may exceed i_size, may allocate page */
86};
87
88#ifdef CONFIG_TMPFS
89static unsigned long shmem_default_max_blocks(void)
90{
91	return totalram_pages / 2;
92}
93
94static unsigned long shmem_default_max_inodes(void)
95{
96	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
97}
98#endif
99
100static int shmem_getpage(struct inode *inode, unsigned long idx,
101			 struct page **pagep, enum sgp_type sgp, int *type);
102
103static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
104{
105	/*
106	 * The above definition of ENTRIES_PER_PAGE, and the use of
107	 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
108	 * might be reconsidered if it ever diverges from PAGE_SIZE.
109	 *
110	 * Mobility flags are masked out as swap vectors cannot move
111	 */
112	return alloc_pages((gfp_mask & ~GFP_MOVABLE_MASK) | __GFP_ZERO,
113				PAGE_CACHE_SHIFT-PAGE_SHIFT);
114}
115
116static inline void shmem_dir_free(struct page *page)
117{
118	__free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
119}
120
121static struct page **shmem_dir_map(struct page *page)
122{
123	return (struct page **)kmap_atomic(page, KM_USER0);
124}
125
126static inline void shmem_dir_unmap(struct page **dir)
127{
128	kunmap_atomic(dir, KM_USER0);
129}
130
131static swp_entry_t *shmem_swp_map(struct page *page)
132{
133	return (swp_entry_t *)kmap_atomic(page, KM_USER1);
134}
135
136static inline void shmem_swp_balance_unmap(void)
137{
138	/*
139	 * When passing a pointer to an i_direct entry, to code which
140	 * also handles indirect entries and so will shmem_swp_unmap,
141	 * we must arrange for the preempt count to remain in balance.
142	 * What kmap_atomic of a lowmem page does depends on config
143	 * and architecture, so pretend to kmap_atomic some lowmem page.
144	 */
145	(void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
146}
147
148static inline void shmem_swp_unmap(swp_entry_t *entry)
149{
150	kunmap_atomic(entry, KM_USER1);
151}
152
153static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
154{
155	return sb->s_fs_info;
156}
157
158/*
159 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
160 * for shared memory and for shared anonymous (/dev/zero) mappings
161 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
162 * consistent with the pre-accounting of private mappings ...
163 */
164static inline int shmem_acct_size(unsigned long flags, loff_t size)
165{
166	return (flags & VM_ACCOUNT)?
167		security_vm_enough_memory(VM_ACCT(size)): 0;
168}
169
170static inline void shmem_unacct_size(unsigned long flags, loff_t size)
171{
172	if (flags & VM_ACCOUNT)
173		vm_unacct_memory(VM_ACCT(size));
174}
175
176/*
177 * ... whereas tmpfs objects are accounted incrementally as
178 * pages are allocated, in order to allow huge sparse files.
179 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
180 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
181 */
182static inline int shmem_acct_block(unsigned long flags)
183{
184	return (flags & VM_ACCOUNT)?
185		0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
186}
187
188static inline void shmem_unacct_blocks(unsigned long flags, long pages)
189{
190	if (!(flags & VM_ACCOUNT))
191		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
192}
193
194static const struct super_operations shmem_ops;
195static const struct address_space_operations shmem_aops;
196static const struct file_operations shmem_file_operations;
197static const struct inode_operations shmem_inode_operations;
198static const struct inode_operations shmem_dir_inode_operations;
199static const struct inode_operations shmem_special_inode_operations;
200static struct vm_operations_struct shmem_vm_ops;
201
202static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
203	.ra_pages	= 0,	/* No readahead */
204	.capabilities	= BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
205	.unplug_io_fn	= default_unplug_io_fn,
206};
207
208static LIST_HEAD(shmem_swaplist);
209static DEFINE_MUTEX(shmem_swaplist_mutex);
210
211static void shmem_free_blocks(struct inode *inode, long pages)
212{
213	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
214	if (sbinfo->max_blocks) {
215		spin_lock(&sbinfo->stat_lock);
216		sbinfo->free_blocks += pages;
217		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
218		spin_unlock(&sbinfo->stat_lock);
219	}
220}
221
222static int shmem_reserve_inode(struct super_block *sb)
223{
224	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
225	if (sbinfo->max_inodes) {
226		spin_lock(&sbinfo->stat_lock);
227		if (!sbinfo->free_inodes) {
228			spin_unlock(&sbinfo->stat_lock);
229			return -ENOSPC;
230		}
231		sbinfo->free_inodes--;
232		spin_unlock(&sbinfo->stat_lock);
233	}
234	return 0;
235}
236
237static void shmem_free_inode(struct super_block *sb)
238{
239	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
240	if (sbinfo->max_inodes) {
241		spin_lock(&sbinfo->stat_lock);
242		sbinfo->free_inodes++;
243		spin_unlock(&sbinfo->stat_lock);
244	}
245}
246
247/**
248 * shmem_recalc_inode - recalculate the size of an inode
249 * @inode: inode to recalc
250 *
251 * We have to calculate the free blocks since the mm can drop
252 * undirtied hole pages behind our back.
253 *
254 * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
255 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
256 *
257 * It has to be called with the spinlock held.
258 */
259static void shmem_recalc_inode(struct inode *inode)
260{
261	struct shmem_inode_info *info = SHMEM_I(inode);
262	long freed;
263
264	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
265	if (freed > 0) {
266		info->alloced -= freed;
267		shmem_unacct_blocks(info->flags, freed);
268		shmem_free_blocks(inode, freed);
269	}
270}
271
272/**
273 * shmem_swp_entry - find the swap vector position in the info structure
274 * @info:  info structure for the inode
275 * @index: index of the page to find
276 * @page:  optional page to add to the structure. Has to be preset to
277 *         all zeros
278 *
279 * If there is no space allocated yet it will return NULL when
280 * page is NULL, else it will use the page for the needed block,
281 * setting it to NULL on return to indicate that it has been used.
282 *
283 * The swap vector is organized the following way:
284 *
285 * There are SHMEM_NR_DIRECT entries directly stored in the
286 * shmem_inode_info structure. So small files do not need an addional
287 * allocation.
288 *
289 * For pages with index > SHMEM_NR_DIRECT there is the pointer
290 * i_indirect which points to a page which holds in the first half
291 * doubly indirect blocks, in the second half triple indirect blocks:
292 *
293 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
294 * following layout (for SHMEM_NR_DIRECT == 16):
295 *
296 * i_indirect -> dir --> 16-19
297 * 	      |	     +-> 20-23
298 * 	      |
299 * 	      +-->dir2 --> 24-27
300 * 	      |	       +-> 28-31
301 * 	      |	       +-> 32-35
302 * 	      |	       +-> 36-39
303 * 	      |
304 * 	      +-->dir3 --> 40-43
305 * 	       	       +-> 44-47
306 * 	      	       +-> 48-51
307 * 	      	       +-> 52-55
308 */
309static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
310{
311	unsigned long offset;
312	struct page **dir;
313	struct page *subdir;
314
315	if (index < SHMEM_NR_DIRECT) {
316		shmem_swp_balance_unmap();
317		return info->i_direct+index;
318	}
319	if (!info->i_indirect) {
320		if (page) {
321			info->i_indirect = *page;
322			*page = NULL;
323		}
324		return NULL;			/* need another page */
325	}
326
327	index -= SHMEM_NR_DIRECT;
328	offset = index % ENTRIES_PER_PAGE;
329	index /= ENTRIES_PER_PAGE;
330	dir = shmem_dir_map(info->i_indirect);
331
332	if (index >= ENTRIES_PER_PAGE/2) {
333		index -= ENTRIES_PER_PAGE/2;
334		dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
335		index %= ENTRIES_PER_PAGE;
336		subdir = *dir;
337		if (!subdir) {
338			if (page) {
339				*dir = *page;
340				*page = NULL;
341			}
342			shmem_dir_unmap(dir);
343			return NULL;		/* need another page */
344		}
345		shmem_dir_unmap(dir);
346		dir = shmem_dir_map(subdir);
347	}
348
349	dir += index;
350	subdir = *dir;
351	if (!subdir) {
352		if (!page || !(subdir = *page)) {
353			shmem_dir_unmap(dir);
354			return NULL;		/* need a page */
355		}
356		*dir = subdir;
357		*page = NULL;
358	}
359	shmem_dir_unmap(dir);
360	return shmem_swp_map(subdir) + offset;
361}
362
363static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
364{
365	long incdec = value? 1: -1;
366
367	entry->val = value;
368	info->swapped += incdec;
369	if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
370		struct page *page = kmap_atomic_to_page(entry);
371		set_page_private(page, page_private(page) + incdec);
372	}
373}
374
375/**
376 * shmem_swp_alloc - get the position of the swap entry for the page.
377 * @info:	info structure for the inode
378 * @index:	index of the page to find
379 * @sgp:	check and recheck i_size? skip allocation?
380 *
381 * If the entry does not exist, allocate it.
382 */
383static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
384{
385	struct inode *inode = &info->vfs_inode;
386	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
387	struct page *page = NULL;
388	swp_entry_t *entry;
389
390	if (sgp != SGP_WRITE &&
391	    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
392		return ERR_PTR(-EINVAL);
393
394	while (!(entry = shmem_swp_entry(info, index, &page))) {
395		if (sgp == SGP_READ)
396			return shmem_swp_map(ZERO_PAGE(0));
397		/*
398		 * Test free_blocks against 1 not 0, since we have 1 data
399		 * page (and perhaps indirect index pages) yet to allocate:
400		 * a waste to allocate index if we cannot allocate data.
401		 */
402		if (sbinfo->max_blocks) {
403			spin_lock(&sbinfo->stat_lock);
404			if (sbinfo->free_blocks <= 1) {
405				spin_unlock(&sbinfo->stat_lock);
406				return ERR_PTR(-ENOSPC);
407			}
408			sbinfo->free_blocks--;
409			inode->i_blocks += BLOCKS_PER_PAGE;
410			spin_unlock(&sbinfo->stat_lock);
411		}
412
413		spin_unlock(&info->lock);
414		page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
415		if (page)
416			set_page_private(page, 0);
417		spin_lock(&info->lock);
418
419		if (!page) {
420			shmem_free_blocks(inode, 1);
421			return ERR_PTR(-ENOMEM);
422		}
423		if (sgp != SGP_WRITE &&
424		    ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
425			entry = ERR_PTR(-EINVAL);
426			break;
427		}
428		if (info->next_index <= index)
429			info->next_index = index + 1;
430	}
431	if (page) {
432		/* another task gave its page, or truncated the file */
433		shmem_free_blocks(inode, 1);
434		shmem_dir_free(page);
435	}
436	if (info->next_index <= index && !IS_ERR(entry))
437		info->next_index = index + 1;
438	return entry;
439}
440
441/**
442 * shmem_free_swp - free some swap entries in a directory
443 * @dir:        pointer to the directory
444 * @edir:       pointer after last entry of the directory
445 * @punch_lock: pointer to spinlock when needed for the holepunch case
446 */
447static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
448						spinlock_t *punch_lock)
449{
450	spinlock_t *punch_unlock = NULL;
451	swp_entry_t *ptr;
452	int freed = 0;
453
454	for (ptr = dir; ptr < edir; ptr++) {
455		if (ptr->val) {
456			if (unlikely(punch_lock)) {
457				punch_unlock = punch_lock;
458				punch_lock = NULL;
459				spin_lock(punch_unlock);
460				if (!ptr->val)
461					continue;
462			}
463			free_swap_and_cache(*ptr);
464			*ptr = (swp_entry_t){0};
465			freed++;
466		}
467	}
468	if (punch_unlock)
469		spin_unlock(punch_unlock);
470	return freed;
471}
472
473static int shmem_map_and_free_swp(struct page *subdir, int offset,
474		int limit, struct page ***dir, spinlock_t *punch_lock)
475{
476	swp_entry_t *ptr;
477	int freed = 0;
478
479	ptr = shmem_swp_map(subdir);
480	for (; offset < limit; offset += LATENCY_LIMIT) {
481		int size = limit - offset;
482		if (size > LATENCY_LIMIT)
483			size = LATENCY_LIMIT;
484		freed += shmem_free_swp(ptr+offset, ptr+offset+size,
485							punch_lock);
486		if (need_resched()) {
487			shmem_swp_unmap(ptr);
488			if (*dir) {
489				shmem_dir_unmap(*dir);
490				*dir = NULL;
491			}
492			cond_resched();
493			ptr = shmem_swp_map(subdir);
494		}
495	}
496	shmem_swp_unmap(ptr);
497	return freed;
498}
499
500static void shmem_free_pages(struct list_head *next)
501{
502	struct page *page;
503	int freed = 0;
504
505	do {
506		page = container_of(next, struct page, lru);
507		next = next->next;
508		shmem_dir_free(page);
509		freed++;
510		if (freed >= LATENCY_LIMIT) {
511			cond_resched();
512			freed = 0;
513		}
514	} while (next);
515}
516
517static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
518{
519	struct shmem_inode_info *info = SHMEM_I(inode);
520	unsigned long idx;
521	unsigned long size;
522	unsigned long limit;
523	unsigned long stage;
524	unsigned long diroff;
525	struct page **dir;
526	struct page *topdir;
527	struct page *middir;
528	struct page *subdir;
529	swp_entry_t *ptr;
530	LIST_HEAD(pages_to_free);
531	long nr_pages_to_free = 0;
532	long nr_swaps_freed = 0;
533	int offset;
534	int freed;
535	int punch_hole;
536	spinlock_t *needs_lock;
537	spinlock_t *punch_lock;
538	unsigned long upper_limit;
539
540	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
541	idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
542	if (idx >= info->next_index)
543		return;
544
545	spin_lock(&info->lock);
546	info->flags |= SHMEM_TRUNCATE;
547	if (likely(end == (loff_t) -1)) {
548		limit = info->next_index;
549		upper_limit = SHMEM_MAX_INDEX;
550		info->next_index = idx;
551		needs_lock = NULL;
552		punch_hole = 0;
553	} else {
554		if (end + 1 >= inode->i_size) {	/* we may free a little more */
555			limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
556							PAGE_CACHE_SHIFT;
557			upper_limit = SHMEM_MAX_INDEX;
558		} else {
559			limit = (end + 1) >> PAGE_CACHE_SHIFT;
560			upper_limit = limit;
561		}
562		needs_lock = &info->lock;
563		punch_hole = 1;
564	}
565
566	topdir = info->i_indirect;
567	if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
568		info->i_indirect = NULL;
569		nr_pages_to_free++;
570		list_add(&topdir->lru, &pages_to_free);
571	}
572	spin_unlock(&info->lock);
573
574	if (info->swapped && idx < SHMEM_NR_DIRECT) {
575		ptr = info->i_direct;
576		size = limit;
577		if (size > SHMEM_NR_DIRECT)
578			size = SHMEM_NR_DIRECT;
579		nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
580	}
581
582	/*
583	 * If there are no indirect blocks or we are punching a hole
584	 * below indirect blocks, nothing to be done.
585	 */
586	if (!topdir || limit <= SHMEM_NR_DIRECT)
587		goto done2;
588
589	/*
590	 * The truncation case has already dropped info->lock, and we're safe
591	 * because i_size and next_index have already been lowered, preventing
592	 * access beyond.  But in the punch_hole case, we still need to take
593	 * the lock when updating the swap directory, because there might be
594	 * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or
595	 * shmem_writepage.  However, whenever we find we can remove a whole
596	 * directory page (not at the misaligned start or end of the range),
597	 * we first NULLify its pointer in the level above, and then have no
598	 * need to take the lock when updating its contents: needs_lock and
599	 * punch_lock (either pointing to info->lock or NULL) manage this.
600	 */
601
602	upper_limit -= SHMEM_NR_DIRECT;
603	limit -= SHMEM_NR_DIRECT;
604	idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
605	offset = idx % ENTRIES_PER_PAGE;
606	idx -= offset;
607
608	dir = shmem_dir_map(topdir);
609	stage = ENTRIES_PER_PAGEPAGE/2;
610	if (idx < ENTRIES_PER_PAGEPAGE/2) {
611		middir = topdir;
612		diroff = idx/ENTRIES_PER_PAGE;
613	} else {
614		dir += ENTRIES_PER_PAGE/2;
615		dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
616		while (stage <= idx)
617			stage += ENTRIES_PER_PAGEPAGE;
618		middir = *dir;
619		if (*dir) {
620			diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
621				ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
622			if (!diroff && !offset && upper_limit >= stage) {
623				if (needs_lock) {
624					spin_lock(needs_lock);
625					*dir = NULL;
626					spin_unlock(needs_lock);
627					needs_lock = NULL;
628				} else
629					*dir = NULL;
630				nr_pages_to_free++;
631				list_add(&middir->lru, &pages_to_free);
632			}
633			shmem_dir_unmap(dir);
634			dir = shmem_dir_map(middir);
635		} else {
636			diroff = 0;
637			offset = 0;
638			idx = stage;
639		}
640	}
641
642	for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
643		if (unlikely(idx == stage)) {
644			shmem_dir_unmap(dir);
645			dir = shmem_dir_map(topdir) +
646			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
647			while (!*dir) {
648				dir++;
649				idx += ENTRIES_PER_PAGEPAGE;
650				if (idx >= limit)
651					goto done1;
652			}
653			stage = idx + ENTRIES_PER_PAGEPAGE;
654			middir = *dir;
655			if (punch_hole)
656				needs_lock = &info->lock;
657			if (upper_limit >= stage) {
658				if (needs_lock) {
659					spin_lock(needs_lock);
660					*dir = NULL;
661					spin_unlock(needs_lock);
662					needs_lock = NULL;
663				} else
664					*dir = NULL;
665				nr_pages_to_free++;
666				list_add(&middir->lru, &pages_to_free);
667			}
668			shmem_dir_unmap(dir);
669			cond_resched();
670			dir = shmem_dir_map(middir);
671			diroff = 0;
672		}
673		punch_lock = needs_lock;
674		subdir = dir[diroff];
675		if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
676			if (needs_lock) {
677				spin_lock(needs_lock);
678				dir[diroff] = NULL;
679				spin_unlock(needs_lock);
680				punch_lock = NULL;
681			} else
682				dir[diroff] = NULL;
683			nr_pages_to_free++;
684			list_add(&subdir->lru, &pages_to_free);
685		}
686		if (subdir && page_private(subdir) /* has swap entries */) {
687			size = limit - idx;
688			if (size > ENTRIES_PER_PAGE)
689				size = ENTRIES_PER_PAGE;
690			freed = shmem_map_and_free_swp(subdir,
691					offset, size, &dir, punch_lock);
692			if (!dir)
693				dir = shmem_dir_map(middir);
694			nr_swaps_freed += freed;
695			if (offset || punch_lock) {
696				spin_lock(&info->lock);
697				set_page_private(subdir,
698					page_private(subdir) - freed);
699				spin_unlock(&info->lock);
700			} else
701				BUG_ON(page_private(subdir) != freed);
702		}
703		offset = 0;
704	}
705done1:
706	shmem_dir_unmap(dir);
707done2:
708	if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
709		/*
710		 * Call truncate_inode_pages again: racing shmem_unuse_inode
711		 * may have swizzled a page in from swap since vmtruncate or
712		 * generic_delete_inode did it, before we lowered next_index.
713		 * Also, though shmem_getpage checks i_size before adding to
714		 * cache, no recheck after: so fix the narrow window there too.
715		 *
716		 * Recalling truncate_inode_pages_range and unmap_mapping_range
717		 * every time for punch_hole (which never got a chance to clear
718		 * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive,
719		 * yet hardly ever necessary: try to optimize them out later.
720		 */
721		truncate_inode_pages_range(inode->i_mapping, start, end);
722		if (punch_hole)
723			unmap_mapping_range(inode->i_mapping, start,
724							end - start, 1);
725	}
726
727	spin_lock(&info->lock);
728	info->flags &= ~SHMEM_TRUNCATE;
729	info->swapped -= nr_swaps_freed;
730	if (nr_pages_to_free)
731		shmem_free_blocks(inode, nr_pages_to_free);
732	shmem_recalc_inode(inode);
733	spin_unlock(&info->lock);
734
735	/*
736	 * Empty swap vector directory pages to be freed?
737	 */
738	if (!list_empty(&pages_to_free)) {
739		pages_to_free.prev->next = NULL;
740		shmem_free_pages(pages_to_free.next);
741	}
742}
743
744static void shmem_truncate(struct inode *inode)
745{
746	shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
747}
748
749static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
750{
751	struct inode *inode = dentry->d_inode;
752	struct page *page = NULL;
753	int error;
754
755	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
756		if (attr->ia_size < inode->i_size) {
757			/*
758			 * If truncating down to a partial page, then
759			 * if that page is already allocated, hold it
760			 * in memory until the truncation is over, so
761			 * truncate_partial_page cannnot miss it were
762			 * it assigned to swap.
763			 */
764			if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
765				(void) shmem_getpage(inode,
766					attr->ia_size>>PAGE_CACHE_SHIFT,
767						&page, SGP_READ, NULL);
768				if (page)
769					unlock_page(page);
770			}
771			/*
772			 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
773			 * detect if any pages might have been added to cache
774			 * after truncate_inode_pages.  But we needn't bother
775			 * if it's being fully truncated to zero-length: the
776			 * nrpages check is efficient enough in that case.
777			 */
778			if (attr->ia_size) {
779				struct shmem_inode_info *info = SHMEM_I(inode);
780				spin_lock(&info->lock);
781				info->flags &= ~SHMEM_PAGEIN;
782				spin_unlock(&info->lock);
783			}
784		}
785	}
786
787	error = inode_change_ok(inode, attr);
788	if (!error)
789		error = inode_setattr(inode, attr);
790#ifdef CONFIG_TMPFS_POSIX_ACL
791	if (!error && (attr->ia_valid & ATTR_MODE))
792		error = generic_acl_chmod(inode, &shmem_acl_ops);
793#endif
794	if (page)
795		page_cache_release(page);
796	return error;
797}
798
799static void shmem_delete_inode(struct inode *inode)
800{
801	struct shmem_inode_info *info = SHMEM_I(inode);
802
803	if (inode->i_op->truncate == shmem_truncate) {
804		truncate_inode_pages(inode->i_mapping, 0);
805		shmem_unacct_size(info->flags, inode->i_size);
806		inode->i_size = 0;
807		shmem_truncate(inode);
808		if (!list_empty(&info->swaplist)) {
809			mutex_lock(&shmem_swaplist_mutex);
810			list_del_init(&info->swaplist);
811			mutex_unlock(&shmem_swaplist_mutex);
812		}
813	}
814	BUG_ON(inode->i_blocks);
815	shmem_free_inode(inode->i_sb);
816	clear_inode(inode);
817}
818
819static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
820{
821	swp_entry_t *ptr;
822
823	for (ptr = dir; ptr < edir; ptr++) {
824		if (ptr->val == entry.val)
825			return ptr - dir;
826	}
827	return -1;
828}
829
830static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
831{
832	struct inode *inode;
833	unsigned long idx;
834	unsigned long size;
835	unsigned long limit;
836	unsigned long stage;
837	struct page **dir;
838	struct page *subdir;
839	swp_entry_t *ptr;
840	int offset;
841	int error;
842
843	idx = 0;
844	ptr = info->i_direct;
845	spin_lock(&info->lock);
846	if (!info->swapped) {
847		list_del_init(&info->swaplist);
848		goto lost2;
849	}
850	limit = info->next_index;
851	size = limit;
852	if (size > SHMEM_NR_DIRECT)
853		size = SHMEM_NR_DIRECT;
854	offset = shmem_find_swp(entry, ptr, ptr+size);
855	if (offset >= 0)
856		goto found;
857	if (!info->i_indirect)
858		goto lost2;
859
860	dir = shmem_dir_map(info->i_indirect);
861	stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
862
863	for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
864		if (unlikely(idx == stage)) {
865			shmem_dir_unmap(dir-1);
866			if (cond_resched_lock(&info->lock)) {
867				/* check it has not been truncated */
868				if (limit > info->next_index) {
869					limit = info->next_index;
870					if (idx >= limit)
871						goto lost2;
872				}
873			}
874			dir = shmem_dir_map(info->i_indirect) +
875			    ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
876			while (!*dir) {
877				dir++;
878				idx += ENTRIES_PER_PAGEPAGE;
879				if (idx >= limit)
880					goto lost1;
881			}
882			stage = idx + ENTRIES_PER_PAGEPAGE;
883			subdir = *dir;
884			shmem_dir_unmap(dir);
885			dir = shmem_dir_map(subdir);
886		}
887		subdir = *dir;
888		if (subdir && page_private(subdir)) {
889			ptr = shmem_swp_map(subdir);
890			size = limit - idx;
891			if (size > ENTRIES_PER_PAGE)
892				size = ENTRIES_PER_PAGE;
893			offset = shmem_find_swp(entry, ptr, ptr+size);
894			shmem_swp_unmap(ptr);
895			if (offset >= 0) {
896				shmem_dir_unmap(dir);
897				goto found;
898			}
899		}
900	}
901lost1:
902	shmem_dir_unmap(dir-1);
903lost2:
904	spin_unlock(&info->lock);
905	return 0;
906found:
907	idx += offset;
908	inode = igrab(&info->vfs_inode);
909	spin_unlock(&info->lock);
910
911	/*
912	 * Move _head_ to start search for next from here.
913	 * But be careful: shmem_delete_inode checks list_empty without taking
914	 * mutex, and there's an instant in list_move_tail when info->swaplist
915	 * would appear empty, if it were the only one on shmem_swaplist.  We
916	 * could avoid doing it if inode NULL; or use this minor optimization.
917	 */
918	if (shmem_swaplist.next != &info->swaplist)
919		list_move_tail(&shmem_swaplist, &info->swaplist);
920	mutex_unlock(&shmem_swaplist_mutex);
921
922	error = 1;
923	if (!inode)
924		goto out;
925	/* Precharge page while we can wait, compensate afterwards */
926	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
927	if (error)
928		goto out;
929	error = radix_tree_preload(GFP_KERNEL);
930	if (error)
931		goto uncharge;
932	error = 1;
933
934	spin_lock(&info->lock);
935	ptr = shmem_swp_entry(info, idx, NULL);
936	if (ptr && ptr->val == entry.val)
937		error = add_to_page_cache(page, inode->i_mapping,
938						idx, GFP_NOWAIT);
939	if (error == -EEXIST) {
940		struct page *filepage = find_get_page(inode->i_mapping, idx);
941		error = 1;
942		if (filepage) {
943			/*
944			 * There might be a more uptodate page coming down
945			 * from a stacked writepage: forget our swappage if so.
946			 */
947			if (PageUptodate(filepage))
948				error = 0;
949			page_cache_release(filepage);
950		}
951	}
952	if (!error) {
953		delete_from_swap_cache(page);
954		set_page_dirty(page);
955		info->flags |= SHMEM_PAGEIN;
956		shmem_swp_set(info, ptr, 0);
957		swap_free(entry);
958		error = 1;	/* not an error, but entry was found */
959	}
960	if (ptr)
961		shmem_swp_unmap(ptr);
962	spin_unlock(&info->lock);
963	radix_tree_preload_end();
964uncharge:
965	mem_cgroup_uncharge_page(page);
966out:
967	unlock_page(page);
968	page_cache_release(page);
969	iput(inode);		/* allows for NULL */
970	return error;
971}
972
973/*
974 * shmem_unuse() search for an eventually swapped out shmem page.
975 */
976int shmem_unuse(swp_entry_t entry, struct page *page)
977{
978	struct list_head *p, *next;
979	struct shmem_inode_info *info;
980	int found = 0;
981
982	mutex_lock(&shmem_swaplist_mutex);
983	list_for_each_safe(p, next, &shmem_swaplist) {
984		info = list_entry(p, struct shmem_inode_info, swaplist);
985		found = shmem_unuse_inode(info, entry, page);
986		cond_resched();
987		if (found)
988			goto out;
989	}
990	mutex_unlock(&shmem_swaplist_mutex);
991out:	return found;	/* 0 or 1 or -ENOMEM */
992}
993
994/*
995 * Move the page from the page cache to the swap cache.
996 */
997static int shmem_writepage(struct page *page, struct writeback_control *wbc)
998{
999	struct shmem_inode_info *info;
1000	swp_entry_t *entry, swap;
1001	struct address_space *mapping;
1002	unsigned long index;
1003	struct inode *inode;
1004
1005	BUG_ON(!PageLocked(page));
1006	mapping = page->mapping;
1007	index = page->index;
1008	inode = mapping->host;
1009	info = SHMEM_I(inode);
1010	if (info->flags & VM_LOCKED)
1011		goto redirty;
1012	if (!total_swap_pages)
1013		goto redirty;
1014
1015	/*
1016	 * shmem_backing_dev_info's capabilities prevent regular writeback or
1017	 * sync from ever calling shmem_writepage; but a stacking filesystem
1018	 * may use the ->writepage of its underlying filesystem, in which case
1019	 * tmpfs should write out to swap only in response to memory pressure,
1020	 * and not for pdflush or sync.  However, in those cases, we do still
1021	 * want to check if there's a redundant swappage to be discarded.
1022	 */
1023	if (wbc->for_reclaim)
1024		swap = get_swap_page();
1025	else
1026		swap.val = 0;
1027
1028	spin_lock(&info->lock);
1029	if (index >= info->next_index) {
1030		BUG_ON(!(info->flags & SHMEM_TRUNCATE));
1031		goto unlock;
1032	}
1033	entry = shmem_swp_entry(info, index, NULL);
1034	if (entry->val) {
1035		/*
1036		 * The more uptodate page coming down from a stacked
1037		 * writepage should replace our old swappage.
1038		 */
1039		free_swap_and_cache(*entry);
1040		shmem_swp_set(info, entry, 0);
1041	}
1042	shmem_recalc_inode(inode);
1043
1044	if (swap.val && add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
1045		remove_from_page_cache(page);
1046		shmem_swp_set(info, entry, swap.val);
1047		shmem_swp_unmap(entry);
1048		if (list_empty(&info->swaplist))
1049			inode = igrab(inode);
1050		else
1051			inode = NULL;
1052		spin_unlock(&info->lock);
1053		swap_duplicate(swap);
1054		BUG_ON(page_mapped(page));
1055		page_cache_release(page);	/* pagecache ref */
1056		set_page_dirty(page);
1057		unlock_page(page);
1058		if (inode) {
1059			mutex_lock(&shmem_swaplist_mutex);
1060			/* move instead of add in case we're racing */
1061			list_move_tail(&info->swaplist, &shmem_swaplist);
1062			mutex_unlock(&shmem_swaplist_mutex);
1063			iput(inode);
1064		}
1065		return 0;
1066	}
1067
1068	shmem_swp_unmap(entry);
1069unlock:
1070	spin_unlock(&info->lock);
1071	swap_free(swap);
1072redirty:
1073	set_page_dirty(page);
1074	if (wbc->for_reclaim)
1075		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
1076	unlock_page(page);
1077	return 0;
1078}
1079
1080#ifdef CONFIG_NUMA
1081#ifdef CONFIG_TMPFS
1082static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1083{
1084	char buffer[64];
1085
1086	if (!mpol || mpol->mode == MPOL_DEFAULT)
1087		return;		/* show nothing */
1088
1089	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
1090
1091	seq_printf(seq, ",mpol=%s", buffer);
1092}
1093
1094static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1095{
1096	struct mempolicy *mpol = NULL;
1097	if (sbinfo->mpol) {
1098		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
1099		mpol = sbinfo->mpol;
1100		mpol_get(mpol);
1101		spin_unlock(&sbinfo->stat_lock);
1102	}
1103	return mpol;
1104}
1105#endif /* CONFIG_TMPFS */
1106
1107static struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1108			struct shmem_inode_info *info, unsigned long idx)
1109{
1110	struct mempolicy mpol, *spol;
1111	struct vm_area_struct pvma;
1112	struct page *page;
1113
1114	spol = mpol_cond_copy(&mpol,
1115				mpol_shared_policy_lookup(&info->policy, idx));
1116
1117	/* Create a pseudo vma that just contains the policy */
1118	pvma.vm_start = 0;
1119	pvma.vm_pgoff = idx;
1120	pvma.vm_ops = NULL;
1121	pvma.vm_policy = spol;
1122	page = swapin_readahead(entry, gfp, &pvma, 0);
1123	return page;
1124}
1125
1126static struct page *shmem_alloc_page(gfp_t gfp,
1127			struct shmem_inode_info *info, unsigned long idx)
1128{
1129	struct vm_area_struct pvma;
1130
1131	/* Create a pseudo vma that just contains the policy */
1132	pvma.vm_start = 0;
1133	pvma.vm_pgoff = idx;
1134	pvma.vm_ops = NULL;
1135	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1136
1137	/*
1138	 * alloc_page_vma() will drop the shared policy reference
1139	 */
1140	return alloc_page_vma(gfp, &pvma, 0);
1141}
1142#else /* !CONFIG_NUMA */
1143#ifdef CONFIG_TMPFS
1144static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *p)
1145{
1146}
1147#endif /* CONFIG_TMPFS */
1148
1149static inline struct page *shmem_swapin(swp_entry_t entry, gfp_t gfp,
1150			struct shmem_inode_info *info, unsigned long idx)
1151{
1152	return swapin_readahead(entry, gfp, NULL, 0);
1153}
1154
1155static inline struct page *shmem_alloc_page(gfp_t gfp,
1156			struct shmem_inode_info *info, unsigned long idx)
1157{
1158	return alloc_page(gfp);
1159}
1160#endif /* CONFIG_NUMA */
1161
1162#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
1163static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1164{
1165	return NULL;
1166}
1167#endif
1168
1169/*
1170 * shmem_getpage - either get the page from swap or allocate a new one
1171 *
1172 * If we allocate a new one we do not mark it dirty. That's up to the
1173 * vm. If we swap it in we mark it dirty since we also free the swap
1174 * entry since a page cannot live in both the swap and page cache
1175 */
1176static int shmem_getpage(struct inode *inode, unsigned long idx,
1177			struct page **pagep, enum sgp_type sgp, int *type)
1178{
1179	struct address_space *mapping = inode->i_mapping;
1180	struct shmem_inode_info *info = SHMEM_I(inode);
1181	struct shmem_sb_info *sbinfo;
1182	struct page *filepage = *pagep;
1183	struct page *swappage;
1184	swp_entry_t *entry;
1185	swp_entry_t swap;
1186	gfp_t gfp;
1187	int error;
1188
1189	if (idx >= SHMEM_MAX_INDEX)
1190		return -EFBIG;
1191
1192	if (type)
1193		*type = 0;
1194
1195	/*
1196	 * Normally, filepage is NULL on entry, and either found
1197	 * uptodate immediately, or allocated and zeroed, or read
1198	 * in under swappage, which is then assigned to filepage.
1199	 * But shmem_readpage (required for splice) passes in a locked
1200	 * filepage, which may be found not uptodate by other callers
1201	 * too, and may need to be copied from the swappage read in.
1202	 */
1203repeat:
1204	if (!filepage)
1205		filepage = find_lock_page(mapping, idx);
1206	if (filepage && PageUptodate(filepage))
1207		goto done;
1208	error = 0;
1209	gfp = mapping_gfp_mask(mapping);
1210	if (!filepage) {
1211		/*
1212		 * Try to preload while we can wait, to not make a habit of
1213		 * draining atomic reserves; but don't latch on to this cpu.
1214		 */
1215		error = radix_tree_preload(gfp & ~__GFP_HIGHMEM);
1216		if (error)
1217			goto failed;
1218		radix_tree_preload_end();
1219	}
1220
1221	spin_lock(&info->lock);
1222	shmem_recalc_inode(inode);
1223	entry = shmem_swp_alloc(info, idx, sgp);
1224	if (IS_ERR(entry)) {
1225		spin_unlock(&info->lock);
1226		error = PTR_ERR(entry);
1227		goto failed;
1228	}
1229	swap = *entry;
1230
1231	if (swap.val) {
1232		/* Look it up and read it in.. */
1233		swappage = lookup_swap_cache(swap);
1234		if (!swappage) {
1235			shmem_swp_unmap(entry);
1236			/* here we actually do the io */
1237			if (type && !(*type & VM_FAULT_MAJOR)) {
1238				__count_vm_event(PGMAJFAULT);
1239				*type |= VM_FAULT_MAJOR;
1240			}
1241			spin_unlock(&info->lock);
1242			swappage = shmem_swapin(swap, gfp, info, idx);
1243			if (!swappage) {
1244				spin_lock(&info->lock);
1245				entry = shmem_swp_alloc(info, idx, sgp);
1246				if (IS_ERR(entry))
1247					error = PTR_ERR(entry);
1248				else {
1249					if (entry->val == swap.val)
1250						error = -ENOMEM;
1251					shmem_swp_unmap(entry);
1252				}
1253				spin_unlock(&info->lock);
1254				if (error)
1255					goto failed;
1256				goto repeat;
1257			}
1258			wait_on_page_locked(swappage);
1259			page_cache_release(swappage);
1260			goto repeat;
1261		}
1262
1263		/* We have to do this with page locked to prevent races */
1264		if (TestSetPageLocked(swappage)) {
1265			shmem_swp_unmap(entry);
1266			spin_unlock(&info->lock);
1267			wait_on_page_locked(swappage);
1268			page_cache_release(swappage);
1269			goto repeat;
1270		}
1271		if (PageWriteback(swappage)) {
1272			shmem_swp_unmap(entry);
1273			spin_unlock(&info->lock);
1274			wait_on_page_writeback(swappage);
1275			unlock_page(swappage);
1276			page_cache_release(swappage);
1277			goto repeat;
1278		}
1279		if (!PageUptodate(swappage)) {
1280			shmem_swp_unmap(entry);
1281			spin_unlock(&info->lock);
1282			unlock_page(swappage);
1283			page_cache_release(swappage);
1284			error = -EIO;
1285			goto failed;
1286		}
1287
1288		if (filepage) {
1289			shmem_swp_set(info, entry, 0);
1290			shmem_swp_unmap(entry);
1291			delete_from_swap_cache(swappage);
1292			spin_unlock(&info->lock);
1293			copy_highpage(filepage, swappage);
1294			unlock_page(swappage);
1295			page_cache_release(swappage);
1296			flush_dcache_page(filepage);
1297			SetPageUptodate(filepage);
1298			set_page_dirty(filepage);
1299			swap_free(swap);
1300		} else if (!(error = add_to_page_cache(
1301				swappage, mapping, idx, GFP_NOWAIT))) {
1302			info->flags |= SHMEM_PAGEIN;
1303			shmem_swp_set(info, entry, 0);
1304			shmem_swp_unmap(entry);
1305			delete_from_swap_cache(swappage);
1306			spin_unlock(&info->lock);
1307			filepage = swappage;
1308			set_page_dirty(filepage);
1309			swap_free(swap);
1310		} else {
1311			shmem_swp_unmap(entry);
1312			spin_unlock(&info->lock);
1313			unlock_page(swappage);
1314			if (error == -ENOMEM) {
1315				/* allow reclaim from this memory cgroup */
1316				error = mem_cgroup_cache_charge(swappage,
1317					current->mm, gfp & ~__GFP_HIGHMEM);
1318				if (error) {
1319					page_cache_release(swappage);
1320					goto failed;
1321				}
1322				mem_cgroup_uncharge_page(swappage);
1323			}
1324			page_cache_release(swappage);
1325			goto repeat;
1326		}
1327	} else if (sgp == SGP_READ && !filepage) {
1328		shmem_swp_unmap(entry);
1329		filepage = find_get_page(mapping, idx);
1330		if (filepage &&
1331		    (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
1332			spin_unlock(&info->lock);
1333			wait_on_page_locked(filepage);
1334			page_cache_release(filepage);
1335			filepage = NULL;
1336			goto repeat;
1337		}
1338		spin_unlock(&info->lock);
1339	} else {
1340		shmem_swp_unmap(entry);
1341		sbinfo = SHMEM_SB(inode->i_sb);
1342		if (sbinfo->max_blocks) {
1343			spin_lock(&sbinfo->stat_lock);
1344			if (sbinfo->free_blocks == 0 ||
1345			    shmem_acct_block(info->flags)) {
1346				spin_unlock(&sbinfo->stat_lock);
1347				spin_unlock(&info->lock);
1348				error = -ENOSPC;
1349				goto failed;
1350			}
1351			sbinfo->free_blocks--;
1352			inode->i_blocks += BLOCKS_PER_PAGE;
1353			spin_unlock(&sbinfo->stat_lock);
1354		} else if (shmem_acct_block(info->flags)) {
1355			spin_unlock(&info->lock);
1356			error = -ENOSPC;
1357			goto failed;
1358		}
1359
1360		if (!filepage) {
1361			spin_unlock(&info->lock);
1362			filepage = shmem_alloc_page(gfp, info, idx);
1363			if (!filepage) {
1364				shmem_unacct_blocks(info->flags, 1);
1365				shmem_free_blocks(inode, 1);
1366				error = -ENOMEM;
1367				goto failed;
1368			}
1369
1370			/* Precharge page while we can wait, compensate after */
1371			error = mem_cgroup_cache_charge(filepage, current->mm,
1372							gfp & ~__GFP_HIGHMEM);
1373			if (error) {
1374				page_cache_release(filepage);
1375				shmem_unacct_blocks(info->flags, 1);
1376				shmem_free_blocks(inode, 1);
1377				filepage = NULL;
1378				goto failed;
1379			}
1380
1381			spin_lock(&info->lock);
1382			entry = shmem_swp_alloc(info, idx, sgp);
1383			if (IS_ERR(entry))
1384				error = PTR_ERR(entry);
1385			else {
1386				swap = *entry;
1387				shmem_swp_unmap(entry);
1388			}
1389			if (error || swap.val || 0 != add_to_page_cache_lru(
1390					filepage, mapping, idx, GFP_NOWAIT)) {
1391				spin_unlock(&info->lock);
1392				mem_cgroup_uncharge_page(filepage);
1393				page_cache_release(filepage);
1394				shmem_unacct_blocks(info->flags, 1);
1395				shmem_free_blocks(inode, 1);
1396				filepage = NULL;
1397				if (error)
1398					goto failed;
1399				goto repeat;
1400			}
1401			mem_cgroup_uncharge_page(filepage);
1402			info->flags |= SHMEM_PAGEIN;
1403		}
1404
1405		info->alloced++;
1406		spin_unlock(&info->lock);
1407		clear_highpage(filepage);
1408		flush_dcache_page(filepage);
1409		SetPageUptodate(filepage);
1410		if (sgp == SGP_DIRTY)
1411			set_page_dirty(filepage);
1412	}
1413done:
1414	*pagep = filepage;
1415	return 0;
1416
1417failed:
1418	if (*pagep != filepage) {
1419		unlock_page(filepage);
1420		page_cache_release(filepage);
1421	}
1422	return error;
1423}
1424
1425static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1426{
1427	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1428	int error;
1429	int ret;
1430
1431	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1432		return VM_FAULT_SIGBUS;
1433
1434	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
1435	if (error)
1436		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1437
1438	mark_page_accessed(vmf->page);
1439	return ret | VM_FAULT_LOCKED;
1440}
1441
1442#ifdef CONFIG_NUMA
1443static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1444{
1445	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1446	return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1447}
1448
1449static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
1450					  unsigned long addr)
1451{
1452	struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1453	unsigned long idx;
1454
1455	idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1456	return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1457}
1458#endif
1459
1460int shmem_lock(struct file *file, int lock, struct user_struct *user)
1461{
1462	struct inode *inode = file->f_path.dentry->d_inode;
1463	struct shmem_inode_info *info = SHMEM_I(inode);
1464	int retval = -ENOMEM;
1465
1466	spin_lock(&info->lock);
1467	if (lock && !(info->flags & VM_LOCKED)) {
1468		if (!user_shm_lock(inode->i_size, user))
1469			goto out_nomem;
1470		info->flags |= VM_LOCKED;
1471	}
1472	if (!lock && (info->flags & VM_LOCKED) && user) {
1473		user_shm_unlock(inode->i_size, user);
1474		info->flags &= ~VM_LOCKED;
1475	}
1476	retval = 0;
1477out_nomem:
1478	spin_unlock(&info->lock);
1479	return retval;
1480}
1481
1482static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1483{
1484	file_accessed(file);
1485	vma->vm_ops = &shmem_vm_ops;
1486	vma->vm_flags |= VM_CAN_NONLINEAR;
1487	return 0;
1488}
1489
1490static struct inode *
1491shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1492{
1493	struct inode *inode;
1494	struct shmem_inode_info *info;
1495	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1496
1497	if (shmem_reserve_inode(sb))
1498		return NULL;
1499
1500	inode = new_inode(sb);
1501	if (inode) {
1502		inode->i_mode = mode;
1503		inode->i_uid = current->fsuid;
1504		inode->i_gid = current->fsgid;
1505		inode->i_blocks = 0;
1506		inode->i_mapping->a_ops = &shmem_aops;
1507		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1508		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1509		inode->i_generation = get_seconds();
1510		info = SHMEM_I(inode);
1511		memset(info, 0, (char *)inode - (char *)info);
1512		spin_lock_init(&info->lock);
1513		INIT_LIST_HEAD(&info->swaplist);
1514
1515		switch (mode & S_IFMT) {
1516		default:
1517			inode->i_op = &shmem_special_inode_operations;
1518			init_special_inode(inode, mode, dev);
1519			break;
1520		case S_IFREG:
1521			inode->i_op = &shmem_inode_operations;
1522			inode->i_fop = &shmem_file_operations;
1523			mpol_shared_policy_init(&info->policy,
1524						 shmem_get_sbmpol(sbinfo));
1525			break;
1526		case S_IFDIR:
1527			inc_nlink(inode);
1528			/* Some things misbehave if size == 0 on a directory */
1529			inode->i_size = 2 * BOGO_DIRENT_SIZE;
1530			inode->i_op = &shmem_dir_inode_operations;
1531			inode->i_fop = &simple_dir_operations;
1532			break;
1533		case S_IFLNK:
1534			/*
1535			 * Must not load anything in the rbtree,
1536			 * mpol_free_shared_policy will not be called.
1537			 */
1538			mpol_shared_policy_init(&info->policy, NULL);
1539			break;
1540		}
1541	} else
1542		shmem_free_inode(sb);
1543	return inode;
1544}
1545
1546#ifdef CONFIG_TMPFS
1547static const struct inode_operations shmem_symlink_inode_operations;
1548static const struct inode_operations shmem_symlink_inline_operations;
1549
1550/*
1551 * Normally tmpfs avoids the use of shmem_readpage and shmem_write_begin;
1552 * but providing them allows a tmpfs file to be used for splice, sendfile, and
1553 * below the loop driver, in the generic fashion that many filesystems support.
1554 */
1555static int shmem_readpage(struct file *file, struct page *page)
1556{
1557	struct inode *inode = page->mapping->host;
1558	int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL);
1559	unlock_page(page);
1560	return error;
1561}
1562
1563static int
1564shmem_write_begin(struct file *file, struct address_space *mapping,
1565			loff_t pos, unsigned len, unsigned flags,
1566			struct page **pagep, void **fsdata)
1567{
1568	struct inode *inode = mapping->host;
1569	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1570	*pagep = NULL;
1571	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
1572}
1573
1574static int
1575shmem_write_end(struct file *file, struct address_space *mapping,
1576			loff_t pos, unsigned len, unsigned copied,
1577			struct page *page, void *fsdata)
1578{
1579	struct inode *inode = mapping->host;
1580
1581	if (pos + copied > inode->i_size)
1582		i_size_write(inode, pos + copied);
1583
1584	unlock_page(page);
1585	set_page_dirty(page);
1586	page_cache_release(page);
1587
1588	return copied;
1589}
1590
1591static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1592{
1593	struct inode *inode = filp->f_path.dentry->d_inode;
1594	struct address_space *mapping = inode->i_mapping;
1595	unsigned long index, offset;
1596	enum sgp_type sgp = SGP_READ;
1597
1598	/*
1599	 * Might this read be for a stacking filesystem?  Then when reading
1600	 * holes of a sparse file, we actually need to allocate those pages,
1601	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
1602	 */
1603	if (segment_eq(get_fs(), KERNEL_DS))
1604		sgp = SGP_DIRTY;
1605
1606	index = *ppos >> PAGE_CACHE_SHIFT;
1607	offset = *ppos & ~PAGE_CACHE_MASK;
1608
1609	for (;;) {
1610		struct page *page = NULL;
1611		unsigned long end_index, nr, ret;
1612		loff_t i_size = i_size_read(inode);
1613
1614		end_index = i_size >> PAGE_CACHE_SHIFT;
1615		if (index > end_index)
1616			break;
1617		if (index == end_index) {
1618			nr = i_size & ~PAGE_CACHE_MASK;
1619			if (nr <= offset)
1620				break;
1621		}
1622
1623		desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
1624		if (desc->error) {
1625			if (desc->error == -EINVAL)
1626				desc->error = 0;
1627			break;
1628		}
1629		if (page)
1630			unlock_page(page);
1631
1632		/*
1633		 * We must evaluate after, since reads (unlike writes)
1634		 * are called without i_mutex protection against truncate
1635		 */
1636		nr = PAGE_CACHE_SIZE;
1637		i_size = i_size_read(inode);
1638		end_index = i_size >> PAGE_CACHE_SHIFT;
1639		if (index == end_index) {
1640			nr = i_size & ~PAGE_CACHE_MASK;
1641			if (nr <= offset) {
1642				if (page)
1643					page_cache_release(page);
1644				break;
1645			}
1646		}
1647		nr -= offset;
1648
1649		if (page) {
1650			/*
1651			 * If users can be writing to this page using arbitrary
1652			 * virtual addresses, take care about potential aliasing
1653			 * before reading the page on the kernel side.
1654			 */
1655			if (mapping_writably_mapped(mapping))
1656				flush_dcache_page(page);
1657			/*
1658			 * Mark the page accessed if we read the beginning.
1659			 */
1660			if (!offset)
1661				mark_page_accessed(page);
1662		} else {
1663			page = ZERO_PAGE(0);
1664			page_cache_get(page);
1665		}
1666
1667		/*
1668		 * Ok, we have the page, and it's up-to-date, so
1669		 * now we can copy it to user space...
1670		 *
1671		 * The actor routine returns how many bytes were actually used..
1672		 * NOTE! This may not be the same as how much of a user buffer
1673		 * we filled up (we may be padding etc), so we can only update
1674		 * "pos" here (the actor routine has to update the user buffer
1675		 * pointers and the remaining count).
1676		 */
1677		ret = actor(desc, page, offset, nr);
1678		offset += ret;
1679		index += offset >> PAGE_CACHE_SHIFT;
1680		offset &= ~PAGE_CACHE_MASK;
1681
1682		page_cache_release(page);
1683		if (ret != nr || !desc->count)
1684			break;
1685
1686		cond_resched();
1687	}
1688
1689	*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1690	file_accessed(filp);
1691}
1692
1693static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1694{
1695	read_descriptor_t desc;
1696
1697	if ((ssize_t) count < 0)
1698		return -EINVAL;
1699	if (!access_ok(VERIFY_WRITE, buf, count))
1700		return -EFAULT;
1701	if (!count)
1702		return 0;
1703
1704	desc.written = 0;
1705	desc.count = count;
1706	desc.arg.buf = buf;
1707	desc.error = 0;
1708
1709	do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1710	if (desc.written)
1711		return desc.written;
1712	return desc.error;
1713}
1714
1715static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1716{
1717	struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1718
1719	buf->f_type = TMPFS_MAGIC;
1720	buf->f_bsize = PAGE_CACHE_SIZE;
1721	buf->f_namelen = NAME_MAX;
1722	spin_lock(&sbinfo->stat_lock);
1723	if (sbinfo->max_blocks) {
1724		buf->f_blocks = sbinfo->max_blocks;
1725		buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1726	}
1727	if (sbinfo->max_inodes) {
1728		buf->f_files = sbinfo->max_inodes;
1729		buf->f_ffree = sbinfo->free_inodes;
1730	}
1731	/* else leave those fields 0 like simple_statfs */
1732	spin_unlock(&sbinfo->stat_lock);
1733	return 0;
1734}
1735
1736/*
1737 * File creation. Allocate an inode, and we're done..
1738 */
1739static int
1740shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1741{
1742	struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1743	int error = -ENOSPC;
1744
1745	if (inode) {
1746		error = security_inode_init_security(inode, dir, NULL, NULL,
1747						     NULL);
1748		if (error) {
1749			if (error != -EOPNOTSUPP) {
1750				iput(inode);
1751				return error;
1752			}
1753		}
1754		error = shmem_acl_init(inode, dir);
1755		if (error) {
1756			iput(inode);
1757			return error;
1758		}
1759		if (dir->i_mode & S_ISGID) {
1760			inode->i_gid = dir->i_gid;
1761			if (S_ISDIR(mode))
1762				inode->i_mode |= S_ISGID;
1763		}
1764		dir->i_size += BOGO_DIRENT_SIZE;
1765		dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1766		d_instantiate(dentry, inode);
1767		dget(dentry); /* Extra count - pin the dentry in core */
1768	}
1769	return error;
1770}
1771
1772static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1773{
1774	int error;
1775
1776	if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1777		return error;
1778	inc_nlink(dir);
1779	return 0;
1780}
1781
1782static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1783		struct nameidata *nd)
1784{
1785	return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1786}
1787
1788/*
1789 * Link a file..
1790 */
1791static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1792{
1793	struct inode *inode = old_dentry->d_inode;
1794	int ret;
1795
1796	/*
1797	 * No ordinary (disk based) filesystem counts links as inodes;
1798	 * but each new link needs a new dentry, pinning lowmem, and
1799	 * tmpfs dentries cannot be pruned until they are unlinked.
1800	 */
1801	ret = shmem_reserve_inode(inode->i_sb);
1802	if (ret)
1803		goto out;
1804
1805	dir->i_size += BOGO_DIRENT_SIZE;
1806	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1807	inc_nlink(inode);
1808	atomic_inc(&inode->i_count);	/* New dentry reference */
1809	dget(dentry);		/* Extra pinning count for the created dentry */
1810	d_instantiate(dentry, inode);
1811out:
1812	return ret;
1813}
1814
1815static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1816{
1817	struct inode *inode = dentry->d_inode;
1818
1819	if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
1820		shmem_free_inode(inode->i_sb);
1821
1822	dir->i_size -= BOGO_DIRENT_SIZE;
1823	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1824	drop_nlink(inode);
1825	dput(dentry);	/* Undo the count from "create" - this does all the work */
1826	return 0;
1827}
1828
1829static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1830{
1831	if (!simple_empty(dentry))
1832		return -ENOTEMPTY;
1833
1834	drop_nlink(dentry->d_inode);
1835	drop_nlink(dir);
1836	return shmem_unlink(dir, dentry);
1837}
1838
1839/*
1840 * The VFS layer already does all the dentry stuff for rename,
1841 * we just have to decrement the usage count for the target if
1842 * it exists so that the VFS layer correctly free's it when it
1843 * gets overwritten.
1844 */
1845static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1846{
1847	struct inode *inode = old_dentry->d_inode;
1848	int they_are_dirs = S_ISDIR(inode->i_mode);
1849
1850	if (!simple_empty(new_dentry))
1851		return -ENOTEMPTY;
1852
1853	if (new_dentry->d_inode) {
1854		(void) shmem_unlink(new_dir, new_dentry);
1855		if (they_are_dirs)
1856			drop_nlink(old_dir);
1857	} else if (they_are_dirs) {
1858		drop_nlink(old_dir);
1859		inc_nlink(new_dir);
1860	}
1861
1862	old_dir->i_size -= BOGO_DIRENT_SIZE;
1863	new_dir->i_size += BOGO_DIRENT_SIZE;
1864	old_dir->i_ctime = old_dir->i_mtime =
1865	new_dir->i_ctime = new_dir->i_mtime =
1866	inode->i_ctime = CURRENT_TIME;
1867	return 0;
1868}
1869
1870static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1871{
1872	int error;
1873	int len;
1874	struct inode *inode;
1875	struct page *page = NULL;
1876	char *kaddr;
1877	struct shmem_inode_info *info;
1878
1879	len = strlen(symname) + 1;
1880	if (len > PAGE_CACHE_SIZE)
1881		return -ENAMETOOLONG;
1882
1883	inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1884	if (!inode)
1885		return -ENOSPC;
1886
1887	error = security_inode_init_security(inode, dir, NULL, NULL,
1888					     NULL);
1889	if (error) {
1890		if (error != -EOPNOTSUPP) {
1891			iput(inode);
1892			return error;
1893		}
1894		error = 0;
1895	}
1896
1897	info = SHMEM_I(inode);
1898	inode->i_size = len-1;
1899	if (len <= (char *)inode - (char *)info) {
1900		/* do it inline */
1901		memcpy(info, symname, len);
1902		inode->i_op = &shmem_symlink_inline_operations;
1903	} else {
1904		error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1905		if (error) {
1906			iput(inode);
1907			return error;
1908		}
1909		unlock_page(page);
1910		inode->i_op = &shmem_symlink_inode_operations;
1911		kaddr = kmap_atomic(page, KM_USER0);
1912		memcpy(kaddr, symname, len);
1913		kunmap_atomic(kaddr, KM_USER0);
1914		set_page_dirty(page);
1915		page_cache_release(page);
1916	}
1917	if (dir->i_mode & S_ISGID)
1918		inode->i_gid = dir->i_gid;
1919	dir->i_size += BOGO_DIRENT_SIZE;
1920	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1921	d_instantiate(dentry, inode);
1922	dget(dentry);
1923	return 0;
1924}
1925
1926static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1927{
1928	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1929	return NULL;
1930}
1931
1932static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1933{
1934	struct page *page = NULL;
1935	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1936	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1937	if (page)
1938		unlock_page(page);
1939	return page;
1940}
1941
1942static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1943{
1944	if (!IS_ERR(nd_get_link(nd))) {
1945		struct page *page = cookie;
1946		kunmap(page);
1947		mark_page_accessed(page);
1948		page_cache_release(page);
1949	}
1950}
1951
1952static const struct inode_operations shmem_symlink_inline_operations = {
1953	.readlink	= generic_readlink,
1954	.follow_link	= shmem_follow_link_inline,
1955};
1956
1957static const struct inode_operations shmem_symlink_inode_operations = {
1958	.truncate	= shmem_truncate,
1959	.readlink	= generic_readlink,
1960	.follow_link	= shmem_follow_link,
1961	.put_link	= shmem_put_link,
1962};
1963
1964#ifdef CONFIG_TMPFS_POSIX_ACL
1965/*
1966 * Superblocks without xattr inode operations will get security.* xattr
1967 * support from the VFS "for free". As soon as we have any other xattrs
1968 * like ACLs, we also need to implement the security.* handlers at
1969 * filesystem level, though.
1970 */
1971
1972static size_t shmem_xattr_security_list(struct inode *inode, char *list,
1973					size_t list_len, const char *name,
1974					size_t name_len)
1975{
1976	return security_inode_listsecurity(inode, list, list_len);
1977}
1978
1979static int shmem_xattr_security_get(struct inode *inode, const char *name,
1980				    void *buffer, size_t size)
1981{
1982	if (strcmp(name, "") == 0)
1983		return -EINVAL;
1984	return xattr_getsecurity(inode, name, buffer, size);
1985}
1986
1987static int shmem_xattr_security_set(struct inode *inode, const char *name,
1988				    const void *value, size_t size, int flags)
1989{
1990	if (strcmp(name, "") == 0)
1991		return -EINVAL;
1992	return security_inode_setsecurity(inode, name, value, size, flags);
1993}
1994
1995static struct xattr_handler shmem_xattr_security_handler = {
1996	.prefix = XATTR_SECURITY_PREFIX,
1997	.list   = shmem_xattr_security_list,
1998	.get    = shmem_xattr_security_get,
1999	.set    = shmem_xattr_security_set,
2000};
2001
2002static struct xattr_handler *shmem_xattr_handlers[] = {
2003	&shmem_xattr_acl_access_handler,
2004	&shmem_xattr_acl_default_handler,
2005	&shmem_xattr_security_handler,
2006	NULL
2007};
2008#endif
2009
2010static struct dentry *shmem_get_parent(struct dentry *child)
2011{
2012	return ERR_PTR(-ESTALE);
2013}
2014
2015static int shmem_match(struct inode *ino, void *vfh)
2016{
2017	__u32 *fh = vfh;
2018	__u64 inum = fh[2];
2019	inum = (inum << 32) | fh[1];
2020	return ino->i_ino == inum && fh[0] == ino->i_generation;
2021}
2022
2023static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
2024		struct fid *fid, int fh_len, int fh_type)
2025{
2026	struct inode *inode;
2027	struct dentry *dentry = NULL;
2028	u64 inum = fid->raw[2];
2029	inum = (inum << 32) | fid->raw[1];
2030
2031	if (fh_len < 3)
2032		return NULL;
2033
2034	inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
2035			shmem_match, fid->raw);
2036	if (inode) {
2037		dentry = d_find_alias(inode);
2038		iput(inode);
2039	}
2040
2041	return dentry;
2042}
2043
2044static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2045				int connectable)
2046{
2047	struct inode *inode = dentry->d_inode;
2048
2049	if (*len < 3)
2050		return 255;
2051
2052	if (hlist_unhashed(&inode->i_hash)) {
2053		/* Unfortunately insert_inode_hash is not idempotent,
2054		 * so as we hash inodes here rather than at creation
2055		 * time, we need a lock to ensure we only try
2056		 * to do it once
2057		 */
2058		static DEFINE_SPINLOCK(lock);
2059		spin_lock(&lock);
2060		if (hlist_unhashed(&inode->i_hash))
2061			__insert_inode_hash(inode,
2062					    inode->i_ino + inode->i_generation);
2063		spin_unlock(&lock);
2064	}
2065
2066	fh[0] = inode->i_generation;
2067	fh[1] = inode->i_ino;
2068	fh[2] = ((__u64)inode->i_ino) >> 32;
2069
2070	*len = 3;
2071	return 1;
2072}
2073
2074static const struct export_operations shmem_export_ops = {
2075	.get_parent     = shmem_get_parent,
2076	.encode_fh      = shmem_encode_fh,
2077	.fh_to_dentry	= shmem_fh_to_dentry,
2078};
2079
2080static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
2081			       bool remount)
2082{
2083	char *this_char, *value, *rest;
2084
2085	while (options != NULL) {
2086		this_char = options;
2087		for (;;) {
2088			/*
2089			 * NUL-terminate this option: unfortunately,
2090			 * mount options form a comma-separated list,
2091			 * but mpol's nodelist may also contain commas.
2092			 */
2093			options = strchr(options, ',');
2094			if (options == NULL)
2095				break;
2096			options++;
2097			if (!isdigit(*options)) {
2098				options[-1] = '\0';
2099				break;
2100			}
2101		}
2102		if (!*this_char)
2103			continue;
2104		if ((value = strchr(this_char,'=')) != NULL) {
2105			*value++ = 0;
2106		} else {
2107			printk(KERN_ERR
2108			    "tmpfs: No value for mount option '%s'\n",
2109			    this_char);
2110			return 1;
2111		}
2112
2113		if (!strcmp(this_char,"size")) {
2114			unsigned long long size;
2115			size = memparse(value,&rest);
2116			if (*rest == '%') {
2117				size <<= PAGE_SHIFT;
2118				size *= totalram_pages;
2119				do_div(size, 100);
2120				rest++;
2121			}
2122			if (*rest)
2123				goto bad_val;
2124			sbinfo->max_blocks =
2125				DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
2126		} else if (!strcmp(this_char,"nr_blocks")) {
2127			sbinfo->max_blocks = memparse(value, &rest);
2128			if (*rest)
2129				goto bad_val;
2130		} else if (!strcmp(this_char,"nr_inodes")) {
2131			sbinfo->max_inodes = memparse(value, &rest);
2132			if (*rest)
2133				goto bad_val;
2134		} else if (!strcmp(this_char,"mode")) {
2135			if (remount)
2136				continue;
2137			sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
2138			if (*rest)
2139				goto bad_val;
2140		} else if (!strcmp(this_char,"uid")) {
2141			if (remount)
2142				continue;
2143			sbinfo->uid = simple_strtoul(value, &rest, 0);
2144			if (*rest)
2145				goto bad_val;
2146		} else if (!strcmp(this_char,"gid")) {
2147			if (remount)
2148				continue;
2149			sbinfo->gid = simple_strtoul(value, &rest, 0);
2150			if (*rest)
2151				goto bad_val;
2152		} else if (!strcmp(this_char,"mpol")) {
2153			if (mpol_parse_str(value, &sbinfo->mpol, 1))
2154				goto bad_val;
2155		} else {
2156			printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2157			       this_char);
2158			return 1;
2159		}
2160	}
2161	return 0;
2162
2163bad_val:
2164	printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2165	       value, this_char);
2166	return 1;
2167
2168}
2169
2170static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2171{
2172	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2173	struct shmem_sb_info config = *sbinfo;
2174	unsigned long blocks;
2175	unsigned long inodes;
2176	int error = -EINVAL;
2177
2178	if (shmem_parse_options(data, &config, true))
2179		return error;
2180
2181	spin_lock(&sbinfo->stat_lock);
2182	blocks = sbinfo->max_blocks - sbinfo->free_blocks;
2183	inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2184	if (config.max_blocks < blocks)
2185		goto out;
2186	if (config.max_inodes < inodes)
2187		goto out;
2188	/*
2189	 * Those tests also disallow limited->unlimited while any are in
2190	 * use, so i_blocks will always be zero when max_blocks is zero;
2191	 * but we must separately disallow unlimited->limited, because
2192	 * in that case we have no record of how much is already in use.
2193	 */
2194	if (config.max_blocks && !sbinfo->max_blocks)
2195		goto out;
2196	if (config.max_inodes && !sbinfo->max_inodes)
2197		goto out;
2198
2199	error = 0;
2200	sbinfo->max_blocks  = config.max_blocks;
2201	sbinfo->free_blocks = config.max_blocks - blocks;
2202	sbinfo->max_inodes  = config.max_inodes;
2203	sbinfo->free_inodes = config.max_inodes - inodes;
2204
2205	mpol_put(sbinfo->mpol);
2206	sbinfo->mpol        = config.mpol;	/* transfers initial ref */
2207out:
2208	spin_unlock(&sbinfo->stat_lock);
2209	return error;
2210}
2211
2212static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
2213{
2214	struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
2215
2216	if (sbinfo->max_blocks != shmem_default_max_blocks())
2217		seq_printf(seq, ",size=%luk",
2218			sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
2219	if (sbinfo->max_inodes != shmem_default_max_inodes())
2220		seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
2221	if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
2222		seq_printf(seq, ",mode=%03o", sbinfo->mode);
2223	if (sbinfo->uid != 0)
2224		seq_printf(seq, ",uid=%u", sbinfo->uid);
2225	if (sbinfo->gid != 0)
2226		seq_printf(seq, ",gid=%u", sbinfo->gid);
2227	shmem_show_mpol(seq, sbinfo->mpol);
2228	return 0;
2229}
2230#endif /* CONFIG_TMPFS */
2231
2232static void shmem_put_super(struct super_block *sb)
2233{
2234	kfree(sb->s_fs_info);
2235	sb->s_fs_info = NULL;
2236}
2237
2238static int shmem_fill_super(struct super_block *sb,
2239			    void *data, int silent)
2240{
2241	struct inode *inode;
2242	struct dentry *root;
2243	struct shmem_sb_info *sbinfo;
2244	int err = -ENOMEM;
2245
2246	/* Round up to L1_CACHE_BYTES to resist false sharing */
2247	sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2248				L1_CACHE_BYTES), GFP_KERNEL);
2249	if (!sbinfo)
2250		return -ENOMEM;
2251
2252	sbinfo->max_blocks = 0;
2253	sbinfo->max_inodes = 0;
2254	sbinfo->mode = S_IRWXUGO | S_ISVTX;
2255	sbinfo->uid = current->fsuid;
2256	sbinfo->gid = current->fsgid;
2257	sbinfo->mpol = NULL;
2258	sb->s_fs_info = sbinfo;
2259
2260#ifdef CONFIG_TMPFS
2261	/*
2262	 * Per default we only allow half of the physical ram per
2263	 * tmpfs instance, limiting inodes to one per page of lowmem;
2264	 * but the internal instance is left unlimited.
2265	 */
2266	if (!(sb->s_flags & MS_NOUSER)) {
2267		sbinfo->max_blocks = shmem_default_max_blocks();
2268		sbinfo->max_inodes = shmem_default_max_inodes();
2269		if (shmem_parse_options(data, sbinfo, false)) {
2270			err = -EINVAL;
2271			goto failed;
2272		}
2273	}
2274	sb->s_export_op = &shmem_export_ops;
2275#else
2276	sb->s_flags |= MS_NOUSER;
2277#endif
2278
2279	spin_lock_init(&sbinfo->stat_lock);
2280	sbinfo->free_blocks = sbinfo->max_blocks;
2281	sbinfo->free_inodes = sbinfo->max_inodes;
2282
2283	sb->s_maxbytes = SHMEM_MAX_BYTES;
2284	sb->s_blocksize = PAGE_CACHE_SIZE;
2285	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2286	sb->s_magic = TMPFS_MAGIC;
2287	sb->s_op = &shmem_ops;
2288	sb->s_time_gran = 1;
2289#ifdef CONFIG_TMPFS_POSIX_ACL
2290	sb->s_xattr = shmem_xattr_handlers;
2291	sb->s_flags |= MS_POSIXACL;
2292#endif
2293
2294	inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0);
2295	if (!inode)
2296		goto failed;
2297	inode->i_uid = sbinfo->uid;
2298	inode->i_gid = sbinfo->gid;
2299	root = d_alloc_root(inode);
2300	if (!root)
2301		goto failed_iput;
2302	sb->s_root = root;
2303	return 0;
2304
2305failed_iput:
2306	iput(inode);
2307failed:
2308	shmem_put_super(sb);
2309	return err;
2310}
2311
2312static struct kmem_cache *shmem_inode_cachep;
2313
2314static struct inode *shmem_alloc_inode(struct super_block *sb)
2315{
2316	struct shmem_inode_info *p;
2317	p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2318	if (!p)
2319		return NULL;
2320	return &p->vfs_inode;
2321}
2322
2323static void shmem_destroy_inode(struct inode *inode)
2324{
2325	if ((inode->i_mode & S_IFMT) == S_IFREG) {
2326		/* only struct inode is valid if it's an inline symlink */
2327		mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2328	}
2329	shmem_acl_destroy_inode(inode);
2330	kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2331}
2332
2333static void init_once(struct kmem_cache *cachep, void *foo)
2334{
2335	struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2336
2337	inode_init_once(&p->vfs_inode);
2338#ifdef CONFIG_TMPFS_POSIX_ACL
2339	p->i_acl = NULL;
2340	p->i_default_acl = NULL;
2341#endif
2342}
2343
2344static int init_inodecache(void)
2345{
2346	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2347				sizeof(struct shmem_inode_info),
2348				0, SLAB_PANIC, init_once);
2349	return 0;
2350}
2351
2352static void destroy_inodecache(void)
2353{
2354	kmem_cache_destroy(shmem_inode_cachep);
2355}
2356
2357static const struct address_space_operations shmem_aops = {
2358	.writepage	= shmem_writepage,
2359	.set_page_dirty	= __set_page_dirty_no_writeback,
2360#ifdef CONFIG_TMPFS
2361	.readpage	= shmem_readpage,
2362	.write_begin	= shmem_write_begin,
2363	.write_end	= shmem_write_end,
2364#endif
2365	.migratepage	= migrate_page,
2366};
2367
2368static const struct file_operations shmem_file_operations = {
2369	.mmap		= shmem_mmap,
2370#ifdef CONFIG_TMPFS
2371	.llseek		= generic_file_llseek,
2372	.read		= shmem_file_read,
2373	.write		= do_sync_write,
2374	.aio_write	= generic_file_aio_write,
2375	.fsync		= simple_sync_file,
2376	.splice_read	= generic_file_splice_read,
2377	.splice_write	= generic_file_splice_write,
2378#endif
2379};
2380
2381static const struct inode_operations shmem_inode_operations = {
2382	.truncate	= shmem_truncate,
2383	.setattr	= shmem_notify_change,
2384	.truncate_range	= shmem_truncate_range,
2385#ifdef CONFIG_TMPFS_POSIX_ACL
2386	.setxattr	= generic_setxattr,
2387	.getxattr	= generic_getxattr,
2388	.listxattr	= generic_listxattr,
2389	.removexattr	= generic_removexattr,
2390	.permission	= shmem_permission,
2391#endif
2392
2393};
2394
2395static const struct inode_operations shmem_dir_inode_operations = {
2396#ifdef CONFIG_TMPFS
2397	.create		= shmem_create,
2398	.lookup		= simple_lookup,
2399	.link		= shmem_link,
2400	.unlink		= shmem_unlink,
2401	.symlink	= shmem_symlink,
2402	.mkdir		= shmem_mkdir,
2403	.rmdir		= shmem_rmdir,
2404	.mknod		= shmem_mknod,
2405	.rename		= shmem_rename,
2406#endif
2407#ifdef CONFIG_TMPFS_POSIX_ACL
2408	.setattr	= shmem_notify_change,
2409	.setxattr	= generic_setxattr,
2410	.getxattr	= generic_getxattr,
2411	.listxattr	= generic_listxattr,
2412	.removexattr	= generic_removexattr,
2413	.permission	= shmem_permission,
2414#endif
2415};
2416
2417static const struct inode_operations shmem_special_inode_operations = {
2418#ifdef CONFIG_TMPFS_POSIX_ACL
2419	.setattr	= shmem_notify_change,
2420	.setxattr	= generic_setxattr,
2421	.getxattr	= generic_getxattr,
2422	.listxattr	= generic_listxattr,
2423	.removexattr	= generic_removexattr,
2424	.permission	= shmem_permission,
2425#endif
2426};
2427
2428static const struct super_operations shmem_ops = {
2429	.alloc_inode	= shmem_alloc_inode,
2430	.destroy_inode	= shmem_destroy_inode,
2431#ifdef CONFIG_TMPFS
2432	.statfs		= shmem_statfs,
2433	.remount_fs	= shmem_remount_fs,
2434	.show_options	= shmem_show_options,
2435#endif
2436	.delete_inode	= shmem_delete_inode,
2437	.drop_inode	= generic_delete_inode,
2438	.put_super	= shmem_put_super,
2439};
2440
2441static struct vm_operations_struct shmem_vm_ops = {
2442	.fault		= shmem_fault,
2443#ifdef CONFIG_NUMA
2444	.set_policy     = shmem_set_policy,
2445	.get_policy     = shmem_get_policy,
2446#endif
2447};
2448
2449
2450static int shmem_get_sb(struct file_system_type *fs_type,
2451	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2452{
2453	return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
2454}
2455
2456static struct file_system_type tmpfs_fs_type = {
2457	.owner		= THIS_MODULE,
2458	.name		= "tmpfs",
2459	.get_sb		= shmem_get_sb,
2460	.kill_sb	= kill_litter_super,
2461};
2462static struct vfsmount *shm_mnt;
2463
2464static int __init init_tmpfs(void)
2465{
2466	int error;
2467
2468	error = bdi_init(&shmem_backing_dev_info);
2469	if (error)
2470		goto out4;
2471
2472	error = init_inodecache();
2473	if (error)
2474		goto out3;
2475
2476	error = register_filesystem(&tmpfs_fs_type);
2477	if (error) {
2478		printk(KERN_ERR "Could not register tmpfs\n");
2479		goto out2;
2480	}
2481
2482	shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2483				tmpfs_fs_type.name, NULL);
2484	if (IS_ERR(shm_mnt)) {
2485		error = PTR_ERR(shm_mnt);
2486		printk(KERN_ERR "Could not kern_mount tmpfs\n");
2487		goto out1;
2488	}
2489	return 0;
2490
2491out1:
2492	unregister_filesystem(&tmpfs_fs_type);
2493out2:
2494	destroy_inodecache();
2495out3:
2496	bdi_destroy(&shmem_backing_dev_info);
2497out4:
2498	shm_mnt = ERR_PTR(error);
2499	return error;
2500}
2501module_init(init_tmpfs)
2502
2503/**
2504 * shmem_file_setup - get an unlinked file living in tmpfs
2505 * @name: name for dentry (to be seen in /proc/<pid>/maps
2506 * @size: size to be set for the file
2507 * @flags: vm_flags
2508 */
2509struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2510{
2511	int error;
2512	struct file *file;
2513	struct inode *inode;
2514	struct dentry *dentry, *root;
2515	struct qstr this;
2516
2517	if (IS_ERR(shm_mnt))
2518		return (void *)shm_mnt;
2519
2520	if (size < 0 || size > SHMEM_MAX_BYTES)
2521		return ERR_PTR(-EINVAL);
2522
2523	if (shmem_acct_size(flags, size))
2524		return ERR_PTR(-ENOMEM);
2525
2526	error = -ENOMEM;
2527	this.name = name;
2528	this.len = strlen(name);
2529	this.hash = 0; /* will go */
2530	root = shm_mnt->mnt_root;
2531	dentry = d_alloc(root, &this);
2532	if (!dentry)
2533		goto put_memory;
2534
2535	error = -ENFILE;
2536	file = get_empty_filp();
2537	if (!file)
2538		goto put_dentry;
2539
2540	error = -ENOSPC;
2541	inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2542	if (!inode)
2543		goto close_file;
2544
2545	SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
2546	d_instantiate(dentry, inode);
2547	inode->i_size = size;
2548	inode->i_nlink = 0;	/* It is unlinked */
2549	init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2550			&shmem_file_operations);
2551	return file;
2552
2553close_file:
2554	put_filp(file);
2555put_dentry:
2556	dput(dentry);
2557put_memory:
2558	shmem_unacct_size(flags, size);
2559	return ERR_PTR(error);
2560}
2561
2562/**
2563 * shmem_zero_setup - setup a shared anonymous mapping
2564 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2565 */
2566int shmem_zero_setup(struct vm_area_struct *vma)
2567{
2568	struct file *file;
2569	loff_t size = vma->vm_end - vma->vm_start;
2570
2571	file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2572	if (IS_ERR(file))
2573		return PTR_ERR(file);
2574
2575	if (vma->vm_file)
2576		fput(vma->vm_file);
2577	vma->vm_file = file;
2578	vma->vm_ops = &shmem_vm_ops;
2579	return 0;
2580}
2581