shmem.c 89.8 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>
Andrew Morton's avatar
Andrew Morton committed
28
#include <linux/ramfs.h>
Hugh Dickins's avatar
Hugh Dickins committed
29
#include <linux/pagemap.h>
30 31
#include <linux/file.h>
#include <linux/mm.h>
32
#include <linux/export.h>
33
#include <linux/swap.h>
34
#include <linux/uio.h>
35 36 37 38

static struct vfsmount *shm_mnt;

#ifdef CONFIG_SHMEM
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44
/*
 * 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.
 */

45
#include <linux/xattr.h>
46
#include <linux/exportfs.h>
47
#include <linux/posix_acl.h>
Christoph Hellwig's avatar
Christoph Hellwig committed
48
#include <linux/posix_acl_xattr.h>
Linus Torvalds's avatar
Linus Torvalds committed
49 50 51 52 53 54 55
#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>
56
#include <linux/pagevec.h>
57
#include <linux/percpu_counter.h>
58
#include <linux/falloc.h>
59
#include <linux/splice.h>
Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63
#include <linux/security.h>
#include <linux/swapops.h>
#include <linux/mempolicy.h>
#include <linux/namei.h>
64
#include <linux/ctype.h>
65
#include <linux/migrate.h>
66
#include <linux/highmem.h>
67
#include <linux/seq_file.h>
Mimi Zohar's avatar
Mimi Zohar committed
68
#include <linux/magic.h>
69
#include <linux/syscalls.h>
David Herrmann's avatar
David Herrmann committed
70
#include <linux/fcntl.h>
71
#include <uapi/linux/memfd.h>
72

Linus Torvalds's avatar
Linus Torvalds committed
73 74 75
#include <asm/uaccess.h>
#include <asm/pgtable.h>

76 77
#include "internal.h"

Hugh Dickins's avatar
Hugh Dickins committed
78
#define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83
#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

84 85 86
/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
#define SHORT_SYMLINK_LEN 128

87
/*
88 89 90
 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
 * inode->i_private (with i_mutex making sure that it has only one user at
 * a time): we would prefer not to enlarge the shmem inode just for that.
91 92
 */
struct shmem_falloc {
93
	wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
94 95 96 97 98 99
	pgoff_t start;		/* start of range currently being fallocated */
	pgoff_t next;		/* the next page offset to be fallocated */
	pgoff_t nr_falloced;	/* how many new pages have been fallocated */
	pgoff_t nr_unswapped;	/* how often writepage refused to swap out */
};

100
/* Flag allocation requirements to shmem_getpage */
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103
enum sgp_type {
	SGP_READ,	/* don't exceed i_size, don't allocate page */
	SGP_CACHE,	/* don't exceed i_size, may allocate page */
104
	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
105 106
	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
107 108
};

Andrew Morton's avatar
Andrew Morton committed
109
#ifdef CONFIG_TMPFS
110 111 112 113 114 115 116 117 118
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
119
#endif
120

121 122 123
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);
124 125 126 127 128 129 130 131 132
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
133 134 135 136 137 138 139 140 141 142 143 144 145 146

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)
{
147
	return (flags & VM_NORESERVE) ?
148
		0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds's avatar
Linus Torvalds committed
149 150 151 152
}

static inline void shmem_unacct_size(unsigned long flags, loff_t size)
{
153
	if (!(flags & VM_NORESERVE))
Linus Torvalds's avatar
Linus Torvalds committed
154 155 156
		vm_unacct_memory(VM_ACCT(size));
}

