shmem.c 73.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8
/*
 * Resizable virtual memory filesystem for Linux.
 *
 * Copyright (C) 2000 Linus Torvalds.
 *		 2000 Transmeta Corp.
 *		 2000-2001 Christoph Rohland
 *		 2000-2001 SAP AG
 *		 2002 Red Hat Inc.
9 10
 * Copyright (C) 2002-2011 Hugh Dickins.
 * Copyright (C) 2011 Google Inc.
11
 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14 15 16 17
 * Copyright (C) 2004 Andi Kleen, SuSE Labs
 *
 * Extended attribute support for tmpfs:
 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
 *
18 19 20
 * tiny-shmem:
 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
 *
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23
 * This file is released under the GPL.
 */

24 25 26 27
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/vfs.h>
#include <linux/mount.h>
Hugh Dickins's avatar
Hugh Dickins committed
28
#include <linux/pagemap.h>
29 30
#include <linux/file.h>
#include <linux/mm.h>
31
#include <linux/export.h>
32 33 34 35 36
#include <linux/swap.h>

static struct vfsmount *shm_mnt;

#ifdef CONFIG_SHMEM
Linus Torvalds's avatar
Linus Torvalds committed
37 38 39 40 41 42
/*
 * This virtual memory filesystem is heavily based on the ramfs. It
 * extends ramfs by the ability to use swap and honor resource limits
 * which makes it a completely usable filesystem.
 */

43
#include <linux/xattr.h>
44
#include <linux/exportfs.h>
45
#include <linux/posix_acl.h>
46
#include <linux/generic_acl.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49 50 51 52 53
#include <linux/mman.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/writeback.h>
#include <linux/blkdev.h>
54
#include <linux/pagevec.h>
55
#include <linux/percpu_counter.h>
56
#include <linux/falloc.h>
57
#include <linux/splice.h>
Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61
#include <linux/security.h>
#include <linux/swapops.h>
#include <linux/mempolicy.h>
#include <linux/namei.h>
62
#include <linux/ctype.h>
63
#include <linux/migrate.h>
64
#include <linux/highmem.h>
65
#include <linux/seq_file.h>
Mimi Zohar's avatar
Mimi Zohar committed
66
#include <linux/magic.h>
67

Linus Torvalds's avatar
Linus Torvalds committed
68 69 70
#include <asm/uaccess.h>
#include <asm/pgtable.h>

Hugh Dickins's avatar
Hugh Dickins committed
71
#define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
Linus Torvalds's avatar
Linus Torvalds committed
72 73 74 75 76
#define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)

/* Pretend that each entry is of this size in directory's i_size */
#define BOGO_DIRENT_SIZE 20

77 78 79
/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
#define SHORT_SYMLINK_LEN 128

80 81 82 83 84 85 86
struct shmem_xattr {
	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
	char *name;		/* xattr name */
	size_t size;
	char value[0];
};

87
/* Flag allocation requirements to shmem_getpage */
Linus Torvalds's avatar
Linus Torvalds committed
88 89 90
enum sgp_type {
	SGP_READ,	/* don't exceed i_size, don't allocate page */
	SGP_CACHE,	/* don't exceed i_size, may allocate page */
91
	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
92 93
	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
Linus Torvalds's avatar
Linus Torvalds committed
94 95
};

Andrew Morton's avatar
Andrew Morton committed
96
#ifdef CONFIG_TMPFS
97 98 99 100 101 102 103 104 105
static unsigned long shmem_default_max_blocks(void)
{
	return totalram_pages / 2;
}

static unsigned long shmem_default_max_inodes(void)
{
	return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
}
Andrew Morton's avatar
Andrew Morton committed
106
#endif
107

108 109 110
static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
static int shmem_replace_page(struct page **pagep, gfp_t gfp,
				struct shmem_inode_info *info, pgoff_t index);
111 112 113 114 115 116 117 118 119
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);

static inline int shmem_getpage(struct inode *inode, pgoff_t index,
	struct page **pagep, enum sgp_type sgp, int *fault_type)
{
	return shmem_getpage_gfp(inode, index, pagep, sgp,
			mapping_gfp_mask(inode->i_mapping), fault_type);
}
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133

static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
{
	return sb->s_fs_info;
}

/*
 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
 * for shared memory and for shared anonymous (/dev/zero) mappings
 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
 * consistent with the pre-accounting of private mappings ...
 */
