shmem.c 65.5 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-2005 Hugh Dickins.
 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds's avatar
Linus Torvalds committed
11 12 13 14 15 16
 * 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>
 *
17 18 19
 * tiny-shmem:
 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
 *
Linus Torvalds's avatar
Linus Torvalds committed
20 21 22
 * This file is released under the GPL.
 */

23 24 25 26
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/vfs.h>
#include <linux/mount.h>
27
#include <linux/pagemap.h>
28 29 30 31 32 33 34 35
#include <linux/file.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/swap.h>

static struct vfsmount *shm_mnt;

#ifdef CONFIG_SHMEM
Linus Torvalds's avatar
Linus Torvalds committed
36 37 38 39 40 41
/*
 * 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.
 */

42
#include <linux/xattr.h>
43
#include <linux/exportfs.h>
44
#include <linux/posix_acl.h>
45
#include <linux/generic_acl.h>
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52
#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>
53
#include <linux/pagevec.h>
54
#include <linux/percpu_counter.h>
55
#include <linux/splice.h>
Linus Torvalds's avatar
Linus Torvalds committed
56 57 58 59
#include <linux/security.h>
#include <linux/swapops.h>
#include <linux/mempolicy.h>
#include <linux/namei.h>
60
#include <linux/ctype.h>
61
#include <linux/migrate.h>
62
#include <linux/highmem.h>
63
#include <linux/seq_file.h>
Mimi Zohar's avatar
Mimi Zohar committed
64
#include <linux/magic.h>
65

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

69
#define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
Linus Torvalds's avatar
Linus Torvalds committed
70 71 72 73 74
#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

75 76 77 78 79 80 81
struct shmem_xattr {
	struct list_head list;	/* anchored by shmem_inode_info->xattr_list */
	char *name;		/* xattr name */
	size_t size;
	char value[0];
};

82
/* Flag allocation requirements to shmem_getpage */
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85
enum sgp_type {
	SGP_READ,	/* don't exceed i_size, don't allocate page */
	SGP_CACHE,	/* don't exceed i_size, may allocate page */
86
	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89
	SGP_WRITE,	/* may exceed i_size, may allocate page */
};

Andrew Morton's avatar
Andrew Morton committed
90
#ifdef CONFIG_TMPFS
91 92 93 94 95 96 97 98 99
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
100
#endif
101

102 103 104 105 106 107 108 109 110
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
111 112 113 114 115 116 117 118 119 120 121 122 123 124

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)
{
125 126
	return (flags & VM_NORESERVE) ?
		0 : security_vm_enough_memory_kern(VM_ACCT(size));
Linus Torvalds's avatar
Linus Torvalds committed
127 128 129 130
}

static inline void shmem_unacct_size(unsigned long flags, loff_t size)
{
131
	if (!(flags & VM_NORESERVE))
Linus Torvalds's avatar
Linus Torvalds committed
132 133 134 135 136 137 138 139 140 141 142
		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)
{
143 144
	return (flags & VM_NORESERVE) ?
		security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147 148
}

static inline void shmem_unacct_blocks(unsigned long flags, long pages)
{
149
	if (flags & VM_NORESERVE)
Linus Torvalds's avatar
Linus Torvalds committed
150 151 152
		vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
}

153
static const struct super_operations shmem_ops;
154
static const struct address_space_operations shmem_aops;
155
static const struct file_operations shmem_file_operations;
156 157 158
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;
159
static const struct vm_operations_struct shmem_vm_ops;
Linus Torvalds's avatar
Linus Torvalds committed
160

161
static struct backing_dev_info shmem_backing_dev_info  __read_mostly = {
Linus Torvalds's avatar
Linus Torvalds committed
162
	.ra_pages	= 0,	/* No readahead */
163
	.capabilities	= BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
Linus Torvalds's avatar
Linus Torvalds committed
164 165 166
};

static LIST_HEAD(shmem_swaplist);
167
static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171

static void shmem_free_blocks(struct inode *inode, long pages)
{
	struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
172
	if (sbinfo->max_blocks) {
173
		percpu_counter_add(&sbinfo->used_blocks, -pages);
Linus Torvalds's avatar
Linus Torvalds committed
174 175 176 177
		inode->i_blocks -= pages*BLOCKS_PER_PAGE;
	}
}

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 222 223 224 225 226 227
 * @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) {
		info->alloced -= freed;
		shmem_unacct_blocks(info->flags, freed);
		shmem_free_blocks(inode, freed);
	}
}