157 158 159 160 161 162 163 164 165 166 167 168 169
static inline int shmem_reacct_size(unsigned long flags,
		loff_t oldsize, loff_t newsize)
{
	if (!(flags & VM_NORESERVE)) {
		if (VM_ACCT(newsize) > VM_ACCT(oldsize))
			return security_vm_enough_memory_mm(current->mm,
					VM_ACCT(newsize) - VM_ACCT(oldsize));
		else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
			vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174 175 176 177
/*
 * ... 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)
{
178
	return (flags & VM_NORESERVE) ?
179
		security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183
}

static inline void shmem_unacct_blocks(unsigned long flags, long pages)
{
184
	if (flags & VM_NORESERVE)
Linus Torvalds's avatar
Linus Torvalds committed
185 186 187
		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
}

188
static const struct super_operations shmem_ops;
189
static const struct address_space_operations shmem_aops;
190
static const struct file_operations shmem_file_operations;
191 192 193
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;
194
static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds's avatar
Linus Torvalds committed
195 196

static LIST_HEAD(shmem_swaplist);
197
static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
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);
	}
}

224
/**
225
 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
 * @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) {
243 244 245
		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
246
		info->alloced -= freed;
247
		inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Linus Torvalds's avatar
Linus Torvalds committed
248 249 250 251
		shmem_unacct_blocks(info->flags, freed);
	}
}

252 253 254 255 256 257 258
/*
 * 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;
259
	void *item;
260 261

	VM_BUG_ON(!expected);
262
	VM_BUG_ON(!replacement);
263
	pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
264 265 266
	if (!pslot)
		return -ENOENT;
	item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
267 268
	if (item != expected)
		return -ENOENT;
269
	radix_tree_replace_slot(pslot, replacement);
270 271 272
	return 0;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
/*
 * Sometimes, before we decide whether to proceed or to fail, we must check
 * that an entry was not already brought back from swap by a racing thread.
 *
 * Checking page is not enough: by the time a SwapCache page is locked, it
 * might be reused, and again be SwapCache, using the same swap as before.
 */
static bool shmem_confirm_swap(struct address_space *mapping,
			       pgoff_t index, swp_entry_t swap)
{
	void *item;

	rcu_read_lock();
	item = radix_tree_lookup(&mapping->page_tree, index);
	rcu_read_unlock();
	return item == swp_to_radix_entry(swap);
}

291 292 293 294 295
/*
 * 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,
296
				   pgoff_t index, void *expected)
297
{
298
	int error;
299

300 301
	VM_BUG_ON_PAGE(!PageLocked(page), page);
	VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
302

303 304 305 306 307
	page_cache_get(page);
	page->mapping = mapping;
	page->index = index;

	spin_lock_irq(&mapping->tree_lock);
308
	if (!expected)
309 310 311 312
		error = radix_tree_insert(&mapping->page_tree, index, page);
	else
		error = shmem_radix_tree_replace(mapping, index, expected,
								 page);
313
	if (!error) {
314 315 316 317 318 319 320 321
		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);
322 323 324 325
	}
	return error;
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/*
 * 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);
}

345 346 347 348 349 350
/*
 * 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)
{
351
	void *old;
352 353

	spin_lock_irq(&mapping->tree_lock);
354
	old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
355
	spin_unlock_irq(&mapping->tree_lock);
356 357 358 359
	if (old != radswap)
		return -ENOENT;
	free_swap_and_cache(radix_to_swp_entry(radswap));
	return 0;
360 361
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/*
 * 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.
		 */
380 381
		pvec.nr = find_get_entries(mapping, index,
					   PAGEVEC_SIZE, pvec.pages, indices);
382 383 384
		if (!pvec.nr)
			break;
		index = indices[pvec.nr - 1] + 1;
385
		pagevec_remove_exceptionals(&pvec);
386 387 388 389
		check_move_unevictable_pages(pvec.pages, pvec.nr);
		pagevec_release(&pvec);
		cond_resched();
	}
390 391 392 393
}

/*
 * Remove range of pages and swap entries from radix tree, and free them.
394
 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
395
 */
396 397
static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
								 bool unfalloc)
Linus Torvalds's avatar
Linus Torvalds committed
398
{
399
	struct address_space *mapping = inode->i_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
400
	struct shmem_inode_info *info = SHMEM_I(inode);
401
	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
402 403 404
	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);
405
	struct pagevec pvec;
406 407
	pgoff_t indices[PAGEVEC_SIZE];
	long nr_swaps_freed = 0;
408
	pgoff_t index;
409 410
	int i;

411 412
	if (lend == -1)
		end = -1;	/* unsigned, so actually very big */
413 414 415

	pagevec_init(&pvec, 0);
	index = start;
416
	while (index < end) {
417 418 419
		pvec.nr = find_get_entries(mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE),
			pvec.pages, indices);
420 421
		if (!pvec.nr)
			break;
422 423 424
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

425
			index = indices[i];