static inline int shmem_acct_size(unsigned long flags, loff_t size)
{
134
	return (flags & VM_NORESERVE) ?
135
		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds's avatar
Linus Torvalds committed
136 137 138 139
}

static inline void shmem_unacct_size(unsigned long flags, loff_t size)
{
140
	if (!(flags & VM_NORESERVE))
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143 144 145 146 147 148 149 150 151
		vm_unacct_memory(VM_ACCT(size));
}

/*
 * ... whereas tmpfs objects are accounted incrementally as
 * pages are allocated, in order to allow huge sparse files.
 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
 */
static inline int shmem_acct_block(unsigned long flags)
{
152
	return (flags & VM_NORESERVE) ?
153
		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
154 155 156 157
}

static inline void shmem_unacct_blocks(unsigned long flags, long pages)
{
158
	if (flags & VM_NORESERVE)
Linus Torvalds's avatar
Linus Torvalds committed
159 160 161
		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
}

162
static const struct super_operations shmem_ops;
163
static const struct address_space_operations shmem_aops;
164
static const struct file_operations shmem_file_operations;
165 166 167
static const struct inode_operations shmem_inode_operations;
static const struct inode_operations shmem_dir_inode_operations;
static const struct inode_operations shmem_special_inode_operations;
168
static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds's avatar
Linus Torvalds committed
169

170
static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
Linus Torvalds's avatar
Linus Torvalds committed
171
	.ra_pages	= 0,	/* No readahead */
172
	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds's avatar
Linus Torvalds committed
173 174 175
};

static LIST_HEAD(shmem_swaplist);
176
static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static int shmem_reserve_inode(struct super_block *sb)
{
	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
	if (sbinfo->max_inodes) {
		spin_lock(&sbinfo->stat_lock);
		if (!sbinfo->free_inodes) {
			spin_unlock(&sbinfo->stat_lock);
			return -ENOSPC;
		}
		sbinfo->free_inodes--;
		spin_unlock(&sbinfo->stat_lock);
	}
	return 0;
}

static void shmem_free_inode(struct super_block *sb)
{
	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
	if (sbinfo->max_inodes) {
		spin_lock(&sbinfo->stat_lock);
		sbinfo->free_inodes++;
		spin_unlock(&sbinfo->stat_lock);
	}
}

203
/**
204
 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds's avatar
Linus Torvalds committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
 * @inode: inode to recalc
 *
 * We have to calculate the free blocks since the mm can drop
 * undirtied hole pages behind our back.
 *
 * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
 *
 * It has to be called with the spinlock held.
 */