228 229
static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
			   swp_entry_t swap)
Linus Torvalds's avatar
Linus Torvalds committed
230
{
231 232
	if (index < SHMEM_NR_DIRECT)
		info->i_direct[index] = swap;
Linus Torvalds's avatar
Linus Torvalds committed
233 234
}

235
static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
236
{
237 238
	return (index < SHMEM_NR_DIRECT) ?
		info->i_direct[index] : (swp_entry_t){0};
Linus Torvalds's avatar
Linus Torvalds committed
239 240
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*
 * 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;
}

/*
 * 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)) {
			if (radix_tree_exceptional_entry(page))
				goto export;
			/* radix_tree_deref_retry(page) */
			goto restart;
		}
		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.
 */
static void shmem_pagevec_release(struct pagevec *pvec)
{
	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;
	pagevec_release(pvec);
}

/*
 * Remove range of pages and swap entries from radix tree, and free them.
 */
346
void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Linus Torvalds's avatar
Linus Torvalds committed
347
{
348
	struct address_space *mapping = inode->i_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
349
	struct shmem_inode_info *info = SHMEM_I(inode);
350
	pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
351
	unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
352
	pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
353
	struct pagevec pvec;
354 355
	pgoff_t indices[PAGEVEC_SIZE];
	long nr_swaps_freed = 0;
356
	pgoff_t index;
357 358 359 360 361 362
	int i;

	BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));

	pagevec_init(&pvec, 0);
	index = start;
363 364 365 366 367 368
	while (index <= end) {
		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
							pvec.pages, indices);
		if (!pvec.nr)
			break;
369 370 371 372
		mem_cgroup_uncharge_start();
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

373
			index = indices[i];
374 375 376
			if (index > end)
				break;

377 378 379
			if (radix_tree_exceptional_entry(page)) {
				nr_swaps_freed += !shmem_free_swap(mapping,
								index, page);
380
				continue;
381 382 383
			}

			if (!trylock_page(page))
384
				continue;
385 386 387
			if (page->mapping == mapping) {
				VM_BUG_ON(PageWriteback(page));
				truncate_inode_page(mapping, page);
388 389 390
			}
			unlock_page(page);
		}
391
		shmem_pagevec_release(&pvec);
392 393 394 395
		mem_cgroup_uncharge_end();
		cond_resched();
		index++;
	}
Linus Torvalds's avatar
Linus Torvalds committed
396

397 398 399 400 401 402 403 404 405 406 407 408 409 410
	if (partial) {
		struct page *page = NULL;
		shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
		if (page) {
			zero_user_segment(page, partial, PAGE_CACHE_SIZE);
			set_page_dirty(page);
			unlock_page(page);
			page_cache_release(page);
		}
	}

	index = start;
	for ( ; ; ) {
		cond_resched();
411 412 413 414
		pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
							pvec.pages, indices);
		if (!pvec.nr) {
415 416 417 418 419
			if (index == start)
				break;
			index = start;
			continue;
		}
420 421
		if (index == start && indices[0] > end) {
			shmem_pagevec_release(&pvec);
422 423 424 425 426 427
			break;
		}
		mem_cgroup_uncharge_start();
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

428
			index = indices[i];
429 430 431
			if (index > end)
				break;

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

438
			lock_page(page);
439 440 441 442
			if (page->mapping == mapping) {
				VM_BUG_ON(PageWriteback(page));
				truncate_inode_page(mapping, page);
			}
443 444
			unlock_page(page);
		}
445
		shmem_pagevec_release(&pvec);
446 447 448
		mem_cgroup_uncharge_end();
		index++;
	}
449

Linus Torvalds's avatar
Linus Torvalds committed
450
	spin_lock(&info->lock);
451
	info->swapped -= nr_swaps_freed;
Linus Torvalds's avatar
Linus Torvalds committed
452 453 454
	shmem_recalc_inode(inode);
	spin_unlock(&info->lock);

455
	inode->i_ctime = inode->i_mtime = CURRENT_TIME;
Linus Torvalds's avatar
Linus Torvalds committed
456
}
457
EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds's avatar
Linus Torvalds committed
458