426
			if (index >= end)
427 428
				break;

429
			if (radix_tree_exceptional_entry(page)) {
430 431
				if (unfalloc)
					continue;
432 433
				nr_swaps_freed += !shmem_free_swap(mapping,
								index, page);
434
				continue;
435 436 437
			}

			if (!trylock_page(page))
438
				continue;
439 440
			if (!unfalloc || !PageUptodate(page)) {
				if (page->mapping == mapping) {
441
					VM_BUG_ON_PAGE(PageWriteback(page), page);
442 443
					truncate_inode_page(mapping, page);
				}
444 445 446
			}
			unlock_page(page);
		}
447
		pagevec_remove_exceptionals(&pvec);
448
		pagevec_release(&pvec);
449 450 451
		cond_resched();
		index++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
452

453
	if (partial_start) {
454 455 456
		struct page *page = NULL;
		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
		if (page) {
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
			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);
473 474 475 476 477
			set_page_dirty(page);
			unlock_page(page);
			page_cache_release(page);
		}
	}
478 479
	if (start >= end)
		return;
480 481

	index = start;
482
	while (index < end) {
483
		cond_resched();
484 485

		pvec.nr = find_get_entries(mapping, index,
486
				min(end - index, (pgoff_t)PAGEVEC_SIZE),
487
				pvec.pages, indices);
488
		if (!pvec.nr) {
489 490
			/* If all gone or hole-punch or unfalloc, we're done */
			if (index == start || end != -1)
491
				break;
492
			/* But if truncating, restart to make sure all gone */
493 494 495 496 497 498
			index = start;
			continue;
		}
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

499
			index = indices[i];
500
			if (index >= end)
501 502
				break;

503
			if (radix_tree_exceptional_entry(page)) {
504 505
				if (unfalloc)
					continue;
506 507 508 509 510 511
				if (shmem_free_swap(mapping, index, page)) {
					/* Swap was replaced by page: retry */
					index--;
					break;
				}
				nr_swaps_freed++;
512 513 514
				continue;
			}

515
			lock_page(page);
516 517
			if (!unfalloc || !PageUptodate(page)) {
				if (page->mapping == mapping) {
518
					VM_BUG_ON_PAGE(PageWriteback(page), page);
519
					truncate_inode_page(mapping, page);
520 521 522 523 524
				} else {
					/* Page was replaced by swap: retry */
					unlock_page(page);
					index--;
					break;
525
				}
526
			}
527 528
			unlock_page(page);
		}
529
		pagevec_remove_exceptionals(&pvec);
530
		pagevec_release(&pvec);
531 532
		index++;
	}
533

Linus Torvalds's avatar
Linus Torvalds committed
534
	spin_lock(&info->lock);
535
	info->swapped -= nr_swaps_freed;
Linus Torvalds's avatar
Linus Torvalds committed
536 537
	shmem_recalc_inode(inode);
	spin_unlock(&info->lock);
538
}
Linus Torvalds's avatar
Linus Torvalds committed
539

540 541 542
void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
{
	shmem_undo_range(inode, lstart, lend, false);
543
	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds's avatar
Linus Torvalds committed
544
}
545
EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds's avatar
Linus Torvalds committed
546

547 548 549 550 551 552
static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
			 struct kstat *stat)
{
	struct inode *inode = dentry->d_inode;
	struct shmem_inode_info *info = SHMEM_I(inode);

553 554 555 556 557
	if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
		spin_lock(&info->lock);
		shmem_recalc_inode(inode);
		spin_unlock(&info->lock);
	}
558 559 560 561
	generic_fillattr(inode, stat);
	return 0;
}