static void shmem_recalc_inode(struct inode *inode)
{
	struct shmem_inode_info *info = SHMEM_I(inode);
	long freed;

	freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
	if (freed > 0) {
222 223 224
		struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
		if (sbinfo->max_blocks)
			percpu_counter_add(&sbinfo->used_blocks, -freed);
Linus Torvalds's avatar
Linus Torvalds committed
225
		info->alloced -= freed;
226
		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230
		shmem_unacct_blocks(info->flags, freed);
	}
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/*
 * Replace item expected in radix tree by a new item, while holding tree lock.
 */
static int shmem_radix_tree_replace(struct address_space *mapping,
			pgoff_t index, void *expected, void *replacement)
{
	void **pslot;
	void *item = NULL;

	VM_BUG_ON(!expected);
	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
	if (pslot)
		item = radix_tree_deref_slot_protected(pslot,
							&mapping->tree_lock);
	if (item != expected)
		return -ENOENT;
	if (replacement)
		radix_tree_replace_slot(pslot, replacement);
	else
		radix_tree_delete(&mapping->page_tree, index);
	return 0;
}

254 255 256 257 258 259 260
/*
 * Like add_to_page_cache_locked, but error if expected item has gone.
 */
static int shmem_add_to_page_cache(struct page *page,
				   struct address_space *mapping,
				   pgoff_t index, gfp_t gfp, void *expected)
{
261
	int error = 0;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

	VM_BUG_ON(!PageLocked(page));
	VM_BUG_ON(!PageSwapBacked(page));

	if (!expected)
		error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
	if (!error) {
		page_cache_get(page);
		page->mapping = mapping;
		page->index = index;

		spin_lock_irq(&mapping->tree_lock);
		if (!expected)
			error = radix_tree_insert(&mapping->page_tree,
							index, page);
		else
			error = shmem_radix_tree_replace(mapping, index,
							expected, page);
		if (!error) {
			mapping->nrpages++;
			__inc_zone_page_state(page, NR_FILE_PAGES);
			__inc_zone_page_state(page, NR_SHMEM);
			spin_unlock_irq(&mapping->tree_lock);
		} else {
			page->mapping = NULL;
			spin_unlock_irq(&mapping->tree_lock);
			page_cache_release(page);
		}
		if (!expected)
			radix_tree_preload_end();
	}
	if (error)
		mem_cgroup_uncharge_cache_page(page);
	return error;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/*
 * Like delete_from_page_cache, but substitutes swap for page.
 */
static void shmem_delete_from_page_cache(struct page *page, void *radswap)
{
	struct address_space *mapping = page->mapping;
	int error;

	spin_lock_irq(&mapping->tree_lock);
	error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
	page->mapping = NULL;
	mapping->nrpages--;
	__dec_zone_page_state(page, NR_FILE_PAGES);
	__dec_zone_page_state(page, NR_SHMEM);
	spin_unlock_irq(&mapping->tree_lock);
	page_cache_release(page);
	BUG_ON(error);
}

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/*
 * Like find_get_pages, but collecting swap entries as well as pages.
 */
static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
					pgoff_t start, unsigned int nr_pages,
					struct page **pages, pgoff_t *indices)
{
	unsigned int i;
	unsigned int ret;
	unsigned int nr_found;

	rcu_read_lock();
restart:
	nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
				(void ***)pages, indices, start, nr_pages);
	ret = 0;
	for (i = 0; i < nr_found; i++) {
		struct page *page;
repeat:
		page = radix_tree_deref_slot((void **)pages[i]);
		if (unlikely(!page))
			continue;
		if (radix_tree_exception(page)) {
340 341 342 343 344 345 346 347
			if (radix_tree_deref_retry(page))
				goto restart;
			/*
			 * Otherwise, we must be storing a swap entry
			 * here as an exceptional entry: so return it
			 * without attempting to raise page count.
			 */
			goto export;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
		}
		if (!page_cache_get_speculative(page))
			goto repeat;

		/* Has the page moved? */
		if (unlikely(page != *((void **)pages[i]))) {
			page_cache_release(page);
			goto repeat;
		}
export:
		indices[ret] = indices[i];
		pages[ret] = page;
		ret++;
	}
	if (unlikely(!ret && nr_found))
		goto restart;
	rcu_read_unlock();
	return ret;
}

/*
 * Remove swap entry from radix tree, free the swap and its page cache.
 */
static int shmem_free_swap(struct address_space *mapping,
			   pgoff_t index, void *radswap)
{
	int error;

	spin_lock_irq(&mapping->tree_lock);
	error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
	spin_unlock_irq(&mapping->tree_lock);
	if (!error)
		free_swap_and_cache(radix_to_swp_entry(radswap));
	return error;
}

/*
 * Pagevec may contain swap entries, so shuffle up pages before releasing.
 */
387
static void shmem_deswap_pagevec(struct pagevec *pvec)
388 389 390 391 392 393 394 395 396
{
	int i, j;

	for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
		struct page *page = pvec->pages[i];
		if (!radix_tree_exceptional_entry(page))
			pvec->pages[j++] = page;
	}
	pvec->nr = j;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
}

/*
 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
 */
void shmem_unlock_mapping(struct address_space *mapping)
{
	struct pagevec pvec;
	pgoff_t indices[PAGEVEC_SIZE];
	pgoff_t index = 0;

	pagevec_init(&pvec, 0);
	/*
	 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
	 */
	while (!mapping_unevictable(mapping)) {
		/*
		 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
		 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
		 */
		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
					PAGEVEC_SIZE, pvec.pages, indices);
		if (!pvec.nr)
			break;
		index = indices[pvec.nr - 1] + 1;
		shmem_deswap_pagevec(&pvec);
		check_move_unevictable_pages(pvec.pages, pvec.nr);
		pagevec_release(&pvec);
		cond_resched();
	}
427 428 429 430
}

/*
 * Remove range of pages and swap entries from radix tree, and free them.
431
 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
432
 */
433 434
static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
								 bool unfalloc)