459
static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463
{
	struct inode *inode = dentry->d_inode;
	int error;

464 465 466 467
	error = inode_change_ok(inode, attr);
	if (error)
		return error;

468 469 470
	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
		loff_t oldsize = inode->i_size;
		loff_t newsize = attr->ia_size;
471

472 473 474 475 476 477 478 479 480 481 482
		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
483 484
	}

485
	setattr_copy(inode, attr);
486
#ifdef CONFIG_TMPFS_POSIX_ACL
487
	if (attr->ia_valid & ATTR_MODE)
488
		error = generic_acl_chmod(inode);
489
#endif
Linus Torvalds's avatar
Linus Torvalds committed
490 491 492
	return error;
}

Al Viro's avatar
Al Viro committed
493
static void shmem_evict_inode(struct inode *inode)
Linus Torvalds's avatar
Linus Torvalds committed
494 495
{
	struct shmem_inode_info *info = SHMEM_I(inode);
496
	struct shmem_xattr *xattr, *nxattr;
Linus Torvalds's avatar
Linus Torvalds committed
497

498
	if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds's avatar
Linus Torvalds committed
499 500
		shmem_unacct_size(info->flags, inode->i_size);
		inode->i_size = 0;
501
		shmem_truncate_range(inode, 0, (loff_t)-1);
Linus Torvalds's avatar
Linus Torvalds committed
502
		if (!list_empty(&info->swaplist)) {
503
			mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
504
			list_del_init(&info->swaplist);
505
			mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
506 507
		}
	}
508 509 510 511 512

	list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
		kfree(xattr->name);
		kfree(xattr);
	}
513
	BUG_ON(inode->i_blocks);
514
	shmem_free_inode(inode->i_sb);
Al Viro's avatar
Al Viro committed
515
	end_writeback(inode);
Linus Torvalds's avatar
Linus Torvalds committed
516 517
}