562
static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
563
{
564
	struct inode *inode = d_inode(dentry);
David Herrmann's avatar
David Herrmann committed
565
	struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds's avatar
Linus Torvalds committed
566 567
	int error;

568 569 570 571
	error = inode_change_ok(inode, attr);
	if (error)
		return error;

572 573 574
	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
		loff_t oldsize = inode->i_size;
		loff_t newsize = attr->ia_size;
575

David Herrmann's avatar
David Herrmann committed
576 577 578 579 580
		/* protected by i_mutex */
		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
			return -EPERM;

581
		if (newsize != oldsize) {
582 583 584 585
			error = shmem_reacct_size(SHMEM_I(inode)->flags,
					oldsize, newsize);
			if (error)
				return error;
586 587 588
			i_size_write(inode, newsize);
			inode->i_ctime = inode->i_mtime = CURRENT_TIME;
		}
589
		if (newsize <= oldsize) {
590
			loff_t holebegin = round_up(newsize, PAGE_SIZE);
591 592 593 594 595 596
			if (oldsize > holebegin)
				unmap_mapping_range(inode->i_mapping,
							holebegin, 0, 1);
			if (info->alloced)
				shmem_truncate_range(inode,
							newsize, (loff_t)-1);
597
			/* unmap again to remove racily COWed private pages */
598 599 600
			if (oldsize > holebegin)
				unmap_mapping_range(inode->i_mapping,
							holebegin, 0, 1);
601
		}
Linus Torvalds's avatar
Linus Torvalds committed
602 603
	}

604 605
	setattr_copy(inode, attr);
	if (attr->ia_valid & ATTR_MODE)
Christoph Hellwig's avatar
Christoph Hellwig committed
606
		error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609
	return error;
}

Al Viro's avatar
Al Viro committed
610
static void shmem_evict_inode(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613
{
	struct shmem_inode_info *info = SHMEM_I(inode);

614
	if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds's avatar
Linus Torvalds committed
615 616
		shmem_unacct_size(info->flags, inode->i_size);
		inode->i_size = 0;
617
		shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds's avatar
Linus Torvalds committed
618
		if (!list_empty(&info->swaplist)) {
619
			mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
620
			list_del_init(&info->swaplist);
621
			mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
622
		}
623 624
	} else
		kfree(info->symlink);
625

626
	simple_xattrs_free(&info->xattrs);
627
	WARN_ON(inode->i_blocks);
628
	shmem_free_inode(inode->i_sb);
629
	clear_inode(inode);
Linus Torvalds's avatar
Linus Torvalds committed
630 631
}

632 633 634
/*
 * If swap found in inode, free it and move page from swapcache to filecache.
 */
635
static int shmem_unuse_inode(struct shmem_inode_info *info,
636
			     swp_entry_t swap, struct page **pagep)
Linus Torvalds's avatar
Linus Torvalds committed
637
{
638
	struct address_space *mapping = info->vfs_inode.i_mapping;
639
	void *radswap;
640
	pgoff_t index;
641 642
	gfp_t gfp;
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
643

644
	radswap = swp_to_radix_entry(swap);
645
	index = radix_tree_locate_item(&mapping->page_tree, radswap);
646
	if (index == -1)
647
		return -EAGAIN;	/* tell shmem_unuse we found nothing */
648

Hugh Dickins's avatar
Hugh Dickins committed
649 650
	/*
	 * Move _head_ to start search for next from here.
Al Viro's avatar
Al Viro committed
651
	 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins's avatar
Hugh Dickins committed
652
	 * mutex, and there's an instant in list_move_tail when info->swaplist
653
	 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins's avatar
Hugh Dickins committed
654 655 656
	 */
	if (shmem_swaplist.next != &info->swaplist)
		list_move_tail(&shmem_swaplist, &info->swaplist);
657

658 659 660 661 662 663 664
	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
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
		 * allocation, but the inode might have been freed while we
		 * dropped it: although a racing shmem_evict_inode() cannot
		 * complete without emptying the radix_tree, our page lock
		 * on this swapcache page is not enough to prevent that -
		 * free_swap_and_cache() of our swap entry will only
		 * trylock_page(), removing swap from radix_tree whatever.
		 *
		 * We must not proceed to shmem_add_to_page_cache() if the
		 * inode has been freed, but of course we cannot rely on
		 * inode or mapping or info to check that.  However, we can
		 * safely check if our swap entry is still in use (and here
		 * it can't have got reused for another page): if it's still
		 * in use, then the inode cannot have been freed yet, and we
		 * can safely proceed (if it's no longer in use, that tells
		 * nothing about the inode, but we don't need to unuse swap).
680 681 682 683 684
		 */
		if (!page_swapcount(*pagep))
			error = -ENOENT;
	}

KAMEZAWA Hiroyuki's avatar
KAMEZAWA Hiroyuki committed
685
	/*
686 687 688
	 * 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
689
	 */
690 691
	if (!error)
		error = shmem_add_to_page_cache(*pagep, mapping, index,
692
						radswap);
693
	if (error != -ENOMEM) {
694 695 696 697
		/*
		 * Truncation and eviction use free_swap_and_cache(), which
		 * only does trylock page: if we raced, best clean up here.
		 */
698 699
		delete_from_swap_cache(*pagep);
		set_page_dirty(*pagep);
700 701 702 703 704 705
		if (!error) {
			spin_lock(&info->lock);
			info->swapped--;
			spin_unlock(&info->lock);
			swap_free(swap);
		}
Linus Torvalds's avatar
Linus Torvalds committed
706
	}
707
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
708 709 710
}

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

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

	/*
	 * 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.
	 */
732
	error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg);