Linus Torvalds's avatar
Linus Torvalds committed
435
{
436
	struct address_space *mapping = inode->i_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
437
	struct shmem_inode_info *info = SHMEM_I(inode);
438
	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
439 440 441
	pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT;
	unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1);
	unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
442
	struct pagevec pvec;
443 444
	pgoff_t indices[PAGEVEC_SIZE];
	long nr_swaps_freed = 0;
445
	pgoff_t index;
446 447
	int i;

448 449
	if (lend == -1)
		end = -1;	/* unsigned, so actually very big */
450 451 452

	pagevec_init(&pvec, 0);
	index = start;
453
	while (index < end) {
454
		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
455
				min(end - index, (pgoff_t)PAGEVEC_SIZE),
456 457 458
							pvec.pages, indices);
		if (!pvec.nr)
			break;
459 460 461 462
		mem_cgroup_uncharge_start();
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

463
			index = indices[i];
464
			if (index >= end)
465 466
				break;

467
			if (radix_tree_exceptional_entry(page)) {
468 469
				if (unfalloc)
					continue;
470 471
				nr_swaps_freed += !shmem_free_swap(mapping,
								index, page);
472
				continue;
473 474 475
			}

			if (!trylock_page(page))
476
				continue;
477 478 479 480 481
			if (!unfalloc || !PageUptodate(page)) {
				if (page->mapping == mapping) {
					VM_BUG_ON(PageWriteback(page));
					truncate_inode_page(mapping, page);
				}
482 483 484
			}
			unlock_page(page);
		}
485 486
		shmem_deswap_pagevec(&pvec);
		pagevec_release(&pvec);
487 488 489 490
		mem_cgroup_uncharge_end();
		cond_resched();
		index++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
491

492
	if (partial_start) {
493 494 495
		struct page *page = NULL;
		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
		if (page) {
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
			unsigned int top = PAGE_CACHE_SIZE;
			if (start > end) {
				top = partial_end;
				partial_end = 0;
			}
			zero_user_segment(page, partial_start, top);
			set_page_dirty(page);
			unlock_page(page);
			page_cache_release(page);
		}
	}
	if (partial_end) {
		struct page *page = NULL;
		shmem_getpage(inode, end, &page, SGP_READ, NULL);
		if (page) {
			zero_user_segment(page, 0, partial_end);
512 513 514 515 516
			set_page_dirty(page);
			unlock_page(page);
			page_cache_release(page);
		}
	}
517 518
	if (start >= end)
		return;
519 520 521 522

	index = start;
	for ( ; ; ) {
		cond_resched();
523
		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
524
				min(end - index, (pgoff_t)PAGEVEC_SIZE),
525 526
							pvec.pages, indices);
		if (!pvec.nr) {
527
			if (index == start || unfalloc)
528 529 530 531
				break;
			index = start;
			continue;
		}
532
		if ((index == start || unfalloc) && indices[0] >= end) {
533 534
			shmem_deswap_pagevec(&pvec);
			pagevec_release(&pvec);
535 536 537 538 539 540
			break;
		}
		mem_cgroup_uncharge_start();
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

541
			index = indices[i];
542
			if (index >= end)
543 544
				break;

545
			if (radix_tree_exceptional_entry(page)) {
546 547
				if (unfalloc)
					continue;
548 549 550 551 552
				nr_swaps_freed += !shmem_free_swap(mapping,
								index, page);
				continue;
			}

553
			lock_page(page);
554 555 556 557 558
			if (!unfalloc || !PageUptodate(page)) {
				if (page->mapping == mapping) {
					VM_BUG_ON(PageWriteback(page));
					truncate_inode_page(mapping, page);
				}
559
			}
560 561
			unlock_page(page);
		}
562 563
		shmem_deswap_pagevec(&pvec);
		pagevec_release(&pvec);
564 565 566
		mem_cgroup_uncharge_end();
		index++;
	}
567

Linus Torvalds's avatar
Linus Torvalds committed
568
	spin_lock(&info->lock);
569
	info->swapped -= nr_swaps_freed;
Linus Torvalds's avatar
Linus Torvalds committed
570 571
	shmem_recalc_inode(inode);
	spin_unlock(&info->lock);
572
}
Linus Torvalds's avatar
Linus Torvalds committed
573

