shmem.c (1635f6a74152f1dcd1b888231609d64875f0a81a) shmem.c (1aac1400319d30786f32b9290e9cc923937b3d57)
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.

--- 70 unchanged lines hidden (view full) ---

79
80struct shmem_xattr {
81 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
82 char *name; /* xattr name */
83 size_t size;
84 char value[0];
85};
86
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.

--- 70 unchanged lines hidden (view full) ---

79
80struct shmem_xattr {
81 struct list_head list; /* anchored by shmem_inode_info->xattr_list */
82 char *name; /* xattr name */
83 size_t size;
84 char value[0];
85};
86
87/*
88 * shmem_fallocate and shmem_writepage communicate via inode->i_private
89 * (with i_mutex making sure that it has only one user at a time):
90 * we would prefer not to enlarge the shmem inode just for that.
91 */
92struct shmem_falloc {
93 pgoff_t start; /* start of range currently being fallocated */
94 pgoff_t next; /* the next page offset to be fallocated */
95 pgoff_t nr_falloced; /* how many new pages have been fallocated */
96 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
97};
98
87/* Flag allocation requirements to shmem_getpage */
88enum sgp_type {
89 SGP_READ, /* don't exceed i_size, don't allocate page */
90 SGP_CACHE, /* don't exceed i_size, may allocate page */
91 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
92 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
93 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
94};

--- 691 unchanged lines hidden (view full) ---

786 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
787 goto redirty;
788 }
789
790 /*
791 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
792 * value into swapfile.c, the only way we can correctly account for a
793 * fallocated page arriving here is now to initialize it and write it.
99/* Flag allocation requirements to shmem_getpage */
100enum sgp_type {
101 SGP_READ, /* don't exceed i_size, don't allocate page */
102 SGP_CACHE, /* don't exceed i_size, may allocate page */
103 SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
104 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
105 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
106};

--- 691 unchanged lines hidden (view full) ---

798 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
799 goto redirty;
800 }
801
802 /*
803 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
804 * value into swapfile.c, the only way we can correctly account for a
805 * fallocated page arriving here is now to initialize it and write it.
806 *
807 * That's okay for a page already fallocated earlier, but if we have
808 * not yet completed the fallocation, then (a) we want to keep track
809 * of this page in case we have to undo it, and (b) it may not be a
810 * good idea to continue anyway, once we're pushing into swap. So
811 * reactivate the page, and let shmem_fallocate() quit when too many.
794 */
795 if (!PageUptodate(page)) {
812 */
813 if (!PageUptodate(page)) {
814 if (inode->i_private) {
815 struct shmem_falloc *shmem_falloc;
816 spin_lock(&inode->i_lock);
817 shmem_falloc = inode->i_private;
818 if (shmem_falloc &&
819 index >= shmem_falloc->start &&
820 index < shmem_falloc->next)
821 shmem_falloc->nr_unswapped++;
822 else
823 shmem_falloc = NULL;
824 spin_unlock(&inode->i_lock);
825 if (shmem_falloc)
826 goto redirty;
827 }
796 clear_highpage(page);
797 flush_dcache_page(page);
798 SetPageUptodate(page);
799 }
800
801 swap = get_swap_page();
802 if (!swap.val)
803 goto redirty;

--- 838 unchanged lines hidden (view full) ---

1642 return error;
1643}
1644
1645static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1646 loff_t len)
1647{
1648 struct inode *inode = file->f_path.dentry->d_inode;
1649 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
828 clear_highpage(page);
829 flush_dcache_page(page);
830 SetPageUptodate(page);
831 }
832
833 swap = get_swap_page();
834 if (!swap.val)
835 goto redirty;

--- 838 unchanged lines hidden (view full) ---

