1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/fs.h>
29#include <linux/xattr.h>
30#include <linux/generic_acl.h>
31#include <linux/mm.h>
32#include <linux/mman.h>
33#include <linux/file.h>
34#include <linux/swap.h>
35#include <linux/pagemap.h>
36#include <linux/string.h>
37#include <linux/slab.h>
38#include <linux/backing-dev.h>
39#include <linux/shmem_fs.h>
40#include <linux/mount.h>
41#include <linux/writeback.h>
42#include <linux/vfs.h>
43#include <linux/blkdev.h>
44#include <linux/security.h>
45#include <linux/swapops.h>
46#include <linux/mempolicy.h>
47#include <linux/namei.h>
48#include <linux/ctype.h>
49#include <linux/migrate.h>
50#include <linux/highmem.h>
51#include <linux/backing-dev.h>
52
53#include <asm/uaccess.h>
54#include <asm/div64.h>
55#include <asm/pgtable.h>
56
57
58#define TMPFS_MAGIC 0x01021994
59
60#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
61#define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)
62#define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
63
64#define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
65#define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT)
66
67#define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
68
69
70#define SHMEM_PAGEIN VM_READ
71#define SHMEM_TRUNCATE VM_WRITE
72
73
74#define LATENCY_LIMIT 64
75
76
77#define BOGO_DIRENT_SIZE 20
78
79
80enum sgp_type {
81 SGP_QUICK,
82 SGP_READ,
83 SGP_CACHE,
84 SGP_WRITE,
85};
86
87static int shmem_getpage(struct inode *inode, unsigned long idx,
88 struct page **pagep, enum sgp_type sgp, int *type);
89
90static inline struct page *shmem_dir_alloc(gfp_t gfp_mask)
91{
92
93
94
95
96
97 return alloc_pages(gfp_mask, PAGE_CACHE_SHIFT-PAGE_SHIFT);
98}
99
100static inline void shmem_dir_free(struct page *page)
101{
102 __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT);
103}
104
105static struct page **shmem_dir_map(struct page *page)
106{
107 return (struct page **)kmap_atomic(page, KM_USER0);
108}
109
110static inline void shmem_dir_unmap(struct page **dir)
111{
112 kunmap_atomic(dir, KM_USER0);
113}
114
115static swp_entry_t *shmem_swp_map(struct page *page)
116{
117 return (swp_entry_t *)kmap_atomic(page, KM_USER1);
118}
119
120static inline void shmem_swp_balance_unmap(void)
121{
122
123
124
125
126
127
128
129 (void) kmap_atomic(ZERO_PAGE(0), KM_USER1);
130}
131
132static inline void shmem_swp_unmap(swp_entry_t *entry)
133{
134 kunmap_atomic(entry, KM_USER1);
135}
136
137static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
138{
139 return sb->s_fs_info;
140}
141
142
143
144
145
146
147
148static inline int shmem_acct_size(unsigned long flags, loff_t size)
149{
150 return (flags & VM_ACCOUNT)?
151 security_vm_enough_memory(VM_ACCT(size)): 0;
152}
153
154static inline void shmem_unacct_size(unsigned long flags, loff_t size)
155{
156 if (flags & VM_ACCOUNT)
157 vm_unacct_memory(VM_ACCT(size));
158}
159
160
161
162
163
164
165
166static inline int shmem_acct_block(unsigned long flags)
167{
168 return (flags & VM_ACCOUNT)?
169 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE));
170}
171
172static inline void shmem_unacct_blocks(unsigned long flags, long pages)
173{
174 if (!(flags & VM_ACCOUNT))
175 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
176}
177
178static const struct super_operations shmem_ops;
179static const struct address_space_operations shmem_aops;
180static const struct file_operations shmem_file_operations;
181static const struct inode_operations shmem_inode_operations;
182static const struct inode_operations shmem_dir_inode_operations;
183static const struct inode_operations shmem_special_inode_operations;
184static struct vm_operations_struct shmem_vm_ops;
185
186static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
187 .ra_pages = 0,
188 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
189 .unplug_io_fn = default_unplug_io_fn,
190};
191
192static LIST_HEAD(shmem_swaplist);
193static DEFINE_SPINLOCK(shmem_swaplist_lock);
194
195static void shmem_free_blocks(struct inode *inode, long pages)
196{
197 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
198 if (sbinfo->max_blocks) {
199 spin_lock(&sbinfo->stat_lock);
200 sbinfo->free_blocks += pages;
201 inode->i_blocks -= pages*BLOCKS_PER_PAGE;
202 spin_unlock(&sbinfo->stat_lock);
203 }
204}
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219static void shmem_recalc_inode(struct inode *inode)
220{
221 struct shmem_inode_info *info = SHMEM_I(inode);
222 long freed;
223
224 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
225 if (freed > 0) {
226 info->alloced -= freed;
227 shmem_unacct_blocks(info->flags, freed);
228 shmem_free_blocks(inode, freed);
229 }
230}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page)
271{
272 unsigned long offset;
273 struct page **dir;
274 struct page *subdir;
275
276 if (index < SHMEM_NR_DIRECT) {
277 shmem_swp_balance_unmap();
278 return info->i_direct+index;
279 }
280 if (!info->i_indirect) {
281 if (page) {
282 info->i_indirect = *page;
283 *page = NULL;
284 }
285 return NULL;
286 }
287
288 index -= SHMEM_NR_DIRECT;
289 offset = index % ENTRIES_PER_PAGE;
290 index /= ENTRIES_PER_PAGE;
291 dir = shmem_dir_map(info->i_indirect);
292
293 if (index >= ENTRIES_PER_PAGE/2) {
294 index -= ENTRIES_PER_PAGE/2;
295 dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE;
296 index %= ENTRIES_PER_PAGE;
297 subdir = *dir;
298 if (!subdir) {
299 if (page) {
300 *dir = *page;
301 *page = NULL;
302 }
303 shmem_dir_unmap(dir);
304 return NULL;
305 }
306 shmem_dir_unmap(dir);
307 dir = shmem_dir_map(subdir);
308 }
309
310 dir += index;
311 subdir = *dir;
312 if (!subdir) {
313 if (!page || !(subdir = *page)) {
314 shmem_dir_unmap(dir);
315 return NULL;
316 }
317 *dir = subdir;
318 *page = NULL;
319 }
320 shmem_dir_unmap(dir);
321 return shmem_swp_map(subdir) + offset;
322}
323
324static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value)
325{
326 long incdec = value? 1: -1;
327
328 entry->val = value;
329 info->swapped += incdec;
330 if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) {
331 struct page *page = kmap_atomic_to_page(entry);
332 set_page_private(page, page_private(page) + incdec);
333 }
334}
335
336
337
338
339
340
341
342
343
344static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp)
345{
346 struct inode *inode = &info->vfs_inode;
347 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
348 struct page *page = NULL;
349 swp_entry_t *entry;
350
351 if (sgp != SGP_WRITE &&
352 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
353 return ERR_PTR(-EINVAL);
354
355 while (!(entry = shmem_swp_entry(info, index, &page))) {
356 if (sgp == SGP_READ)
357 return shmem_swp_map(ZERO_PAGE(0));
358
359
360
361
362
363 if (sbinfo->max_blocks) {
364 spin_lock(&sbinfo->stat_lock);
365 if (sbinfo->free_blocks <= 1) {
366 spin_unlock(&sbinfo->stat_lock);
367 return ERR_PTR(-ENOSPC);
368 }
369 sbinfo->free_blocks--;
370 inode->i_blocks += BLOCKS_PER_PAGE;
371 spin_unlock(&sbinfo->stat_lock);
372 }
373
374 spin_unlock(&info->lock);
375 page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping) | __GFP_ZERO);
376 if (page)
377 set_page_private(page, 0);
378 spin_lock(&info->lock);
379
380 if (!page) {
381 shmem_free_blocks(inode, 1);
382 return ERR_PTR(-ENOMEM);
383 }
384 if (sgp != SGP_WRITE &&
385 ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) {
386 entry = ERR_PTR(-EINVAL);
387 break;
388 }
389 if (info->next_index <= index)
390 info->next_index = index + 1;
391 }
392 if (page) {
393
394 shmem_free_blocks(inode, 1);
395 shmem_dir_free(page);
396 }
397 if (info->next_index <= index && !IS_ERR(entry))
398 info->next_index = index + 1;
399 return entry;
400}
401
402
403
404
405
406
407
408
409static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir,
410 spinlock_t *punch_lock)
411{
412 spinlock_t *punch_unlock = NULL;
413 swp_entry_t *ptr;
414 int freed = 0;
415
416 for (ptr = dir; ptr < edir; ptr++) {
417 if (ptr->val) {
418 if (unlikely(punch_lock)) {
419 punch_unlock = punch_lock;
420 punch_lock = NULL;
421 spin_lock(punch_unlock);
422 if (!ptr->val)
423 continue;
424 }
425 free_swap_and_cache(*ptr);
426 *ptr = (swp_entry_t){0};
427 freed++;
428 }
429 }
430 if (punch_unlock)
431 spin_unlock(punch_unlock);
432 return freed;
433}
434
435static int shmem_map_and_free_swp(struct page *subdir, int offset,
436 int limit, struct page ***dir, spinlock_t *punch_lock)
437{
438 swp_entry_t *ptr;
439 int freed = 0;
440
441 ptr = shmem_swp_map(subdir);
442 for (; offset < limit; offset += LATENCY_LIMIT) {
443 int size = limit - offset;
444 if (size > LATENCY_LIMIT)
445 size = LATENCY_LIMIT;
446 freed += shmem_free_swp(ptr+offset, ptr+offset+size,
447 punch_lock);
448 if (need_resched()) {
449 shmem_swp_unmap(ptr);
450 if (*dir) {
451 shmem_dir_unmap(*dir);
452 *dir = NULL;
453 }
454 cond_resched();
455 ptr = shmem_swp_map(subdir);
456 }
457 }
458 shmem_swp_unmap(ptr);
459 return freed;
460}
461
462static void shmem_free_pages(struct list_head *next)
463{
464 struct page *page;
465 int freed = 0;
466
467 do {
468 page = container_of(next, struct page, lru);
469 next = next->next;
470 shmem_dir_free(page);
471 freed++;
472 if (freed >= LATENCY_LIMIT) {
473 cond_resched();
474 freed = 0;
475 }
476 } while (next);
477}
478
479static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end)
480{
481 struct shmem_inode_info *info = SHMEM_I(inode);
482 unsigned long idx;
483 unsigned long size;
484 unsigned long limit;
485 unsigned long stage;
486 unsigned long diroff;
487 struct page **dir;
488 struct page *topdir;
489 struct page *middir;
490 struct page *subdir;
491 swp_entry_t *ptr;
492 LIST_HEAD(pages_to_free);
493 long nr_pages_to_free = 0;
494 long nr_swaps_freed = 0;
495 int offset;
496 int freed;
497 int punch_hole;
498 spinlock_t *needs_lock;
499 spinlock_t *punch_lock;
500 unsigned long upper_limit;
501
502 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
503 idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
504 if (idx >= info->next_index)
505 return;
506
507 spin_lock(&info->lock);
508 info->flags |= SHMEM_TRUNCATE;
509 if (likely(end == (loff_t) -1)) {
510 limit = info->next_index;
511 upper_limit = SHMEM_MAX_INDEX;
512 info->next_index = idx;
513 needs_lock = NULL;
514 punch_hole = 0;
515 } else {
516 if (end + 1 >= inode->i_size) {
517 limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >>
518 PAGE_CACHE_SHIFT;
519 upper_limit = SHMEM_MAX_INDEX;
520 } else {
521 limit = (end + 1) >> PAGE_CACHE_SHIFT;
522 upper_limit = limit;
523 }
524 needs_lock = &info->lock;
525 punch_hole = 1;
526 }
527
528 topdir = info->i_indirect;
529 if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) {
530 info->i_indirect = NULL;
531 nr_pages_to_free++;
532 list_add(&topdir->lru, &pages_to_free);
533 }
534 spin_unlock(&info->lock);
535
536 if (info->swapped && idx < SHMEM_NR_DIRECT) {
537 ptr = info->i_direct;
538 size = limit;
539 if (size > SHMEM_NR_DIRECT)
540 size = SHMEM_NR_DIRECT;
541 nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock);
542 }
543
544
545
546
547
548 if (!topdir || limit <= SHMEM_NR_DIRECT)
549 goto done2;
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564 upper_limit -= SHMEM_NR_DIRECT;
565 limit -= SHMEM_NR_DIRECT;
566 idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0;
567 offset = idx % ENTRIES_PER_PAGE;
568 idx -= offset;
569
570 dir = shmem_dir_map(topdir);
571 stage = ENTRIES_PER_PAGEPAGE/2;
572 if (idx < ENTRIES_PER_PAGEPAGE/2) {
573 middir = topdir;
574 diroff = idx/ENTRIES_PER_PAGE;
575 } else {
576 dir += ENTRIES_PER_PAGE/2;
577 dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE;
578 while (stage <= idx)
579 stage += ENTRIES_PER_PAGEPAGE;
580 middir = *dir;
581 if (*dir) {
582 diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) %
583 ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE;
584 if (!diroff && !offset && upper_limit >= stage) {
585 if (needs_lock) {
586 spin_lock(needs_lock);
587 *dir = NULL;
588 spin_unlock(needs_lock);
589 needs_lock = NULL;
590 } else
591 *dir = NULL;
592 nr_pages_to_free++;
593 list_add(&middir->lru, &pages_to_free);
594 }
595 shmem_dir_unmap(dir);
596 dir = shmem_dir_map(middir);
597 } else {
598 diroff = 0;
599 offset = 0;
600 idx = stage;
601 }
602 }
603
604 for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) {
605 if (unlikely(idx == stage)) {
606 shmem_dir_unmap(dir);
607 dir = shmem_dir_map(topdir) +
608 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
609 while (!*dir) {
610 dir++;
611 idx += ENTRIES_PER_PAGEPAGE;
612 if (idx >= limit)
613 goto done1;
614 }
615 stage = idx + ENTRIES_PER_PAGEPAGE;
616 middir = *dir;
617 if (punch_hole)
618 needs_lock = &info->lock;
619 if (upper_limit >= stage) {
620 if (needs_lock) {
621 spin_lock(needs_lock);
622 *dir = NULL;
623 spin_unlock(needs_lock);
624 needs_lock = NULL;
625 } else
626 *dir = NULL;
627 nr_pages_to_free++;
628 list_add(&middir->lru, &pages_to_free);
629 }
630 shmem_dir_unmap(dir);
631 cond_resched();
632 dir = shmem_dir_map(middir);
633 diroff = 0;
634 }
635 punch_lock = needs_lock;
636 subdir = dir[diroff];
637 if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) {
638 if (needs_lock) {
639 spin_lock(needs_lock);
640 dir[diroff] = NULL;
641 spin_unlock(needs_lock);
642 punch_lock = NULL;
643 } else
644 dir[diroff] = NULL;
645 nr_pages_to_free++;
646 list_add(&subdir->lru, &pages_to_free);
647 }
648 if (subdir && page_private(subdir) ) {
649 size = limit - idx;
650 if (size > ENTRIES_PER_PAGE)
651 size = ENTRIES_PER_PAGE;
652 freed = shmem_map_and_free_swp(subdir,
653 offset, size, &dir, punch_lock);
654 if (!dir)
655 dir = shmem_dir_map(middir);
656 nr_swaps_freed += freed;
657 if (offset || punch_lock) {
658 spin_lock(&info->lock);
659 set_page_private(subdir,
660 page_private(subdir) - freed);
661 spin_unlock(&info->lock);
662 } else
663 BUG_ON(page_private(subdir) != freed);
664 }
665 offset = 0;
666 }
667done1:
668 shmem_dir_unmap(dir);
669done2:
670 if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) {
671
672
673
674
675
676
677
678
679
680
681
682
683 truncate_inode_pages_range(inode->i_mapping, start, end);
684 if (punch_hole)
685 unmap_mapping_range(inode->i_mapping, start,
686 end - start, 1);
687 }
688
689 spin_lock(&info->lock);
690 info->flags &= ~SHMEM_TRUNCATE;
691 info->swapped -= nr_swaps_freed;
692 if (nr_pages_to_free)
693 shmem_free_blocks(inode, nr_pages_to_free);
694 shmem_recalc_inode(inode);
695 spin_unlock(&info->lock);
696
697
698
699
700 if (!list_empty(&pages_to_free)) {
701 pages_to_free.prev->next = NULL;
702 shmem_free_pages(pages_to_free.next);
703 }
704}
705
706static void shmem_truncate(struct inode *inode)
707{
708 shmem_truncate_range(inode, inode->i_size, (loff_t)-1);
709}
710
711static int shmem_notify_change(struct dentry *dentry, struct iattr *attr)
712{
713 struct inode *inode = dentry->d_inode;
714 struct page *page = NULL;
715 int error;
716
717 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
718 if (attr->ia_size < inode->i_size) {
719
720
721
722
723
724
725
726 if (attr->ia_size & (PAGE_CACHE_SIZE-1)) {
727 (void) shmem_getpage(inode,
728 attr->ia_size>>PAGE_CACHE_SHIFT,
729 &page, SGP_READ, NULL);
730 }
731
732
733
734
735
736
737
738 if (attr->ia_size) {
739 struct shmem_inode_info *info = SHMEM_I(inode);
740 spin_lock(&info->lock);
741 info->flags &= ~SHMEM_PAGEIN;
742 spin_unlock(&info->lock);
743 }
744 }
745 }
746
747 error = inode_change_ok(inode, attr);
748 if (!error)
749 error = inode_setattr(inode, attr);
750#ifdef CONFIG_TMPFS_POSIX_ACL
751 if (!error && (attr->ia_valid & ATTR_MODE))
752 error = generic_acl_chmod(inode, &shmem_acl_ops);
753#endif
754 if (page)
755 page_cache_release(page);
756 return error;
757}
758
759static void shmem_delete_inode(struct inode *inode)
760{
761 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
762 struct shmem_inode_info *info = SHMEM_I(inode);
763
764 if (inode->i_op->truncate == shmem_truncate) {
765 truncate_inode_pages(inode->i_mapping, 0);
766 shmem_unacct_size(info->flags, inode->i_size);
767 inode->i_size = 0;
768 shmem_truncate(inode);
769 if (!list_empty(&info->swaplist)) {
770 spin_lock(&shmem_swaplist_lock);
771 list_del_init(&info->swaplist);
772 spin_unlock(&shmem_swaplist_lock);
773 }
774 }
775 BUG_ON(inode->i_blocks);
776 if (sbinfo->max_inodes) {
777 spin_lock(&sbinfo->stat_lock);
778 sbinfo->free_inodes++;
779 spin_unlock(&sbinfo->stat_lock);
780 }
781 clear_inode(inode);
782}
783
784static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir)
785{
786 swp_entry_t *ptr;
787
788 for (ptr = dir; ptr < edir; ptr++) {
789 if (ptr->val == entry.val)
790 return ptr - dir;
791 }
792 return -1;
793}
794
795static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page)
796{
797 struct inode *inode;
798 unsigned long idx;
799 unsigned long size;
800 unsigned long limit;
801 unsigned long stage;
802 struct page **dir;
803 struct page *subdir;
804 swp_entry_t *ptr;
805 int offset;
806
807 idx = 0;
808 ptr = info->i_direct;
809 spin_lock(&info->lock);
810 limit = info->next_index;
811 size = limit;
812 if (size > SHMEM_NR_DIRECT)
813 size = SHMEM_NR_DIRECT;
814 offset = shmem_find_swp(entry, ptr, ptr+size);
815 if (offset >= 0) {
816 shmem_swp_balance_unmap();
817 goto found;
818 }
819 if (!info->i_indirect)
820 goto lost2;
821
822 dir = shmem_dir_map(info->i_indirect);
823 stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2;
824
825 for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) {
826 if (unlikely(idx == stage)) {
827 shmem_dir_unmap(dir-1);
828 dir = shmem_dir_map(info->i_indirect) +
829 ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE;
830 while (!*dir) {
831 dir++;
832 idx += ENTRIES_PER_PAGEPAGE;
833 if (idx >= limit)
834 goto lost1;
835 }
836 stage = idx + ENTRIES_PER_PAGEPAGE;
837 subdir = *dir;
838 shmem_dir_unmap(dir);
839 dir = shmem_dir_map(subdir);
840 }
841 subdir = *dir;
842 if (subdir && page_private(subdir)) {
843 ptr = shmem_swp_map(subdir);
844 size = limit - idx;
845 if (size > ENTRIES_PER_PAGE)
846 size = ENTRIES_PER_PAGE;
847 offset = shmem_find_swp(entry, ptr, ptr+size);
848 if (offset >= 0) {
849 shmem_dir_unmap(dir);
850 goto found;
851 }
852 shmem_swp_unmap(ptr);
853 }
854 }
855lost1:
856 shmem_dir_unmap(dir-1);
857lost2:
858 spin_unlock(&info->lock);
859 return 0;
860found:
861 idx += offset;
862 inode = &info->vfs_inode;
863 if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) {
864 info->flags |= SHMEM_PAGEIN;
865 shmem_swp_set(info, ptr + offset, 0);
866 }
867 shmem_swp_unmap(ptr);
868 spin_unlock(&info->lock);
869
870
871
872
873 swap_free(entry);
874 return 1;
875}
876
877
878
879
880int shmem_unuse(swp_entry_t entry, struct page *page)
881{
882 struct list_head *p, *next;
883 struct shmem_inode_info *info;
884 int found = 0;
885
886 spin_lock(&shmem_swaplist_lock);
887 list_for_each_safe(p, next, &shmem_swaplist) {
888 info = list_entry(p, struct shmem_inode_info, swaplist);
889 if (!info->swapped)
890 list_del_init(&info->swaplist);
891 else if (shmem_unuse_inode(info, entry, page)) {
892
893 list_move_tail(&shmem_swaplist, &info->swaplist);
894 found = 1;
895 break;
896 }
897 }
898 spin_unlock(&shmem_swaplist_lock);
899 return found;
900}
901
902
903
904
905static int shmem_writepage(struct page *page, struct writeback_control *wbc)
906{
907 struct shmem_inode_info *info;
908 swp_entry_t *entry, swap;
909 struct address_space *mapping;
910 unsigned long index;
911 struct inode *inode;
912
913 BUG_ON(!PageLocked(page));
914
915
916
917
918
919
920
921
922
923
924 if (!wbc->for_reclaim) {
925 set_page_dirty(page);
926 unlock_page(page);
927 return 0;
928 }
929 BUG_ON(page_mapped(page));
930
931 mapping = page->mapping;
932 index = page->index;
933 inode = mapping->host;
934 info = SHMEM_I(inode);
935 if (info->flags & VM_LOCKED)
936 goto redirty;
937 swap = get_swap_page();
938 if (!swap.val)
939 goto redirty;
940
941 spin_lock(&info->lock);
942 shmem_recalc_inode(inode);
943 if (index >= info->next_index) {
944 BUG_ON(!(info->flags & SHMEM_TRUNCATE));
945 goto unlock;
946 }
947 entry = shmem_swp_entry(info, index, NULL);
948 BUG_ON(!entry);
949 BUG_ON(entry->val);
950
951 if (move_to_swap_cache(page, swap) == 0) {
952 shmem_swp_set(info, entry, swap.val);
953 shmem_swp_unmap(entry);
954 spin_unlock(&info->lock);
955 if (list_empty(&info->swaplist)) {
956 spin_lock(&shmem_swaplist_lock);
957
958 list_move_tail(&info->swaplist, &shmem_swaplist);
959 spin_unlock(&shmem_swaplist_lock);
960 }
961 unlock_page(page);
962 return 0;
963 }
964
965 shmem_swp_unmap(entry);
966unlock:
967 spin_unlock(&info->lock);
968 swap_free(swap);
969redirty:
970 set_page_dirty(page);
971 return AOP_WRITEPAGE_ACTIVATE;
972}
973
974#ifdef CONFIG_NUMA
975static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
976{
977 char *nodelist = strchr(value, ':');
978 int err = 1;
979
980 if (nodelist) {
981
982 *nodelist++ = '\0';
983 if (nodelist_parse(nodelist, *policy_nodes))
984 goto out;
985 if (!nodes_subset(*policy_nodes, node_online_map))
986 goto out;
987 }
988 if (!strcmp(value, "default")) {
989 *policy = MPOL_DEFAULT;
990
991 if (!nodelist)
992 err = 0;
993 } else if (!strcmp(value, "prefer")) {
994 *policy = MPOL_PREFERRED;
995
996 if (nodelist) {
997 char *rest = nodelist;
998 while (isdigit(*rest))
999 rest++;
1000 if (!*rest)
1001 err = 0;
1002 }
1003 } else if (!strcmp(value, "bind")) {
1004 *policy = MPOL_BIND;
1005
1006 if (nodelist)
1007 err = 0;
1008 } else if (!strcmp(value, "interleave")) {
1009 *policy = MPOL_INTERLEAVE;
1010
1011 if (!nodelist)
1012 *policy_nodes = node_online_map;
1013 err = 0;
1014 }
1015out:
1016
1017 if (nodelist)
1018 *--nodelist = ':';
1019 return err;
1020}
1021
1022static struct page *shmem_swapin_async(struct shared_policy *p,
1023 swp_entry_t entry, unsigned long idx)
1024{
1025 struct page *page;
1026 struct vm_area_struct pvma;
1027
1028
1029 memset(&pvma, 0, sizeof(struct vm_area_struct));
1030 pvma.vm_end = PAGE_SIZE;
1031 pvma.vm_pgoff = idx;
1032 pvma.vm_policy = mpol_shared_policy_lookup(p, idx);
1033 page = read_swap_cache_async(entry, &pvma, 0);
1034 mpol_free(pvma.vm_policy);
1035 return page;
1036}
1037
1038struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry,
1039 unsigned long idx)
1040{
1041 struct shared_policy *p = &info->policy;
1042 int i, num;
1043 struct page *page;
1044 unsigned long offset;
1045
1046 num = valid_swaphandles(entry, &offset);
1047 for (i = 0; i < num; offset++, i++) {
1048 page = shmem_swapin_async(p,
1049 swp_entry(swp_type(entry), offset), idx);
1050 if (!page)
1051 break;
1052 page_cache_release(page);
1053 }
1054 lru_add_drain();
1055 return shmem_swapin_async(p, entry, idx);
1056}
1057
1058static struct page *
1059shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info,
1060 unsigned long idx)
1061{
1062 struct vm_area_struct pvma;
1063 struct page *page;
1064
1065 memset(&pvma, 0, sizeof(struct vm_area_struct));
1066 pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx);
1067 pvma.vm_pgoff = idx;
1068 pvma.vm_end = PAGE_SIZE;
1069 page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0);
1070 mpol_free(pvma.vm_policy);
1071 return page;
1072}
1073#else
1074static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes)
1075{
1076 return 1;
1077}
1078
1079static inline struct page *
1080shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx)
1081{
1082 swapin_readahead(entry, 0, NULL);
1083 return read_swap_cache_async(entry, NULL, 0);
1084}
1085
1086static inline struct page *
1087shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx)
1088{
1089 return alloc_page(gfp | __GFP_ZERO);
1090}
1091#endif
1092
1093
1094
1095
1096
1097
1098
1099
1100static int shmem_getpage(struct inode *inode, unsigned long idx,
1101 struct page **pagep, enum sgp_type sgp, int *type)
1102{
1103 struct address_space *mapping = inode->i_mapping;
1104 struct shmem_inode_info *info = SHMEM_I(inode);
1105 struct shmem_sb_info *sbinfo;
1106 struct page *filepage = *pagep;
1107 struct page *swappage;
1108 swp_entry_t *entry;
1109 swp_entry_t swap;
1110 int error;
1111
1112 if (idx >= SHMEM_MAX_INDEX)
1113 return -EFBIG;
1114
1115
1116
1117
1118
1119
1120
1121
1122repeat:
1123 if (!filepage)
1124 filepage = find_lock_page(mapping, idx);
1125 if (filepage && PageUptodate(filepage))
1126 goto done;
1127 error = 0;
1128 if (sgp == SGP_QUICK)
1129 goto failed;
1130
1131 spin_lock(&info->lock);
1132 shmem_recalc_inode(inode);
1133 entry = shmem_swp_alloc(info, idx, sgp);
1134 if (IS_ERR(entry)) {
1135 spin_unlock(&info->lock);
1136 error = PTR_ERR(entry);
1137 goto failed;
1138 }
1139 swap = *entry;
1140
1141 if (swap.val) {
1142
1143 swappage = lookup_swap_cache(swap);
1144 if (!swappage) {
1145 shmem_swp_unmap(entry);
1146
1147 if (type && *type == VM_FAULT_MINOR) {
1148 __count_vm_event(PGMAJFAULT);
1149 *type = VM_FAULT_MAJOR;
1150 }
1151 spin_unlock(&info->lock);
1152 swappage = shmem_swapin(info, swap, idx);
1153 if (!swappage) {
1154 spin_lock(&info->lock);
1155 entry = shmem_swp_alloc(info, idx, sgp);
1156 if (IS_ERR(entry))
1157 error = PTR_ERR(entry);
1158 else {
1159 if (entry->val == swap.val)
1160 error = -ENOMEM;
1161 shmem_swp_unmap(entry);
1162 }
1163 spin_unlock(&info->lock);
1164 if (error)
1165 goto failed;
1166 goto repeat;
1167 }
1168 wait_on_page_locked(swappage);
1169 page_cache_release(swappage);
1170 goto repeat;
1171 }
1172
1173
1174 if (TestSetPageLocked(swappage)) {
1175 shmem_swp_unmap(entry);
1176 spin_unlock(&info->lock);
1177 wait_on_page_locked(swappage);
1178 page_cache_release(swappage);
1179 goto repeat;
1180 }
1181 if (PageWriteback(swappage)) {
1182 shmem_swp_unmap(entry);
1183 spin_unlock(&info->lock);
1184 wait_on_page_writeback(swappage);
1185 unlock_page(swappage);
1186 page_cache_release(swappage);
1187 goto repeat;
1188 }
1189 if (!PageUptodate(swappage)) {
1190 shmem_swp_unmap(entry);
1191 spin_unlock(&info->lock);
1192 unlock_page(swappage);
1193 page_cache_release(swappage);
1194 error = -EIO;
1195 goto failed;
1196 }
1197
1198 if (filepage) {
1199 shmem_swp_set(info, entry, 0);
1200 shmem_swp_unmap(entry);
1201 delete_from_swap_cache(swappage);
1202 spin_unlock(&info->lock);
1203 copy_highpage(filepage, swappage);
1204 unlock_page(swappage);
1205 page_cache_release(swappage);
1206 flush_dcache_page(filepage);
1207 SetPageUptodate(filepage);
1208 set_page_dirty(filepage);
1209 swap_free(swap);
1210 } else if (!(error = move_from_swap_cache(
1211 swappage, idx, mapping))) {
1212 info->flags |= SHMEM_PAGEIN;
1213 shmem_swp_set(info, entry, 0);
1214 shmem_swp_unmap(entry);
1215 spin_unlock(&info->lock);
1216 filepage = swappage;
1217 swap_free(swap);
1218 } else {
1219 shmem_swp_unmap(entry);
1220 spin_unlock(&info->lock);
1221 unlock_page(swappage);
1222 page_cache_release(swappage);
1223 if (error == -ENOMEM) {
1224
1225 congestion_wait(WRITE, HZ/50);
1226 }
1227 goto repeat;
1228 }
1229 } else if (sgp == SGP_READ && !filepage) {
1230 shmem_swp_unmap(entry);
1231 filepage = find_get_page(mapping, idx);
1232 if (filepage &&
1233 (!PageUptodate(filepage) || TestSetPageLocked(filepage))) {
1234 spin_unlock(&info->lock);
1235 wait_on_page_locked(filepage);
1236 page_cache_release(filepage);
1237 filepage = NULL;
1238 goto repeat;
1239 }
1240 spin_unlock(&info->lock);
1241 } else {
1242 shmem_swp_unmap(entry);
1243 sbinfo = SHMEM_SB(inode->i_sb);
1244 if (sbinfo->max_blocks) {
1245 spin_lock(&sbinfo->stat_lock);
1246 if (sbinfo->free_blocks == 0 ||
1247 shmem_acct_block(info->flags)) {
1248 spin_unlock(&sbinfo->stat_lock);
1249 spin_unlock(&info->lock);
1250 error = -ENOSPC;
1251 goto failed;
1252 }
1253 sbinfo->free_blocks--;
1254 inode->i_blocks += BLOCKS_PER_PAGE;
1255 spin_unlock(&sbinfo->stat_lock);
1256 } else if (shmem_acct_block(info->flags)) {
1257 spin_unlock(&info->lock);
1258 error = -ENOSPC;
1259 goto failed;
1260 }
1261
1262 if (!filepage) {
1263 spin_unlock(&info->lock);
1264 filepage = shmem_alloc_page(mapping_gfp_mask(mapping),
1265 info,
1266 idx);
1267 if (!filepage) {
1268 shmem_unacct_blocks(info->flags, 1);
1269 shmem_free_blocks(inode, 1);
1270 error = -ENOMEM;
1271 goto failed;
1272 }
1273
1274 spin_lock(&info->lock);
1275 entry = shmem_swp_alloc(info, idx, sgp);
1276 if (IS_ERR(entry))
1277 error = PTR_ERR(entry);
1278 else {
1279 swap = *entry;
1280 shmem_swp_unmap(entry);
1281 }
1282 if (error || swap.val || 0 != add_to_page_cache_lru(
1283 filepage, mapping, idx, GFP_ATOMIC)) {
1284 spin_unlock(&info->lock);
1285 page_cache_release(filepage);
1286 shmem_unacct_blocks(info->flags, 1);
1287 shmem_free_blocks(inode, 1);
1288 filepage = NULL;
1289 if (error)
1290 goto failed;
1291 goto repeat;
1292 }
1293 info->flags |= SHMEM_PAGEIN;
1294 }
1295
1296 info->alloced++;
1297 spin_unlock(&info->lock);
1298 flush_dcache_page(filepage);
1299 SetPageUptodate(filepage);
1300 }
1301done:
1302 if (*pagep != filepage) {
1303 unlock_page(filepage);
1304 *pagep = filepage;
1305 }
1306 return 0;
1307
1308failed:
1309 if (*pagep != filepage) {
1310 unlock_page(filepage);
1311 page_cache_release(filepage);
1312 }
1313 return error;
1314}
1315
1316static struct page *shmem_nopage(struct vm_area_struct *vma,
1317 unsigned long address, int *type)
1318{
1319 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1320 struct page *page = NULL;
1321 unsigned long idx;
1322 int error;
1323
1324 idx = (address - vma->vm_start) >> PAGE_SHIFT;
1325 idx += vma->vm_pgoff;
1326 idx >>= PAGE_CACHE_SHIFT - PAGE_SHIFT;
1327 if (((loff_t) idx << PAGE_CACHE_SHIFT) >= i_size_read(inode))
1328 return NOPAGE_SIGBUS;
1329
1330 error = shmem_getpage(inode, idx, &page, SGP_CACHE, type);
1331 if (error)
1332 return (error == -ENOMEM)? NOPAGE_OOM: NOPAGE_SIGBUS;
1333
1334 mark_page_accessed(page);
1335 return page;
1336}
1337
1338static int shmem_populate(struct vm_area_struct *vma,
1339 unsigned long addr, unsigned long len,
1340 pgprot_t prot, unsigned long pgoff, int nonblock)
1341{
1342 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1343 struct mm_struct *mm = vma->vm_mm;
1344 enum sgp_type sgp = nonblock? SGP_QUICK: SGP_CACHE;
1345 unsigned long size;
1346
1347 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1348 if (pgoff >= size || pgoff + (len >> PAGE_SHIFT) > size)
1349 return -EINVAL;
1350
1351 while ((long) len > 0) {
1352 struct page *page = NULL;
1353 int err;
1354
1355
1356
1357 err = shmem_getpage(inode, pgoff, &page, sgp, NULL);
1358 if (err)
1359 return err;
1360
1361 if (page) {
1362 mark_page_accessed(page);
1363 err = install_page(mm, vma, addr, page, prot);
1364 if (err) {
1365 page_cache_release(page);
1366 return err;
1367 }
1368 } else if (vma->vm_flags & VM_NONLINEAR) {
1369
1370
1371
1372 err = install_file_pte(mm, vma, addr, pgoff, prot);
1373 if (err)
1374 return err;
1375 }
1376
1377 len -= PAGE_SIZE;
1378 addr += PAGE_SIZE;
1379 pgoff++;
1380 }
1381 return 0;
1382}
1383
1384#ifdef CONFIG_NUMA
1385int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
1386{
1387 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1388 return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new);
1389}
1390
1391struct mempolicy *
1392shmem_get_policy(struct vm_area_struct *vma, unsigned long addr)
1393{
1394 struct inode *i = vma->vm_file->f_path.dentry->d_inode;
1395 unsigned long idx;
1396
1397 idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
1398 return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx);
1399}
1400#endif
1401
1402int shmem_lock(struct file *file, int lock, struct user_struct *user)
1403{
1404 struct inode *inode = file->f_path.dentry->d_inode;
1405 struct shmem_inode_info *info = SHMEM_I(inode);
1406 int retval = -ENOMEM;
1407
1408 spin_lock(&info->lock);
1409 if (lock && !(info->flags & VM_LOCKED)) {
1410 if (!user_shm_lock(inode->i_size, user))
1411 goto out_nomem;
1412 info->flags |= VM_LOCKED;
1413 }
1414 if (!lock && (info->flags & VM_LOCKED) && user) {
1415 user_shm_unlock(inode->i_size, user);
1416 info->flags &= ~VM_LOCKED;
1417 }
1418 retval = 0;
1419out_nomem:
1420 spin_unlock(&info->lock);
1421 return retval;
1422}
1423
1424static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1425{
1426 file_accessed(file);
1427 vma->vm_ops = &shmem_vm_ops;
1428 return 0;
1429}
1430
1431static struct inode *
1432shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1433{
1434 struct inode *inode;
1435 struct shmem_inode_info *info;
1436 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1437
1438 if (sbinfo->max_inodes) {
1439 spin_lock(&sbinfo->stat_lock);
1440 if (!sbinfo->free_inodes) {
1441 spin_unlock(&sbinfo->stat_lock);
1442 return NULL;
1443 }
1444 sbinfo->free_inodes--;
1445 spin_unlock(&sbinfo->stat_lock);
1446 }
1447
1448 inode = new_inode(sb);
1449 if (inode) {
1450 inode->i_mode = mode;
1451 inode->i_uid = current->fsuid;
1452 inode->i_gid = current->fsgid;
1453 inode->i_blocks = 0;
1454 inode->i_mapping->a_ops = &shmem_aops;
1455 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1456 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1457 inode->i_generation = get_seconds();
1458 info = SHMEM_I(inode);
1459 memset(info, 0, (char *)inode - (char *)info);
1460 spin_lock_init(&info->lock);
1461 INIT_LIST_HEAD(&info->swaplist);
1462
1463 switch (mode & S_IFMT) {
1464 default:
1465 inode->i_op = &shmem_special_inode_operations;
1466 init_special_inode(inode, mode, dev);
1467 break;
1468 case S_IFREG:
1469 inode->i_op = &shmem_inode_operations;
1470 inode->i_fop = &shmem_file_operations;
1471 mpol_shared_policy_init(&info->policy, sbinfo->policy,
1472 &sbinfo->policy_nodes);
1473 break;
1474 case S_IFDIR:
1475 inc_nlink(inode);
1476
1477 inode->i_size = 2 * BOGO_DIRENT_SIZE;
1478 inode->i_op = &shmem_dir_inode_operations;
1479 inode->i_fop = &simple_dir_operations;
1480 break;
1481 case S_IFLNK:
1482
1483
1484
1485
1486 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT,
1487 NULL);
1488 break;
1489 }
1490 } else if (sbinfo->max_inodes) {
1491 spin_lock(&sbinfo->stat_lock);
1492 sbinfo->free_inodes++;
1493 spin_unlock(&sbinfo->stat_lock);
1494 }
1495 return inode;
1496}
1497
1498#ifdef CONFIG_TMPFS
1499static const struct inode_operations shmem_symlink_inode_operations;
1500static const struct inode_operations shmem_symlink_inline_operations;
1501
1502
1503
1504
1505
1506static int
1507shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1508{
1509 struct inode *inode = page->mapping->host;
1510 return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL);
1511}
1512
1513static ssize_t
1514shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
1515{
1516 struct inode *inode = file->f_path.dentry->d_inode;
1517 loff_t pos;
1518 unsigned long written;
1519 ssize_t err;
1520
1521 if ((ssize_t) count < 0)
1522 return -EINVAL;
1523
1524 if (!access_ok(VERIFY_READ, buf, count))
1525 return -EFAULT;
1526
1527 mutex_lock(&inode->i_mutex);
1528
1529 pos = *ppos;
1530 written = 0;
1531
1532 err = generic_write_checks(file, &pos, &count, 0);
1533 if (err || !count)
1534 goto out;
1535
1536 err = remove_suid(file->f_path.dentry);
1537 if (err)
1538 goto out;
1539
1540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
1541
1542 do {
1543 struct page *page = NULL;
1544 unsigned long bytes, index, offset;
1545 char *kaddr;
1546 int left;
1547
1548 offset = (pos & (PAGE_CACHE_SIZE -1));
1549 index = pos >> PAGE_CACHE_SHIFT;
1550 bytes = PAGE_CACHE_SIZE - offset;
1551 if (bytes > count)
1552 bytes = count;
1553
1554
1555
1556
1557
1558
1559
1560 err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL);
1561 if (err)
1562 break;
1563
1564 left = bytes;
1565 if (PageHighMem(page)) {
1566 volatile unsigned char dummy;
1567 __get_user(dummy, buf);
1568 __get_user(dummy, buf + bytes - 1);
1569
1570 kaddr = kmap_atomic(page, KM_USER0);
1571 left = __copy_from_user_inatomic(kaddr + offset,
1572 buf, bytes);
1573 kunmap_atomic(kaddr, KM_USER0);
1574 }
1575 if (left) {
1576 kaddr = kmap(page);
1577 left = __copy_from_user(kaddr + offset, buf, bytes);
1578 kunmap(page);
1579 }
1580
1581 written += bytes;
1582 count -= bytes;
1583 pos += bytes;
1584 buf += bytes;
1585 if (pos > inode->i_size)
1586 i_size_write(inode, pos);
1587
1588 flush_dcache_page(page);
1589 set_page_dirty(page);
1590 mark_page_accessed(page);
1591 page_cache_release(page);
1592
1593 if (left) {
1594 pos -= left;
1595 written -= left;
1596 err = -EFAULT;
1597 break;
1598 }
1599
1600
1601
1602
1603
1604
1605 cond_resched();
1606 } while (count);
1607
1608 *ppos = pos;
1609 if (written)
1610 err = written;
1611out:
1612 mutex_unlock(&inode->i_mutex);
1613 return err;
1614}
1615
1616static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
1617{
1618 struct inode *inode = filp->f_path.dentry->d_inode;
1619 struct address_space *mapping = inode->i_mapping;
1620 unsigned long index, offset;
1621
1622 index = *ppos >> PAGE_CACHE_SHIFT;
1623 offset = *ppos & ~PAGE_CACHE_MASK;
1624
1625 for (;;) {
1626 struct page *page = NULL;
1627 unsigned long end_index, nr, ret;
1628 loff_t i_size = i_size_read(inode);
1629
1630 end_index = i_size >> PAGE_CACHE_SHIFT;
1631 if (index > end_index)
1632 break;
1633 if (index == end_index) {
1634 nr = i_size & ~PAGE_CACHE_MASK;
1635 if (nr <= offset)
1636 break;
1637 }
1638
1639 desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL);
1640 if (desc->error) {
1641 if (desc->error == -EINVAL)
1642 desc->error = 0;
1643 break;
1644 }
1645
1646
1647
1648
1649
1650 nr = PAGE_CACHE_SIZE;
1651 i_size = i_size_read(inode);
1652 end_index = i_size >> PAGE_CACHE_SHIFT;
1653 if (index == end_index) {
1654 nr = i_size & ~PAGE_CACHE_MASK;
1655 if (nr <= offset) {
1656 if (page)
1657 page_cache_release(page);
1658 break;
1659 }
1660 }
1661 nr -= offset;
1662
1663 if (page) {
1664
1665
1666
1667
1668
1669 if (mapping_writably_mapped(mapping))
1670 flush_dcache_page(page);
1671
1672
1673
1674 if (!offset)
1675 mark_page_accessed(page);
1676 } else {
1677 page = ZERO_PAGE(0);
1678 page_cache_get(page);
1679 }
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691 ret = actor(desc, page, offset, nr);
1692 offset += ret;
1693 index += offset >> PAGE_CACHE_SHIFT;
1694 offset &= ~PAGE_CACHE_MASK;
1695
1696 page_cache_release(page);
1697 if (ret != nr || !desc->count)
1698 break;
1699
1700 cond_resched();
1701 }
1702
1703 *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
1704 file_accessed(filp);
1705}
1706
1707static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
1708{
1709 read_descriptor_t desc;
1710
1711 if ((ssize_t) count < 0)
1712 return -EINVAL;
1713 if (!access_ok(VERIFY_WRITE, buf, count))
1714 return -EFAULT;
1715 if (!count)
1716 return 0;
1717
1718 desc.written = 0;
1719 desc.count = count;
1720 desc.arg.buf = buf;
1721 desc.error = 0;
1722
1723 do_shmem_file_read(filp, ppos, &desc, file_read_actor);
1724 if (desc.written)
1725 return desc.written;
1726 return desc.error;
1727}
1728
1729static ssize_t shmem_file_sendfile(struct file *in_file, loff_t *ppos,
1730 size_t count, read_actor_t actor, void *target)
1731{
1732 read_descriptor_t desc;
1733
1734 if (!count)
1735 return 0;
1736
1737 desc.written = 0;
1738 desc.count = count;
1739 desc.arg.data = target;
1740 desc.error = 0;
1741
1742 do_shmem_file_read(in_file, ppos, &desc, actor);
1743 if (desc.written)
1744 return desc.written;
1745 return desc.error;
1746}
1747
1748static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1749{
1750 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1751
1752 buf->f_type = TMPFS_MAGIC;
1753 buf->f_bsize = PAGE_CACHE_SIZE;
1754 buf->f_namelen = NAME_MAX;
1755 spin_lock(&sbinfo->stat_lock);
1756 if (sbinfo->max_blocks) {
1757 buf->f_blocks = sbinfo->max_blocks;
1758 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
1759 }
1760 if (sbinfo->max_inodes) {
1761 buf->f_files = sbinfo->max_inodes;
1762 buf->f_ffree = sbinfo->free_inodes;
1763 }
1764
1765 spin_unlock(&sbinfo->stat_lock);
1766 return 0;
1767}
1768
1769
1770
1771
1772static int
1773shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1774{
1775 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1776 int error = -ENOSPC;
1777
1778 if (inode) {
1779 error = security_inode_init_security(inode, dir, NULL, NULL,
1780 NULL);
1781 if (error) {
1782 if (error != -EOPNOTSUPP) {
1783 iput(inode);
1784 return error;
1785 }
1786 }
1787 error = shmem_acl_init(inode, dir);
1788 if (error) {
1789 iput(inode);
1790 return error;
1791 }
1792 if (dir->i_mode & S_ISGID) {
1793 inode->i_gid = dir->i_gid;
1794 if (S_ISDIR(mode))
1795 inode->i_mode |= S_ISGID;
1796 }
1797 dir->i_size += BOGO_DIRENT_SIZE;
1798 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1799 d_instantiate(dentry, inode);
1800 dget(dentry);
1801 }
1802 return error;
1803}
1804
1805static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1806{
1807 int error;
1808
1809 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
1810 return error;
1811 inc_nlink(dir);
1812 return 0;
1813}
1814
1815static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
1816 struct nameidata *nd)
1817{
1818 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
1819}
1820
1821
1822
1823
1824static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
1825{
1826 struct inode *inode = old_dentry->d_inode;
1827 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1828
1829
1830
1831
1832
1833
1834 if (sbinfo->max_inodes) {
1835 spin_lock(&sbinfo->stat_lock);
1836 if (!sbinfo->free_inodes) {
1837 spin_unlock(&sbinfo->stat_lock);
1838 return -ENOSPC;
1839 }
1840 sbinfo->free_inodes--;
1841 spin_unlock(&sbinfo->stat_lock);
1842 }
1843
1844 dir->i_size += BOGO_DIRENT_SIZE;
1845 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1846 inc_nlink(inode);
1847 atomic_inc(&inode->i_count);
1848 dget(dentry);
1849 d_instantiate(dentry, inode);
1850 return 0;
1851}
1852
1853static int shmem_unlink(struct inode *dir, struct dentry *dentry)
1854{
1855 struct inode *inode = dentry->d_inode;
1856
1857 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) {
1858 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1859 if (sbinfo->max_inodes) {
1860 spin_lock(&sbinfo->stat_lock);
1861 sbinfo->free_inodes++;
1862 spin_unlock(&sbinfo->stat_lock);
1863 }
1864 }
1865
1866 dir->i_size -= BOGO_DIRENT_SIZE;
1867 inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1868 drop_nlink(inode);
1869 dput(dentry);
1870 return 0;
1871}
1872
1873static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
1874{
1875 if (!simple_empty(dentry))
1876 return -ENOTEMPTY;
1877
1878 drop_nlink(dentry->d_inode);
1879 drop_nlink(dir);
1880 return shmem_unlink(dir, dentry);
1881}
1882
1883
1884
1885
1886
1887
1888
1889static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
1890{
1891 struct inode *inode = old_dentry->d_inode;
1892 int they_are_dirs = S_ISDIR(inode->i_mode);
1893
1894 if (!simple_empty(new_dentry))
1895 return -ENOTEMPTY;
1896
1897 if (new_dentry->d_inode) {
1898 (void) shmem_unlink(new_dir, new_dentry);
1899 if (they_are_dirs)
1900 drop_nlink(old_dir);
1901 } else if (they_are_dirs) {
1902 drop_nlink(old_dir);
1903 inc_nlink(new_dir);
1904 }
1905
1906 old_dir->i_size -= BOGO_DIRENT_SIZE;
1907 new_dir->i_size += BOGO_DIRENT_SIZE;
1908 old_dir->i_ctime = old_dir->i_mtime =
1909 new_dir->i_ctime = new_dir->i_mtime =
1910 inode->i_ctime = CURRENT_TIME;
1911 return 0;
1912}
1913
1914static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
1915{
1916 int error;
1917 int len;
1918 struct inode *inode;
1919 struct page *page = NULL;
1920 char *kaddr;
1921 struct shmem_inode_info *info;
1922
1923 len = strlen(symname) + 1;
1924 if (len > PAGE_CACHE_SIZE)
1925 return -ENAMETOOLONG;
1926
1927 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1928 if (!inode)
1929 return -ENOSPC;
1930
1931 error = security_inode_init_security(inode, dir, NULL, NULL,
1932 NULL);
1933 if (error) {
1934 if (error != -EOPNOTSUPP) {
1935 iput(inode);
1936 return error;
1937 }
1938 error = 0;
1939 }
1940
1941 info = SHMEM_I(inode);
1942 inode->i_size = len-1;
1943 if (len <= (char *)inode - (char *)info) {
1944
1945 memcpy(info, symname, len);
1946 inode->i_op = &shmem_symlink_inline_operations;
1947 } else {
1948 error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
1949 if (error) {
1950 iput(inode);
1951 return error;
1952 }
1953 inode->i_op = &shmem_symlink_inode_operations;
1954 kaddr = kmap_atomic(page, KM_USER0);
1955 memcpy(kaddr, symname, len);
1956 kunmap_atomic(kaddr, KM_USER0);
1957 set_page_dirty(page);
1958 page_cache_release(page);
1959 }
1960 if (dir->i_mode & S_ISGID)
1961 inode->i_gid = dir->i_gid;
1962 dir->i_size += BOGO_DIRENT_SIZE;
1963 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1964 d_instantiate(dentry, inode);
1965 dget(dentry);
1966 return 0;
1967}
1968
1969static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
1970{
1971 nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
1972 return NULL;
1973}
1974
1975static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
1976{
1977 struct page *page = NULL;
1978 int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
1979 nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
1980 return page;
1981}
1982
1983static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1984{
1985 if (!IS_ERR(nd_get_link(nd))) {
1986 struct page *page = cookie;
1987 kunmap(page);
1988 mark_page_accessed(page);
1989 page_cache_release(page);
1990 }
1991}
1992
1993static const struct inode_operations shmem_symlink_inline_operations = {
1994 .readlink = generic_readlink,
1995 .follow_link = shmem_follow_link_inline,
1996};
1997
1998static const struct inode_operations shmem_symlink_inode_operations = {
1999 .truncate = shmem_truncate,
2000 .readlink = generic_readlink,
2001 .follow_link = shmem_follow_link,
2002 .put_link = shmem_put_link,
2003};
2004
2005#ifdef CONFIG_TMPFS_POSIX_ACL
2006
2007
2008
2009
2010
2011
2012
2013static size_t shmem_xattr_security_list(struct inode *inode, char *list,
2014 size_t list_len, const char *name,
2015 size_t name_len)
2016{
2017 return security_inode_listsecurity(inode, list, list_len);
2018}
2019
2020static int shmem_xattr_security_get(struct inode *inode, const char *name,
2021 void *buffer, size_t size)
2022{
2023 if (strcmp(name, "") == 0)
2024 return -EINVAL;
2025 return security_inode_getsecurity(inode, name, buffer, size,
2026 -EOPNOTSUPP);
2027}
2028
2029static int shmem_xattr_security_set(struct inode *inode, const char *name,
2030 const void *value, size_t size, int flags)
2031{
2032 if (strcmp(name, "") == 0)
2033 return -EINVAL;
2034 return security_inode_setsecurity(inode, name, value, size, flags);
2035}
2036
2037static struct xattr_handler shmem_xattr_security_handler = {
2038 .prefix = XATTR_SECURITY_PREFIX,
2039 .list = shmem_xattr_security_list,
2040 .get = shmem_xattr_security_get,
2041 .set = shmem_xattr_security_set,
2042};
2043
2044static struct xattr_handler *shmem_xattr_handlers[] = {
2045 &shmem_xattr_acl_access_handler,
2046 &shmem_xattr_acl_default_handler,
2047 &shmem_xattr_security_handler,
2048 NULL
2049};
2050#endif
2051
2052static struct dentry *shmem_get_parent(struct dentry *child)
2053{
2054 return ERR_PTR(-ESTALE);
2055}
2056
2057static int shmem_match(struct inode *ino, void *vfh)
2058{
2059 __u32 *fh = vfh;
2060 __u64 inum = fh[2];
2061 inum = (inum << 32) | fh[1];
2062 return ino->i_ino == inum && fh[0] == ino->i_generation;
2063}
2064
2065static struct dentry *shmem_get_dentry(struct super_block *sb, void *vfh)
2066{
2067 struct dentry *de = NULL;
2068 struct inode *inode;
2069 __u32 *fh = vfh;
2070 __u64 inum = fh[2];
2071 inum = (inum << 32) | fh[1];
2072
2073 inode = ilookup5(sb, (unsigned long)(inum+fh[0]), shmem_match, vfh);
2074 if (inode) {
2075 de = d_find_alias(inode);
2076 iput(inode);
2077 }
2078
2079 return de? de: ERR_PTR(-ESTALE);
2080}
2081
2082static struct dentry *shmem_decode_fh(struct super_block *sb, __u32 *fh,
2083 int len, int type,
2084 int (*acceptable)(void *context, struct dentry *de),
2085 void *context)
2086{
2087 if (len < 3)
2088 return ERR_PTR(-ESTALE);
2089
2090 return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable,
2091 context);
2092}
2093
2094static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
2095 int connectable)
2096{
2097 struct inode *inode = dentry->d_inode;
2098
2099 if (*len < 3)
2100 return 255;
2101
2102 if (hlist_unhashed(&inode->i_hash)) {
2103
2104
2105
2106
2107
2108 static DEFINE_SPINLOCK(lock);
2109 spin_lock(&lock);
2110 if (hlist_unhashed(&inode->i_hash))
2111 __insert_inode_hash(inode,
2112 inode->i_ino + inode->i_generation);
2113 spin_unlock(&lock);
2114 }
2115
2116 fh[0] = inode->i_generation;
2117 fh[1] = inode->i_ino;
2118 fh[2] = ((__u64)inode->i_ino) >> 32;
2119
2120 *len = 3;
2121 return 1;
2122}
2123
2124static struct export_operations shmem_export_ops = {
2125 .get_parent = shmem_get_parent,
2126 .get_dentry = shmem_get_dentry,
2127 .encode_fh = shmem_encode_fh,
2128 .decode_fh = shmem_decode_fh,
2129};
2130
2131static int shmem_parse_options(char *options, int *mode, uid_t *uid,
2132 gid_t *gid, unsigned long *blocks, unsigned long *inodes,
2133 int *policy, nodemask_t *policy_nodes)
2134{
2135 char *this_char, *value, *rest;
2136
2137 while (options != NULL) {
2138 this_char = options;
2139 for (;;) {
2140
2141
2142
2143
2144
2145 options = strchr(options, ',');
2146 if (options == NULL)
2147 break;
2148 options++;
2149 if (!isdigit(*options)) {
2150 options[-1] = '\0';
2151 break;
2152 }
2153 }
2154 if (!*this_char)
2155 continue;
2156 if ((value = strchr(this_char,'=')) != NULL) {
2157 *value++ = 0;
2158 } else {
2159 printk(KERN_ERR
2160 "tmpfs: No value for mount option '%s'\n",
2161 this_char);
2162 return 1;
2163 }
2164
2165 if (!strcmp(this_char,"size")) {
2166 unsigned long long size;
2167 size = memparse(value,&rest);
2168 if (*rest == '%') {
2169 size <<= PAGE_SHIFT;
2170 size *= totalram_pages;
2171 do_div(size, 100);
2172 rest++;
2173 }
2174 if (*rest)
2175 goto bad_val;
2176 *blocks = size >> PAGE_CACHE_SHIFT;
2177 } else if (!strcmp(this_char,"nr_blocks")) {
2178 *blocks = memparse(value,&rest);
2179 if (*rest)
2180 goto bad_val;
2181 } else if (!strcmp(this_char,"nr_inodes")) {
2182 *inodes = memparse(value,&rest);
2183 if (*rest)
2184 goto bad_val;
2185 } else if (!strcmp(this_char,"mode")) {
2186 if (!mode)
2187 continue;
2188 *mode = simple_strtoul(value,&rest,8);
2189 if (*rest)
2190 goto bad_val;
2191 } else if (!strcmp(this_char,"uid")) {
2192 if (!uid)
2193 continue;
2194 *uid = simple_strtoul(value,&rest,0);
2195 if (*rest)
2196 goto bad_val;
2197 } else if (!strcmp(this_char,"gid")) {
2198 if (!gid)
2199 continue;
2200 *gid = simple_strtoul(value,&rest,0);
2201 if (*rest)
2202 goto bad_val;
2203 } else if (!strcmp(this_char,"mpol")) {
2204 if (shmem_parse_mpol(value,policy,policy_nodes))
2205 goto bad_val;
2206 } else {
2207 printk(KERN_ERR "tmpfs: Bad mount option %s\n",
2208 this_char);
2209 return 1;
2210 }
2211 }
2212 return 0;
2213
2214bad_val:
2215 printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
2216 value, this_char);
2217 return 1;
2218
2219}
2220
2221static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
2222{
2223 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2224 unsigned long max_blocks = sbinfo->max_blocks;
2225 unsigned long max_inodes = sbinfo->max_inodes;
2226 int policy = sbinfo->policy;
2227 nodemask_t policy_nodes = sbinfo->policy_nodes;
2228 unsigned long blocks;
2229 unsigned long inodes;
2230 int error = -EINVAL;
2231
2232 if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks,
2233 &max_inodes, &policy, &policy_nodes))
2234 return error;
2235
2236 spin_lock(&sbinfo->stat_lock);
2237 blocks = sbinfo->max_blocks - sbinfo->free_blocks;
2238 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
2239 if (max_blocks < blocks)
2240 goto out;
2241 if (max_inodes < inodes)
2242 goto out;
2243
2244
2245
2246
2247
2248
2249 if (max_blocks && !sbinfo->max_blocks)
2250 goto out;
2251 if (max_inodes && !sbinfo->max_inodes)
2252 goto out;
2253
2254 error = 0;
2255 sbinfo->max_blocks = max_blocks;
2256 sbinfo->free_blocks = max_blocks - blocks;
2257 sbinfo->max_inodes = max_inodes;
2258 sbinfo->free_inodes = max_inodes - inodes;
2259 sbinfo->policy = policy;
2260 sbinfo->policy_nodes = policy_nodes;
2261out:
2262 spin_unlock(&sbinfo->stat_lock);
2263 return error;
2264}
2265#endif
2266
2267static void shmem_put_super(struct super_block *sb)
2268{
2269 kfree(sb->s_fs_info);
2270 sb->s_fs_info = NULL;
2271}
2272
2273static int shmem_fill_super(struct super_block *sb,
2274 void *data, int silent)
2275{
2276 struct inode *inode;
2277 struct dentry *root;
2278 int mode = S_IRWXUGO | S_ISVTX;
2279 uid_t uid = current->fsuid;
2280 gid_t gid = current->fsgid;
2281 int err = -ENOMEM;
2282 struct shmem_sb_info *sbinfo;
2283 unsigned long blocks = 0;
2284 unsigned long inodes = 0;
2285 int policy = MPOL_DEFAULT;
2286 nodemask_t policy_nodes = node_online_map;
2287
2288#ifdef CONFIG_TMPFS
2289
2290
2291
2292
2293
2294 if (!(sb->s_flags & MS_NOUSER)) {
2295 blocks = totalram_pages / 2;
2296 inodes = totalram_pages - totalhigh_pages;
2297 if (inodes > blocks)
2298 inodes = blocks;
2299 if (shmem_parse_options(data, &mode, &uid, &gid, &blocks,
2300 &inodes, &policy, &policy_nodes))
2301 return -EINVAL;
2302 }
2303 sb->s_export_op = &shmem_export_ops;
2304#else
2305 sb->s_flags |= MS_NOUSER;
2306#endif
2307
2308
2309 sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info),
2310 L1_CACHE_BYTES), GFP_KERNEL);
2311 if (!sbinfo)
2312 return -ENOMEM;
2313
2314 spin_lock_init(&sbinfo->stat_lock);
2315 sbinfo->max_blocks = blocks;
2316 sbinfo->free_blocks = blocks;
2317 sbinfo->max_inodes = inodes;
2318 sbinfo->free_inodes = inodes;
2319 sbinfo->policy = policy;
2320 sbinfo->policy_nodes = policy_nodes;
2321
2322 sb->s_fs_info = sbinfo;
2323 sb->s_maxbytes = SHMEM_MAX_BYTES;
2324 sb->s_blocksize = PAGE_CACHE_SIZE;
2325 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2326 sb->s_magic = TMPFS_MAGIC;
2327 sb->s_op = &shmem_ops;
2328 sb->s_time_gran = 1;
2329#ifdef CONFIG_TMPFS_POSIX_ACL
2330 sb->s_xattr = shmem_xattr_handlers;
2331 sb->s_flags |= MS_POSIXACL;
2332#endif
2333
2334 inode = shmem_get_inode(sb, S_IFDIR | mode, 0);
2335 if (!inode)
2336 goto failed;
2337 inode->i_uid = uid;
2338 inode->i_gid = gid;
2339 root = d_alloc_root(inode);
2340 if (!root)
2341 goto failed_iput;
2342 sb->s_root = root;
2343 return 0;
2344
2345failed_iput:
2346 iput(inode);
2347failed:
2348 shmem_put_super(sb);
2349 return err;
2350}
2351
2352static struct kmem_cache *shmem_inode_cachep;
2353
2354static struct inode *shmem_alloc_inode(struct super_block *sb)
2355{
2356 struct shmem_inode_info *p;
2357 p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
2358 if (!p)
2359 return NULL;
2360 return &p->vfs_inode;
2361}
2362
2363static void shmem_destroy_inode(struct inode *inode)
2364{
2365 if ((inode->i_mode & S_IFMT) == S_IFREG) {
2366
2367 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
2368 }
2369 shmem_acl_destroy_inode(inode);
2370 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
2371}
2372
2373static void init_once(void *foo, struct kmem_cache *cachep,
2374 unsigned long flags)
2375{
2376 struct shmem_inode_info *p = (struct shmem_inode_info *) foo;
2377
2378 inode_init_once(&p->vfs_inode);
2379#ifdef CONFIG_TMPFS_POSIX_ACL
2380 p->i_acl = NULL;
2381 p->i_default_acl = NULL;
2382#endif
2383}
2384
2385static int init_inodecache(void)
2386{
2387 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
2388 sizeof(struct shmem_inode_info),
2389 0, 0, init_once, NULL);
2390 if (shmem_inode_cachep == NULL)
2391 return -ENOMEM;
2392 return 0;
2393}
2394
2395static void destroy_inodecache(void)
2396{
2397 kmem_cache_destroy(shmem_inode_cachep);
2398}
2399
2400static const struct address_space_operations shmem_aops = {
2401 .writepage = shmem_writepage,
2402 .set_page_dirty = __set_page_dirty_no_writeback,
2403#ifdef CONFIG_TMPFS
2404 .prepare_write = shmem_prepare_write,
2405 .commit_write = simple_commit_write,
2406#endif
2407 .migratepage = migrate_page,
2408};
2409
2410static const struct file_operations shmem_file_operations = {
2411 .mmap = shmem_mmap,
2412#ifdef CONFIG_TMPFS
2413 .llseek = generic_file_llseek,
2414 .read = shmem_file_read,
2415 .write = shmem_file_write,
2416 .fsync = simple_sync_file,
2417 .sendfile = shmem_file_sendfile,
2418#endif
2419};
2420
2421static const struct inode_operations shmem_inode_operations = {
2422 .truncate = shmem_truncate,
2423 .setattr = shmem_notify_change,
2424 .truncate_range = shmem_truncate_range,
2425#ifdef CONFIG_TMPFS_POSIX_ACL
2426 .setxattr = generic_setxattr,
2427 .getxattr = generic_getxattr,
2428 .listxattr = generic_listxattr,
2429 .removexattr = generic_removexattr,
2430 .permission = shmem_permission,
2431#endif
2432
2433};
2434
2435static const struct inode_operations shmem_dir_inode_operations = {
2436#ifdef CONFIG_TMPFS
2437 .create = shmem_create,
2438 .lookup = simple_lookup,
2439 .link = shmem_link,
2440 .unlink = shmem_unlink,
2441 .symlink = shmem_symlink,
2442 .mkdir = shmem_mkdir,
2443 .rmdir = shmem_rmdir,
2444 .mknod = shmem_mknod,
2445 .rename = shmem_rename,
2446#endif
2447#ifdef CONFIG_TMPFS_POSIX_ACL
2448 .setattr = shmem_notify_change,
2449 .setxattr = generic_setxattr,
2450 .getxattr = generic_getxattr,
2451 .listxattr = generic_listxattr,
2452 .removexattr = generic_removexattr,
2453 .permission = shmem_permission,
2454#endif
2455};
2456
2457static const struct inode_operations shmem_special_inode_operations = {
2458#ifdef CONFIG_TMPFS_POSIX_ACL
2459 .setattr = shmem_notify_change,
2460 .setxattr = generic_setxattr,
2461 .getxattr = generic_getxattr,
2462 .listxattr = generic_listxattr,
2463 .removexattr = generic_removexattr,
2464 .permission = shmem_permission,
2465#endif
2466};
2467
2468static const struct super_operations shmem_ops = {
2469 .alloc_inode = shmem_alloc_inode,
2470 .destroy_inode = shmem_destroy_inode,
2471#ifdef CONFIG_TMPFS
2472 .statfs = shmem_statfs,
2473 .remount_fs = shmem_remount_fs,
2474#endif
2475 .delete_inode = shmem_delete_inode,
2476 .drop_inode = generic_delete_inode,
2477 .put_super = shmem_put_super,
2478};
2479
2480static struct vm_operations_struct shmem_vm_ops = {
2481 .nopage = shmem_nopage,
2482 .populate = shmem_populate,
2483#ifdef CONFIG_NUMA
2484 .set_policy = shmem_set_policy,
2485 .get_policy = shmem_get_policy,
2486#endif
2487};
2488
2489
2490static int shmem_get_sb(struct file_system_type *fs_type,
2491 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2492{
2493 return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
2494}
2495
2496static struct file_system_type tmpfs_fs_type = {
2497 .owner = THIS_MODULE,
2498 .name = "tmpfs",
2499 .get_sb = shmem_get_sb,
2500 .kill_sb = kill_litter_super,
2501};
2502static struct vfsmount *shm_mnt;
2503
2504static int __init init_tmpfs(void)
2505{
2506 int error;
2507
2508 error = init_inodecache();
2509 if (error)
2510 goto out3;
2511
2512 error = register_filesystem(&tmpfs_fs_type);
2513 if (error) {
2514 printk(KERN_ERR "Could not register tmpfs\n");
2515 goto out2;
2516 }
2517
2518 shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER,
2519 tmpfs_fs_type.name, NULL);
2520 if (IS_ERR(shm_mnt)) {
2521 error = PTR_ERR(shm_mnt);
2522 printk(KERN_ERR "Could not kern_mount tmpfs\n");
2523 goto out1;
2524 }
2525 return 0;
2526
2527out1:
2528 unregister_filesystem(&tmpfs_fs_type);
2529out2:
2530 destroy_inodecache();
2531out3:
2532 shm_mnt = ERR_PTR(error);
2533 return error;
2534}
2535module_init(init_tmpfs)
2536
2537
2538
2539
2540
2541
2542
2543
2544struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2545{
2546 int error;
2547 struct file *file;
2548 struct inode *inode;
2549 struct dentry *dentry, *root;
2550 struct qstr this;
2551
2552 if (IS_ERR(shm_mnt))
2553 return (void *)shm_mnt;
2554
2555 if (size < 0 || size > SHMEM_MAX_BYTES)
2556 return ERR_PTR(-EINVAL);
2557
2558 if (shmem_acct_size(flags, size))
2559 return ERR_PTR(-ENOMEM);
2560
2561 error = -ENOMEM;
2562 this.name = name;
2563 this.len = strlen(name);
2564 this.hash = 0;
2565 root = shm_mnt->mnt_root;
2566 dentry = d_alloc(root, &this);
2567 if (!dentry)
2568 goto put_memory;
2569
2570 error = -ENFILE;
2571 file = get_empty_filp();
2572 if (!file)
2573 goto put_dentry;
2574
2575 error = -ENOSPC;
2576 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2577 if (!inode)
2578 goto close_file;
2579
2580 SHMEM_I(inode)->flags = flags & VM_ACCOUNT;
2581 d_instantiate(dentry, inode);
2582 inode->i_size = size;
2583 inode->i_nlink = 0;
2584 file->f_path.mnt = mntget(shm_mnt);
2585 file->f_path.dentry = dentry;
2586 file->f_mapping = inode->i_mapping;
2587 file->f_op = &shmem_file_operations;
2588 file->f_mode = FMODE_WRITE | FMODE_READ;
2589 return file;
2590
2591close_file:
2592 put_filp(file);
2593put_dentry:
2594 dput(dentry);
2595put_memory:
2596 shmem_unacct_size(flags, size);
2597 return ERR_PTR(error);
2598}
2599
2600
2601
2602
2603
2604
2605int shmem_zero_setup(struct vm_area_struct *vma)
2606{
2607 struct file *file;
2608 loff_t size = vma->vm_end - vma->vm_start;
2609
2610 file = shmem_file_setup("dev/zero", size, vma->vm_flags);
2611 if (IS_ERR(file))
2612 return PTR_ERR(file);
2613
2614 if (vma->vm_file)
2615 fput(vma->vm_file);
2616 vma->vm_file = file;
2617 vma->vm_ops = &shmem_vm_ops;
2618 return 0;
2619}
2620