574 575 576
void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
{
	shmem_undo_range(inode, lstart, lend, false);
577
	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds's avatar
Linus Torvalds committed
578
}
579
EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds's avatar
Linus Torvalds committed
580

581
static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584 585
{
	struct inode *inode = dentry->d_inode;
	int error;

586 587 588 589
	error = inode_change_ok(inode, attr);
	if (error)
		return error;

590 591 592
	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
		loff_t oldsize = inode->i_size;
		loff_t newsize = attr->ia_size;
593

594 595 596 597 598 599 600 601 602 603 604
		if (newsize != oldsize) {
			i_size_write(inode, newsize);
			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
		}
		if (newsize < oldsize) {
			loff_t holebegin = round_up(newsize, PAGE_SIZE);
			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
			shmem_truncate_range(inode, newsize, (loff_t)-1);
			/* unmap again to remove racily COWed private pages */
			unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
		}
Linus Torvalds's avatar
Linus Torvalds committed
605 606
	}

607
	setattr_copy(inode, attr);
608
#ifdef CONFIG_TMPFS_POSIX_ACL
609
	if (attr->ia_valid & ATTR_MODE)
610
		error = generic_acl_chmod(inode);
611
#endif
Linus Torvalds's avatar
Linus Torvalds committed
612 613 614
	return error;
}

Al Viro's avatar
Al Viro committed
615
static void shmem_evict_inode(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
616 617
{
	struct shmem_inode_info *info = SHMEM_I(inode);
618
	struct shmem_xattr *xattr, *nxattr;
Linus Torvalds's avatar
Linus Torvalds committed
619

620
	if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds's avatar
Linus Torvalds committed
621 622
		shmem_unacct_size(info->flags, inode->i_size);
		inode->i_size = 0;
623
		shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds's avatar
Linus Torvalds committed
624
		if (!list_empty(&info->swaplist)) {
625
			mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
626
			list_del_init(&info->swaplist);
627
			mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
628
		}
629 630
	} else
		kfree(info->symlink);
631 632 633 634 635

	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
		kfree(xattr->name);
		kfree(xattr);
	}
636
	BUG_ON(inode->i_blocks);
637
	shmem_free_inode(inode->i_sb);
638
	clear_inode(inode);
Linus Torvalds's avatar
Linus Torvalds committed
639 640
}

641 642 643
/*
 * If swap found in inode, free it and move page from swapcache to filecache.
 */
644
static int shmem_unuse_inode(struct shmem_inode_info *info,
645
			     swp_entry_t swap, struct page **pagep)
Linus Torvalds's avatar
Linus Torvalds committed
646
{
647
	struct address_space *mapping = info->vfs_inode.i_mapping;
648
	void *radswap;
649
	pgoff_t index;
650 651
	gfp_t gfp;
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
652

653
	radswap = swp_to_radix_entry(swap);
654
	index = radix_tree_locate_item(&mapping->page_tree, radswap);
655
	if (index == -1)
656
		return 0;
657

Hugh Dickins's avatar
Hugh Dickins committed
658 659
	/*
	 * Move _head_ to start search for next from here.
Al Viro's avatar
Al Viro committed
660
	 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins's avatar
Hugh Dickins committed
661
	 * mutex, and there's an instant in list_move_tail when info->swaplist
662
	 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins's avatar
Hugh Dickins committed
663 664 665
	 */
	if (shmem_swaplist.next != &info->swaplist)
		list_move_tail(&shmem_swaplist, &info->swaplist);
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	gfp = mapping_gfp_mask(mapping);
	if (shmem_should_replace_page(*pagep, gfp)) {
		mutex_unlock(&shmem_swaplist_mutex);
		error = shmem_replace_page(pagep, gfp, info, index);
		mutex_lock(&shmem_swaplist_mutex);
		/*
		 * We needed to drop mutex to make that restrictive page
		 * allocation; but the inode might already be freed by now,
		 * and we cannot refer to inode or mapping or info to check.
		 * However, we do hold page lock on the PageSwapCache page,
		 * so can check if that still has our reference remaining.
		 */
		if (!page_swapcount(*pagep))
			error = -ENOENT;
	}

KAMEZAWA Hiroyuki's avatar
KAMEZAWA Hiroyuki committed
683
	/*
684 685 686
	 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
	 * but also to hold up shmem_evict_inode(): so inode cannot be freed
	 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyuki's avatar
KAMEZAWA Hiroyuki committed
687
	 */
688 689
	if (!error)
		error = shmem_add_to_page_cache(*pagep, mapping, index,
690
						GFP_NOWAIT, radswap);
691
	if (error != -ENOMEM) {
692 693 694 695
		/*
		 * Truncation and eviction use free_swap_and_cache(), which
		 * only does trylock page: if we raced, best clean up here.
		 */
696 697
		delete_from_swap_cache(*pagep);
		set_page_dirty(*pagep);
698 699 700 701 702 703
		if (!error) {
			spin_lock(&info->lock);
			info->swapped--;
			spin_unlock(&info->lock);
			swap_free(swap);
		}
704
		error = 1;	/* not an error, but entry was found */
Linus Torvalds's avatar
Linus Torvalds committed
705
	}
706
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
707 708 709
}