518 519
static int shmem_unuse_inode(struct shmem_inode_info *info,
			     swp_entry_t swap, struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
520
{
521
	struct address_space *mapping = info->vfs_inode.i_mapping;
522
	pgoff_t index;
523
	int error;
Linus Torvalds's avatar
Linus Torvalds committed
524

525 526
	for (index = 0; index < SHMEM_NR_DIRECT; index++)
		if (shmem_get_swap(info, index).val == swap.val)
527
			goto found;
Linus Torvalds's avatar
Linus Torvalds committed
528 529
	return 0;
found:
530
	spin_lock(&info->lock);
531
	if (shmem_get_swap(info, index).val != swap.val) {
532 533 534
		spin_unlock(&info->lock);
		return 0;
	}
535

536 537
	/*
	 * Move _head_ to start search for next from here.
Al Viro's avatar
Al Viro committed
538
	 * But be careful: shmem_evict_inode checks list_empty without taking
539
	 * mutex, and there's an instant in list_move_tail when info->swaplist
540
	 * would appear empty, if it were the only one on shmem_swaplist.
541 542 543
	 */
	if (shmem_swaplist.next != &info->swaplist)
		list_move_tail(&shmem_swaplist, &info->swaplist);
544

545
	/*
546 547 548
	 * 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).
549
	 */
550
	error = add_to_page_cache_locked(page, mapping, index, GFP_NOWAIT);
551
	/* which does mem_cgroup_uncharge_cache_page on error */
552

553
	if (error != -ENOMEM) {
554 555
		delete_from_swap_cache(page);
		set_page_dirty(page);
556
		shmem_put_swap(info, index, (swp_entry_t){0});
557
		info->swapped--;
558
		swap_free(swap);
559
		error = 1;	/* not an error, but entry was found */
Linus Torvalds's avatar
Linus Torvalds committed
560 561
	}
	spin_unlock(&info->lock);
562
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565 566 567
}

/*
 * shmem_unuse() search for an eventually swapped out shmem page.
 */
568
int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
569
{
570
	struct list_head *this, *next;
Linus Torvalds's avatar
Linus Torvalds committed
571 572
	struct shmem_inode_info *info;
	int found = 0;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	int error;

	/*
	 * 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.
	 * add_to_page_cache() will be called with GFP_NOWAIT.
	 */
	error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
	if (error)
		goto out;
	/*
	 * Try to preload while we can wait, to not make a habit of
	 * draining atomic reserves; but don't latch on to this cpu,
	 * it's okay if sometimes we get rescheduled after this.
	 */
	error = radix_tree_preload(GFP_KERNEL);
	if (error)
		goto uncharge;
	radix_tree_preload_end();
Linus Torvalds's avatar
Linus Torvalds committed
593

594
	mutex_lock(&shmem_swaplist_mutex);
595 596
	list_for_each_safe(this, next, &shmem_swaplist) {
		info = list_entry(this, struct shmem_inode_info, swaplist);
597 598 599 600 601 602 603
		if (!info->swapped) {
			spin_lock(&info->lock);
			if (!info->swapped)
				list_del_init(&info->swaplist);
			spin_unlock(&info->lock);
		}
		if (info->swapped)
604
			found = shmem_unuse_inode(info, swap, page);
605
		cond_resched();
606
		if (found)
607
			break;
Linus Torvalds's avatar
Linus Torvalds committed
608
	}
609
	mutex_unlock(&shmem_swaplist_mutex);
610 611 612 613 614 615 616

uncharge:
	if (!found)
		mem_cgroup_uncharge_cache_page(page);
	if (found < 0)
		error = found;
out:
617 618
	unlock_page(page);
	page_cache_release(page);
619
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
620 621 622 623 624 625 626 627
}

/*
 * 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;
628
	swp_entry_t swap, oswap;
Linus Torvalds's avatar
Linus Torvalds committed
629
	struct address_space *mapping;
630
	pgoff_t index;
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635 636 637 638 639
	struct inode *inode;

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

643 644 645
	/*
	 * shmem_backing_dev_info's capabilities prevent regular writeback or
	 * sync from ever calling shmem_writepage; but a stacking filesystem
646
	 * might use ->writepage of its underlying filesystem, in which case
647
	 * tmpfs should write out to swap only in response to memory pressure,
648
	 * and not for the writeback threads or sync.
649
	 */
650 651 652 653
	if (!wbc->for_reclaim) {
		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
		goto redirty;
	}
654 655

	/*
656 657
	 * Disable even the toy swapping implementation, while we convert
	 * functions one by one to having swap entries in the radix tree.
658
	 */
659
	if (index < ULONG_MAX)
660 661
		goto redirty;

662 663 664
	swap = get_swap_page();
	if (!swap.val)
		goto redirty;
665

666 667 668 669 670 671 672 673 674
	/*
	 * Add inode to shmem_unuse()'s list of swapped-out inodes,
	 * if it's not already there.  Do it now because we cannot take
	 * mutex while holding spinlock, and must do so before the page
	 * is moved to swap cache, when its pagelock no longer protects
	 * the inode from eviction.  But don't unlock the mutex until
	 * we've taken the spinlock, because shmem_unuse_inode() will
	 * prune a !swapped inode from the swaplist under both locks.
	 */
675 676 677
	mutex_lock(&shmem_swaplist_mutex);
	if (list_empty(&info->swaplist))
		list_add_tail(&info->swaplist, &shmem_swaplist);
678

Linus Torvalds's avatar
Linus Torvalds committed
679
	spin_lock(&info->lock);
680
	mutex_unlock(&shmem_swaplist_mutex);
681

682 683
	oswap = shmem_get_swap(info, index);
	if (oswap.val) {
684
		WARN_ON_ONCE(1);	/* Still happens? Tell us about it! */
685 686 687
		free_swap_and_cache(oswap);
		shmem_put_swap(info, index, (swp_entry_t){0});
		info->swapped--;
688 689
	}
	shmem_recalc_inode(inode);
Linus Torvalds's avatar
Linus Torvalds committed
690

691
	if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
692
		delete_from_page_cache(page);
693 694
		shmem_put_swap(info, index, swap);
		info->swapped++;
695
		swap_shmem_alloc(swap);
696
		spin_unlock(&info->lock);
697
		BUG_ON(page_mapped(page));
698
		swap_writepage(page, wbc);
Linus Torvalds's avatar
Linus Torvalds committed
699 700 701 702
		return 0;
	}

	spin_unlock(&info->lock);
703
	swapcache_free(swap, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
704 705
redirty:
	set_page_dirty(page);
706 707 708 709
	if (wbc->for_reclaim)
		return AOP_WRITEPAGE_ACTIVATE;	/* Return with page locked */
	unlock_page(page);
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712
}

