shmem.c (17cf28afea2a1112f240a3a2da8af883be024811) shmem.c (e2d12e22c59ce714008aa5266d769f8568d74eac)
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.

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

1597 }
1598 return error;
1599}
1600
1601static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1602 loff_t len)
1603{
1604 struct inode *inode = file->f_path.dentry->d_inode;
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.

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

1597 }
1598 return error;
1599}
1600
1601static long shmem_fallocate(struct file *file, int mode, loff_t offset,
1602 loff_t len)
1603{
1604 struct inode *inode = file->f_path.dentry->d_inode;
1605 int error = -EOPNOTSUPP;
1605 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
1606 pgoff_t start, index, end;
1607 int error;
1606
1607 mutex_lock(&inode->i_mutex);
1608
1609 if (mode & FALLOC_FL_PUNCH_HOLE) {
1610 struct address_space *mapping = file->f_mapping;
1611 loff_t unmap_start = round_up(offset, PAGE_SIZE);
1612 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
1613
1614 if ((u64)unmap_end > (u64)unmap_start)
1615 unmap_mapping_range(mapping, unmap_start,
1616 1 + unmap_end - unmap_start, 0);
1617 shmem_truncate_range(inode, offset, offset + len - 1);
1618 /* No need to unmap again: hole-punching leaves COWed pages */
1619 error = 0;
1608
1609 mutex_lock(&inode->i_mutex);
1610
1611 if (mode & FALLOC_FL_PUNCH_HOLE) {
1612 struct address_space *mapping = file->f_mapping;
1613 loff_t unmap_start = round_up(offset, PAGE_SIZE);
1614 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
1615
1616 if ((u64)unmap_end > (u64)unmap_start)
1617 unmap_mapping_range(mapping, unmap_start,
1618 1 + unmap_end - unmap_start, 0);
1619 shmem_truncate_range(inode, offset, offset + len - 1);
1620 /* No need to unmap again: hole-punching leaves COWed pages */
1621 error = 0;
1622 goto out;
1620 }
1621
1623 }
1624
1625 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
1626 error = inode_newsize_ok(inode, offset + len);
1627 if (error)
1628 goto out;
1629
1630 start = offset >> PAGE_CACHE_SHIFT;
1631 end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1632 /* Try to avoid a swapstorm if len is impossible to satisfy */
1633 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
1634 error = -ENOSPC;
1635 goto out;
1636 }
1637
1638 for (index = start; index < end; index++) {
1639 struct page *page;
1640
1641 /*
1642 * Good, the fallocate(2) manpage permits EINTR: we may have
1643 * been interrupted because we are using up too much memory.
1644 */
1645 if (signal_pending(current))
1646 error = -EINTR;
1647 else
1648 error = shmem_getpage(inode, index, &page, SGP_WRITE,
1649 NULL);
1650 if (error) {
1651 /*
1652 * We really ought to free what we allocated so far,
1653 * but it would be wrong to free pages allocated
1654 * earlier, or already now in use: i_mutex does not
1655 * exclude all cases. We do not know what to free.
1656 */
1657 goto ctime;
1658 }
1659
1660 if (!PageUptodate(page)) {
1661 clear_highpage(page);
1662 flush_dcache_page(page);
1663 SetPageUptodate(page);
1664 }
1665 /*
1666 * set_page_dirty so that memory pressure will swap rather
1667 * than free the pages we are allocating (and SGP_CACHE pages
1668 * might still be clean: we now need to mark those dirty too).
1669 */
1670 set_page_dirty(page);
1671 unlock_page(page);
1672 page_cache_release(page);
1673 cond_resched();
1674 }
1675
1676 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
1677 i_size_write(inode, offset + len);
1678ctime:
1679 inode->i_ctime = CURRENT_TIME;
1680out:
1622 mutex_unlock(&inode->i_mutex);
1623 return error;
1624}
1625
1626static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1627{
1628 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1629

--- 1211 unchanged lines hidden ---
1681 mutex_unlock(&inode->i_mutex);
1682 return error;
1683}
1684
1685static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1686{
1687 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
1688

--- 1211 unchanged lines hidden ---