/*
710
 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds's avatar
Linus Torvalds committed
711
 */
712
int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
713
{
714
	struct list_head *this, *next;
Linus Torvalds's avatar
Linus Torvalds committed
715 716
	struct shmem_inode_info *info;
	int found = 0;
717 718 719 720 721 722 723 724
	int error = 0;

	/*
	 * There's a faint possibility that swap page was replaced before
	 * caller locked it: it will come back later with the right page.
	 */
	if (unlikely(!PageSwapCache(page)))
		goto out;
725 726 727 728 729 730 731 732 733

	/*
	 * Charge page using GFP_KERNEL while we can wait, before taking
	 * the shmem_swaplist_mutex which might hold up shmem_writepage().
	 * Charged back to the user (not to caller) when swap account is used.
	 */
	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
	if (error)
		goto out;
734
	/* No radix_tree_preload: swap entry keeps a place for page in tree */
Linus Torvalds's avatar
Linus Torvalds committed
735

736
	mutex_lock(&shmem_swaplist_mutex);
737 738
	list_for_each_safe(this, next, &shmem_swaplist) {
		info = list_entry(this, struct shmem_inode_info, swaplist);
739
		if (info->swapped)
740
			found = shmem_unuse_inode(info, swap, &page);
741 742
		else
			list_del_init(&info->swaplist);
743
		cond_resched();
744
		if (found)
745
			break;
Linus Torvalds's avatar
Linus Torvalds committed
746
	}
747
	mutex_unlock(&shmem_swaplist_mutex);
748 749 750 751

	if (found < 0)
		error = found;
out:
Hugh Dickins's avatar
Hugh Dickins committed
752 753
	unlock_page(page);
	page_cache_release(page);
754
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760 761 762 763 764
}

/*
 * Move the page from the page cache to the swap cache.
 */
static int shmem_writepage(struct page *page, struct writeback_control *wbc)
{
	struct shmem_inode_info *info;
	struct address_space *mapping;
	struct inode *inode;
765 766
	swp_entry_t swap;
	pgoff_t index;
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770 771 772 773 774

	BUG_ON(!PageLocked(page));
	mapping = page->mapping;
	index = page->index;
	inode = mapping->host;
	info = SHMEM_I(inode);
	if (info->flags & VM_LOCKED)
		goto redirty;
775
	if (!total_swap_pages)
Linus Torvalds's avatar
Linus Torvalds committed
776 777
		goto redirty;

778 779 780
	/*
	 * shmem_backing_dev_info's capabilities prevent regular writeback or
	 * sync from ever calling shmem_writepage; but a stacking filesystem
781
	 * might use ->writepage of its underlying filesystem, in which case
782
	 * tmpfs should write out to swap only in response to memory pressure,
783
	 * and not for the writeback threads or sync.
784
	 */
785 786 787 788
	if (!wbc->for_reclaim) {
		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
		goto redirty;
	}
789 790 791 792 793 794 795 796 797 798 799 800

	/*
	 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
	 * value into swapfile.c, the only way we can correctly account for a
	 * fallocated page arriving here is now to initialize it and write it.
	 */
	if (!PageUptodate(page)) {
		clear_highpage(page);
		flush_dcache_page(page);
		SetPageUptodate(page);
	}

801 802 803
	swap = get_swap_page();
	if (!swap.val)
		goto redirty;
804

805 806
	/*
	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
807 808
	 * if it's not already there.  Do it now before the page is
	 * moved to swap cache, when its pagelock no longer protects
809
	 * the inode from eviction.  But don't unlock the mutex until
810 811
	 * we've incremented swapped, because shmem_unuse_inode() will
	 * prune a !swapped inode from the swaplist under this mutex.
812
	 */
813 814 815
	mutex_lock(&shmem_swaplist_mutex);
	if (list_empty(&info->swaplist))
		list_add_tail(&info->swaplist, &shmem_swaplist);
816

817
	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickins's avatar
Hugh Dickins committed
818
		swap_shmem_alloc(swap);
819 820 821 822 823
		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));

		spin_lock(&info->lock);
		info->swapped++;
		shmem_recalc_inode(inode);