#ifdef CONFIG_NUMA
713
#ifdef CONFIG_TMPFS
714
static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
715
{
716
	char buffer[64];
717

718
	if (!mpol || mpol->mode == MPOL_DEFAULT)
719
		return;		/* show nothing */
720

721
	mpol_to_str(buffer, sizeof(buffer), mpol, 1);
722 723

	seq_printf(seq, ",mpol=%s", buffer);
724
}
725 726 727 728 729 730 731 732 733 734 735 736

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;
}
737 738
#endif /* CONFIG_TMPFS */

739 740
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
741
{
742
	struct mempolicy mpol, *spol;
Linus Torvalds's avatar
Linus Torvalds committed
743 744
	struct vm_area_struct pvma;

745
	spol = mpol_cond_copy(&mpol,
746
			mpol_shared_policy_lookup(&info->policy, index));
747

Linus Torvalds's avatar
Linus Torvalds committed
748
	/* Create a pseudo vma that just contains the policy */
749
	pvma.vm_start = 0;
750
	pvma.vm_pgoff = index;
751
	pvma.vm_ops = NULL;
752
	pvma.vm_policy = spol;
753
	return swapin_readahead(swap, gfp, &pvma, 0);
Linus Torvalds's avatar
Linus Torvalds committed
754 755
}