1674 return error;
1675}
1676
1677static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1678 loff_t len)
1679{
1680 struct inode *inode = file->f_path.dentry->d_inode;
1681 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1682 struct shmem_falloc shmem_falloc;
1650 pgoff_t start, index, end;
1651 int error;
1652
1653 mutex_lock(&inode->i_mutex);
1654
1655 if (mode & FALLOC_FL_PUNCH_HOLE) {
1656 struct address_space *mapping = file->f_mapping;
1657 loff_t unmap_start = round_up(offset, PAGE_SIZE);

--- 16 unchanged lines hidden (view full) ---

1674 start = offset >> PAGE_CACHE_SHIFT;
1675 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1676 /* Try to avoid a swapstorm if len is impossible to satisfy */
1677 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1678 error = -ENOSPC;
1679 goto out;
1680 }
1681
1683 pgoff_t start, index, end;
1684 int error;
1685
1686 mutex_lock(&inode->i_mutex);
1687
1688 if (mode & FALLOC_FL_PUNCH_HOLE) {
1689 struct address_space *mapping = file->f_mapping;
1690 loff_t unmap_start = round_up(offset, PAGE_SIZE);

--- 16 unchanged lines hidden (view full) ---

1707 start = offset >> PAGE_CACHE_SHIFT;
1708 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1709 /* Try to avoid a swapstorm if len is impossible to satisfy */
1710 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1711 error = -ENOSPC;
1712 goto out;
1713 }
1714
1715 shmem_falloc.start = start;
1716 shmem_falloc.next = start;
1717 shmem_falloc.nr_falloced = 0;
1718 shmem_falloc.nr_unswapped = 0;
1719 spin_lock(&inode->i_lock);
1720 inode->i_private = &shmem_falloc;
1721 spin_unlock(&inode->i_lock);
1722
1682 for (index = start; index < end; index++) {
1683 struct page *page;
1684
1685 /*
1686 * Good, the fallocate(2) manpage permits EINTR: we may have
1687 * been interrupted because we are using up too much memory.
1688 */
1689 if (signal_pending(current))
1690 error = -EINTR;
1723 for (index = start; index < end; index++) {
1724 struct page *page;
1725
1726 /*
1727 * Good, the fallocate(2) manpage permits EINTR: we may have
1728 * been interrupted because we are using up too much memory.
1729 */
1730 if (signal_pending(current))
1731 error = -EINTR;
1732 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
1733 error = -ENOMEM;
1691 else
1692 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
1693 NULL);
1694 if (error) {
1695 /* Remove the !PageUptodate pages we added */
1696 shmem_undo_range(inode,
1697 (loff_t)start << PAGE_CACHE_SHIFT,
1698 (loff_t)index << PAGE_CACHE_SHIFT, true);
1734 else
1735 error = shmem_getpage(inode, index, &page, SGP_FALLOC,
1736 NULL);
1737 if (error) {
1738 /* Remove the !PageUptodate pages we added */
1739 shmem_undo_range(inode,
1740 (loff_t)start << PAGE_CACHE_SHIFT,
1741 (loff_t)index << PAGE_CACHE_SHIFT, true);
1699 goto ctime;
1742 goto undone;
1700 }
1701
1702 /*
1743 }
1744
1745 /*
1746 * Inform shmem_writepage() how far we have reached.
1747 * No need for lock or barrier: we have the page lock.
1748 */
1749 shmem_falloc.next++;
1750 if (!PageUptodate(page))
1751 shmem_falloc.nr_falloced++;
1752
1753 /*
1703 * If !PageUptodate, leave it that way so that freeable pages
1704 * can be recognized if we need to rollback on error later.
1705 * But set_page_dirty so that memory pressure will swap rather
1706 * than free the pages we are allocating (and SGP_CACHE pages
1707 * might still be clean: we now need to mark those dirty too).
1708 */
1709 set_page_dirty(page);
1710 unlock_page(page);
1711 page_cache_release(page);
1712 cond_resched();
1713 }
1714
1715 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1716 i_size_write(inode, offset + len);
1754 * If !PageUptodate, leave it that way so that freeable pages
1755 * can be recognized if we need to rollback on error later.
1756 * But set_page_dirty so that memory pressure will swap rather
1757 * than free the pages we are allocating (and SGP_CACHE pages
1758 * might still be clean: we now need to mark those dirty too).
1759 */
1760 set_page_dirty(page);
1761 unlock_page(page);
1762 page_cache_release(page);
1763 cond_resched();
1764 }
1765
1766 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1767 i_size_write(inode, offset + len);
1717ctime:
1718 inode->i_ctime = CURRENT_TIME;
1768 inode->i_ctime = CURRENT_TIME;
1769undone:
1770 spin_lock(&inode->i_lock);
1771 inode->i_private = NULL;
1772 spin_unlock(&inode->i_lock);
1719out:
1720 mutex_unlock(&inode->i_mutex);
1721 return error;
1722}
1723
1724static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1725{
1726 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);

--- 1212 unchanged lines hidden ---
1773out:
1774 mutex_unlock(&inode->i_mutex);
1775 return error;
1776}
1777
1778static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1779{
1780 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);

--- 1212 unchanged lines hidden ---