824
		spin_unlock(&info->lock);
825 826

		mutex_unlock(&shmem_swaplist_mutex);
827
		BUG_ON(page_mapped(page));
828
		swap_writepage(page, wbc);
Linus Torvalds's avatar
Linus Torvalds committed
829 830 831
		return 0;
	}

832
	mutex_unlock(&shmem_swaplist_mutex);
833
	swapcache_free(swap, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
834 835
redirty:
	set_page_dirty(page);
836 837 838 839
	if (wbc->for_reclaim)
		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
	unlock_page(page);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842
}

#ifdef CONFIG_NUMA
843
#ifdef CONFIG_TMPFS
844
static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
845
{
846
	char buffer[64];
847

848
	if (!mpol || mpol->mode == MPOL_DEFAULT)
849
		return;		/* show nothing */
850

851
	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
852 853

	seq_printf(seq, ",mpol=%s", buffer);
854
}
855 856 857 858 859 860 861 862 863 864 865 866

static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
{
	struct mempolicy *mpol = NULL;
	if (sbinfo->mpol) {
		spin_lock(&sbinfo->stat_lock);	/* prevent replace/use races */
		mpol = sbinfo->mpol;
		mpol_get(mpol);
		spin_unlock(&sbinfo->stat_lock);
	}
	return mpol;
}
867 868
#endif /* CONFIG_TMPFS */

869 870
static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
871
{
872
	struct mempolicy mpol, *spol;
Linus Torvalds's avatar
Linus Torvalds committed
873 874
	struct vm_area_struct pvma;

875
	spol = mpol_cond_copy(&mpol,
876
			mpol_shared_policy_lookup(&info->policy, index));
877

Linus Torvalds's avatar
Linus Torvalds committed
878
	/* Create a pseudo vma that just contains the policy */
879
	pvma.vm_start = 0;
880
	pvma.vm_pgoff = index;
881
	pvma.vm_ops = NULL;
882
	pvma.vm_policy = spol;
883
	return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds's avatar
Linus Torvalds committed
884 885
}