733 734
	if (error)
		goto out;
735
	/* No radix_tree_preload: swap entry keeps a place for page in tree */
736
	error = -EAGAIN;
Linus Torvalds's avatar
Linus Torvalds committed
737

738
	mutex_lock(&shmem_swaplist_mutex);
739 740
	list_for_each_safe(this, next, &shmem_swaplist) {
		info = list_entry(this, struct shmem_inode_info, swaplist);
741
		if (info->swapped)
742
			error = shmem_unuse_inode(info, swap, &page);
743 744
		else
			list_del_init(&info->swaplist);
745
		cond_resched();
746
		if (error != -EAGAIN)
747
			break;
748
		/* found nothing in this: move on to search the next */
Linus Torvalds's avatar
Linus Torvalds committed
749
	}
750
	mutex_unlock(&shmem_swaplist_mutex);
751

752 753 754 755 756 757
	if (error) {
		if (error != -ENOMEM)
			error = 0;
		mem_cgroup_cancel_charge(page, memcg);
	} else
		mem_cgroup_commit_charge(page, memcg, true);
758
out:
Hugh Dickins's avatar
Hugh Dickins committed
759 760
	unlock_page(page);
	page_cache_release(page);
761
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
762 763 764 765 766 767 768 769 770 771
}

/*
 * 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;
772 773
	swp_entry_t swap;
	pgoff_t index;
Linus Torvalds's avatar
Linus Torvalds committed
774 775 776 777 778 779 780 781

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

785
	/*
786 787 788 789 790
	 * Our capabilities prevent regular writeback or sync from ever calling
	 * shmem_writepage; but a stacking filesystem might use ->writepage of
	 * its underlying filesystem, in which case tmpfs should write out to
	 * swap only in response to memory pressure, and not for the writeback
	 * threads or sync.
791
	 */
792 793 794 795
	if (!wbc->for_reclaim) {
		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
		goto redirty;
	}
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.
801 802 803 804 805 806
	 *
	 * That's okay for a page already fallocated earlier, but if we have
	 * not yet completed the fallocation, then (a) we want to keep track
	 * of this page in case we have to undo it, and (b) it may not be a
	 * good idea to continue anyway, once we're pushing into swap.  So
	 * reactivate the page, and let shmem_fallocate() quit when too many.
807 808
	 */
	if (!PageUptodate(page)) {
809 810 811 812 813
		if (inode->i_private) {
			struct shmem_falloc *shmem_falloc;
			spin_lock(&inode->i_lock);
			shmem_falloc = inode->i_private;
			if (shmem_falloc &&
814
			    !shmem_falloc->waitq &&
815 816 817 818 819 820 821 822 823
			    index >= shmem_falloc->start &&
			    index < shmem_falloc->next)
				shmem_falloc->nr_unswapped++;
			else
				shmem_falloc = NULL;
			spin_unlock(&inode->i_lock);
			if (shmem_falloc)
				goto redirty;
		}
824 825 826 827 828
		clear_highpage(page);
		flush_dcache_page(page);
		SetPageUptodate(page);
	}

829 830 831
	swap = get_swap_page();
	if (!swap.val)
		goto redirty;
832

833 834
	/*
	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
835 836
	 * if it's not already there.  Do it now before the page is
	 * moved to swap cache, when its pagelock no longer protects
837
	 * the inode from eviction.  But don't unlock the mutex until
838 839
	 * we've incremented swapped, because shmem_unuse_inode() will
	 * prune a !swapped inode from the swaplist under this mutex.
840
	 */