756
static struct page *shmem_alloc_page(gfp_t gfp,
757
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
758 759 760
{
	struct vm_area_struct pvma;

761 762
	/* Create a pseudo vma that just contains the policy */
	pvma.vm_start = 0;
763
	pvma.vm_pgoff = index;
764
	pvma.vm_ops = NULL;
765
	pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
766 767 768 769 770

	/*
	 * alloc_page_vma() will drop the shared policy reference
	 */
	return alloc_page_vma(gfp, &pvma, 0);
Linus Torvalds's avatar
Linus Torvalds committed
771
}
772 773
#else /* !CONFIG_NUMA */
#ifdef CONFIG_TMPFS
774
static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
775 776 777 778
{
}
#endif /* CONFIG_TMPFS */

779 780
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
781
{
782
	return swapin_readahead(swap, gfp, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
783 784
}

785
static inline struct page *shmem_alloc_page(gfp_t gfp,
786
			struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds's avatar
Linus Torvalds committed
787
{
788
	return alloc_page(gfp);
Linus Torvalds's avatar
Linus Torvalds committed
789
}
790
#endif /* CONFIG_NUMA */
Linus Torvalds's avatar
Linus Torvalds committed
791

792 793 794 795 796 797 798
#if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
{
	return NULL;
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
799
/*
800
 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds's avatar
Linus Torvalds committed
801 802 803 804 805
 *
 * 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
 */
806
static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
807
	struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
Linus Torvalds's avatar
Linus Torvalds committed
808 809 810 811
{
	struct address_space *mapping = inode->i_mapping;
	struct shmem_inode_info *info = SHMEM_I(inode);
	struct shmem_sb_info *sbinfo;
812
	struct page *page;
813
	struct page *prealloc_page = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
814 815 816
	swp_entry_t swap;
	int error;

817
	if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
Linus Torvalds's avatar
Linus Torvalds committed
818 819
		return -EFBIG;
repeat:
820
	page = find_lock_page(mapping, index);
821
	if (page) {
Hugh Dickins's avatar
Hugh Dickins committed
822
		/*
823 824 825
		 * Once we can get the page lock, it must be uptodate:
		 * if there were an error in reading back from swap,
		 * the page would not be inserted into the filecache.
Hugh Dickins's avatar
Hugh Dickins committed
826
		 */
827 828 829 830 831 832 833 834 835 836 837 838 839 840
		BUG_ON(!PageUptodate(page));
		goto done;
	}

	/*
	 * Try to preload while we can wait, to not make a habit of
	 * draining atomic reserves; but don't latch on to this cpu.
	 */
	error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
	if (error)
		goto out;
	radix_tree_preload_end();

	if (sgp != SGP_READ && !prealloc_page) {
841
		prealloc_page = shmem_alloc_page(gfp, info, index);
842 843 844 845 846 847
		if (prealloc_page) {
			SetPageSwapBacked(prealloc_page);
			if (mem_cgroup_cache_charge(prealloc_page,
					current->mm, GFP_KERNEL)) {
				page_cache_release(prealloc_page);
				prealloc_page = NULL;
848 849
			}
		}
Hugh Dickins's avatar
Hugh Dickins committed
850
	}
Linus Torvalds's avatar
Linus Torvalds committed
851 852 853

	spin_lock(&info->lock);
	shmem_recalc_inode(inode);
854
	swap = shmem_get_swap(info, index);
Linus Torvalds's avatar
Linus Torvalds committed
855 856
	if (swap.val) {
		/* Look it up and read it in.. */
857 858
		page = lookup_swap_cache(swap);
		if (!page) {
859
			spin_unlock(&info->lock);
Linus Torvalds's avatar
Linus Torvalds committed
860
			/* here we actually do the io */
861 862
			if (fault_type)
				*fault_type |= VM_FAULT_MAJOR;
863
			page = shmem_swapin(swap, gfp, info, index);
864
			if (!page) {
865
				swp_entry_t nswap = shmem_get_swap(info, index);
866 867
				if (nswap.val == swap.val) {
					error = -ENOMEM;
868
					goto out;
869
				}
Linus Torvalds's avatar
Linus Torvalds committed
870 871
				goto repeat;
			}
872 873
			wait_on_page_locked(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
874 875 876 877
			goto repeat;
		}

		/* We have to do this with page locked to prevent races */
878
		if (!trylock_page(page)) {
Linus Torvalds's avatar
Linus Torvalds committed
879
			spin_unlock(&info->lock);
880 881
			wait_on_page_locked(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
882 883
			goto repeat;
		}
884
		if (PageWriteback(page)) {
Linus Torvalds's avatar
Linus Torvalds committed
885
			spin_unlock(&info->lock);
886 887 888
			wait_on_page_writeback(page);
			unlock_page(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
889 890
			goto repeat;
		}
891
		if (!PageUptodate(page)) {
Linus Torvalds's avatar
Linus Torvalds committed
892
			spin_unlock(&info->lock);
893 894
			unlock_page(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
895
			error = -EIO;
896
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
897 898
		}

899
		error = add_to_page_cache_locked(page, mapping,
900
						 index, GFP_NOWAIT);
901
		if (error) {
Linus Torvalds's avatar
Linus Torvalds committed
902
			spin_unlock(&info->lock);
903
			if (error == -ENOMEM) {
904 905 906 907 908
				/*
				 * reclaim from proper memory cgroup and
				 * call memcg's OOM if needed.
				 */
				error = mem_cgroup_shmem_charge_fallback(
909
						page, current->mm, gfp);
910
				if (error) {
911 912 913
					unlock_page(page);
					page_cache_release(page);
					goto out;
914
				}
915
			}
916 917
			unlock_page(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
918 919
			goto repeat;
		}
920 921

		delete_from_swap_cache(page);
922
		shmem_put_swap(info, index, (swp_entry_t){0});
923
		info->swapped--;
924 925 926 927 928
		spin_unlock(&info->lock);
		set_page_dirty(page);
		swap_free(swap);

	} else if (sgp == SGP_READ) {
929
		page = find_get_page(mapping, index);
930
		if (page && !trylock_page(page)) {
Linus Torvalds's avatar
Linus Torvalds committed
931
			spin_unlock(&info->lock);
932 933
			wait_on_page_locked(page);
			page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
934 935 936
			goto repeat;
		}
		spin_unlock(&info->lock);
Hugh Dickins's avatar
Hugh Dickins committed
937 938

	} else if (prealloc_page) {
Linus Torvalds's avatar
Linus Torvalds committed
939
		sbinfo = SHMEM_SB(inode->i_sb);
940
		if (sbinfo->max_blocks) {
941 942
			if (percpu_counter_compare(&sbinfo->used_blocks,
						sbinfo->max_blocks) >= 0 ||
943 944
			    shmem_acct_block(info->flags))
				goto nospace;
945
			percpu_counter_inc(&sbinfo->used_blocks);
Linus Torvalds's avatar
Linus Torvalds committed
946
			inode->i_blocks += BLOCKS_PER_PAGE;
947 948
		} else if (shmem_acct_block(info->flags))
			goto nospace;
Linus Torvalds's avatar
Linus Torvalds committed
949

950 951
		page = prealloc_page;
		prealloc_page = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
952

953
		swap = shmem_get_swap(info, index);
954
		if (swap.val)
955 956
			mem_cgroup_uncharge_cache_page(page);
		else
957
			error = add_to_page_cache_lru(page, mapping,
958
						index, GFP_NOWAIT);
959 960 961 962
		/*
		 * At add_to_page_cache_lru() failure,
		 * uncharge will be done automatically.
		 */
963
		if (swap.val || error) {
964 965 966 967 968
			shmem_unacct_blocks(info->flags, 1);
			shmem_free_blocks(inode, 1);
			spin_unlock(&info->lock);
			page_cache_release(page);
			goto repeat;
Linus Torvalds's avatar
Linus Torvalds committed
969 970 971 972
		}

		info->alloced++;
		spin_unlock(&info->lock);
973 974 975
		clear_highpage(page);
		flush_dcache_page(page);
		SetPageUptodate(page);
976
		if (sgp == SGP_DIRTY)
977 978
			set_page_dirty(page);

Hugh Dickins's avatar
Hugh Dickins committed
979 980 981 982
	} else {
		spin_unlock(&info->lock);
		error = -ENOMEM;
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
983 984
	}
done:
985
	*pagep = page;
986
	error = 0;
Hugh Dickins's avatar
Hugh Dickins committed
987 988 989 990 991 992
out:
	if (prealloc_page) {
		mem_cgroup_uncharge_cache_page(prealloc_page);
		page_cache_release(prealloc_page);
	}
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
993

994 995 996 997 998
nospace:
	/*
	 * Perhaps the page was brought in from swap between find_lock_page
	 * and taking info->lock?  We allow for that at add_to_page_cache_lru,
	 * but must also avoid reporting a spurious ENOSPC while working on a
Hugh Dickins's avatar
Hugh Dickins committed
999
	 * full tmpfs.
1000
	 */
1001
	page = find_get_page(mapping, index);
1002
	spin_unlock(&info->lock);
1003 1004 1005
	if (page) {
		page_cache_release(page);
		goto repeat;
1006
	}
1007
	error = -ENOSPC;
Hugh Dickins's avatar
Hugh Dickins committed
1008
	goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1009 1010
}

Nick Piggin's avatar
Nick Piggin committed
1011
static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds's avatar
Linus Torvalds committed
1012
{
1013
	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
1014
	int error;
1015
	int ret = VM_FAULT_LOCKED;
Linus Torvalds's avatar
Linus Torvalds committed
1016

Nick Piggin's avatar
Nick Piggin committed
1017 1018
	if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
		return VM_FAULT_SIGBUS;
1019

1020
	error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
Nick Piggin's avatar
Nick Piggin committed
1021 1022
	if (error)
		return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
1023

1024 1025 1026 1027
	if (ret & VM_FAULT_MAJOR) {
		count_vm_event(PGMAJFAULT);
		mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
	}
1028
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1029 1030 1031
}

#ifdef CONFIG_NUMA
1032
static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds's avatar
Linus Torvalds committed
1033
{
1034 1035
	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
	return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037
}

1038 1039
static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
					  unsigned long addr)
Linus Torvalds's avatar
Linus Torvalds committed
1040
{
1041 1042
	struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
	pgoff_t index;
Linus Torvalds's avatar
Linus Torvalds committed
1043

1044 1045
	index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
	return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds's avatar
Linus Torvalds committed
1046 1047 1048 1049 1050
}
#endif

int shmem_lock(struct file *file, int lock, struct user_struct *user)
{
1051
	struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
1052 1053 1054 1055 1056 1057 1058 1059
	struct shmem_inode_info *info = SHMEM_I(inode);
	int retval = -ENOMEM;

	spin_lock(&info->lock);
	if (lock && !(info->flags & VM_LOCKED)) {
		if (!user_shm_lock(inode->i_size, user))
			goto out_nomem;
		info->flags |= VM_LOCKED;
1060
		mapping_set_unevictable(file->f_mapping);
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062 1063 1064
	}
	if (!lock && (info->flags & VM_LOCKED) && user) {
		user_shm_unlock(inode->i_size, user);
		info->flags &= ~VM_LOCKED;
1065 1066
		mapping_clear_unevictable(file->f_mapping);
		scan_mapping_unevictable_pages(file->f_mapping);
Linus Torvalds's avatar
Linus Torvalds committed
1067 1068
	}
	retval = 0;
1069

Linus Torvalds's avatar
Linus Torvalds committed
1070 1071 1072 1073 1074
out_nomem:
	spin_unlock(&info->lock);
	return retval;
}

1075
static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
1076 1077 1078
{
	file_accessed(file);
	vma->vm_ops = &shmem_vm_ops;
Nick Piggin's avatar
Nick Piggin committed
1079
	vma->vm_flags |= VM_CAN_NONLINEAR;
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081 1082
	return 0;
}

1083 1084
static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
				     int mode, dev_t dev, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
1085 1086 1087 1088 1089
{
	struct inode *inode;
	struct shmem_inode_info *info;
	struct shmem_sb_info *sbinfo = SHMEM_SB(sb);

1090 1091
	if (shmem_reserve_inode(sb))
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1092 1093 1094

	inode = new_inode(sb);
	if (inode) {
1095
		inode->i_ino = get_next_ino();
1096
		inode_init_owner(inode, dir, mode);
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098 1099
		inode->i_blocks = 0;
		inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1100
		inode->i_generation = get_seconds();
Linus Torvalds's avatar
Linus Torvalds committed
1101 1102 1103
		info = SHMEM_I(inode);
		memset(info, 0, (char *)inode - (char *)info);
		spin_lock_init(&info->lock);
1104
		info->flags = flags & VM_NORESERVE;
Linus Torvalds's avatar
Linus Torvalds committed
1105
		INIT_LIST_HEAD(&info->swaplist);
1106
		INIT_LIST_HEAD(&info->xattr_list);
1107
		cache_no_acl(inode);
Linus Torvalds's avatar
Linus Torvalds committed
1108 1109 1110

		switch (mode & S_IFMT) {
		default:
1111
			inode->i_op = &shmem_special_inode_operations;
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114
			init_special_inode(inode, mode, dev);
			break;
		case S_IFREG:
1115
			inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds's avatar
Linus Torvalds committed
1116 1117
			inode->i_op = &shmem_inode_operations;
			inode->i_fop = &shmem_file_operations;
1118 1119
			mpol_shared_policy_init(&info->policy,
						 shmem_get_sbmpol(sbinfo));
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121
			break;
		case S_IFDIR:
1122
			inc_nlink(inode);
Linus Torvalds's avatar
Linus Torvalds committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
			/* Some things misbehave if size == 0 on a directory */
			inode->i_size = 2 * BOGO_DIRENT_SIZE;
			inode->i_op = &shmem_dir_inode_operations;
			inode->i_fop = &simple_dir_operations;
			break;
		case S_IFLNK:
			/*
			 * Must not load anything in the rbtree,
			 * mpol_free_shared_policy will not be called.
			 */
1133
			mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1134 1135
			break;
		}
1136 1137
	} else
		shmem_free_inode(sb);
Linus Torvalds's avatar
Linus Torvalds committed
1138 1139 1140 1141
	return inode;
}

#ifdef CONFIG_TMPFS
1142 1143
static const struct inode_operations shmem_symlink_inode_operations;
static const struct inode_operations shmem_symlink_inline_operations;
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145

static int
Nick Piggin's avatar
Nick Piggin committed
1146 1147 1148
shmem_write_begin(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned flags,
			struct page **pagep, void **fsdata)
Linus Torvalds's avatar
Linus Torvalds committed
1149
{
Nick Piggin's avatar
Nick Piggin committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	struct inode *inode = mapping->host;
	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
	return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
}

static int
shmem_write_end(struct file *file, struct address_space *mapping,
			loff_t pos, unsigned len, unsigned copied,
			struct page *page, void *fsdata)
{
	struct inode *inode = mapping->host;

1162 1163 1164
	if (pos + copied > inode->i_size)
		i_size_write(inode, pos + copied);

Nick Piggin's avatar
Nick Piggin committed
1165
	set_page_dirty(page);
1166
	unlock_page(page);
Nick Piggin's avatar
Nick Piggin committed
1167 1168 1169
	page_cache_release(page);

	return copied;
Linus Torvalds's avatar
Linus Torvalds committed
1170 1171 1172 1173
}

static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
{
1174
	struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds's avatar
Linus Torvalds committed
1175
	struct address_space *mapping = inode->i_mapping;
1176 1177
	pgoff_t index;
	unsigned long offset;
1178 1179 1180 1181 1182 1183 1184 1185 1186
	enum sgp_type sgp = SGP_READ;

	/*
	 * Might this read be for a stacking filesystem?  Then when reading
	 * holes of a sparse file, we actually need to allocate those pages,
	 * and even mark them dirty, so it cannot exceed the max_blocks limit.
	 */
	if (segment_eq(get_fs(), KERNEL_DS))
		sgp = SGP_DIRTY;
Linus Torvalds's avatar
Linus Torvalds committed