ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / mm / shmem.c
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-2003 Hugh Dickins.
10  * Copyright (C) 2002-2003 VERITAS Software Corporation.
11  *
12  * This file is released under the GPL.
13  */
14
15 /*
16  * This virtual memory filesystem is heavily based on the ramfs. It
17  * extends ramfs by the ability to use swap and honor resource limits
18  * which makes it a completely usable filesystem.
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/devfs_fs_kernel.h>
25 #include <linux/fs.h>
26 #include <linux/mm.h>
27 #include <linux/mman.h>
28 #include <linux/file.h>
29 #include <linux/swap.h>
30 #include <linux/pagemap.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/backing-dev.h>
34 #include <linux/shmem_fs.h>
35 #include <linux/mount.h>
36 #include <linux/writeback.h>
37 #include <linux/vfs.h>
38 #include <linux/blkdev.h>
39 #include <linux/security.h>
40 #include <asm/uaccess.h>
41 #include <asm/div64.h>
42
43 /* This magic number is used in glibc for posix shared memory */
44 #define TMPFS_MAGIC     0x01021994
45
46 #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
47 #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
48 #define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
49
50 #define SHMEM_MAX_INDEX  (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
51 #define SHMEM_MAX_BYTES  ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
52
53 #define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
54
55 /* info->flags needs VM_flags to handle pagein/truncate races efficiently */
56 #define SHMEM_PAGEIN     VM_READ
57 #define SHMEM_TRUNCATE   VM_WRITE
58
59 /* Pretend that each entry is of this size in directory's i_size */
60 #define BOGO_DIRENT_SIZE 20
61
62 /* Keep swapped page count in private field of indirect struct page */
63 #define nr_swapped              private
64
65 /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
66 enum sgp_type {
67         SGP_QUICK,      /* don't try more than file page cache lookup */
68         SGP_READ,       /* don't exceed i_size, don't allocate page */
69         SGP_CACHE,      /* don't exceed i_size, may allocate page */
70         SGP_WRITE,      /* may exceed i_size, may allocate page */
71 };
72
73 static int shmem_getpage(struct inode *inode, unsigned long idx,
74                          struct page **pagep, enum sgp_type sgp, int *type);
75
76 static inline struct page *shmem_dir_alloc(unsigned int gfp_mask)
77 {
78         /*
79          * The above definition of ENTRIES_PER_PAGE, and the use of
80          * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE:
81          * might be reconsidered if it ever diverges from PAGE_SIZE.
82          */
83         return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
84 }
85
86 static inline void shmem_dir_free(struct page *page)
87 {
88         __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
89 }
90
91 static struct page **shmem_dir_map(struct page *page)
92 {
93         return (struct page **)kmap_atomic(page, KM_USER0);
94 }
95
96 static inline void shmem_dir_unmap(struct page **dir)
97 {
98         kunmap_atomic(dir, KM_USER0);
99 }
100
101 static swp_entry_t *shmem_swp_map(struct page *page)
102 {
103         /*
104          * We have to avoid the unconditional inc_preempt_count()
105          * in kmap_atomic(), since shmem_swp_unmap() will also be
106          * applied to the low memory addresses within i_direct[].
107          * PageHighMem and high_memory tests are good for all arches
108          * and configs: highmem_start_page and FIXADDR_START are not.
109          */
110         return PageHighMem(page)?
111                 (swp_entry_t *)kmap_atomic(page, KM_USER1):
112                 (swp_entry_t *)page_address(page);
113 }
114
115 static inline void shmem_swp_unmap(swp_entry_t *entry)
116 {
117         if (entry >= (swp_entry_t *)high_memory)
118                 kunmap_atomic(entry, KM_USER1);
119 }
120
121 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
122 {
123         return sb->s_fs_info;
124 }
125
126 /*
127  * shmem_file_setup pre-accounts the whole fixed size of a VM object,
128  * for shared memory and for shared anonymous (/dev/zero) mappings
129  * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
130  * consistent with the pre-accounting of private mappings ...
131  */
132 static inline int shmem_acct_size(unsigned long flags, loff_t size)
133 {
134         return (flags & VM_ACCOUNT)?
135                 security_vm_enough_memory(VM_ACCT(size)): 0;
136 }
137
138 static inline void shmem_unacct_size(unsigned long flags, loff_t size)
139 {
140         if (flags & VM_ACCOUNT)
141                 vm_unacct_memory(VM_ACCT(size));
142 }
143
144 /*
145  * ... whereas tmpfs objects are accounted incrementally as
146  * pages are allocated, in order to allow huge sparse files.
147  * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
148  * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
149  */
150 static inline int shmem_acct_block(unsigned long flags)
151 {
152         return (flags & VM_ACCOUNT)?
153                 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
154 }
155
156 static inline void shmem_unacct_blocks(unsigned long flags, long pages)
157 {
158         if (!(flags & VM_ACCOUNT))
159                 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
160 }
161
162 static struct super_operations shmem_ops;
163 static struct address_space_operations shmem_aops;
164 static struct file_operations shmem_file_operations;
165 static struct inode_operations shmem_inode_operations;
166 static struct inode_operations shmem_dir_inode_operations;
167 static struct vm_operations_struct shmem_vm_ops;
168
169 static struct backing_dev_info shmem_backing_dev_info = {
170         .ra_pages       = 0,    /* No readahead */
171         .memory_backed  = 1,    /* Does not contribute to dirty memory */
172         .unplug_io_fn = default_unplug_io_fn,
173 };
174
175 LIST_HEAD(shmem_inodes);
176 static spinlock_t shmem_ilock = SPIN_LOCK_UNLOCKED;
177
178 static void shmem_free_block(struct inode *inode)
179 {
180         struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
181         spin_lock(&sbinfo->stat_lock);
182         sbinfo->free_blocks++;
183         inode->i_blocks -= BLOCKS_PER_PAGE;
184         spin_unlock(&sbinfo->stat_lock);
185 }
186
187 /*
188  * shmem_recalc_inode - recalculate the size of an inode
189  *
190  * @inode: inode to recalc
191  *
192  * We have to calculate the free blocks since the mm can drop
193  * undirtied hole pages behind our back.
194  *
195  * But normally   info->alloced == inode->i_mapping->nrpages + info->swapped
196  * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
197  *
198  * It has to be called with the spinlock held.
199  */
200 static void shmem_recalc_inode(struct inode *inode)
201 {
202         struct shmem_inode_info *info = SHMEM_I(inode);
203         long freed;
204
205         freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
206         if (freed > 0) {
207                 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
208                 info->alloced -= freed;
209                 spin_lock(&sbinfo->stat_lock);
210                 sbinfo->free_blocks += freed;
211                 inode->i_blocks -= freed*BLOCKS_PER_PAGE;
212                 spin_unlock(&sbinfo->stat_lock);
213                 shmem_unacct_blocks(info->flags, freed);
214         }
215 }
216
217 /*
218  * shmem_swp_entry - find the swap vector position in the info structure
219  *
220  * @info:  info structure for the inode
221  * @index: index of the page to find
222  * @page:  optional page to add to the structure. Has to be preset to
223  *         all zeros
224  *
225  * If there is no space allocated yet it will return NULL when
226  * page is NULL, else it will use the page for the needed block,
227  * setting it to NULL on return to indicate that it has been used.
228  *
229  * The swap vector is organized the following way:
230  *
231  * There are SHMEM_NR_DIRECT entries directly stored in the
232  * shmem_inode_info structure. So small files do not need an addional
233  * allocation.
234  *
235  * For pages with index > SHMEM_NR_DIRECT there is the pointer
236  * i_indirect which points to a page which holds in the first half
237  * doubly indirect blocks, in the second half triple indirect blocks:
238  *
239  * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the
240  * following layout (for SHMEM_NR_DIRECT == 16):
241  *
242  * i_indirect -> dir --> 16-19
243  *            |      +-> 20-23
244  *            |
245  *            +-->dir2 --> 24-27
246  *            |        +-> 28-31
247  *            |        +-> 32-35
248  *            |        +-> 36-39
249  *            |
250  *            +-->dir3 --> 40-43
251  *                     +-> 44-47
252  *                     +-> 48-51
253  *                     +-> 52-55
254  */
255 static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
256 {
257         unsigned long offset;
258         struct page **dir;
259         struct page *subdir;
260
261         if (index < SHMEM_NR_DIRECT)
262                 return info->i_direct+index;
263         if (!info->i_indirect) {
264                 if (page) {
265                         info->i_indirect = *page;
266                         *page = NULL;
267                 }
268                 return NULL;                    /* need another page */
269         }
270
271         index -= SHMEM_NR_DIRECT;
272         offset = index % ENTRIES_PER_PAGE;
273         index /= ENTRIES_PER_PAGE;
274         dir = shmem_dir_map(info->i_indirect);
275
276         if (index >= ENTRIES_PER_PAGE/2) {
277                 index -= ENTRIES_PER_PAGE/2;
278                 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
279                 index %= ENTRIES_PER_PAGE;
280                 subdir = *dir;
281                 if (!subdir) {
282                         if (page) {
283                                 *dir = *page;
284                                 *page = NULL;
285                         }
286                         shmem_dir_unmap(dir);
287                         return NULL;            /* need another page */
288                 }
289                 shmem_dir_unmap(dir);
290                 dir = shmem_dir_map(subdir);
291         }
292
293         dir += index;
294         subdir = *dir;
295         if (!subdir) {
296                 if (!page || !(subdir = *page)) {
297                         shmem_dir_unmap(dir);
298                         return NULL;            /* need a page */
299                 }
300                 *dir = subdir;
301                 *page = NULL;
302         }
303         shmem_dir_unmap(dir);
304
305         /*
306          * With apologies... caller shmem_swp_alloc passes non-NULL
307          * page (though perhaps NULL *page); and now we know that this
308          * indirect page has been allocated, we can shortcut the final
309          * kmap if we know it contains no swap entries, as is commonly
310          * the case: return pointer to a 0 which doesn't need kmapping.
311          */
312         return (page && !subdir->nr_swapped)?
313                 (swp_entry_t *)&subdir->nr_swapped:
314                 shmem_swp_map(subdir) + offset;
315 }
316
317 static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
318 {
319         long incdec = value? 1: -1;
320
321         entry->val = value;
322         info->swapped += incdec;
323         if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT)
324                 kmap_atomic_to_page(entry)->nr_swapped += incdec;
325 }
326
327 /*
328  * shmem_swp_alloc - get the position of the swap entry for the page.
329  *                   If it does not exist allocate the entry.
330  *
331  * @info:       info structure for the inode
332  * @index:      index of the page to find
333  * @sgp:        check and recheck i_size? skip allocation?
334  */
335 static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
336 {
337         struct inode *inode = &info->vfs_inode;
338         struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
339         struct page *page = NULL;
340         swp_entry_t *entry;
341         static const swp_entry_t unswapped = { 0 };
342
343         if (sgp != SGP_WRITE &&
344             ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
345                 return ERR_PTR(-EINVAL);
346
347         while (!(entry = shmem_swp_entry(info, index, &page))) {
348                 if (sgp == SGP_READ)
349                         return (swp_entry_t *) &unswapped;
350                 /*
351                  * Test free_blocks against 1 not 0, since we have 1 data
352                  * page (and perhaps indirect index pages) yet to allocate:
353                  * a waste to allocate index if we cannot allocate data.
354                  */
355                 spin_lock(&sbinfo->stat_lock);
356                 if (sbinfo->free_blocks <= 1) {
357                         spin_unlock(&sbinfo->stat_lock);
358                         return ERR_PTR(-ENOSPC);
359                 }
360                 sbinfo->free_blocks--;
361                 inode->i_blocks += BLOCKS_PER_PAGE;
362                 spin_unlock(&sbinfo->stat_lock);
363
364                 spin_unlock(&info->lock);
365                 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping));
366                 if (page) {
367                         clear_highpage(page);
368                         page->nr_swapped = 0;
369                 }
370                 spin_lock(&info->lock);
371
372                 if (!page) {
373                         shmem_free_block(inode);
374                         return ERR_PTR(-ENOMEM);
375                 }
376                 if (sgp != SGP_WRITE &&
377                     ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
378                         entry = ERR_PTR(-EINVAL);
379                         break;
380                 }
381                 if (info->next_index <= index)
382                         info->next_index = index + 1;
383         }
384         if (page) {
385                 /* another task gave its page, or truncated the file */
386                 shmem_free_block(inode);
387                 shmem_dir_free(page);
388         }
389         if (info->next_index <= index && !IS_ERR(entry))
390                 info->next_index = index + 1;
391         return entry;
392 }
393
394 /*
395  * shmem_free_swp - free some swap entries in a directory
396  *
397  * @dir:   pointer to the directory
398  * @edir:  pointer after last entry of the directory
399  */
400 static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir)
401 {
402         swp_entry_t *ptr;
403         int freed = 0;
404
405         for (ptr = dir; ptr < edir; ptr++) {
406                 if (ptr->val) {
407                         free_swap_and_cache(*ptr);
408                         *ptr = (swp_entry_t){0};
409                         freed++;
410                 }
411         }
412         return freed;
413 }
414
415 static void shmem_truncate(struct inode *inode)
416 {
417         struct shmem_inode_info *info = SHMEM_I(inode);
418         unsigned long idx;
419         unsigned long size;
420         unsigned long limit;
421         unsigned long stage;
422         struct page **dir;
423         struct page *subdir;
424         struct page *empty;
425         swp_entry_t *ptr;
426         int offset;
427         int freed;
428
429         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
430         idx = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
431         if (idx >= info->next_index)
432                 return;
433
434         spin_lock(&info->lock);
435         info->flags |= SHMEM_TRUNCATE;
436         limit = info->next_index;
437         info->next_index = idx;
438         if (info->swapped && idx < SHMEM_NR_DIRECT) {
439                 ptr = info->i_direct;
440                 size = limit;
441                 if (size > SHMEM_NR_DIRECT)
442                         size = SHMEM_NR_DIRECT;
443                 info->swapped -= shmem_free_swp(ptr+idx, ptr+size);
444         }
445         if (!info->i_indirect)
446                 goto done2;
447
448         BUG_ON(limit <= SHMEM_NR_DIRECT);
449         limit -= SHMEM_NR_DIRECT;
450         idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
451         offset = idx % ENTRIES_PER_PAGE;
452         idx -= offset;
453
454         empty = NULL;
455         dir = shmem_dir_map(info->i_indirect);
456         stage = ENTRIES_PER_PAGEPAGE/2;
457         if (idx < ENTRIES_PER_PAGEPAGE/2)
458                 dir += idx/ENTRIES_PER_PAGE;
459         else {
460                 dir += ENTRIES_PER_PAGE/2;
461                 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
462                 while (stage <= idx)
463                         stage += ENTRIES_PER_PAGEPAGE;
464                 if (*dir) {
465                         subdir = *dir;
466                         size = ((idx - ENTRIES_PER_PAGEPAGE/2) %
467                                 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
468                         if (!size && !offset) {
469                                 empty = subdir;
470                                 *dir = NULL;
471                         }
472                         shmem_dir_unmap(dir);
473                         dir = shmem_dir_map(subdir) + size;
474                 } else {
475                         offset = 0;
476                         idx = stage;
477                 }
478         }
479
480         for (; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
481                 if (unlikely(idx == stage)) {
482                         shmem_dir_unmap(dir-1);
483                         dir = shmem_dir_map(info->i_indirect) +
484                             ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
485                         while (!*dir) {
486                                 dir++;
487                                 idx += ENTRIES_PER_PAGEPAGE;
488                                 if (idx >= limit)
489                                         goto done1;
490                         }
491                         stage = idx + ENTRIES_PER_PAGEPAGE;
492                         subdir = *dir;
493                         *dir = NULL;
494                         shmem_dir_unmap(dir);
495                         if (empty) {
496                                 shmem_dir_free(empty);
497                                 shmem_free_block(inode);
498                         }
499                         empty = subdir;
500                         cond_resched_lock(&info->lock);
501                         dir = shmem_dir_map(subdir);
502                 }
503                 subdir = *dir;
504                 if (subdir && subdir->nr_swapped) {
505                         ptr = shmem_swp_map(subdir);
506                         size = limit - idx;
507                         if (size > ENTRIES_PER_PAGE)
508                                 size = ENTRIES_PER_PAGE;
509                         freed = shmem_free_swp(ptr+offset, ptr+size);
510                         shmem_swp_unmap(ptr);
511                         info->swapped -= freed;
512                         subdir->nr_swapped -= freed;
513                         BUG_ON(subdir->nr_swapped > offset);
514                 }
515                 if (offset)
516                         offset = 0;
517                 else if (subdir) {
518                         *dir = NULL;
519                         shmem_dir_free(subdir);
520                         shmem_free_block(inode);
521                 }
522         }
523 done1:
524         shmem_dir_unmap(dir-1);
525         if (empty) {
526                 shmem_dir_free(empty);
527                 shmem_free_block(inode);
528         }
529         if (info->next_index <= SHMEM_NR_DIRECT) {
530                 shmem_dir_free(info->i_indirect);
531                 info->i_indirect = NULL;
532                 shmem_free_block(inode);
533         }
534 done2:
535         BUG_ON(info->swapped > info->next_index);
536         if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
537                 /*
538                  * Call truncate_inode_pages again: racing shmem_unuse_inode
539                  * may have swizzled a page in from swap since vmtruncate or
540                  * generic_delete_inode did it, before we lowered next_index.
541                  * Also, though shmem_getpage checks i_size before adding to
542                  * cache, no recheck after: so fix the narrow window there too.
543                  */
544                 spin_unlock(&info->lock);
545                 truncate_inode_pages(inode->i_mapping, inode->i_size);
546                 spin_lock(&info->lock);
547         }
548         info->flags &= ~SHMEM_TRUNCATE;
549         shmem_recalc_inode(inode);
550         spin_unlock(&info->lock);
551 }
552
553 static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
554 {
555         struct inode *inode = dentry->d_inode;
556         struct page *page = NULL;
557         int error;
558
559         if (attr->ia_valid & ATTR_SIZE) {
560                 if (attr->ia_size < inode->i_size) {
561                         /*
562                          * If truncating down to a partial page, then
563                          * if that page is already allocated, hold it
564                          * in memory until the truncation is over, so
565                          * truncate_partial_page cannnot miss it were
566                          * it assigned to swap.
567                          */
568                         if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
569                                 (void) shmem_getpage(inode,
570                                         attr->ia_size>>PAGE_CACHE_SHIFT,
571                                                 &page, SGP_READ, NULL);
572                         }
573                         /*
574                          * Reset SHMEM_PAGEIN flag so that shmem_truncate can
575                          * detect if any pages might have been added to cache
576                          * after truncate_inode_pages.  But we needn't bother
577                          * if it's being fully truncated to zero-length: the
578                          * nrpages check is efficient enough in that case.
579                          */
580                         if (attr->ia_size) {
581                                 struct shmem_inode_info *info = SHMEM_I(inode);
582                                 spin_lock(&info->lock);
583                                 info->flags &= ~SHMEM_PAGEIN;
584                                 spin_unlock(&info->lock);
585                         }
586                 }
587         }
588
589         error = inode_change_ok(inode, attr);
590         if (!error)
591                 error = inode_setattr(inode, attr);
592         if (page)
593                 page_cache_release(page);
594         return error;
595 }
596
597 static void shmem_delete_inode(struct inode *inode)
598 {
599         struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
600         struct shmem_inode_info *info = SHMEM_I(inode);
601
602         if (inode->i_op->truncate == shmem_truncate) {
603                 spin_lock(&shmem_ilock);
604                 list_del(&info->list);
605                 spin_unlock(&shmem_ilock);
606                 shmem_unacct_size(info->flags, inode->i_size);
607                 inode->i_size = 0;
608                 shmem_truncate(inode);
609         }
610         BUG_ON(inode->i_blocks);
611         spin_lock(&sbinfo->stat_lock);
612         sbinfo->free_inodes++;
613         spin_unlock(&sbinfo->stat_lock);
614         clear_inode(inode);
615 }
616
617 static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
618 {
619         swp_entry_t *ptr;
620
621         for (ptr = dir; ptr < edir; ptr++) {
622                 if (ptr->val == entry.val)
623                         return ptr - dir;
624         }
625         return -1;
626 }
627
628 static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
629 {
630         struct inode *inode;
631         unsigned long idx;
632         unsigned long size;
633         unsigned long limit;
634         unsigned long stage;
635         struct page **dir;
636         struct page *subdir;
637         swp_entry_t *ptr;
638         int offset;
639
640         idx = 0;
641         ptr = info->i_direct;
642         spin_lock(&info->lock);
643         limit = info->next_index;
644         size = limit;
645         if (size > SHMEM_NR_DIRECT)
646                 size = SHMEM_NR_DIRECT;
647         offset = shmem_find_swp(entry, ptr, ptr+size);
648         if (offset >= 0)
649                 goto found;
650         if (!info->i_indirect)
651                 goto lost2;
652         /* we might be racing with shmem_truncate */
653         if (limit <= SHMEM_NR_DIRECT)
654                 goto lost2;
655
656         dir = shmem_dir_map(info->i_indirect);
657         stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
658
659         for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
660                 if (unlikely(idx == stage)) {
661                         shmem_dir_unmap(dir-1);
662                         dir = shmem_dir_map(info->i_indirect) +
663                             ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
664                         while (!*dir) {
665                                 dir++;
666                                 idx += ENTRIES_PER_PAGEPAGE;
667                                 if (idx >= limit)
668                                         goto lost1;
669                         }
670                         stage = idx + ENTRIES_PER_PAGEPAGE;
671                         subdir = *dir;
672                         shmem_dir_unmap(dir);
673                         dir = shmem_dir_map(subdir);
674                 }
675                 subdir = *dir;
676                 if (subdir && subdir->nr_swapped) {
677                         ptr = shmem_swp_map(subdir);
678                         size = limit - idx;
679                         if (size > ENTRIES_PER_PAGE)
680                                 size = ENTRIES_PER_PAGE;
681                         offset = shmem_find_swp(entry, ptr, ptr+size);
682                         if (offset >= 0) {
683                                 shmem_dir_unmap(dir);
684                                 goto found;
685                         }
686                         shmem_swp_unmap(ptr);
687                 }
688         }
689 lost1:
690         shmem_dir_unmap(dir-1);
691 lost2:
692         spin_unlock(&info->lock);
693         return 0;
694 found:
695         idx += offset;
696         inode = &info->vfs_inode;
697         if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
698                 info->flags |= SHMEM_PAGEIN;
699                 shmem_swp_set(info, ptr + offset, 0);
700         }
701         shmem_swp_unmap(ptr);
702         spin_unlock(&info->lock);
703         /*
704          * Decrement swap count even when the entry is left behind:
705          * try_to_unuse will skip over mms, then reincrement count.
706          */
707         swap_free(entry);
708         return 1;
709 }
710
711 /*
712  * shmem_unuse() search for an eventually swapped out shmem page.
713  */
714 int shmem_unuse(swp_entry_t entry, struct page *page)
715 {
716         struct list_head *p;
717         struct shmem_inode_info *info;
718         int found = 0;
719
720         spin_lock(&shmem_ilock);
721         list_for_each(p, &shmem_inodes) {
722                 info = list_entry(p, struct shmem_inode_info, list);
723
724                 if (info->swapped && shmem_unuse_inode(info, entry, page)) {
725                         /* move head to start search for next from here */
726                         list_move_tail(&shmem_inodes, &info->list);
727                         found = 1;
728                         break;
729                 }
730         }
731         spin_unlock(&shmem_ilock);
732         return found;
733 }
734
735 /*
736  * Move the page from the page cache to the swap cache.
737  */
738 static int shmem_writepage(struct page *page, struct writeback_control *wbc)
739 {
740         struct shmem_inode_info *info;
741         swp_entry_t *entry, swap;
742         struct address_space *mapping;
743         unsigned long index;
744         struct inode *inode;
745
746         BUG_ON(!PageLocked(page));
747         BUG_ON(page_mapped(page));
748
749         mapping = page->mapping;
750         index = page->index;
751         inode = mapping->host;
752         info = SHMEM_I(inode);
753         if (info->flags & VM_LOCKED)
754                 goto redirty;
755         swap = get_swap_page();
756         if (!swap.val)
757                 goto redirty;
758
759         spin_lock(&info->lock);
760         shmem_recalc_inode(inode);
761         if (index >= info->next_index) {
762                 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
763                 goto unlock;
764         }
765         entry = shmem_swp_entry(info, index, NULL);
766         BUG_ON(!entry);
767         BUG_ON(entry->val);
768
769         if (move_to_swap_cache(page, swap) == 0) {
770                 shmem_swp_set(info, entry, swap.val);
771                 shmem_swp_unmap(entry);
772                 spin_unlock(&info->lock);
773                 unlock_page(page);
774                 return 0;
775         }
776
777         shmem_swp_unmap(entry);
778 unlock:
779         spin_unlock(&info->lock);
780         swap_free(swap);
781 redirty:
782         set_page_dirty(page);
783         return WRITEPAGE_ACTIVATE;      /* Return with the page locked */
784 }
785
786 /*
787  * shmem_getpage - either get the page from swap or allocate a new one
788  *
789  * If we allocate a new one we do not mark it dirty. That's up to the
790  * vm. If we swap it in we mark it dirty since we also free the swap
791  * entry since a page cannot live in both the swap and page cache
792  */
793 static int shmem_getpage(struct inode *inode, unsigned long idx, struct page **pagep, enum sgp_type sgp, int *type)
794 {
795         struct address_space *mapping = inode->i_mapping;
796         struct shmem_inode_info *info = SHMEM_I(inode);
797         struct shmem_sb_info *sbinfo;
798         struct page *filepage = *pagep;
799         struct page *swappage;
800         swp_entry_t *entry;
801         swp_entry_t swap;
802         int error, majmin = VM_FAULT_MINOR;
803
804         if (idx >= SHMEM_MAX_INDEX)
805                 return -EFBIG;
806         /*
807          * Normally, filepage is NULL on entry, and either found
808          * uptodate immediately, or allocated and zeroed, or read
809          * in under swappage, which is then assigned to filepage.
810          * But shmem_prepare_write passes in a locked filepage,
811          * which may be found not uptodate by other callers too,
812          * and may need to be copied from the swappage read in.
813          */
814 repeat:
815         if (!filepage)
816                 filepage = find_lock_page(mapping, idx);
817         if (filepage && PageUptodate(filepage))
818                 goto done;
819         error = 0;
820         if (sgp == SGP_QUICK)
821                 goto failed;
822
823         spin_lock(&info->lock);
824         shmem_recalc_inode(inode);
825         entry = shmem_swp_alloc(info, idx, sgp);
826         if (IS_ERR(entry)) {
827                 spin_unlock(&info->lock);
828                 error = PTR_ERR(entry);
829                 goto failed;
830         }
831         swap = *entry;
832
833         if (swap.val) {
834                 /* Look it up and read it in.. */
835                 swappage = lookup_swap_cache(swap);
836                 if (!swappage) {
837                         shmem_swp_unmap(entry);
838                         spin_unlock(&info->lock);
839                         /* here we actually do the io */
840                         if (majmin == VM_FAULT_MINOR && type)
841                                 inc_page_state(pgmajfault);
842                         majmin = VM_FAULT_MAJOR;
843                         swapin_readahead(swap);
844                         swappage = read_swap_cache_async(swap);
845                         if (!swappage) {
846                                 spin_lock(&info->lock);
847                                 entry = shmem_swp_alloc(info, idx, sgp);
848                                 if (IS_ERR(entry))
849                                         error = PTR_ERR(entry);
850                                 else {
851                                         if (entry->val == swap.val)
852                                                 error = -ENOMEM;
853                                         shmem_swp_unmap(entry);
854                                 }
855                                 spin_unlock(&info->lock);
856                                 if (error)
857                                         goto failed;
858                                 goto repeat;
859                         }
860                         wait_on_page_locked(swappage);
861                         page_cache_release(swappage);
862                         goto repeat;
863                 }
864
865                 /* We have to do this with page locked to prevent races */
866                 if (TestSetPageLocked(swappage)) {
867                         shmem_swp_unmap(entry);
868                         spin_unlock(&info->lock);
869                         wait_on_page_locked(swappage);
870                         page_cache_release(swappage);
871                         goto repeat;
872                 }
873                 if (PageWriteback(swappage)) {
874                         shmem_swp_unmap(entry);
875                         spin_unlock(&info->lock);
876                         wait_on_page_writeback(swappage);
877                         unlock_page(swappage);
878                         page_cache_release(swappage);
879                         goto repeat;
880                 }
881                 if (!PageUptodate(swappage)) {
882                         shmem_swp_unmap(entry);
883                         spin_unlock(&info->lock);
884                         unlock_page(swappage);
885                         page_cache_release(swappage);
886                         error = -EIO;
887                         goto failed;
888                 }
889
890                 if (filepage) {
891                         shmem_swp_set(info, entry, 0);
892                         shmem_swp_unmap(entry);
893                         delete_from_swap_cache(swappage);
894                         spin_unlock(&info->lock);
895                         copy_highpage(filepage, swappage);
896                         unlock_page(swappage);
897                         page_cache_release(swappage);
898                         flush_dcache_page(filepage);
899                         SetPageUptodate(filepage);
900                         set_page_dirty(filepage);
901                         swap_free(swap);
902                 } else if (!(error = move_from_swap_cache(
903                                 swappage, idx, mapping))) {
904                         info->flags |= SHMEM_PAGEIN;
905                         shmem_swp_set(info, entry, 0);
906                         shmem_swp_unmap(entry);
907                         spin_unlock(&info->lock);
908                         filepage = swappage;
909                         swap_free(swap);
910                 } else {
911                         shmem_swp_unmap(entry);
912                         spin_unlock(&info->lock);
913                         unlock_page(swappage);
914                         page_cache_release(swappage);
915                         if (error == -ENOMEM) {
916                                 /* let kswapd refresh zone for GFP_ATOMICs */
917                                 blk_congestion_wait(WRITE, HZ/50);
918                         }
919                         goto repeat;
920                 }
921         } else if (sgp == SGP_READ && !filepage) {
922                 shmem_swp_unmap(entry);
923                 filepage = find_get_page(mapping, idx);
924                 if (filepage &&
925                     (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
926                         spin_unlock(&info->lock);
927                         wait_on_page_locked(filepage);
928                         page_cache_release(filepage);
929                         filepage = NULL;
930                         goto repeat;
931                 }
932                 spin_unlock(&info->lock);
933         } else {
934                 shmem_swp_unmap(entry);
935                 sbinfo = SHMEM_SB(inode->i_sb);
936                 spin_lock(&sbinfo->stat_lock);
937                 if (sbinfo->free_blocks == 0 || shmem_acct_block(info->flags)) {
938                         spin_unlock(&sbinfo->stat_lock);
939                         spin_unlock(&info->lock);
940                         error = -ENOSPC;
941                         goto failed;
942                 }
943                 sbinfo->free_blocks--;
944                 inode->i_blocks += BLOCKS_PER_PAGE;
945                 spin_unlock(&sbinfo->stat_lock);
946
947                 if (!filepage) {
948                         spin_unlock(&info->lock);
949                         filepage = page_cache_alloc(mapping);
950                         if (!filepage) {
951                                 shmem_unacct_blocks(info->flags, 1);
952                                 shmem_free_block(inode);
953                                 error = -ENOMEM;
954                                 goto failed;
955                         }
956
957                         spin_lock(&info->lock);
958                         entry = shmem_swp_alloc(info, idx, sgp);
959                         if (IS_ERR(entry))
960                                 error = PTR_ERR(entry);
961                         else {
962                                 swap = *entry;
963                                 shmem_swp_unmap(entry);
964                         }
965                         if (error || swap.val || 0 != add_to_page_cache_lru(
966                                         filepage, mapping, idx, GFP_ATOMIC)) {
967                                 spin_unlock(&info->lock);
968                                 page_cache_release(filepage);
969                                 shmem_unacct_blocks(info->flags, 1);
970                                 shmem_free_block(inode);
971                                 filepage = NULL;
972                                 if (error)
973                                         goto failed;
974                                 goto repeat;
975                         }
976                         info->flags |= SHMEM_PAGEIN;
977                 }
978
979                 info->alloced++;
980                 spin_unlock(&info->lock);
981                 clear_highpage(filepage);
982                 flush_dcache_page(filepage);
983                 SetPageUptodate(filepage);
984         }
985 done:
986         if (!*pagep) {
987                 if (filepage) {
988                         unlock_page(filepage);
989                         *pagep = filepage;
990                 } else
991                         *pagep = ZERO_PAGE(0);
992         }
993         if (type)
994                 *type = majmin;
995         return 0;
996
997 failed:
998         if (*pagep != filepage) {
999                 unlock_page(filepage);
1000                 page_cache_release(filepage);
1001         }
1002         return error;
1003 }
1004
1005 struct page *shmem_nopage(struct vm_area_struct *vma, unsigned long address, int *type)
1006 {
1007         struct inode *inode = vma->vm_file->f_dentry->d_inode;
1008         struct page *page = NULL;
1009         unsigned long idx;
1010         int error;
1011
1012         idx = (address - vma->vm_start) >> PAGE_SHIFT;
1013         idx += vma->vm_pgoff;
1014         idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
1015
1016         error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
1017         if (error)
1018                 return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
1019
1020         mark_page_accessed(page);
1021         return page;
1022 }
1023
1024 static int shmem_populate(struct vm_area_struct *vma,
1025         unsigned long addr, unsigned long len,
1026         pgprot_t prot, unsigned long pgoff, int nonblock)
1027 {
1028         struct inode *inode = vma->vm_file->f_dentry->d_inode;
1029         struct mm_struct *mm = vma->vm_mm;
1030         enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
1031         unsigned long size;
1032
1033         size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1034         if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
1035                 return -EINVAL;
1036
1037         while ((long) len > 0) {
1038                 struct page *page = NULL;
1039                 int err;
1040                 /*
1041                  * Will need changing if PAGE_CACHE_SIZE != PAGE_SIZE
1042                  */
1043                 err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
1044                 if (err)
1045                         return err;
1046                 if (page) {
1047                         mark_page_accessed(page);
1048                         err = install_page(mm, vma, addr, page, prot);
1049                         if (err) {
1050                                 page_cache_release(page);
1051                                 return err;
1052                         }
1053                 } else if (nonblock) {
1054                         /*
1055                          * If a nonlinear mapping then store the file page
1056                          * offset in the pte.
1057                          */
1058                         if (pgoff != linear_page_index(vma, addr)) {
1059                                 err = install_file_pte(mm, vma, addr, pgoff, prot);
1060                                 if (err)
1061                                         return err;
1062                         }
1063                 }
1064
1065                 len -= PAGE_SIZE;
1066                 addr += PAGE_SIZE;
1067                 pgoff++;
1068         }
1069         return 0;
1070 }
1071
1072 void shmem_lock(struct file *file, int lock)
1073 {
1074         struct inode *inode = file->f_dentry->d_inode;
1075         struct shmem_inode_info *info = SHMEM_I(inode);
1076
1077         spin_lock(&info->lock);
1078         if (lock)
1079                 info->flags |= VM_LOCKED;
1080         else
1081                 info->flags &= ~VM_LOCKED;
1082         spin_unlock(&info->lock);
1083 }
1084
1085 static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1086 {
1087         file_accessed(file);
1088         vma->vm_ops = &shmem_vm_ops;
1089         return 0;
1090 }
1091
1092 static struct inode *
1093 shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1094 {
1095         struct inode *inode;
1096         struct shmem_inode_info *info;
1097         struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1098
1099         spin_lock(&sbinfo->stat_lock);
1100         if (!sbinfo->free_inodes) {
1101                 spin_unlock(&sbinfo->stat_lock);
1102                 return NULL;
1103         }
1104         sbinfo->free_inodes--;
1105         spin_unlock(&sbinfo->stat_lock);
1106
1107         inode = new_inode(sb);
1108         if (inode) {
1109                 inode->i_mode = mode;
1110                 inode->i_uid = current->fsuid;
1111                 inode->i_gid = current->fsgid;
1112                 inode->i_blksize = PAGE_CACHE_SIZE;
1113                 inode->i_blocks = 0;
1114                 inode->i_mapping->a_ops = &shmem_aops;
1115                 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1116                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1117                 info = SHMEM_I(inode);
1118                 memset(info, 0, (char *)inode - (char *)info);
1119                 spin_lock_init(&info->lock);
1120                 switch (mode & S_IFMT) {
1121                 default:
1122                         init_special_inode(inode, mode, dev);
1123                         break;
1124                 case S_IFREG:
1125                         inode->i_op = &shmem_inode_operations;
1126                         inode->i_fop = &shmem_file_operations;
1127                         spin_lock(&shmem_ilock);
1128                         list_add_tail(&info->list, &shmem_inodes);
1129                         spin_unlock(&shmem_ilock);
1130                         break;
1131                 case S_IFDIR:
1132                         inode->i_nlink++;
1133                         /* Some things misbehave if size == 0 on a directory */
1134                         inode->i_size = 2 * BOGO_DIRENT_SIZE;
1135                         inode->i_op = &shmem_dir_inode_operations;
1136                         inode->i_fop = &simple_dir_operations;
1137                         break;
1138                 case S_IFLNK:
1139                         break;
1140                 }
1141         }
1142         return inode;
1143 }
1144
1145 static int shmem_set_size(struct shmem_sb_info *info,
1146                           unsigned long max_blocks, unsigned long max_inodes)
1147 {
1148         int error;
1149         unsigned long blocks, inodes;
1150
1151         spin_lock(&info->stat_lock);
1152         blocks = info->max_blocks - info->free_blocks;
1153         inodes = info->max_inodes - info->free_inodes;
1154         error = -EINVAL;
1155         if (max_blocks < blocks)
1156                 goto out;
1157         if (max_inodes < inodes)
1158                 goto out;
1159         error = 0;
1160         info->max_blocks  = max_blocks;
1161         info->free_blocks = max_blocks - blocks;
1162         info->max_inodes  = max_inodes;
1163         info->free_inodes = max_inodes - inodes;
1164 out:
1165         spin_unlock(&info->stat_lock);
1166         return error;
1167 }
1168
1169 #ifdef CONFIG_TMPFS
1170
1171 static struct inode_operations shmem_symlink_inode_operations;
1172 static struct inode_operations shmem_symlink_inline_operations;
1173
1174 /*
1175  * Normally tmpfs makes no use of shmem_prepare_write, but it
1176  * lets a tmpfs file be used read-write below the loop driver.
1177  */
1178 static int
1179 shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1180 {
1181         struct inode *inode = page->mapping->host;
1182         return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
1183 }
1184
1185 static ssize_t
1186 shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1187 {
1188         struct inode    *inode = file->f_dentry->d_inode;
1189         loff_t          pos;
1190         unsigned long   written;
1191         int             err;
1192
1193         if ((ssize_t) count < 0)
1194                 return -EINVAL;
1195
1196         if (!access_ok(VERIFY_READ, buf, count))
1197                 return -EFAULT;
1198
1199         down(&inode->i_sem);
1200
1201         pos = *ppos;
1202         written = 0;
1203
1204         err = generic_write_checks(file, &pos, &count, 0);
1205         if (err || !count)
1206                 goto out;
1207
1208         err = remove_suid(file->f_dentry);
1209         if (err)
1210                 goto out;
1211
1212         inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1213
1214         do {
1215                 struct page *page = NULL;
1216                 unsigned long bytes, index, offset;
1217                 char *kaddr;
1218                 int left;
1219
1220                 offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
1221                 index = pos >> PAGE_CACHE_SHIFT;
1222                 bytes = PAGE_CACHE_SIZE - offset;
1223                 if (bytes > count)
1224                         bytes = count;
1225
1226                 /*
1227                  * We don't hold page lock across copy from user -
1228                  * what would it guard against? - so no deadlock here.
1229                  * But it still may be a good idea to prefault below.
1230                  */
1231
1232                 err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
1233                 if (err)
1234                         break;
1235
1236                 left = bytes;
1237                 if (PageHighMem(page)) {
1238                         volatile unsigned char dummy;
1239                         __get_user(dummy, buf);
1240                         __get_user(dummy, buf + bytes - 1);
1241
1242                         kaddr = kmap_atomic(page, KM_USER0);
1243                         left = __copy_from_user(kaddr + offset, buf, bytes);
1244                         kunmap_atomic(kaddr, KM_USER0);
1245                 }
1246                 if (left) {
1247                         kaddr = kmap(page);
1248                         left = __copy_from_user(kaddr + offset, buf, bytes);
1249                         kunmap(page);
1250                 }
1251
1252                 written += bytes;
1253                 count -= bytes;
1254                 pos += bytes;
1255                 buf += bytes;
1256                 if (pos > inode->i_size)
1257                         i_size_write(inode, pos);
1258
1259                 flush_dcache_page(page);
1260                 set_page_dirty(page);
1261                 mark_page_accessed(page);
1262                 page_cache_release(page);
1263
1264                 if (left) {
1265                         pos -= left;
1266                         written -= left;
1267                         err = -EFAULT;
1268                         break;
1269                 }
1270
1271                 /*
1272                  * Our dirty pages are not counted in nr_dirty,
1273                  * and we do not attempt to balance dirty pages.
1274                  */
1275
1276                 cond_resched();
1277         } while (count);
1278
1279         *ppos = pos;
1280         if (written)
1281                 err = written;
1282 out:
1283         up(&inode->i_sem);
1284         return err;
1285 }
1286
1287 static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1288 {
1289         struct inode *inode = filp->f_dentry->d_inode;
1290         struct address_space *mapping = inode->i_mapping;
1291         unsigned long index, offset;
1292
1293         index = *ppos >> PAGE_CACHE_SHIFT;
1294         offset = *ppos & ~PAGE_CACHE_MASK;
1295
1296         for (;;) {
1297                 struct page *page = NULL;
1298                 unsigned long end_index, nr, ret;
1299                 loff_t i_size = i_size_read(inode);
1300
1301                 end_index = i_size >> PAGE_CACHE_SHIFT;
1302                 if (index > end_index)
1303                         break;
1304                 if (index == end_index) {
1305                         nr = i_size & ~PAGE_CACHE_MASK;
1306                         if (nr <= offset)
1307                                 break;
1308                 }
1309
1310                 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1311                 if (desc->error) {
1312                         if (desc->error == -EINVAL)
1313                                 desc->error = 0;
1314                         break;
1315                 }
1316
1317                 /*
1318                  * We must evaluate after, since reads (unlike writes)
1319                  * are called without i_sem protection against truncate
1320                  */
1321                 nr = PAGE_CACHE_SIZE;
1322                 i_size = i_size_read(inode);
1323                 end_index = i_size >> PAGE_CACHE_SHIFT;
1324                 if (index == end_index) {
1325                         nr = i_size & ~PAGE_CACHE_MASK;
1326                         if (nr <= offset) {
1327                                 page_cache_release(page);
1328                                 break;
1329                         }
1330                 }
1331                 nr -= offset;
1332
1333                 if (page != ZERO_PAGE(0)) {
1334                         /*
1335                          * If users can be writing to this page using arbitrary
1336                          * virtual addresses, take care about potential aliasing
1337                          * before reading the page on the kernel side.
1338                          */
1339                         if (mapping_writably_mapped(mapping))
1340                                 flush_dcache_page(page);
1341                         /*
1342                          * Mark the page accessed if we read the beginning.
1343                          */
1344                         if (!offset)
1345                                 mark_page_accessed(page);
1346                 }
1347
1348                 /*
1349                  * Ok, we have the page, and it's up-to-date, so
1350                  * now we can copy it to user space...
1351                  *
1352                  * The actor routine returns how many bytes were actually used..
1353                  * NOTE! This may not be the same as how much of a user buffer
1354                  * we filled up (we may be padding etc), so we can only update
1355                  * "pos" here (the actor routine has to update the user buffer
1356                  * pointers and the remaining count).
1357                  */
1358                 ret = actor(desc, page, offset, nr);
1359                 offset += ret;
1360                 index += offset >> PAGE_CACHE_SHIFT;
1361                 offset &= ~PAGE_CACHE_MASK;
1362
1363                 page_cache_release(page);
1364                 if (ret != nr || !desc->count)
1365                         break;
1366
1367                 cond_resched();
1368         }
1369
1370         *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1371         file_accessed(filp);
1372 }
1373
1374 static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1375 {
1376         read_descriptor_t desc;
1377
1378         if ((ssize_t) count < 0)
1379                 return -EINVAL;
1380         if (!access_ok(VERIFY_WRITE, buf, count))
1381                 return -EFAULT;
1382         if (!count)
1383                 return 0;
1384
1385         desc.written = 0;
1386         desc.count = count;
1387         desc.buf = buf;
1388         desc.error = 0;
1389
1390         do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1391         if (desc.written)
1392                 return desc.written;
1393         return desc.error;
1394 }
1395
1396 static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
1397                          size_t count, read_actor_t actor, void __user *target)
1398 {
1399         read_descriptor_t desc;
1400
1401         if (!count)
1402                 return 0;
1403
1404         desc.written = 0;
1405         desc.count = count;
1406         desc.buf = target;
1407         desc.error = 0;
1408
1409         do_shmem_file_read(in_file, ppos, &desc, actor);
1410         if (desc.written)
1411                 return desc.written;
1412         return desc.error;
1413 }
1414
1415 static int shmem_statfs(struct super_block *sb, struct kstatfs *buf)
1416 {
1417         struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1418
1419         buf->f_type = TMPFS_MAGIC;
1420         buf->f_bsize = PAGE_CACHE_SIZE;
1421         spin_lock(&sbinfo->stat_lock);
1422         buf->f_blocks = sbinfo->max_blocks;
1423         buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1424         buf->f_files = sbinfo->max_inodes;
1425         buf->f_ffree = sbinfo->free_inodes;
1426         spin_unlock(&sbinfo->stat_lock);
1427         buf->f_namelen = NAME_MAX;
1428         return 0;
1429 }
1430
1431 /*
1432  * File creation. Allocate an inode, and we're done..
1433  */
1434 static int
1435 shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1436 {
1437         struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1438         int error = -ENOSPC;
1439
1440         if (inode) {
1441                 if (dir->i_mode & S_ISGID) {
1442                         inode->i_gid = dir->i_gid;
1443                         if (S_ISDIR(mode))
1444                                 inode->i_mode |= S_ISGID;
1445                 }
1446                 dir->i_size += BOGO_DIRENT_SIZE;
1447                 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1448                 d_instantiate(dentry, inode);
1449                 dget(dentry); /* Extra count - pin the dentry in core */
1450                 error = 0;
1451         }
1452         return error;
1453 }
1454
1455 static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1456 {
1457         int error;
1458
1459         if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1460                 return error;
1461         dir->i_nlink++;
1462         return 0;
1463 }
1464
1465 static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1466                 struct nameidata *nd)
1467 {
1468         return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1469 }
1470
1471 /*
1472  * Link a file..
1473  */
1474 static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1475 {
1476         struct inode *inode = old_dentry->d_inode;
1477
1478         dir->i_size += BOGO_DIRENT_SIZE;
1479         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1480         inode->i_nlink++;
1481         atomic_inc(&inode->i_count);    /* New dentry reference */
1482         dget(dentry);           /* Extra pinning count for the created dentry */
1483         d_instantiate(dentry, inode);
1484         return 0;
1485 }
1486
1487 static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1488 {
1489         struct inode *inode = dentry->d_inode;
1490
1491         dir->i_size -= BOGO_DIRENT_SIZE;
1492         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1493         inode->i_nlink--;
1494         dput(dentry);   /* Undo the count from "create" - this does all the work */
1495         return 0;
1496 }
1497
1498 static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1499 {
1500         if (!simple_empty(dentry))
1501                 return -ENOTEMPTY;
1502
1503         dir->i_nlink--;
1504         return shmem_unlink(dir, dentry);
1505 }
1506
1507 /*
1508  * The VFS layer already does all the dentry stuff for rename,
1509  * we just have to decrement the usage count for the target if
1510  * it exists so that the VFS layer correctly free's it when it
1511  * gets overwritten.
1512  */
1513 static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1514 {
1515         struct inode *inode = old_dentry->d_inode;
1516         int they_are_dirs = S_ISDIR(inode->i_mode);
1517
1518         if (!simple_empty(new_dentry))
1519                 return -ENOTEMPTY;
1520
1521         if (new_dentry->d_inode) {
1522                 (void) shmem_unlink(new_dir, new_dentry);
1523                 if (they_are_dirs)
1524                         old_dir->i_nlink--;
1525         } else if (they_are_dirs) {
1526                 old_dir->i_nlink--;
1527                 new_dir->i_nlink++;
1528         }
1529
1530         old_dir->i_size -= BOGO_DIRENT_SIZE;
1531         new_dir->i_size += BOGO_DIRENT_SIZE;
1532         old_dir->i_ctime = old_dir->i_mtime =
1533         new_dir->i_ctime = new_dir->i_mtime =
1534         inode->i_ctime = CURRENT_TIME;
1535         return 0;
1536 }
1537
1538 static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1539 {
1540         int error;
1541         int len;
1542         struct inode *inode;
1543         struct page *page = NULL;
1544         char *kaddr;
1545         struct shmem_inode_info *info;
1546
1547         len = strlen(symname) + 1;
1548         if (len > PAGE_CACHE_SIZE)
1549                 return -ENAMETOOLONG;
1550
1551         inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1552         if (!inode)
1553                 return -ENOSPC;
1554
1555         info = SHMEM_I(inode);
1556         inode->i_size = len-1;
1557         if (len <= (char *)inode - (char *)info) {
1558                 /* do it inline */
1559                 memcpy(info, symname, len);
1560                 inode->i_op = &shmem_symlink_inline_operations;
1561         } else {
1562                 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1563                 if (error) {
1564                         iput(inode);
1565                         return error;
1566                 }
1567                 inode->i_op = &shmem_symlink_inode_operations;
1568                 spin_lock(&shmem_ilock);
1569                 list_add_tail(&info->list, &shmem_inodes);
1570                 spin_unlock(&shmem_ilock);
1571                 kaddr = kmap_atomic(page, KM_USER0);
1572                 memcpy(kaddr, symname, len);
1573                 kunmap_atomic(kaddr, KM_USER0);
1574                 set_page_dirty(page);
1575                 page_cache_release(page);
1576         }
1577         if (dir->i_mode & S_ISGID)
1578                 inode->i_gid = dir->i_gid;
1579         dir->i_size += BOGO_DIRENT_SIZE;
1580         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1581         d_instantiate(dentry, inode);
1582         dget(dentry);
1583         return 0;
1584 }
1585
1586 static int shmem_readlink_inline(struct dentry *dentry, char __user *buffer, int buflen)
1587 {
1588         return vfs_readlink(dentry, buffer, buflen, (const char *)SHMEM_I(dentry->d_inode));
1589 }
1590
1591 static int shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1592 {
1593         return vfs_follow_link(nd, (const char *)SHMEM_I(dentry->d_inode));
1594 }
1595
1596 static int shmem_readlink(struct dentry *dentry, char __user *buffer, int buflen)
1597 {
1598         struct page *page = NULL;
1599         int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1600         if (res)
1601                 return res;
1602         res = vfs_readlink(dentry, buffer, buflen, kmap(page));
1603         kunmap(page);
1604         mark_page_accessed(page);
1605         page_cache_release(page);
1606         return res;
1607 }
1608
1609 static int shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1610 {
1611         struct page *page = NULL;
1612         int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1613         if (res)
1614                 return res;
1615         res = vfs_follow_link(nd, kmap(page));
1616         kunmap(page);
1617         mark_page_accessed(page);
1618         page_cache_release(page);
1619         return res;
1620 }
1621
1622 static struct inode_operations shmem_symlink_inline_operations = {
1623         .readlink       = shmem_readlink_inline,
1624         .follow_link    = shmem_follow_link_inline,
1625 };
1626
1627 static struct inode_operations shmem_symlink_inode_operations = {
1628         .truncate       = shmem_truncate,
1629         .readlink       = shmem_readlink,
1630         .follow_link    = shmem_follow_link,
1631 };
1632
1633 static int shmem_parse_options(char *options, int *mode, uid_t *uid, gid_t *gid, unsigned long *blocks, unsigned long *inodes)
1634 {
1635         char *this_char, *value, *rest;
1636
1637         while ((this_char = strsep(&options, ",")) != NULL) {
1638                 if (!*this_char)
1639                         continue;
1640                 if ((value = strchr(this_char,'=')) != NULL) {
1641                         *value++ = 0;
1642                 } else {
1643                         printk(KERN_ERR
1644                             "tmpfs: No value for mount option '%s'\n",
1645                             this_char);
1646                         return 1;
1647                 }
1648
1649                 if (!strcmp(this_char,"size")) {
1650                         unsigned long long size;
1651                         size = memparse(value,&rest);
1652                         if (*rest == '%') {
1653                                 size <<= PAGE_SHIFT;
1654                                 size *= totalram_pages;
1655                                 do_div(size, 100);
1656                                 rest++;
1657                         }
1658                         if (*rest)
1659                                 goto bad_val;
1660                         *blocks = size >> PAGE_CACHE_SHIFT;
1661                 } else if (!strcmp(this_char,"nr_blocks")) {
1662                         *blocks = memparse(value,&rest);
1663                         if (*rest)
1664                                 goto bad_val;
1665                 } else if (!strcmp(this_char,"nr_inodes")) {
1666                         *inodes = memparse(value,&rest);
1667                         if (*rest)
1668                                 goto bad_val;
1669                 } else if (!strcmp(this_char,"mode")) {
1670                         if (!mode)
1671                                 continue;
1672                         *mode = simple_strtoul(value,&rest,8);
1673                         if (*rest)
1674                                 goto bad_val;
1675                 } else if (!strcmp(this_char,"uid")) {
1676                         if (!uid)
1677                                 continue;
1678                         *uid = simple_strtoul(value,&rest,0);
1679                         if (*rest)
1680                                 goto bad_val;
1681                 } else if (!strcmp(this_char,"gid")) {
1682                         if (!gid)
1683                                 continue;
1684                         *gid = simple_strtoul(value,&rest,0);
1685                         if (*rest)
1686                                 goto bad_val;
1687                 } else {
1688                         printk(KERN_ERR "tmpfs: Bad mount option %s\n",
1689                                this_char);
1690                         return 1;
1691                 }
1692         }
1693         return 0;
1694
1695 bad_val:
1696         printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
1697                value, this_char);
1698         return 1;
1699
1700 }
1701
1702 static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
1703 {
1704         struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1705         unsigned long max_blocks = sbinfo->max_blocks;
1706         unsigned long max_inodes = sbinfo->max_inodes;
1707
1708         if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks, &max_inodes))
1709                 return -EINVAL;
1710         return shmem_set_size(sbinfo, max_blocks, max_inodes);
1711 }
1712 #endif
1713
1714 static int shmem_fill_super(struct super_block *sb,
1715                             void *data, int silent)
1716 {
1717         struct inode *inode;
1718         struct dentry *root;
1719         unsigned long blocks, inodes;
1720         int mode   = S_IRWXUGO | S_ISVTX;
1721         uid_t uid = current->fsuid;
1722         gid_t gid = current->fsgid;
1723         struct shmem_sb_info *sbinfo;
1724         int err = -ENOMEM;
1725
1726         sbinfo = kmalloc(sizeof(struct shmem_sb_info), GFP_KERNEL);
1727         if (!sbinfo)
1728                 return -ENOMEM;
1729         sb->s_fs_info = sbinfo;
1730         memset(sbinfo, 0, sizeof(struct shmem_sb_info));
1731
1732         /*
1733          * Per default we only allow half of the physical ram per
1734          * tmpfs instance
1735          */
1736         blocks = inodes = totalram_pages / 2;
1737
1738 #ifdef CONFIG_TMPFS
1739         if (shmem_parse_options(data, &mode, &uid, &gid, &blocks, &inodes)) {
1740                 err = -EINVAL;
1741                 goto failed;
1742         }
1743 #else
1744         sb->s_flags |= MS_NOUSER;
1745 #endif
1746
1747         spin_lock_init(&sbinfo->stat_lock);
1748         sbinfo->max_blocks = blocks;
1749         sbinfo->free_blocks = blocks;
1750         sbinfo->max_inodes = inodes;
1751         sbinfo->free_inodes = inodes;
1752         sb->s_maxbytes = SHMEM_MAX_BYTES;
1753         sb->s_blocksize = PAGE_CACHE_SIZE;
1754         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1755         sb->s_magic = TMPFS_MAGIC;
1756         sb->s_op = &shmem_ops;
1757         inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
1758         if (!inode)
1759                 goto failed;
1760         inode->i_uid = uid;
1761         inode->i_gid = gid;
1762         root = d_alloc_root(inode);
1763         if (!root)
1764                 goto failed_iput;
1765         sb->s_root = root;
1766         return 0;
1767
1768 failed_iput:
1769         iput(inode);
1770 failed:
1771         kfree(sbinfo);
1772         sb->s_fs_info = NULL;
1773         return err;
1774 }
1775
1776 static void shmem_put_super(struct super_block *sb)
1777 {
1778         kfree(sb->s_fs_info);
1779         sb->s_fs_info = NULL;
1780 }
1781
1782 static kmem_cache_t *shmem_inode_cachep;
1783
1784 static struct inode *shmem_alloc_inode(struct super_block *sb)
1785 {
1786         struct shmem_inode_info *p;
1787         p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, SLAB_KERNEL);
1788         if (!p)
1789                 return NULL;
1790         return &p->vfs_inode;
1791 }
1792
1793 static void shmem_destroy_inode(struct inode *inode)
1794 {
1795         kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
1796 }
1797
1798 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
1799 {
1800         struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
1801
1802         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1803             SLAB_CTOR_CONSTRUCTOR) {
1804                 inode_init_once(&p->vfs_inode);
1805         }
1806 }
1807
1808 static int init_inodecache(void)
1809 {
1810         shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
1811                                              sizeof(struct shmem_inode_info),
1812                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1813                                              init_once, NULL);
1814         if (shmem_inode_cachep == NULL)
1815                 return -ENOMEM;
1816         return 0;
1817 }
1818
1819 static void destroy_inodecache(void)
1820 {
1821         if (kmem_cache_destroy(shmem_inode_cachep))
1822                 printk(KERN_INFO "shmem_inode_cache: not all structures were freed\n");
1823 }
1824
1825 static struct address_space_operations shmem_aops = {
1826         .writepage      = shmem_writepage,
1827         .set_page_dirty = __set_page_dirty_nobuffers,
1828 #ifdef CONFIG_TMPFS
1829         .prepare_write  = shmem_prepare_write,
1830         .commit_write   = simple_commit_write,
1831 #endif
1832 };
1833
1834 static struct file_operations shmem_file_operations = {
1835         .mmap           = shmem_mmap,
1836 #ifdef CONFIG_TMPFS
1837         .llseek         = generic_file_llseek,
1838         .read           = shmem_file_read,
1839         .write          = shmem_file_write,
1840         .fsync          = simple_sync_file,
1841         .sendfile       = shmem_file_sendfile,
1842 #endif
1843 };
1844
1845 static struct inode_operations shmem_inode_operations = {
1846         .truncate       = shmem_truncate,
1847         .setattr        = shmem_notify_change,
1848 };
1849
1850 static struct inode_operations shmem_dir_inode_operations = {
1851 #ifdef CONFIG_TMPFS
1852         .create         = shmem_create,
1853         .lookup         = simple_lookup,
1854         .link           = shmem_link,
1855         .unlink         = shmem_unlink,
1856         .symlink        = shmem_symlink,
1857         .mkdir          = shmem_mkdir,
1858         .rmdir          = shmem_rmdir,
1859         .mknod          = shmem_mknod,
1860         .rename         = shmem_rename,
1861 #endif
1862 };
1863
1864 static struct super_operations shmem_ops = {
1865         .alloc_inode    = shmem_alloc_inode,
1866         .destroy_inode  = shmem_destroy_inode,
1867 #ifdef CONFIG_TMPFS
1868         .statfs         = shmem_statfs,
1869         .remount_fs     = shmem_remount_fs,
1870 #endif
1871         .delete_inode   = shmem_delete_inode,
1872         .drop_inode     = generic_delete_inode,
1873         .put_super      = shmem_put_super,
1874 };
1875
1876 static struct vm_operations_struct shmem_vm_ops = {
1877         .nopage         = shmem_nopage,
1878         .populate       = shmem_populate,
1879 };
1880
1881 static struct super_block *shmem_get_sb(struct file_system_type *fs_type,
1882         int flags, const char *dev_name, void *data)
1883 {
1884         return get_sb_nodev(fs_type, flags, data, shmem_fill_super);
1885 }
1886
1887 static struct file_system_type tmpfs_fs_type = {
1888         .owner          = THIS_MODULE,
1889         .name           = "tmpfs",
1890         .get_sb         = shmem_get_sb,
1891         .kill_sb        = kill_litter_super,
1892 };
1893 static struct vfsmount *shm_mnt;
1894
1895 static int __init init_tmpfs(void)
1896 {
1897         int error;
1898
1899         error = init_inodecache();
1900         if (error)
1901                 goto out3;
1902
1903         error = register_filesystem(&tmpfs_fs_type);
1904         if (error) {
1905                 printk(KERN_ERR "Could not register tmpfs\n");
1906                 goto out2;
1907         }
1908 #ifdef CONFIG_TMPFS
1909         devfs_mk_dir("shm");
1910 #endif
1911         shm_mnt = kern_mount(&tmpfs_fs_type);
1912         if (IS_ERR(shm_mnt)) {
1913                 error = PTR_ERR(shm_mnt);
1914                 printk(KERN_ERR "Could not kern_mount tmpfs\n");
1915                 goto out1;
1916         }
1917
1918         /* The internal instance should not do size checking */
1919         shmem_set_size(SHMEM_SB(shm_mnt->mnt_sb), ULONG_MAX, ULONG_MAX);
1920         return 0;
1921
1922 out1:
1923         unregister_filesystem(&tmpfs_fs_type);
1924 out2:
1925         destroy_inodecache();
1926 out3:
1927         shm_mnt = ERR_PTR(error);
1928         return error;
1929 }
1930 module_init(init_tmpfs)
1931
1932 /*
1933  * shmem_file_setup - get an unlinked file living in tmpfs
1934  *
1935  * @name: name for dentry (to be seen in /proc/<pid>/maps
1936  * @size: size to be set for the file
1937  *
1938  */
1939 struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
1940 {
1941         int error;
1942         struct file *file;
1943         struct inode *inode;
1944         struct dentry *dentry, *root;
1945         struct qstr this;
1946
1947         if (IS_ERR(shm_mnt))
1948                 return (void *)shm_mnt;
1949
1950         if (size > SHMEM_MAX_BYTES)
1951                 return ERR_PTR(-EINVAL);
1952
1953         if (shmem_acct_size(flags, size))
1954                 return ERR_PTR(-ENOMEM);
1955
1956         error = -ENOMEM;
1957         this.name = name;
1958         this.len = strlen(name);
1959         this.hash = 0; /* will go */
1960         root = shm_mnt->mnt_root;
1961         dentry = d_alloc(root, &this);
1962         if (!dentry)
1963                 goto put_memory;
1964
1965         error = -ENFILE;
1966         file = get_empty_filp();
1967         if (!file)
1968                 goto put_dentry;
1969
1970         error = -ENOSPC;
1971         inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
1972         if (!inode)
1973                 goto close_file;
1974
1975         SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
1976         d_instantiate(dentry, inode);
1977         inode->i_size = size;
1978         inode->i_nlink = 0;     /* It is unlinked */
1979         file->f_vfsmnt = mntget(shm_mnt);
1980         file->f_dentry = dentry;
1981         file->f_mapping = inode->i_mapping;
1982         file->f_op = &shmem_file_operations;
1983         file->f_mode = FMODE_WRITE | FMODE_READ;
1984         return(file);
1985
1986 close_file:
1987         put_filp(file);
1988 put_dentry:
1989         dput(dentry);
1990 put_memory:
1991         shmem_unacct_size(flags, size);
1992         return ERR_PTR(error);
1993 }
1994
1995 /*
1996  * shmem_zero_setup - setup a shared anonymous mapping
1997  *
1998  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
1999  */
2000 int shmem_zero_setup(struct vm_area_struct *vma)
2001 {
2002         struct file *file;
2003         loff_t size = vma->vm_end - vma->vm_start;
2004
2005         file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2006         if (IS_ERR(file))
2007                 return PTR_ERR(file);
2008
2009         if (vma->vm_file)
2010                 fput(vma->vm_file);
2011         vma->vm_file = file;
2012         vma->vm_ops = &shmem_vm_ops;
2013         return 0;
2014 }
2015
2016 EXPORT_SYMBOL(shmem_file_setup);