841 842 843
	mutex_lock(&shmem_swaplist_mutex);
	if (list_empty(&info->swaplist))
		list_add_tail(&info->swaplist, &shmem_swaplist);
844

845
	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Hugh Dickins's avatar
Hugh Dickins committed
846
		swap_shmem_alloc(swap);
847 848 849 850 851
		shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));

		spin_lock(&info->lock);
		info->swapped++;
		shmem_recalc_inode(inode);
852
		spin_unlock(&info->lock);
853 854

		mutex_unlock(&shmem_swaplist_mutex);
855
		BUG_ON(page_mapped(page));
856
		swap_writepage(page, wbc);
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859
		return 0;
	}

860
	mutex_unlock(&shmem_swaplist_mutex);
861
	swapcache_free(swap);
Linus Torvalds's avatar
Linus Torvalds committed
862 863
redirty:
	set_page_dirty(page);
864 865 866 867
	if (wbc->for_reclaim)
		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
	unlock_page(page);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
868 869 870
}

#ifdef CONFIG_NUMA
871
#ifdef CONFIG_TMPFS
872
static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
873
{
874
	char buffer[64];
875

876
	if (!mpol || mpol->mode == MPOL_DEFAULT)
877
		return;		/* show nothing */
878

879
	mpol_to_str(buffer, sizeof(buffer), mpol);
880 881

	seq_printf(seq, ",mpol=%s", buffer);
882
}
883 884 885 886 887 888 889 890 891 892 893 894

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;
}
895 896
#endif /* CONFIG_TMPFS */

897 898
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
899 900
{
	struct vm_area_struct pvma;
Mel Gorman's avatar
Mel Gorman committed
901
	struct page *page;
902

Linus Torvalds's avatar
Linus Torvalds committed
903
	/* Create a pseudo vma that just contains the policy */
904
	pvma.vm_start = 0;
905 906
	/* Bias interleave by inode number to distribute better across nodes */
	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
907
	pvma.vm_ops = NULL;
Mel Gorman's avatar
Mel Gorman committed
908 909 910 911 912 913 914 915
	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);

	page = swapin_readahead(swap, gfp, &pvma, 0);

	/* Drop reference taken by mpol_shared_policy_lookup() */
	mpol_cond_put(pvma.vm_policy);

	return page;
Linus Torvalds's avatar
Linus Torvalds committed
916 917
}

918
static struct page *shmem_alloc_page(gfp_t gfp,
919
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
920 921
{
	struct vm_area_struct pvma;
Mel Gorman's avatar
Mel Gorman committed
922
	struct page *page;
Linus Torvalds's avatar
Linus Torvalds committed
923

924 925
	/* Create a pseudo vma that just contains the policy */
	pvma.vm_start = 0;
926 927
	/* Bias interleave by inode number to distribute better across nodes */
	pvma.vm_pgoff = index + info->vfs_inode.i_ino;
928
	pvma.vm_ops = NULL;
929
	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
930

Mel Gorman's avatar
Mel Gorman committed
931 932 933 934 935 936
	page = alloc_page_vma(gfp, &pvma, 0);

	/* Drop reference taken by mpol_shared_policy_lookup() */
	mpol_cond_put(pvma.vm_policy);

	return page;
Linus Torvalds's avatar
Linus Torvalds committed
937
}
938 939
#else /* !CONFIG_NUMA */
#ifdef CONFIG_TMPFS
940
static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
941 942 943 944
{
}
#endif /* CONFIG_TMPFS */

945 946
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
947
{
948
	return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
949 950
}

951
static inline struct page *shmem_alloc_page(gfp_t gfp,
952
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
953
{
954
	return alloc_page(gfp);
Linus Torvalds's avatar
Linus Torvalds committed
955
}
956
#endif /* CONFIG_NUMA */
Linus Torvalds's avatar
Linus Torvalds committed
957

958 959 960 961 962 963 964
#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
{
	return NULL;
}
#endif

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
/*
 * 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;

	page_cache_get(newpage);
	copy_highpage(newpage, oldpage);
1005
	flush_dcache_page(newpage);