886
static struct page *shmem_alloc_page(gfp_t gfp,
887
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
888 889 890
{
	struct vm_area_struct pvma;

891 892
	/* Create a pseudo vma that just contains the policy */
	pvma.vm_start = 0;
893
	pvma.vm_pgoff = index;
894
	pvma.vm_ops = NULL;
895
	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
896 897 898 899 900

	/*
	 * alloc_page_vma() will drop the shared policy reference
	 */
	return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds's avatar
Linus Torvalds committed
901
}
902 903
#else /* !CONFIG_NUMA */
#ifdef CONFIG_TMPFS
904
static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
905 906 907 908
{
}
#endif /* CONFIG_TMPFS */

909 910
static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
911
{
912
	return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
913 914
}

915
static inline struct page *shmem_alloc_page(gfp_t gfp,
916
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
917
{
918
	return alloc_page(gfp);
Linus Torvalds's avatar
Linus Torvalds committed
919
}
920
#endif /* CONFIG_NUMA */
Linus Torvalds's avatar
Linus Torvalds committed
921

922 923 924 925 926 927 928
#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
{
	return NULL;
}
#endif

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
/*
 * When a page is moved from swapcache to shmem filecache (either by the
 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
 * shmem_unuse_inode()), it may have been read in earlier from swap, in
 * ignorance of the mapping it belongs to.  If that mapping has special
 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
 * we may need to copy to a suitable page before moving to filecache.
 *
 * In a future release, this may well be extended to respect cpuset and
 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
 * but for now it is a simple matter of zone.
 */
static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
{
	return page_zonenum(page) > gfp_zone(gfp);
}

static int shmem_replace_page(struct page **pagep, gfp_t gfp,
				struct shmem_inode_info *info, pgoff_t index)
{
	struct page *oldpage, *newpage;
	struct address_space *swap_mapping;
	pgoff_t swap_index;
	int error;

	oldpage = *pagep;
	swap_index = page_private(oldpage);
	swap_mapping = page_mapping(oldpage);

	/*
	 * We have arrived here because our zones are constrained, so don't
	 * limit chance of success by further cpuset and node constraints.
	 */
	gfp &= ~GFP_CONSTRAINT_MASK;
	newpage = shmem_alloc_page(gfp, info, index);
	if (!newpage)
		return -ENOMEM;
	VM_BUG_ON(shmem_should_replace_page(newpage, gfp));

	*pagep = newpage;
	page_cache_get(newpage);
	copy_highpage(newpage, oldpage);

	VM_BUG_ON(!PageLocked(oldpage));
	__set_page_locked(newpage);
	VM_BUG_ON(!PageUptodate(oldpage));
	SetPageUptodate(newpage);
	VM_BUG_ON(!PageSwapBacked(oldpage));
	SetPageSwapBacked(newpage);
	VM_BUG_ON(!swap_index);
	set_page_private(newpage, swap_index);
	VM_BUG_ON(!PageSwapCache(oldpage));
	SetPageSwapCache(newpage);

	/*
	 * Our caller will very soon move newpage out of swapcache, but it's
	 * a nice clean interface for us to replace oldpage by newpage there.
	 */
	spin_lock_irq(&swap_mapping->tree_lock);
	error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
								   newpage);
	__inc_zone_page_state(newpage, NR_FILE_PAGES);
	__dec_zone_page_state(oldpage, NR_FILE_PAGES);
	spin_unlock_irq(&swap_mapping->tree_lock);
	BUG_ON(error);

	mem_cgroup_replace_page_cache(oldpage, newpage);
	lru_cache_add_anon(newpage);

	ClearPageSwapCache(oldpage);
	set_page_private(oldpage, 0);

	unlock_page(oldpage);
	page_cache_release(oldpage);
	page_cache_release(oldpage);
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1007
/*
1008
 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010 1011 1012 1013
 *
 * If we allocate a new one we do not mark it dirty. That's up to the
 * vm. If we swap it in we mark it dirty since we also free the swap
 * entry since a page cannot live in both the swap and page cache
 */
1014
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
1015
	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds's avatar
Linus Torvalds committed
1016 1017
{
	struct address_space *mapping = inode->i_mapping;
1018
	struct shmem_inode_info *info;
Linus Torvalds's avatar
Linus Torvalds committed
1019
	struct shmem_sb_info *sbinfo;
1020
	struct page *page;
Linus Torvalds's avatar
Linus Torvalds committed
1021 1022
	swp_entry_t swap;
	int error;
1023
	int once = 0;
1024
	int alloced = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1025

1026
	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028
		return -EFBIG;
repeat:
1029
	swap.val = 0;
1030
	page = find_lock_page(mapping, index);
1031 1032 1033 1034 1035
	if (radix_tree_exceptional_entry(page)) {
		swap = radix_to_swp_entry(page);
		page = NULL;
	}

1036
	if (sgp != SGP_WRITE && sgp != SGP_FALLOC &&
1037 1038 1039 1040 1041
	    ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
		error = -EINVAL;
		goto failed;
	}

1042 1043 1044 1045 1046 1047 1048 1049
	/* fallocated page? */
	if (page && !PageUptodate(page)) {
		if (sgp != SGP_READ)
			goto clear;
		unlock_page(page);
		page_cache_release(page);
		page = NULL;
	}
1050 1051 1052
	if (page || (sgp == SGP_READ && !swap.val)) {
		*pagep = page;
		return 0;
1053 1054 1055
	}

	/*
1056 1057
	 * Fast cache lookup did not find it:
	 * bring it back from swap or allocate.
1058
	 */
1059 1060
	info = SHMEM_I(inode);
	sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062 1063

	if (swap.val) {
		/* Look it up and read it in.. */
1064 1065
		page = lookup_swap_cache(swap);
		if (!page) {
Linus Torvalds's avatar
Linus Torvalds committed
1066
			/* here we actually do the io */
1067 1068
			if (fault_type)
				*fault_type |= VM_FAULT_MAJOR;