ia64/linux-2.6.18-xen.hg

view mm/shmem.c @ 749:2892ca2b9c17

linux/x86: cleanup IO-APIC code

- get 32-bit code in sync with 64-bit wrt ExtINT pin detection being
unnecessary
- eliminate build warnings resulting from c/s 725

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 28 13:31:21 2008 +0000 (2008-11-28)
parents 3e8752eb6d9c
children
line source
1 /*
2 * Resizable virtual memory filesystem for Linux.
3 *
4 * Copyright (C) 2000 Linus Torvalds.
5 * 2000 Transmeta Corp.
6 * 2000-2001 Christoph Rohland
7 * 2000-2001 SAP AG
8 * 2002 Red Hat Inc.
9 * Copyright (C) 2002-2005 Hugh Dickins.
10 * Copyright (C) 2002-2005 VERITAS Software Corporation.
11 * Copyright (C) 2004 Andi Kleen, SuSE Labs
12 *
13 * Extended attribute support for tmpfs:
14 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
15 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
16 *
17 * This file is released under the GPL.
18 */
20 /*
21 * This virtual memory filesystem is heavily based on the ramfs. It
22 * extends ramfs by the ability to use swap and honor resource limits
23 * which makes it a completely usable filesystem.
24 */
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/mm.h>
30 #include <linux/mman.h>
31 #include <linux/file.h>
32 #include <linux/swap.h>
33 #include <linux/pagemap.h>
34 #include <linux/string.h>
35 #include <linux/slab.h>
36 #include <linux/backing-dev.h>
37 #include <linux/shmem_fs.h>
38 #include <linux/mount.h>
39 #include <linux/writeback.h>
40 #include <linux/vfs.h>
41 #include <linux/blkdev.h>
42 #include <linux/security.h>
43 #include <linux/swapops.h>
44 #include <linux/mempolicy.h>
45 #include <linux/namei.h>
46 #include <linux/ctype.h>
47 #include <linux/migrate.h>
49 #include <asm/uaccess.h>
50 #include <asm/div64.h>
51 #include <asm/pgtable.h>
53 /* This magic number is used in glibc for posix shared memory */
54 #define TMPFS_MAGIC 0x01021994
56 #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
57 #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
58 #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
60 #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
61 #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
63 #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
65 /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
66 #define SHMEM_PAGEIN VM_READ
67 #define SHMEM_TRUNCATE VM_WRITE
69 /* Definition to limit shmem_truncate's steps between cond_rescheds */
70 #define LATENCY_LIMIT 64
72 /* Pretend that each entry is of this size in directory's i_size */
73 #define BOGO_DIRENT_SIZE 20
75 /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
76 enum sgp_type {
77 SGP_QUICK, /* don't try more than file page cache lookup */
78 SGP_READ, /* don't exceed i_size, don't allocate page */
79 SGP_CACHE, /* don't exceed i_size, may allocate page */
80 SGP_WRITE, /* may exceed i_size, may allocate page */
81 };
83 static int shmem_getpage(struct inode *inode, unsigned long idx,
84 struct page **pagep, enum sgp_type sgp, int *type);
86 static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
87 {
88 /*
89 * The above definition of ENTRIES_PER_PAGE, and the use of
90 * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
91 * might be reconsidered if it ever diverges from PAGE_SIZE.
92 */
93 return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
94 }
96 static inline void shmem_dir_free(struct page *page)
97 {
98 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
99 }
101 static struct page **shmem_dir_map(struct page *page)
102 {
103 return (struct page **)kmap_atomic(page, KM_USER0);
104 }
106 static inline void shmem_dir_unmap(struct page **dir)
107 {
108 kunmap_atomic(dir, KM_USER0);
109 }
111 static swp_entry_t *shmem_swp_map(struct page *page)
112 {
113 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
114 }
116 static inline void shmem_swp_balance_unmap(void)
117 {
118 /*
119 * When passing a pointer to an i_direct entry, to code which
120 * also handles indirect entries and so will shmem_swp_unmap,
121 * we must arrange for the preempt count to remain in balance.
122 * What kmap_atomic of a lowmem page does depends on config
123 * and architecture, so pretend to kmap_atomic some lowmem page.
124 */
125 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
126 }
128 static inline void shmem_swp_unmap(swp_entry_t *entry)
129 {
130 kunmap_atomic(entry, KM_USER1);
131 }
133 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
134 {
135 return sb->s_fs_info;
136 }
138 /*
139 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
140 * for shared memory and for shared anonymous (/dev/zero) mappings
141 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
142 * consistent with the pre-accounting of private mappings ...
143 */
144 static inline int shmem_acct_size(unsigned long flags, loff_t size)
145 {
146 return (flags & VM_ACCOUNT)?
147 security_vm_enough_memory(VM_ACCT(size)): 0;
148 }
150 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
151 {
152 if (flags & VM_ACCOUNT)
153 vm_unacct_memory(VM_ACCT(size));
154 }
156 /*
157 * ... whereas tmpfs objects are accounted incrementally as
158 * pages are allocated, in order to allow huge sparse files.
159 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
160 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
161 */
162 static inline int shmem_acct_block(unsigned long flags)
163 {
164 return (flags & VM_ACCOUNT)?
165 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
166 }
168 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
169 {
170 if (!(flags & VM_ACCOUNT))
171 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
172 }
174 static struct super_operations shmem_ops;
175 static const struct address_space_operations shmem_aops;
176 static struct file_operations shmem_file_operations;
177 static struct inode_operations shmem_inode_operations;
178 static struct inode_operations shmem_dir_inode_operations;
179 static struct vm_operations_struct shmem_vm_ops;
181 static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
182 .ra_pages = 0, /* No readahead */
183 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
184 .unplug_io_fn = default_unplug_io_fn,
185 };
187 static LIST_HEAD(shmem_swaplist);
188 static DEFINE_SPINLOCK(shmem_swaplist_lock);
190 static void shmem_free_blocks(struct inode *inode, long pages)
191 {
192 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
193 if (sbinfo->max_blocks) {
194 spin_lock(&sbinfo->stat_lock);
195 sbinfo->free_blocks += pages;
196 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
197 spin_unlock(&sbinfo->stat_lock);
198 }
199 }
201 /*
202 * shmem_recalc_inode - recalculate the size of an inode
203 *
204 * @inode: inode to recalc
205 *
206 * We have to calculate the free blocks since the mm can drop
207 * undirtied hole pages behind our back.
208 *
209 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
210 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
211 *
212 * It has to be called with the spinlock held.
213 */
214 static void shmem_recalc_inode(struct inode *inode)
215 {
216 struct shmem_inode_info *info = SHMEM_I(inode);
217 long freed;
219 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
220 if (freed > 0) {
221 info->alloced -= freed;
222 shmem_unacct_blocks(info->flags, freed);
223 shmem_free_blocks(inode, freed);
224 }
225 }
227 /*
228 * shmem_swp_entry - find the swap vector position in the info structure
229 *
230 * @info: info structure for the inode
231 * @index: index of the page to find
232 * @page: optional page to add to the structure. Has to be preset to
233 * all zeros
234 *
235 * If there is no space allocated yet it will return NULL when
236 * page is NULL, else it will use the page for the needed block,
237 * setting it to NULL on return to indicate that it has been used.
238 *
239 * The swap vector is organized the following way:
240 *
241 * There are SHMEM_NR_DIRECT entries directly stored in the
242 * shmem_inode_info structure. So small files do not need an addional
243 * allocation.
244 *
245 * For pages with index > SHMEM_NR_DIRECT there is the pointer
246 * i_indirect which points to a page which holds in the first half
247 * doubly indirect blocks, in the second half triple indirect blocks:
248 *
249 * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
250 * following layout (for SHMEM_NR_DIRECT == 16):
251 *
252 * i_indirect -> dir --> 16-19
253 * | +-> 20-23
254 * |
255 * +-->dir2 --> 24-27
256 * | +-> 28-31
257 * | +-> 32-35
258 * | +-> 36-39
259 * |
260 * +-->dir3 --> 40-43
261 * +-> 44-47
262 * +-> 48-51
263 * +-> 52-55
264 */
265 static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
266 {
267 unsigned long offset;
268 struct page **dir;
269 struct page *subdir;
271 if (index < SHMEM_NR_DIRECT) {
272 shmem_swp_balance_unmap();
273 return info->i_direct+index;
274 }
275 if (!info->i_indirect) {
276 if (page) {
277 info->i_indirect = *page;
278 *page = NULL;
279 }
280 return NULL; /* need another page */
281 }
283 index -= SHMEM_NR_DIRECT;
284 offset = index % ENTRIES_PER_PAGE;
285 index /= ENTRIES_PER_PAGE;
286 dir = shmem_dir_map(info->i_indirect);
288 if (index >= ENTRIES_PER_PAGE/2) {
289 index -= ENTRIES_PER_PAGE/2;
290 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
291 index %= ENTRIES_PER_PAGE;
292 subdir = *dir;
293 if (!subdir) {
294 if (page) {
295 *dir = *page;
296 *page = NULL;
297 }
298 shmem_dir_unmap(dir);
299 return NULL; /* need another page */
300 }
301 shmem_dir_unmap(dir);
302 dir = shmem_dir_map(subdir);
303 }
305 dir += index;
306 subdir = *dir;
307 if (!subdir) {
308 if (!page || !(subdir = *page)) {
309 shmem_dir_unmap(dir);
310 return NULL; /* need a page */
311 }
312 *dir = subdir;
313 *page = NULL;
314 }
315 shmem_dir_unmap(dir);
316 return shmem_swp_map(subdir) + offset;
317 }
319 static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
320 {
321 long incdec = value? 1: -1;
323 entry->val = value;
324 info->swapped += incdec;
325 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
326 struct page *page = kmap_atomic_to_page(entry);
327 set_page_private(page, page_private(page) + incdec);
328 }
329 }
331 /*
332 * shmem_swp_alloc - get the position of the swap entry for the page.
333 * If it does not exist allocate the entry.
334 *
335 * @info: info structure for the inode
336 * @index: index of the page to find
337 * @sgp: check and recheck i_size? skip allocation?
338 */
339 static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
340 {
341 struct inode *inode = &info->vfs_inode;
342 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
343 struct page *page = NULL;
344 swp_entry_t *entry;
346 if (sgp != SGP_WRITE &&
347 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
348 return ERR_PTR(-EINVAL);
350 while (!(entry = shmem_swp_entry(info, index, &page))) {
351 if (sgp == SGP_READ)
352 return shmem_swp_map(ZERO_PAGE(0));
353 /*
354 * Test free_blocks against 1 not 0, since we have 1 data
355 * page (and perhaps indirect index pages) yet to allocate:
356 * a waste to allocate index if we cannot allocate data.
357 */
358 if (sbinfo->max_blocks) {
359 spin_lock(&sbinfo->stat_lock);
360 if (sbinfo->free_blocks <= 1) {
361 spin_unlock(&sbinfo->stat_lock);
362 return ERR_PTR(-ENOSPC);
363 }
364 sbinfo->free_blocks--;
365 inode->i_blocks += BLOCKS_PER_PAGE;
366 spin_unlock(&sbinfo->stat_lock);
367 }
369 spin_unlock(&info->lock);
370 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO);
371 if (page)
372 set_page_private(page, 0);
373 spin_lock(&info->lock);
375 if (!page) {
376 shmem_free_blocks(inode, 1);
377 return ERR_PTR(-ENOMEM);
378 }
379 if (sgp != SGP_WRITE &&
380 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
381 entry = ERR_PTR(-EINVAL);
382 break;
383 }
384 if (info->next_index <= index)
385 info->next_index = index + 1;
386 }
387 if (page) {
388 /* another task gave its page, or truncated the file */
389 shmem_free_blocks(inode, 1);
390 shmem_dir_free(page);
391 }
392 if (info->next_index <= index && !IS_ERR(entry))
393 info->next_index = index + 1;
394 return entry;
395 }
397 /*
398 * shmem_free_swp - free some swap entries in a directory
399 *
400 * @dir: pointer to the directory
401 * @edir: pointer after last entry of the directory
402 */
403 static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
404 {
405 swp_entry_t *ptr;
406 int freed = 0;
408 for (ptr = dir; ptr < edir; ptr++) {
409 if (ptr->val) {
410 free_swap_and_cache(*ptr);
411 *ptr = (swp_entry_t){0};
412 freed++;
413 }
414 }
415 return freed;
416 }
418 static int shmem_map_and_free_swp(struct page *subdir,
419 int offset, int limit, struct page ***dir)
420 {
421 swp_entry_t *ptr;
422 int freed = 0;
424 ptr = shmem_swp_map(subdir);
425 for (; offset < limit; offset += LATENCY_LIMIT) {
426 int size = limit - offset;
427 if (size > LATENCY_LIMIT)
428 size = LATENCY_LIMIT;
429 freed += shmem_free_swp(ptr+offset, ptr+offset+size);
430 if (need_resched()) {
431 shmem_swp_unmap(ptr);
432 if (*dir) {
433 shmem_dir_unmap(*dir);
434 *dir = NULL;
435 }
436 cond_resched();
437 ptr = shmem_swp_map(subdir);
438 }
439 }
440 shmem_swp_unmap(ptr);
441 return freed;
442 }
444 static void shmem_free_pages(struct list_head *next)
445 {
446 struct page *page;
447 int freed = 0;
449 do {
450 page = container_of(next, struct page, lru);
451 next = next->next;
452 shmem_dir_free(page);
453 freed++;
454 if (freed >= LATENCY_LIMIT) {
455 cond_resched();
456 freed = 0;
457 }
458 } while (next);
459 }
461 static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
462 {
463 struct shmem_inode_info *info = SHMEM_I(inode);
464 unsigned long idx;
465 unsigned long size;
466 unsigned long limit;
467 unsigned long stage;
468 unsigned long diroff;
469 struct page **dir;
470 struct page *topdir;
471 struct page *middir;
472 struct page *subdir;
473 swp_entry_t *ptr;
474 LIST_HEAD(pages_to_free);
475 long nr_pages_to_free = 0;
476 long nr_swaps_freed = 0;
477 int offset;
478 int freed;
479 int punch_hole = 0;
481 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
482 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
483 if (idx >= info->next_index)
484 return;
486 spin_lock(&info->lock);
487 info->flags |= SHMEM_TRUNCATE;
488 if (likely(end == (loff_t) -1)) {
489 limit = info->next_index;
490 info->next_index = idx;
491 } else {
492 limit = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
493 if (limit > info->next_index)
494 limit = info->next_index;
495 punch_hole = 1;
496 }
498 topdir = info->i_indirect;
499 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
500 info->i_indirect = NULL;
501 nr_pages_to_free++;
502 list_add(&topdir->lru, &pages_to_free);
503 }
504 spin_unlock(&info->lock);
506 if (info->swapped && idx < SHMEM_NR_DIRECT) {
507 ptr = info->i_direct;
508 size = limit;
509 if (size > SHMEM_NR_DIRECT)
510 size = SHMEM_NR_DIRECT;
511 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size);
512 }
514 /*
515 * If there are no indirect blocks or we are punching a hole
516 * below indirect blocks, nothing to be done.
517 */
518 if (!topdir || (punch_hole && (limit <= SHMEM_NR_DIRECT)))
519 goto done2;
521 BUG_ON(limit <= SHMEM_NR_DIRECT);
522 limit -= SHMEM_NR_DIRECT;
523 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
524 offset = idx % ENTRIES_PER_PAGE;
525 idx -= offset;
527 dir = shmem_dir_map(topdir);
528 stage = ENTRIES_PER_PAGEPAGE/2;
529 if (idx < ENTRIES_PER_PAGEPAGE/2) {
530 middir = topdir;
531 diroff = idx/ENTRIES_PER_PAGE;
532 } else {
533 dir += ENTRIES_PER_PAGE/2;
534 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
535 while (stage <= idx)
536 stage += ENTRIES_PER_PAGEPAGE;
537 middir = *dir;
538 if (*dir) {
539 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
540 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
541 if (!diroff && !offset) {
542 *dir = NULL;
543 nr_pages_to_free++;
544 list_add(&middir->lru, &pages_to_free);
545 }
546 shmem_dir_unmap(dir);
547 dir = shmem_dir_map(middir);
548 } else {
549 diroff = 0;
550 offset = 0;
551 idx = stage;
552 }
553 }
555 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
556 if (unlikely(idx == stage)) {
557 shmem_dir_unmap(dir);
558 dir = shmem_dir_map(topdir) +
559 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
560 while (!*dir) {
561 dir++;
562 idx += ENTRIES_PER_PAGEPAGE;
563 if (idx >= limit)
564 goto done1;
565 }
566 stage = idx + ENTRIES_PER_PAGEPAGE;
567 middir = *dir;
568 *dir = NULL;
569 nr_pages_to_free++;
570 list_add(&middir->lru, &pages_to_free);
571 shmem_dir_unmap(dir);
572 cond_resched();
573 dir = shmem_dir_map(middir);
574 diroff = 0;
575 }
576 subdir = dir[diroff];
577 if (subdir && page_private(subdir)) {
578 size = limit - idx;
579 if (size > ENTRIES_PER_PAGE)
580 size = ENTRIES_PER_PAGE;
581 freed = shmem_map_and_free_swp(subdir,
582 offset, size, &dir);
583 if (!dir)
584 dir = shmem_dir_map(middir);
585 nr_swaps_freed += freed;
586 if (offset)
587 spin_lock(&info->lock);
588 set_page_private(subdir, page_private(subdir) - freed);
589 if (offset)
590 spin_unlock(&info->lock);
591 if (!punch_hole)
592 BUG_ON(page_private(subdir) > offset);
593 }
594 if (offset)
595 offset = 0;
596 else if (subdir && !page_private(subdir)) {
597 dir[diroff] = NULL;
598 nr_pages_to_free++;
599 list_add(&subdir->lru, &pages_to_free);
600 }
601 }
602 done1:
603 shmem_dir_unmap(dir);
604 done2:
605 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
606 /*
607 * Call truncate_inode_pages again: racing shmem_unuse_inode
608 * may have swizzled a page in from swap since vmtruncate or
609 * generic_delete_inode did it, before we lowered next_index.
610 * Also, though shmem_getpage checks i_size before adding to
611 * cache, no recheck after: so fix the narrow window there too.
612 */
613 truncate_inode_pages_range(inode->i_mapping, start, end);
614 }
616 spin_lock(&info->lock);
617 info->flags &= ~SHMEM_TRUNCATE;
618 info->swapped -= nr_swaps_freed;
619 if (nr_pages_to_free)
620 shmem_free_blocks(inode, nr_pages_to_free);
621 shmem_recalc_inode(inode);
622 spin_unlock(&info->lock);
624 /*
625 * Empty swap vector directory pages to be freed?
626 */
627 if (!list_empty(&pages_to_free)) {
628 pages_to_free.prev->next = NULL;
629 shmem_free_pages(pages_to_free.next);
630 }
631 }
633 static void shmem_truncate(struct inode *inode)
634 {
635 shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
636 }
638 static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
639 {
640 struct inode *inode = dentry->d_inode;
641 struct page *page = NULL;
642 int error;
644 if (attr->ia_valid & ATTR_SIZE) {
645 if (attr->ia_size < inode->i_size) {
646 /*
647 * If truncating down to a partial page, then
648 * if that page is already allocated, hold it
649 * in memory until the truncation is over, so
650 * truncate_partial_page cannnot miss it were
651 * it assigned to swap.
652 */
653 if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
654 (void) shmem_getpage(inode,
655 attr->ia_size>>PAGE_CACHE_SHIFT,
656 &page, SGP_READ, NULL);
657 }
658 /*
659 * Reset SHMEM_PAGEIN flag so that shmem_truncate can
660 * detect if any pages might have been added to cache
661 * after truncate_inode_pages. But we needn't bother
662 * if it's being fully truncated to zero-length: the
663 * nrpages check is efficient enough in that case.
664 */
665 if (attr->ia_size) {
666 struct shmem_inode_info *info = SHMEM_I(inode);
667 spin_lock(&info->lock);
668 info->flags &= ~SHMEM_PAGEIN;
669 spin_unlock(&info->lock);
670 }
671 }
672 }
674 error = inode_change_ok(inode, attr);
675 if (!error)
676 error = inode_setattr(inode, attr);
677 if (page)
678 page_cache_release(page);
679 return error;
680 }
682 static void shmem_delete_inode(struct inode *inode)
683 {
684 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
685 struct shmem_inode_info *info = SHMEM_I(inode);
687 if (inode->i_op->truncate == shmem_truncate) {
688 truncate_inode_pages(inode->i_mapping, 0);
689 shmem_unacct_size(info->flags, inode->i_size);
690 inode->i_size = 0;
691 shmem_truncate(inode);
692 if (!list_empty(&info->swaplist)) {
693 spin_lock(&shmem_swaplist_lock);
694 list_del_init(&info->swaplist);
695 spin_unlock(&shmem_swaplist_lock);
696 }
697 }
698 BUG_ON(inode->i_blocks);
699 if (sbinfo->max_inodes) {
700 spin_lock(&sbinfo->stat_lock);
701 sbinfo->free_inodes++;
702 spin_unlock(&sbinfo->stat_lock);
703 }
704 clear_inode(inode);
705 }
707 static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
708 {
709 swp_entry_t *ptr;
711 for (ptr = dir; ptr < edir; ptr++) {
712 if (ptr->val == entry.val)
713 return ptr - dir;
714 }
715 return -1;
716 }
718 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
719 {
720 struct inode *inode;
721 unsigned long idx;
722 unsigned long size;
723 unsigned long limit;
724 unsigned long stage;
725 struct page **dir;
726 struct page *subdir;
727 swp_entry_t *ptr;
728 int offset;
730 idx = 0;
731 ptr = info->i_direct;
732 spin_lock(&info->lock);
733 limit = info->next_index;
734 size = limit;
735 if (size > SHMEM_NR_DIRECT)
736 size = SHMEM_NR_DIRECT;
737 offset = shmem_find_swp(entry, ptr, ptr+size);
738 if (offset >= 0) {
739 shmem_swp_balance_unmap();
740 goto found;
741 }
742 if (!info->i_indirect)
743 goto lost2;
745 dir = shmem_dir_map(info->i_indirect);
746 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
748 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
749 if (unlikely(idx == stage)) {
750 shmem_dir_unmap(dir-1);
751 dir = shmem_dir_map(info->i_indirect) +
752 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
753 while (!*dir) {
754 dir++;
755 idx += ENTRIES_PER_PAGEPAGE;
756 if (idx >= limit)
757 goto lost1;
758 }
759 stage = idx + ENTRIES_PER_PAGEPAGE;
760 subdir = *dir;
761 shmem_dir_unmap(dir);
762 dir = shmem_dir_map(subdir);
763 }
764 subdir = *dir;
765 if (subdir && page_private(subdir)) {
766 ptr = shmem_swp_map(subdir);
767 size = limit - idx;
768 if (size > ENTRIES_PER_PAGE)
769 size = ENTRIES_PER_PAGE;
770 offset = shmem_find_swp(entry, ptr, ptr+size);
771 if (offset >= 0) {
772 shmem_dir_unmap(dir);
773 goto found;
774 }
775 shmem_swp_unmap(ptr);
776 }
777 }
778 lost1:
779 shmem_dir_unmap(dir-1);
780 lost2:
781 spin_unlock(&info->lock);
782 return 0;
783 found:
784 idx += offset;
785 inode = &info->vfs_inode;
786 if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
787 info->flags |= SHMEM_PAGEIN;
788 shmem_swp_set(info, ptr + offset, 0);
789 }
790 shmem_swp_unmap(ptr);
791 spin_unlock(&info->lock);
792 /*
793 * Decrement swap count even when the entry is left behind:
794 * try_to_unuse will skip over mms, then reincrement count.
795 */
796 swap_free(entry);
797 return 1;
798 }
800 /*
801 * shmem_unuse() search for an eventually swapped out shmem page.
802 */
803 int shmem_unuse(swp_entry_t entry, struct page *page)
804 {
805 struct list_head *p, *next;
806 struct shmem_inode_info *info;
807 int found = 0;
809 spin_lock(&shmem_swaplist_lock);
810 list_for_each_safe(p, next, &shmem_swaplist) {
811 info = list_entry(p, struct shmem_inode_info, swaplist);
812 if (!info->swapped)
813 list_del_init(&info->swaplist);
814 else if (shmem_unuse_inode(info, entry, page)) {
815 /* move head to start search for next from here */
816 list_move_tail(&shmem_swaplist, &info->swaplist);
817 found = 1;
818 break;
819 }
820 }
821 spin_unlock(&shmem_swaplist_lock);
822 return found;
823 }
825 /*
826 * Move the page from the page cache to the swap cache.
827 */
828 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
829 {
830 struct shmem_inode_info *info;
831 swp_entry_t *entry, swap;
832 struct address_space *mapping;
833 unsigned long index;
834 struct inode *inode;
836 BUG_ON(!PageLocked(page));
837 BUG_ON(page_mapped(page));
839 mapping = page->mapping;
840 index = page->index;
841 inode = mapping->host;
842 info = SHMEM_I(inode);
843 if (info->flags & VM_LOCKED)
844 goto redirty;
845 swap = get_swap_page();
846 if (!swap.val)
847 goto redirty;
849 spin_lock(&info->lock);
850 shmem_recalc_inode(inode);
851 if (index >= info->next_index) {
852 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
853 goto unlock;
854 }
855 entry = shmem_swp_entry(info, index, NULL);
856 BUG_ON(!entry);
857 BUG_ON(entry->val);
859 if (move_to_swap_cache(page, swap) == 0) {
860 shmem_swp_set(info, entry, swap.val);
861 shmem_swp_unmap(entry);
862 spin_unlock(&info->lock);
863 if (list_empty(&info->swaplist)) {
864 spin_lock(&shmem_swaplist_lock);
865 /* move instead of add in case we're racing */
866 list_move_tail(&info->swaplist, &shmem_swaplist);
867 spin_unlock(&shmem_swaplist_lock);
868 }
869 unlock_page(page);
870 return 0;
871 }
873 shmem_swp_unmap(entry);
874 unlock:
875 spin_unlock(&info->lock);
876 swap_free(swap);
877 redirty:
878 set_page_dirty(page);
879 return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */
880 }
882 #ifdef CONFIG_NUMA
883 static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
884 {
885 char *nodelist = strchr(value, ':');
886 int err = 1;
888 if (nodelist) {
889 /* NUL-terminate policy string */
890 *nodelist++ = '\0';
891 if (nodelist_parse(nodelist, *policy_nodes))
892 goto out;
893 }
894 if (!strcmp(value, "default")) {
895 *policy = MPOL_DEFAULT;
896 /* Don't allow a nodelist */
897 if (!nodelist)
898 err = 0;
899 } else if (!strcmp(value, "prefer")) {
900 *policy = MPOL_PREFERRED;
901 /* Insist on a nodelist of one node only */
902 if (nodelist) {
903 char *rest = nodelist;
904 while (isdigit(*rest))
905 rest++;
906 if (!*rest)
907 err = 0;
908 }
909 } else if (!strcmp(value, "bind")) {
910 *policy = MPOL_BIND;
911 /* Insist on a nodelist */
912 if (nodelist)
913 err = 0;
914 } else if (!strcmp(value, "interleave")) {
915 *policy = MPOL_INTERLEAVE;
916 /* Default to nodes online if no nodelist */
917 if (!nodelist)
918 *policy_nodes = node_online_map;
919 err = 0;
920 }
921 out:
922 /* Restore string for error message */
923 if (nodelist)
924 *--nodelist = ':';
925 return err;
926 }
928 static struct page *shmem_swapin_async(struct shared_policy *p,
929 swp_entry_t entry, unsigned long idx)
930 {
931 struct page *page;
932 struct vm_area_struct pvma;
934 /* Create a pseudo vma that just contains the policy */
935 memset(&pvma, 0, sizeof(struct vm_area_struct));
936 pvma.vm_end = PAGE_SIZE;
937 pvma.vm_pgoff = idx;
938 pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
939 page = read_swap_cache_async(entry, &pvma, 0);
940 mpol_free(pvma.vm_policy);
941 return page;
942 }
944 struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
945 unsigned long idx)
946 {
947 struct shared_policy *p = &info->policy;
948 int i, num;
949 struct page *page;
950 unsigned long offset;
952 num = valid_swaphandles(entry, &offset);
953 for (i = 0; i < num; offset++, i++) {
954 page = shmem_swapin_async(p,
955 swp_entry(swp_type(entry), offset), idx);
956 if (!page)
957 break;
958 page_cache_release(page);
959 }
960 lru_add_drain(); /* Push any new pages onto the LRU now */
961 return shmem_swapin_async(p, entry, idx);
962 }
964 static struct page *
965 shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
966 unsigned long idx)
967 {
968 struct vm_area_struct pvma;
969 struct page *page;
971 memset(&pvma, 0, sizeof(struct vm_area_struct));
972 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
973 pvma.vm_pgoff = idx;
974 pvma.vm_end = PAGE_SIZE;
975 page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
976 mpol_free(pvma.vm_policy);
977 return page;
978 }
979 #else
980 static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
981 {
982 return 1;
983 }
985 static inline struct page *
986 shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
987 {
988 swapin_readahead(entry, 0, NULL);
989 return read_swap_cache_async(entry, NULL, 0);
990 }
992 static inline struct page *
993 shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
994 {
995 return alloc_page(gfp | __GFP_ZERO);
996 }
997 #endif
999 /*
1000 * shmem_getpage - either get the page from swap or allocate a new one
1002 * If we allocate a new one we do not mark it dirty. That's up to the
1003 * vm. If we swap it in we mark it dirty since we also free the swap
1004 * entry since a page cannot live in both the swap and page cache
1005 */
1006 static int shmem_getpage(struct inode *inode, unsigned long idx,
1007 struct page **pagep, enum sgp_type sgp, int *type)
1009 struct address_space *mapping = inode->i_mapping;
1010 struct shmem_inode_info *info = SHMEM_I(inode);
1011 struct shmem_sb_info *sbinfo;
1012 struct page *filepage = *pagep;
1013 struct page *swappage;
1014 swp_entry_t *entry;
1015 swp_entry_t swap;
1016 int error;
1018 if (idx >= SHMEM_MAX_INDEX)
1019 return -EFBIG;
1020 /*
1021 * Normally, filepage is NULL on entry, and either found
1022 * uptodate immediately, or allocated and zeroed, or read
1023 * in under swappage, which is then assigned to filepage.
1024 * But shmem_prepare_write passes in a locked filepage,
1025 * which may be found not uptodate by other callers too,
1026 * and may need to be copied from the swappage read in.
1027 */
1028 repeat:
1029 if (!filepage)
1030 filepage = find_lock_page(mapping, idx);
1031 if (filepage && PageUptodate(filepage))
1032 goto done;
1033 error = 0;
1034 if (sgp == SGP_QUICK)
1035 goto failed;
1037 spin_lock(&info->lock);
1038 shmem_recalc_inode(inode);
1039 entry = shmem_swp_alloc(info, idx, sgp);
1040 if (IS_ERR(entry)) {
1041 spin_unlock(&info->lock);
1042 error = PTR_ERR(entry);
1043 goto failed;
1045 swap = *entry;
1047 if (swap.val) {
1048 /* Look it up and read it in.. */
1049 swappage = lookup_swap_cache(swap);
1050 if (!swappage) {
1051 shmem_swp_unmap(entry);
1052 /* here we actually do the io */
1053 if (type && *type == VM_FAULT_MINOR) {
1054 __count_vm_event(PGMAJFAULT);
1055 *type = VM_FAULT_MAJOR;
1057 spin_unlock(&info->lock);
1058 swappage = shmem_swapin(info, swap, idx);
1059 if (!swappage) {
1060 spin_lock(&info->lock);
1061 entry = shmem_swp_alloc(info, idx, sgp);
1062 if (IS_ERR(entry))
1063 error = PTR_ERR(entry);
1064 else {
1065 if (entry->val == swap.val)
1066 error = -ENOMEM;
1067 shmem_swp_unmap(entry);
1069 spin_unlock(&info->lock);
1070 if (error)
1071 goto failed;
1072 goto repeat;
1074 wait_on_page_locked(swappage);
1075 page_cache_release(swappage);
1076 goto repeat;
1079 /* We have to do this with page locked to prevent races */
1080 if (TestSetPageLocked(swappage)) {
1081 shmem_swp_unmap(entry);
1082 spin_unlock(&info->lock);
1083 wait_on_page_locked(swappage);
1084 page_cache_release(swappage);
1085 goto repeat;
1087 if (PageWriteback(swappage)) {
1088 shmem_swp_unmap(entry);
1089 spin_unlock(&info->lock);
1090 wait_on_page_writeback(swappage);
1091 unlock_page(swappage);
1092 page_cache_release(swappage);
1093 goto repeat;
1095 if (!PageUptodate(swappage)) {
1096 shmem_swp_unmap(entry);
1097 spin_unlock(&info->lock);
1098 unlock_page(swappage);
1099 page_cache_release(swappage);
1100 error = -EIO;
1101 goto failed;
1104 if (filepage) {
1105 shmem_swp_set(info, entry, 0);
1106 shmem_swp_unmap(entry);
1107 delete_from_swap_cache(swappage);
1108 spin_unlock(&info->lock);
1109 copy_highpage(filepage, swappage);
1110 unlock_page(swappage);
1111 page_cache_release(swappage);
1112 flush_dcache_page(filepage);
1113 SetPageUptodate(filepage);
1114 set_page_dirty(filepage);
1115 swap_free(swap);
1116 } else if (!(error = move_from_swap_cache(
1117 swappage, idx, mapping))) {
1118 info->flags |= SHMEM_PAGEIN;
1119 shmem_swp_set(info, entry, 0);
1120 shmem_swp_unmap(entry);
1121 spin_unlock(&info->lock);
1122 filepage = swappage;
1123 swap_free(swap);
1124 } else {
1125 shmem_swp_unmap(entry);
1126 spin_unlock(&info->lock);
1127 unlock_page(swappage);
1128 page_cache_release(swappage);
1129 if (error == -ENOMEM) {
1130 /* let kswapd refresh zone for GFP_ATOMICs */
1131 blk_congestion_wait(WRITE, HZ/50);
1133 goto repeat;
1135 } else if (sgp == SGP_READ && !filepage) {
1136 shmem_swp_unmap(entry);
1137 filepage = find_get_page(mapping, idx);
1138 if (filepage &&
1139 (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
1140 spin_unlock(&info->lock);
1141 wait_on_page_locked(filepage);
1142 page_cache_release(filepage);
1143 filepage = NULL;
1144 goto repeat;
1146 spin_unlock(&info->lock);
1147 } else {
1148 shmem_swp_unmap(entry);
1149 sbinfo = SHMEM_SB(inode->i_sb);
1150 if (sbinfo->max_blocks) {
1151 spin_lock(&sbinfo->stat_lock);
1152 if (sbinfo->free_blocks == 0 ||
1153 shmem_acct_block(info->flags)) {
1154 spin_unlock(&sbinfo->stat_lock);
1155 spin_unlock(&info->lock);
1156 error = -ENOSPC;
1157 goto failed;
1159 sbinfo->free_blocks--;
1160 inode->i_blocks += BLOCKS_PER_PAGE;
1161 spin_unlock(&sbinfo->stat_lock);
1162 } else if (shmem_acct_block(info->flags)) {
1163 spin_unlock(&info->lock);
1164 error = -ENOSPC;
1165 goto failed;
1168 if (!filepage) {
1169 spin_unlock(&info->lock);
1170 filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
1171 info,
1172 idx);
1173 if (!filepage) {
1174 shmem_unacct_blocks(info->flags, 1);
1175 shmem_free_blocks(inode, 1);
1176 error = -ENOMEM;
1177 goto failed;
1180 spin_lock(&info->lock);
1181 entry = shmem_swp_alloc(info, idx, sgp);
1182 if (IS_ERR(entry))
1183 error = PTR_ERR(entry);
1184 else {
1185 swap = *entry;
1186 shmem_swp_unmap(entry);
1188 if (error || swap.val || 0 != add_to_page_cache_lru(
1189 filepage, mapping, idx, GFP_ATOMIC)) {
1190 spin_unlock(&info->lock);
1191 page_cache_release(filepage);
1192 shmem_unacct_blocks(info->flags, 1);
1193 shmem_free_blocks(inode, 1);
1194 filepage = NULL;
1195 if (error)
1196 goto failed;
1197 goto repeat;
1199 info->flags |= SHMEM_PAGEIN;
1202 info->alloced++;
1203 spin_unlock(&info->lock);
1204 flush_dcache_page(filepage);
1205 SetPageUptodate(filepage);
1207 done:
1208 if (*pagep != filepage) {
1209 unlock_page(filepage);
1210 *pagep = filepage;
1212 return 0;
1214 failed:
1215 if (*pagep != filepage) {
1216 unlock_page(filepage);
1217 page_cache_release(filepage);
1219 return error;
1222 struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
1224 struct inode *inode = vma->vm_file->f_dentry->d_inode;
1225 struct page *page = NULL;
1226 unsigned long idx;
1227 int error;
1229 idx = (address - vma->vm_start) >> PAGE_SHIFT;
1230 idx += vma->vm_pgoff;
1231 idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
1232 if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1233 return NOPAGE_SIGBUS;
1235 error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
1236 if (error)
1237 return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
1239 mark_page_accessed(page);
1240 return page;
1243 static int shmem_populate(struct vm_area_struct *vma,
1244 unsigned long addr, unsigned long len,
1245 pgprot_t prot, unsigned long pgoff, int nonblock)
1247 struct inode *inode = vma->vm_file->f_dentry->d_inode;
1248 struct mm_struct *mm = vma->vm_mm;
1249 enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
1250 unsigned long size;
1252 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1253 if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
1254 return -EINVAL;
1256 while ((long) len > 0) {
1257 struct page *page = NULL;
1258 int err;
1259 /*
1260 * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
1261 */
1262 err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
1263 if (err)
1264 return err;
1265 /* Page may still be null, but only if nonblock was set. */
1266 if (page) {
1267 mark_page_accessed(page);
1268 err = install_page(mm, vma, addr, page, prot);
1269 if (err) {
1270 page_cache_release(page);
1271 return err;
1273 } else if (vma->vm_flags & VM_NONLINEAR) {
1274 /* No page was found just because we can't read it in
1275 * now (being here implies nonblock != 0), but the page
1276 * may exist, so set the PTE to fault it in later. */
1277 err = install_file_pte(mm, vma, addr, pgoff, prot);
1278 if (err)
1279 return err;
1282 len -= PAGE_SIZE;
1283 addr += PAGE_SIZE;
1284 pgoff++;
1286 return 0;
1289 #ifdef CONFIG_NUMA
1290 int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1292 struct inode *i = vma->vm_file->f_dentry->d_inode;
1293 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1296 struct mempolicy *
1297 shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
1299 struct inode *i = vma->vm_file->f_dentry->d_inode;
1300 unsigned long idx;
1302 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1303 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1305 #endif
1307 int shmem_lock(struct file *file, int lock, struct user_struct *user)
1309 struct inode *inode = file->f_dentry->d_inode;
1310 struct shmem_inode_info *info = SHMEM_I(inode);
1311 int retval = -ENOMEM;
1313 spin_lock(&info->lock);
1314 if (lock && !(info->flags & VM_LOCKED)) {
1315 if (!user_shm_lock(inode->i_size, user))
1316 goto out_nomem;
1317 info->flags |= VM_LOCKED;
1319 if (!lock && (info->flags & VM_LOCKED) && user) {
1320 user_shm_unlock(inode->i_size, user);
1321 info->flags &= ~VM_LOCKED;
1323 retval = 0;
1324 out_nomem:
1325 spin_unlock(&info->lock);
1326 return retval;
1329 int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1331 file_accessed(file);
1332 vma->vm_ops = &shmem_vm_ops;
1333 return 0;
1336 static struct inode *
1337 shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1339 struct inode *inode;
1340 struct shmem_inode_info *info;
1341 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1343 if (sbinfo->max_inodes) {
1344 spin_lock(&sbinfo->stat_lock);
1345 if (!sbinfo->free_inodes) {
1346 spin_unlock(&sbinfo->stat_lock);
1347 return NULL;
1349 sbinfo->free_inodes--;
1350 spin_unlock(&sbinfo->stat_lock);
1353 inode = new_inode(sb);
1354 if (inode) {
1355 inode->i_mode = mode;
1356 inode->i_uid = current->fsuid;
1357 inode->i_gid = current->fsgid;
1358 inode->i_blksize = PAGE_CACHE_SIZE;
1359 inode->i_blocks = 0;
1360 inode->i_mapping->a_ops = &shmem_aops;
1361 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1362 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1363 info = SHMEM_I(inode);
1364 memset(info, 0, (char *)inode - (char *)info);
1365 spin_lock_init(&info->lock);
1366 INIT_LIST_HEAD(&info->swaplist);
1368 switch (mode & S_IFMT) {
1369 default:
1370 init_special_inode(inode, mode, dev);
1371 break;
1372 case S_IFREG:
1373 inode->i_op = &shmem_inode_operations;
1374 inode->i_fop = &shmem_file_operations;
1375 mpol_shared_policy_init(&info->policy, sbinfo->policy,
1376 &sbinfo->policy_nodes);
1377 break;
1378 case S_IFDIR:
1379 inode->i_nlink++;
1380 /* Some things misbehave if size == 0 on a directory */
1381 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1382 inode->i_op = &shmem_dir_inode_operations;
1383 inode->i_fop = &simple_dir_operations;
1384 break;
1385 case S_IFLNK:
1386 /*
1387 * Must not load anything in the rbtree,
1388 * mpol_free_shared_policy will not be called.
1389 */
1390 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
1391 NULL);
1392 break;
1394 } else if (sbinfo->max_inodes) {
1395 spin_lock(&sbinfo->stat_lock);
1396 sbinfo->free_inodes++;
1397 spin_unlock(&sbinfo->stat_lock);
1399 return inode;
1402 #ifdef CONFIG_TMPFS
1403 static struct inode_operations shmem_symlink_inode_operations;
1404 static struct inode_operations shmem_symlink_inline_operations;
1406 /*
1407 * Normally tmpfs makes no use of shmem_prepare_write, but it
1408 * lets a tmpfs file be used read-write below the loop driver.
1409 */
1410 static int
1411 shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1413 struct inode *inode = page->mapping->host;
1414 return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
1417 static ssize_t
1418 shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1420 struct inode *inode = file->f_dentry->d_inode;
1421 loff_t pos;
1422 unsigned long written;
1423 ssize_t err;
1425 if ((ssize_t) count < 0)
1426 return -EINVAL;
1428 if (!access_ok(VERIFY_READ, buf, count))
1429 return -EFAULT;
1431 mutex_lock(&inode->i_mutex);
1433 pos = *ppos;
1434 written = 0;
1436 err = generic_write_checks(file, &pos, &count, 0);
1437 if (err || !count)
1438 goto out;
1440 err = remove_suid(file->f_dentry);
1441 if (err)
1442 goto out;
1444 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1446 do {
1447 struct page *page = NULL;
1448 unsigned long bytes, index, offset;
1449 char *kaddr;
1450 int left;
1452 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1453 index = pos >> PAGE_CACHE_SHIFT;
1454 bytes = PAGE_CACHE_SIZE - offset;
1455 if (bytes > count)
1456 bytes = count;
1458 /*
1459 * We don't hold page lock across copy from user -
1460 * what would it guard against? - so no deadlock here.
1461 * But it still may be a good idea to prefault below.
1462 */
1464 err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
1465 if (err)
1466 break;
1468 left = bytes;
1469 if (PageHighMem(page)) {
1470 volatile unsigned char dummy;
1471 __get_user(dummy, buf);
1472 __get_user(dummy, buf + bytes - 1);
1474 kaddr = kmap_atomic(page, KM_USER0);
1475 left = __copy_from_user_inatomic(kaddr + offset,
1476 buf, bytes);
1477 kunmap_atomic(kaddr, KM_USER0);
1479 if (left) {
1480 kaddr = kmap(page);
1481 left = __copy_from_user(kaddr + offset, buf, bytes);
1482 kunmap(page);
1485 written += bytes;
1486 count -= bytes;
1487 pos += bytes;
1488 buf += bytes;
1489 if (pos > inode->i_size)
1490 i_size_write(inode, pos);
1492 flush_dcache_page(page);
1493 set_page_dirty(page);
1494 mark_page_accessed(page);
1495 page_cache_release(page);
1497 if (left) {
1498 pos -= left;
1499 written -= left;
1500 err = -EFAULT;
1501 break;
1504 /*
1505 * Our dirty pages are not counted in nr_dirty,
1506 * and we do not attempt to balance dirty pages.
1507 */
1509 cond_resched();
1510 } while (count);
1512 *ppos = pos;
1513 if (written)
1514 err = written;
1515 out:
1516 mutex_unlock(&inode->i_mutex);
1517 return err;
1520 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1522 struct inode *inode = filp->f_dentry->d_inode;
1523 struct address_space *mapping = inode->i_mapping;
1524 unsigned long index, offset;
1526 index = *ppos >> PAGE_CACHE_SHIFT;
1527 offset = *ppos & ~PAGE_CACHE_MASK;
1529 for (;;) {
1530 struct page *page = NULL;
1531 unsigned long end_index, nr, ret;
1532 loff_t i_size = i_size_read(inode);
1534 end_index = i_size >> PAGE_CACHE_SHIFT;
1535 if (index > end_index)
1536 break;
1537 if (index == end_index) {
1538 nr = i_size & ~PAGE_CACHE_MASK;
1539 if (nr <= offset)
1540 break;
1543 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1544 if (desc->error) {
1545 if (desc->error == -EINVAL)
1546 desc->error = 0;
1547 break;
1550 /*
1551 * We must evaluate after, since reads (unlike writes)
1552 * are called without i_mutex protection against truncate
1553 */
1554 nr = PAGE_CACHE_SIZE;
1555 i_size = i_size_read(inode);
1556 end_index = i_size >> PAGE_CACHE_SHIFT;
1557 if (index == end_index) {
1558 nr = i_size & ~PAGE_CACHE_MASK;
1559 if (nr <= offset) {
1560 if (page)
1561 page_cache_release(page);
1562 break;
1565 nr -= offset;
1567 if (page) {
1568 /*
1569 * If users can be writing to this page using arbitrary
1570 * virtual addresses, take care about potential aliasing
1571 * before reading the page on the kernel side.
1572 */
1573 if (mapping_writably_mapped(mapping))
1574 flush_dcache_page(page);
1575 /*
1576 * Mark the page accessed if we read the beginning.
1577 */
1578 if (!offset)
1579 mark_page_accessed(page);
1580 } else {
1581 page = ZERO_PAGE(0);
1582 page_cache_get(page);
1585 /*
1586 * Ok, we have the page, and it's up-to-date, so
1587 * now we can copy it to user space...
1589 * The actor routine returns how many bytes were actually used..
1590 * NOTE! This may not be the same as how much of a user buffer
1591 * we filled up (we may be padding etc), so we can only update
1592 * "pos" here (the actor routine has to update the user buffer
1593 * pointers and the remaining count).
1594 */
1595 ret = actor(desc, page, offset, nr);
1596 offset += ret;
1597 index += offset >> PAGE_CACHE_SHIFT;
1598 offset &= ~PAGE_CACHE_MASK;
1600 page_cache_release(page);
1601 if (ret != nr || !desc->count)
1602 break;
1604 cond_resched();
1607 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1608 file_accessed(filp);
1611 static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1613 read_descriptor_t desc;
1615 if ((ssize_t) count < 0)
1616 return -EINVAL;
1617 if (!access_ok(VERIFY_WRITE, buf, count))
1618 return -EFAULT;
1619 if (!count)
1620 return 0;
1622 desc.written = 0;
1623 desc.count = count;
1624 desc.arg.buf = buf;
1625 desc.error = 0;
1627 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1628 if (desc.written)
1629 return desc.written;
1630 return desc.error;
1633 static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
1634 size_t count, read_actor_t actor, void *target)
1636 read_descriptor_t desc;
1638 if (!count)
1639 return 0;
1641 desc.written = 0;
1642 desc.count = count;
1643 desc.arg.data = target;
1644 desc.error = 0;
1646 do_shmem_file_read(in_file, ppos, &desc, actor);
1647 if (desc.written)
1648 return desc.written;
1649 return desc.error;
1652 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1654 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1656 buf->f_type = TMPFS_MAGIC;
1657 buf->f_bsize = PAGE_CACHE_SIZE;
1658 buf->f_namelen = NAME_MAX;
1659 spin_lock(&sbinfo->stat_lock);
1660 if (sbinfo->max_blocks) {
1661 buf->f_blocks = sbinfo->max_blocks;
1662 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1664 if (sbinfo->max_inodes) {
1665 buf->f_files = sbinfo->max_inodes;
1666 buf->f_ffree = sbinfo->free_inodes;
1668 /* else leave those fields 0 like simple_statfs */
1669 spin_unlock(&sbinfo->stat_lock);
1670 return 0;
1673 /*
1674 * File creation. Allocate an inode, and we're done..
1675 */
1676 static int
1677 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1679 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1680 int error = -ENOSPC;
1682 if (inode) {
1683 error = security_inode_init_security(inode, dir, NULL, NULL,
1684 NULL);
1685 if (error) {
1686 if (error != -EOPNOTSUPP) {
1687 iput(inode);
1688 return error;
1690 error = 0;
1692 if (dir->i_mode & S_ISGID) {
1693 inode->i_gid = dir->i_gid;
1694 if (S_ISDIR(mode))
1695 inode->i_mode |= S_ISGID;
1697 dir->i_size += BOGO_DIRENT_SIZE;
1698 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1699 d_instantiate(dentry, inode);
1700 dget(dentry); /* Extra count - pin the dentry in core */
1702 return error;
1705 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1707 int error;
1709 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1710 return error;
1711 dir->i_nlink++;
1712 return 0;
1715 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1716 struct nameidata *nd)
1718 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1721 /*
1722 * Link a file..
1723 */
1724 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1726 struct inode *inode = old_dentry->d_inode;
1727 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1729 /*
1730 * No ordinary (disk based) filesystem counts links as inodes;
1731 * but each new link needs a new dentry, pinning lowmem, and
1732 * tmpfs dentries cannot be pruned until they are unlinked.
1733 */
1734 if (sbinfo->max_inodes) {
1735 spin_lock(&sbinfo->stat_lock);
1736 if (!sbinfo->free_inodes) {
1737 spin_unlock(&sbinfo->stat_lock);
1738 return -ENOSPC;
1740 sbinfo->free_inodes--;
1741 spin_unlock(&sbinfo->stat_lock);
1744 dir->i_size += BOGO_DIRENT_SIZE;
1745 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1746 inode->i_nlink++;
1747 atomic_inc(&inode->i_count); /* New dentry reference */
1748 dget(dentry); /* Extra pinning count for the created dentry */
1749 d_instantiate(dentry, inode);
1750 return 0;
1753 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1755 struct inode *inode = dentry->d_inode;
1757 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
1758 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1759 if (sbinfo->max_inodes) {
1760 spin_lock(&sbinfo->stat_lock);
1761 sbinfo->free_inodes++;
1762 spin_unlock(&sbinfo->stat_lock);
1766 dir->i_size -= BOGO_DIRENT_SIZE;
1767 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1768 inode->i_nlink--;
1769 dput(dentry); /* Undo the count from "create" - this does all the work */
1770 return 0;
1773 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1775 if (!simple_empty(dentry))
1776 return -ENOTEMPTY;
1778 dentry->d_inode->i_nlink--;
1779 dir->i_nlink--;
1780 return shmem_unlink(dir, dentry);
1783 /*
1784 * The VFS layer already does all the dentry stuff for rename,
1785 * we just have to decrement the usage count for the target if
1786 * it exists so that the VFS layer correctly free's it when it
1787 * gets overwritten.
1788 */
1789 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1791 struct inode *inode = old_dentry->d_inode;
1792 int they_are_dirs = S_ISDIR(inode->i_mode);
1794 if (!simple_empty(new_dentry))
1795 return -ENOTEMPTY;
1797 if (new_dentry->d_inode) {
1798 (void) shmem_unlink(new_dir, new_dentry);
1799 if (they_are_dirs)
1800 old_dir->i_nlink--;
1801 } else if (they_are_dirs) {
1802 old_dir->i_nlink--;
1803 new_dir->i_nlink++;
1806 old_dir->i_size -= BOGO_DIRENT_SIZE;
1807 new_dir->i_size += BOGO_DIRENT_SIZE;
1808 old_dir->i_ctime = old_dir->i_mtime =
1809 new_dir->i_ctime = new_dir->i_mtime =
1810 inode->i_ctime = CURRENT_TIME;
1811 return 0;
1814 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1816 int error;
1817 int len;
1818 struct inode *inode;
1819 struct page *page = NULL;
1820 char *kaddr;
1821 struct shmem_inode_info *info;
1823 len = strlen(symname) + 1;
1824 if (len > PAGE_CACHE_SIZE)
1825 return -ENAMETOOLONG;
1827 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1828 if (!inode)
1829 return -ENOSPC;
1831 error = security_inode_init_security(inode, dir, NULL, NULL,
1832 NULL);
1833 if (error) {
1834 if (error != -EOPNOTSUPP) {
1835 iput(inode);
1836 return error;
1838 error = 0;
1841 info = SHMEM_I(inode);
1842 inode->i_size = len-1;
1843 if (len <= (char *)inode - (char *)info) {
1844 /* do it inline */
1845 memcpy(info, symname, len);
1846 inode->i_op = &shmem_symlink_inline_operations;
1847 } else {
1848 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1849 if (error) {
1850 iput(inode);
1851 return error;
1853 inode->i_op = &shmem_symlink_inode_operations;
1854 kaddr = kmap_atomic(page, KM_USER0);
1855 memcpy(kaddr, symname, len);
1856 kunmap_atomic(kaddr, KM_USER0);
1857 set_page_dirty(page);
1858 page_cache_release(page);
1860 if (dir->i_mode & S_ISGID)
1861 inode->i_gid = dir->i_gid;
1862 dir->i_size += BOGO_DIRENT_SIZE;
1863 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1864 d_instantiate(dentry, inode);
1865 dget(dentry);
1866 return 0;
1869 static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1871 nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1872 return NULL;
1875 static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1877 struct page *page = NULL;
1878 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1879 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1880 return page;
1883 static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1885 if (!IS_ERR(nd_get_link(nd))) {
1886 struct page *page = cookie;
1887 kunmap(page);
1888 mark_page_accessed(page);
1889 page_cache_release(page);
1893 static struct inode_operations shmem_symlink_inline_operations = {
1894 .readlink = generic_readlink,
1895 .follow_link = shmem_follow_link_inline,
1896 };
1898 static struct inode_operations shmem_symlink_inode_operations = {
1899 .truncate = shmem_truncate,
1900 .readlink = generic_readlink,
1901 .follow_link = shmem_follow_link,
1902 .put_link = shmem_put_link,
1903 };
1905 static int shmem_parse_options(char *options, int *mode, uid_t *uid,
1906 gid_t *gid, unsigned long *blocks, unsigned long *inodes,
1907 int *policy, nodemask_t *policy_nodes)
1909 char *this_char, *value, *rest;
1911 while (options != NULL) {
1912 this_char = options;
1913 for (;;) {
1914 /*
1915 * NUL-terminate this option: unfortunately,
1916 * mount options form a comma-separated list,
1917 * but mpol's nodelist may also contain commas.
1918 */
1919 options = strchr(options, ',');
1920 if (options == NULL)
1921 break;
1922 options++;
1923 if (!isdigit(*options)) {
1924 options[-1] = '\0';
1925 break;
1928 if (!*this_char)
1929 continue;
1930 if ((value = strchr(this_char,'=')) != NULL) {
1931 *value++ = 0;
1932 } else {
1933 printk(KERN_ERR
1934 "tmpfs: No value for mount option '%s'\n",
1935 this_char);
1936 return 1;
1939 if (!strcmp(this_char,"size")) {
1940 unsigned long long size;
1941 size = memparse(value,&rest);
1942 if (*rest == '%') {
1943 size <<= PAGE_SHIFT;
1944 size *= totalram_pages;
1945 do_div(size, 100);
1946 rest++;
1948 if (*rest)
1949 goto bad_val;
1950 *blocks = size >> PAGE_CACHE_SHIFT;
1951 } else if (!strcmp(this_char,"nr_blocks")) {
1952 *blocks = memparse(value,&rest);
1953 if (*rest)
1954 goto bad_val;
1955 } else if (!strcmp(this_char,"nr_inodes")) {
1956 *inodes = memparse(value,&rest);
1957 if (*rest)
1958 goto bad_val;
1959 } else if (!strcmp(this_char,"mode")) {
1960 if (!mode)
1961 continue;
1962 *mode = simple_strtoul(value,&rest,8);
1963 if (*rest)
1964 goto bad_val;
1965 } else if (!strcmp(this_char,"uid")) {
1966 if (!uid)
1967 continue;
1968 *uid = simple_strtoul(value,&rest,0);
1969 if (*rest)
1970 goto bad_val;
1971 } else if (!strcmp(this_char,"gid")) {
1972 if (!gid)
1973 continue;
1974 *gid = simple_strtoul(value,&rest,0);
1975 if (*rest)
1976 goto bad_val;
1977 } else if (!strcmp(this_char,"mpol")) {
1978 if (shmem_parse_mpol(value,policy,policy_nodes))
1979 goto bad_val;
1980 } else {
1981 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1982 this_char);
1983 return 1;
1986 return 0;
1988 bad_val:
1989 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1990 value, this_char);
1991 return 1;
1995 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1997 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1998 unsigned long max_blocks = sbinfo->max_blocks;
1999 unsigned long max_inodes = sbinfo->max_inodes;
2000 int policy = sbinfo->policy;
2001 nodemask_t policy_nodes = sbinfo->policy_nodes;
2002 unsigned long blocks;
2003 unsigned long inodes;
2004 int error = -EINVAL;
2006 if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
2007 &max_inodes, &policy, &policy_nodes))
2008 return error;
2010 spin_lock(&sbinfo->stat_lock);
2011 blocks = sbinfo->max_blocks - sbinfo->free_blocks;
2012 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2013 if (max_blocks < blocks)
2014 goto out;
2015 if (max_inodes < inodes)
2016 goto out;
2017 /*
2018 * Those tests also disallow limited->unlimited while any are in
2019 * use, so i_blocks will always be zero when max_blocks is zero;
2020 * but we must separately disallow unlimited->limited, because
2021 * in that case we have no record of how much is already in use.
2022 */
2023 if (max_blocks && !sbinfo->max_blocks)
2024 goto out;
2025 if (max_inodes && !sbinfo->max_inodes)
2026 goto out;
2028 error = 0;
2029 sbinfo->max_blocks = max_blocks;
2030 sbinfo->free_blocks = max_blocks - blocks;
2031 sbinfo->max_inodes = max_inodes;
2032 sbinfo->free_inodes = max_inodes - inodes;
2033 sbinfo->policy = policy;
2034 sbinfo->policy_nodes = policy_nodes;
2035 out:
2036 spin_unlock(&sbinfo->stat_lock);
2037 return error;
2039 #endif
2041 static void shmem_put_super(struct super_block *sb)
2043 kfree(sb->s_fs_info);
2044 sb->s_fs_info = NULL;
2047 static int shmem_fill_super(struct super_block *sb,
2048 void *data, int silent)
2050 struct inode *inode;
2051 struct dentry *root;
2052 int mode = S_IRWXUGO | S_ISVTX;
2053 uid_t uid = current->fsuid;
2054 gid_t gid = current->fsgid;
2055 int err = -ENOMEM;
2056 struct shmem_sb_info *sbinfo;
2057 unsigned long blocks = 0;
2058 unsigned long inodes = 0;
2059 int policy = MPOL_DEFAULT;
2060 nodemask_t policy_nodes = node_online_map;
2062 #ifdef CONFIG_TMPFS
2063 /*
2064 * Per default we only allow half of the physical ram per
2065 * tmpfs instance, limiting inodes to one per page of lowmem;
2066 * but the internal instance is left unlimited.
2067 */
2068 if (!(sb->s_flags & MS_NOUSER)) {
2069 blocks = totalram_pages / 2;
2070 inodes = totalram_pages - totalhigh_pages;
2071 if (inodes > blocks)
2072 inodes = blocks;
2073 if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
2074 &inodes, &policy, &policy_nodes))
2075 return -EINVAL;
2077 #else
2078 sb->s_flags |= MS_NOUSER;
2079 #endif
2081 /* Round up to L1_CACHE_BYTES to resist false sharing */
2082 sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2083 L1_CACHE_BYTES), GFP_KERNEL);
2084 if (!sbinfo)
2085 return -ENOMEM;
2087 spin_lock_init(&sbinfo->stat_lock);
2088 sbinfo->max_blocks = blocks;
2089 sbinfo->free_blocks = blocks;
2090 sbinfo->max_inodes = inodes;
2091 sbinfo->free_inodes = inodes;
2092 sbinfo->policy = policy;
2093 sbinfo->policy_nodes = policy_nodes;
2095 sb->s_fs_info = sbinfo;
2096 sb->s_maxbytes = SHMEM_MAX_BYTES;
2097 sb->s_blocksize = PAGE_CACHE_SIZE;
2098 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2099 sb->s_magic = TMPFS_MAGIC;
2100 sb->s_op = &shmem_ops;
2101 sb->s_time_gran = 1;
2103 inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
2104 if (!inode)
2105 goto failed;
2106 inode->i_uid = uid;
2107 inode->i_gid = gid;
2108 root = d_alloc_root(inode);
2109 if (!root)
2110 goto failed_iput;
2111 sb->s_root = root;
2112 return 0;
2114 failed_iput:
2115 iput(inode);
2116 failed:
2117 shmem_put_super(sb);
2118 return err;
2121 static struct kmem_cache *shmem_inode_cachep;
2123 static struct inode *shmem_alloc_inode(struct super_block *sb)
2125 struct shmem_inode_info *p;
2126 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
2127 if (!p)
2128 return NULL;
2129 return &p->vfs_inode;
2132 static void shmem_destroy_inode(struct inode *inode)
2134 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2135 /* only struct inode is valid if it's an inline symlink */
2136 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2138 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2141 static void init_once(void *foo, struct kmem_cache *cachep,
2142 unsigned long flags)
2144 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2146 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
2147 SLAB_CTOR_CONSTRUCTOR) {
2148 inode_init_once(&p->vfs_inode);
2152 static int init_inodecache(void)
2154 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2155 sizeof(struct shmem_inode_info),
2156 0, 0, init_once, NULL);
2157 if (shmem_inode_cachep == NULL)
2158 return -ENOMEM;
2159 return 0;
2162 static void destroy_inodecache(void)
2164 if (kmem_cache_destroy(shmem_inode_cachep))
2165 printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
2168 static const struct address_space_operations shmem_aops = {
2169 .writepage = shmem_writepage,
2170 .set_page_dirty = __set_page_dirty_nobuffers,
2171 #ifdef CONFIG_TMPFS
2172 .prepare_write = shmem_prepare_write,
2173 .commit_write = simple_commit_write,
2174 #endif
2175 .migratepage = migrate_page,
2176 };
2178 static struct file_operations shmem_file_operations = {
2179 .mmap = shmem_mmap,
2180 #ifdef CONFIG_TMPFS
2181 .llseek = generic_file_llseek,
2182 .read = shmem_file_read,
2183 .write = shmem_file_write,
2184 .fsync = simple_sync_file,
2185 .sendfile = shmem_file_sendfile,
2186 #endif
2187 };
2189 static struct inode_operations shmem_inode_operations = {
2190 .truncate = shmem_truncate,
2191 .setattr = shmem_notify_change,
2192 .truncate_range = shmem_truncate_range,
2193 };
2195 static struct inode_operations shmem_dir_inode_operations = {
2196 #ifdef CONFIG_TMPFS
2197 .create = shmem_create,
2198 .lookup = simple_lookup,
2199 .link = shmem_link,
2200 .unlink = shmem_unlink,
2201 .symlink = shmem_symlink,
2202 .mkdir = shmem_mkdir,
2203 .rmdir = shmem_rmdir,
2204 .mknod = shmem_mknod,
2205 .rename = shmem_rename,
2206 #endif
2207 };
2209 static struct super_operations shmem_ops = {
2210 .alloc_inode = shmem_alloc_inode,
2211 .destroy_inode = shmem_destroy_inode,
2212 #ifdef CONFIG_TMPFS
2213 .statfs = shmem_statfs,
2214 .remount_fs = shmem_remount_fs,
2215 #endif
2216 .delete_inode = shmem_delete_inode,
2217 .drop_inode = generic_delete_inode,
2218 .put_super = shmem_put_super,
2219 };
2221 static struct vm_operations_struct shmem_vm_ops = {
2222 .nopage = shmem_nopage,
2223 .populate = shmem_populate,
2224 #ifdef CONFIG_NUMA
2225 .set_policy = shmem_set_policy,
2226 .get_policy = shmem_get_policy,
2227 #endif
2228 };
2231 static int shmem_get_sb(struct file_system_type *fs_type,
2232 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2234 return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
2237 static struct file_system_type tmpfs_fs_type = {
2238 .owner = THIS_MODULE,
2239 .name = "tmpfs",
2240 .get_sb = shmem_get_sb,
2241 .kill_sb = kill_litter_super,
2242 };
2243 static struct vfsmount *shm_mnt;
2245 static int __init init_tmpfs(void)
2247 int error;
2249 error = init_inodecache();
2250 if (error)
2251 goto out3;
2253 error = register_filesystem(&tmpfs_fs_type);
2254 if (error) {
2255 printk(KERN_ERR "Could not register tmpfs\n");
2256 goto out2;
2259 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2260 tmpfs_fs_type.name, NULL);
2261 if (IS_ERR(shm_mnt)) {
2262 error = PTR_ERR(shm_mnt);
2263 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2264 goto out1;
2266 return 0;
2268 out1:
2269 unregister_filesystem(&tmpfs_fs_type);
2270 out2:
2271 destroy_inodecache();
2272 out3:
2273 shm_mnt = ERR_PTR(error);
2274 return error;
2276 module_init(init_tmpfs)
2278 /*
2279 * shmem_file_setup - get an unlinked file living in tmpfs
2281 * @name: name for dentry (to be seen in /proc/<pid>/maps
2282 * @size: size to be set for the file
2284 */
2285 struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2287 int error;
2288 struct file *file;
2289 struct inode *inode;
2290 struct dentry *dentry, *root;
2291 struct qstr this;
2293 if (IS_ERR(shm_mnt))
2294 return (void *)shm_mnt;
2296 if (size < 0 || size > SHMEM_MAX_BYTES)
2297 return ERR_PTR(-EINVAL);
2299 if (shmem_acct_size(flags, size))
2300 return ERR_PTR(-ENOMEM);
2302 error = -ENOMEM;
2303 this.name = name;
2304 this.len = strlen(name);
2305 this.hash = 0; /* will go */
2306 root = shm_mnt->mnt_root;
2307 dentry = d_alloc(root, &this);
2308 if (!dentry)
2309 goto put_memory;
2311 error = -ENFILE;
2312 file = get_empty_filp();
2313 if (!file)
2314 goto put_dentry;
2316 error = -ENOSPC;
2317 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2318 if (!inode)
2319 goto close_file;
2321 SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
2322 d_instantiate(dentry, inode);
2323 inode->i_size = size;
2324 inode->i_nlink = 0; /* It is unlinked */
2325 file->f_vfsmnt = mntget(shm_mnt);
2326 file->f_dentry = dentry;
2327 file->f_mapping = inode->i_mapping;
2328 file->f_op = &shmem_file_operations;
2329 file->f_mode = FMODE_WRITE | FMODE_READ;
2330 return file;
2332 close_file:
2333 put_filp(file);
2334 put_dentry:
2335 dput(dentry);
2336 put_memory:
2337 shmem_unacct_size(flags, size);
2338 return ERR_PTR(error);
2341 /*
2342 * shmem_zero_setup - setup a shared anonymous mapping
2344 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
2345 */
2346 int shmem_zero_setup(struct vm_area_struct *vma)
2348 struct file *file;
2349 loff_t size = vma->vm_end - vma->vm_start;
2351 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2352 if (IS_ERR(file))
2353 return PTR_ERR(file);
2355 if (vma->vm_file)
2356 fput(vma->vm_file);
2357 vma->vm_file = file;
2358 vma->vm_ops = &shmem_vm_ops;
2359 return 0;