diff options
Diffstat (limited to 'fs/ntfs/aops.c')
| -rw-r--r-- | fs/ntfs/aops.c | 832 |
1 files changed, 2 insertions, 830 deletions
diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c index 5e80c07c6a4d..1c0a4315876a 100644 --- a/fs/ntfs/aops.c +++ b/fs/ntfs/aops.c | |||
| @@ -1391,8 +1391,7 @@ retry_writepage: | |||
| 1391 | if (NInoEncrypted(ni)) { | 1391 | if (NInoEncrypted(ni)) { |
| 1392 | unlock_page(page); | 1392 | unlock_page(page); |
| 1393 | BUG_ON(ni->type != AT_DATA); | 1393 | BUG_ON(ni->type != AT_DATA); |
| 1394 | ntfs_debug("Denying write access to encrypted " | 1394 | ntfs_debug("Denying write access to encrypted file."); |
| 1395 | "file."); | ||
| 1396 | return -EACCES; | 1395 | return -EACCES; |
| 1397 | } | 1396 | } |
| 1398 | /* Compressed data streams are handled in compress.c. */ | 1397 | /* Compressed data streams are handled in compress.c. */ |
| @@ -1508,8 +1507,8 @@ retry_writepage: | |||
| 1508 | /* Zero out of bounds area in the page cache page. */ | 1507 | /* Zero out of bounds area in the page cache page. */ |
| 1509 | memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); | 1508 | memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); |
| 1510 | kunmap_atomic(kaddr, KM_USER0); | 1509 | kunmap_atomic(kaddr, KM_USER0); |
| 1511 | flush_dcache_mft_record_page(ctx->ntfs_ino); | ||
| 1512 | flush_dcache_page(page); | 1510 | flush_dcache_page(page); |
| 1511 | flush_dcache_mft_record_page(ctx->ntfs_ino); | ||
| 1513 | /* We are done with the page. */ | 1512 | /* We are done with the page. */ |
| 1514 | end_page_writeback(page); | 1513 | end_page_writeback(page); |
| 1515 | /* Finally, mark the mft record dirty, so it gets written back. */ | 1514 | /* Finally, mark the mft record dirty, so it gets written back. */ |
| @@ -1542,830 +1541,6 @@ err_out: | |||
| 1542 | return err; | 1541 | return err; |
| 1543 | } | 1542 | } |
| 1544 | 1543 | ||
| 1545 | /** | ||
| 1546 | * ntfs_prepare_nonresident_write - | ||
| 1547 | * | ||
| 1548 | */ | ||
| 1549 | static int ntfs_prepare_nonresident_write(struct page *page, | ||
| 1550 | unsigned from, unsigned to) | ||
| 1551 | { | ||
| 1552 | VCN vcn; | ||
| 1553 | LCN lcn; | ||
| 1554 | s64 initialized_size; | ||
| 1555 | loff_t i_size; | ||
| 1556 | sector_t block, ablock, iblock; | ||
| 1557 | struct inode *vi; | ||
| 1558 | ntfs_inode *ni; | ||
| 1559 | ntfs_volume *vol; | ||
| 1560 | runlist_element *rl; | ||
| 1561 | struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; | ||
| 1562 | unsigned long flags; | ||
| 1563 | unsigned int vcn_ofs, block_start, block_end, blocksize; | ||
| 1564 | int err; | ||
| 1565 | BOOL is_retry; | ||
| 1566 | unsigned char blocksize_bits; | ||
| 1567 | |||
| 1568 | vi = page->mapping->host; | ||
| 1569 | ni = NTFS_I(vi); | ||
| 1570 | vol = ni->vol; | ||
| 1571 | |||
| 1572 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " | ||
| 1573 | "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type, | ||
| 1574 | page->index, from, to); | ||
| 1575 | |||
| 1576 | BUG_ON(!NInoNonResident(ni)); | ||
| 1577 | |||
| 1578 | blocksize_bits = vi->i_blkbits; | ||
| 1579 | blocksize = 1 << blocksize_bits; | ||
| 1580 | |||
| 1581 | /* | ||
| 1582 | * create_empty_buffers() will create uptodate/dirty buffers if the | ||
| 1583 | * page is uptodate/dirty. | ||
| 1584 | */ | ||
| 1585 | if (!page_has_buffers(page)) | ||
| 1586 | create_empty_buffers(page, blocksize, 0); | ||
| 1587 | bh = head = page_buffers(page); | ||
| 1588 | if (unlikely(!bh)) | ||
| 1589 | return -ENOMEM; | ||
| 1590 | |||
| 1591 | /* The first block in the page. */ | ||
| 1592 | block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); | ||
| 1593 | |||
| 1594 | read_lock_irqsave(&ni->size_lock, flags); | ||
| 1595 | /* | ||
| 1596 | * The first out of bounds block for the allocated size. No need to | ||
| 1597 | * round up as allocated_size is in multiples of cluster size and the | ||
| 1598 | * minimum cluster size is 512 bytes, which is equal to the smallest | ||
| 1599 | * blocksize. | ||
| 1600 | */ | ||
| 1601 | ablock = ni->allocated_size >> blocksize_bits; | ||
| 1602 | i_size = i_size_read(vi); | ||
| 1603 | initialized_size = ni->initialized_size; | ||
| 1604 | read_unlock_irqrestore(&ni->size_lock, flags); | ||
| 1605 | |||
| 1606 | /* The last (fully or partially) initialized block. */ | ||
| 1607 | iblock = initialized_size >> blocksize_bits; | ||
| 1608 | |||
| 1609 | /* Loop through all the buffers in the page. */ | ||
| 1610 | block_start = 0; | ||
| 1611 | rl = NULL; | ||
| 1612 | err = 0; | ||
| 1613 | do { | ||
| 1614 | block_end = block_start + blocksize; | ||
| 1615 | /* | ||
| 1616 | * If buffer @bh is outside the write, just mark it uptodate | ||
| 1617 | * if the page is uptodate and continue with the next buffer. | ||
| 1618 | */ | ||
| 1619 | if (block_end <= from || block_start >= to) { | ||
| 1620 | if (PageUptodate(page)) { | ||
| 1621 | if (!buffer_uptodate(bh)) | ||
| 1622 | set_buffer_uptodate(bh); | ||
| 1623 | } | ||
| 1624 | continue; | ||
| 1625 | } | ||
| 1626 | /* | ||
| 1627 | * @bh is at least partially being written to. | ||
| 1628 | * Make sure it is not marked as new. | ||
| 1629 | */ | ||
| 1630 | //if (buffer_new(bh)) | ||
| 1631 | // clear_buffer_new(bh); | ||
| 1632 | |||
| 1633 | if (block >= ablock) { | ||
| 1634 | // TODO: block is above allocated_size, need to | ||
| 1635 | // allocate it. Best done in one go to accommodate not | ||
| 1636 | // only block but all above blocks up to and including: | ||
| 1637 | // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize | ||
| 1638 | // - 1) >> blobksize_bits. Obviously will need to round | ||
| 1639 | // up to next cluster boundary, too. This should be | ||
| 1640 | // done with a helper function, so it can be reused. | ||
| 1641 | ntfs_error(vol->sb, "Writing beyond allocated size " | ||
| 1642 | "is not supported yet. Sorry."); | ||
| 1643 | err = -EOPNOTSUPP; | ||
| 1644 | goto err_out; | ||
| 1645 | // Need to update ablock. | ||
| 1646 | // Need to set_buffer_new() on all block bhs that are | ||
| 1647 | // newly allocated. | ||
| 1648 | } | ||
| 1649 | /* | ||
| 1650 | * Now we have enough allocated size to fulfill the whole | ||
| 1651 | * request, i.e. block < ablock is true. | ||
| 1652 | */ | ||
| 1653 | if (unlikely((block >= iblock) && | ||
| 1654 | (initialized_size < i_size))) { | ||
| 1655 | /* | ||
| 1656 | * If this page is fully outside initialized size, zero | ||
| 1657 | * out all pages between the current initialized size | ||
| 1658 | * and the current page. Just use ntfs_readpage() to do | ||
| 1659 | * the zeroing transparently. | ||
| 1660 | */ | ||
| 1661 | if (block > iblock) { | ||
| 1662 | // TODO: | ||
| 1663 | // For each page do: | ||
| 1664 | // - read_cache_page() | ||
| 1665 | // Again for each page do: | ||
| 1666 | // - wait_on_page_locked() | ||
| 1667 | // - Check (PageUptodate(page) && | ||
| 1668 | // !PageError(page)) | ||
| 1669 | // Update initialized size in the attribute and | ||
| 1670 | // in the inode. | ||
| 1671 | // Again, for each page do: | ||
| 1672 | // __set_page_dirty_buffers(); | ||
| 1673 | // page_cache_release() | ||
| 1674 | // We don't need to wait on the writes. | ||
| 1675 | // Update iblock. | ||
| 1676 | } | ||
| 1677 | /* | ||
| 1678 | * The current page straddles initialized size. Zero | ||
| 1679 | * all non-uptodate buffers and set them uptodate (and | ||
| 1680 | * dirty?). Note, there aren't any non-uptodate buffers | ||
| 1681 | * if the page is uptodate. | ||
| 1682 | * FIXME: For an uptodate page, the buffers may need to | ||
| 1683 | * be written out because they were not initialized on | ||
| 1684 | * disk before. | ||
| 1685 | */ | ||
| 1686 | if (!PageUptodate(page)) { | ||
| 1687 | // TODO: | ||
| 1688 | // Zero any non-uptodate buffers up to i_size. | ||
| 1689 | // Set them uptodate and dirty. | ||
| 1690 | } | ||
| 1691 | // TODO: | ||
| 1692 | // Update initialized size in the attribute and in the | ||
| 1693 | // inode (up to i_size). | ||
| 1694 | // Update iblock. | ||
| 1695 | // FIXME: This is inefficient. Try to batch the two | ||
| 1696 | // size changes to happen in one go. | ||
| 1697 | ntfs_error(vol->sb, "Writing beyond initialized size " | ||
| 1698 | "is not supported yet. Sorry."); | ||
| 1699 | err = -EOPNOTSUPP; | ||
| 1700 | goto err_out; | ||
| 1701 | // Do NOT set_buffer_new() BUT DO clear buffer range | ||
| 1702 | // outside write request range. | ||
| 1703 | // set_buffer_uptodate() on complete buffers as well as | ||
| 1704 | // set_buffer_dirty(). | ||
| 1705 | } | ||
| 1706 | |||
| 1707 | /* Need to map unmapped buffers. */ | ||
| 1708 | if (!buffer_mapped(bh)) { | ||
| 1709 | /* Unmapped buffer. Need to map it. */ | ||
| 1710 | bh->b_bdev = vol->sb->s_bdev; | ||
| 1711 | |||
| 1712 | /* Convert block into corresponding vcn and offset. */ | ||
| 1713 | vcn = (VCN)block << blocksize_bits >> | ||
| 1714 | vol->cluster_size_bits; | ||
| 1715 | vcn_ofs = ((VCN)block << blocksize_bits) & | ||
| 1716 | vol->cluster_size_mask; | ||
| 1717 | |||
| 1718 | is_retry = FALSE; | ||
| 1719 | if (!rl) { | ||
| 1720 | lock_retry_remap: | ||
| 1721 | down_read(&ni->runlist.lock); | ||
| 1722 | rl = ni->runlist.rl; | ||
| 1723 | } | ||
| 1724 | if (likely(rl != NULL)) { | ||
| 1725 | /* Seek to element containing target vcn. */ | ||
| 1726 | while (rl->length && rl[1].vcn <= vcn) | ||
| 1727 | rl++; | ||
| 1728 | lcn = ntfs_rl_vcn_to_lcn(rl, vcn); | ||
| 1729 | } else | ||
| 1730 | lcn = LCN_RL_NOT_MAPPED; | ||
| 1731 | if (unlikely(lcn < 0)) { | ||
| 1732 | /* | ||
| 1733 | * We extended the attribute allocation above. | ||
| 1734 | * If we hit an ENOENT here it means that the | ||
| 1735 | * allocation was insufficient which is a bug. | ||
| 1736 | */ | ||
| 1737 | BUG_ON(lcn == LCN_ENOENT); | ||
| 1738 | |||
| 1739 | /* It is a hole, need to instantiate it. */ | ||
| 1740 | if (lcn == LCN_HOLE) { | ||
| 1741 | // TODO: Instantiate the hole. | ||
| 1742 | // clear_buffer_new(bh); | ||
| 1743 | // unmap_underlying_metadata(bh->b_bdev, | ||
| 1744 | // bh->b_blocknr); | ||
| 1745 | // For non-uptodate buffers, need to | ||
| 1746 | // zero out the region outside the | ||
| 1747 | // request in this bh or all bhs, | ||
| 1748 | // depending on what we implemented | ||
| 1749 | // above. | ||
| 1750 | // Need to flush_dcache_page(). | ||
| 1751 | // Or could use set_buffer_new() | ||
| 1752 | // instead? | ||
| 1753 | ntfs_error(vol->sb, "Writing into " | ||
| 1754 | "sparse regions is " | ||
| 1755 | "not supported yet. " | ||
| 1756 | "Sorry."); | ||
| 1757 | err = -EOPNOTSUPP; | ||
| 1758 | if (!rl) | ||
| 1759 | up_read(&ni->runlist.lock); | ||
| 1760 | goto err_out; | ||
| 1761 | } else if (!is_retry && | ||
| 1762 | lcn == LCN_RL_NOT_MAPPED) { | ||
| 1763 | is_retry = TRUE; | ||
| 1764 | /* | ||
| 1765 | * Attempt to map runlist, dropping | ||
| 1766 | * lock for the duration. | ||
| 1767 | */ | ||
| 1768 | up_read(&ni->runlist.lock); | ||
| 1769 | err = ntfs_map_runlist(ni, vcn); | ||
| 1770 | if (likely(!err)) | ||
| 1771 | goto lock_retry_remap; | ||
| 1772 | rl = NULL; | ||
| 1773 | } else if (!rl) | ||
| 1774 | up_read(&ni->runlist.lock); | ||
| 1775 | /* | ||
| 1776 | * Failed to map the buffer, even after | ||
| 1777 | * retrying. | ||
| 1778 | */ | ||
| 1779 | if (!err) | ||
| 1780 | err = -EIO; | ||
| 1781 | bh->b_blocknr = -1; | ||
| 1782 | ntfs_error(vol->sb, "Failed to write to inode " | ||
| 1783 | "0x%lx, attribute type 0x%x, " | ||
| 1784 | "vcn 0x%llx, offset 0x%x " | ||
| 1785 | "because its location on disk " | ||
| 1786 | "could not be determined%s " | ||
| 1787 | "(error code %i).", | ||
| 1788 | ni->mft_no, ni->type, | ||
| 1789 | (unsigned long long)vcn, | ||
| 1790 | vcn_ofs, is_retry ? " even " | ||
| 1791 | "after retrying" : "", err); | ||
| 1792 | goto err_out; | ||
| 1793 | } | ||
| 1794 | /* We now have a successful remap, i.e. lcn >= 0. */ | ||
| 1795 | |||
| 1796 | /* Setup buffer head to correct block. */ | ||
| 1797 | bh->b_blocknr = ((lcn << vol->cluster_size_bits) | ||
| 1798 | + vcn_ofs) >> blocksize_bits; | ||
| 1799 | set_buffer_mapped(bh); | ||
| 1800 | |||
| 1801 | // FIXME: Something analogous to this is needed for | ||
| 1802 | // each newly allocated block, i.e. BH_New. | ||
| 1803 | // FIXME: Might need to take this out of the | ||
| 1804 | // if (!buffer_mapped(bh)) {}, depending on how we | ||
| 1805 | // implement things during the allocated_size and | ||
| 1806 | // initialized_size extension code above. | ||
| 1807 | if (buffer_new(bh)) { | ||
| 1808 | clear_buffer_new(bh); | ||
| 1809 | unmap_underlying_metadata(bh->b_bdev, | ||
| 1810 | bh->b_blocknr); | ||
| 1811 | if (PageUptodate(page)) { | ||
| 1812 | set_buffer_uptodate(bh); | ||
| 1813 | continue; | ||
| 1814 | } | ||
| 1815 | /* | ||
| 1816 | * Page is _not_ uptodate, zero surrounding | ||
| 1817 | * region. NOTE: This is how we decide if to | ||
| 1818 | * zero or not! | ||
| 1819 | */ | ||
| 1820 | if (block_end > to || block_start < from) { | ||
| 1821 | void *kaddr; | ||
| 1822 | |||
| 1823 | kaddr = kmap_atomic(page, KM_USER0); | ||
| 1824 | if (block_end > to) | ||
| 1825 | memset(kaddr + to, 0, | ||
| 1826 | block_end - to); | ||
| 1827 | if (block_start < from) | ||
| 1828 | memset(kaddr + block_start, 0, | ||
| 1829 | from - | ||
| 1830 | block_start); | ||
| 1831 | flush_dcache_page(page); | ||
| 1832 | kunmap_atomic(kaddr, KM_USER0); | ||
| 1833 | } | ||
| 1834 | continue; | ||
| 1835 | } | ||
| 1836 | } | ||
| 1837 | /* @bh is mapped, set it uptodate if the page is uptodate. */ | ||
| 1838 | if (PageUptodate(page)) { | ||
| 1839 | if (!buffer_uptodate(bh)) | ||
| 1840 | set_buffer_uptodate(bh); | ||
| 1841 | continue; | ||
| 1842 | } | ||
| 1843 | /* | ||
| 1844 | * The page is not uptodate. The buffer is mapped. If it is not | ||
| 1845 | * uptodate, and it is only partially being written to, we need | ||
| 1846 | * to read the buffer in before the write, i.e. right now. | ||
| 1847 | */ | ||
| 1848 | if (!buffer_uptodate(bh) && | ||
| 1849 | (block_start < from || block_end > to)) { | ||
| 1850 | ll_rw_block(READ, 1, &bh); | ||
| 1851 | *wait_bh++ = bh; | ||
| 1852 | } | ||
| 1853 | } while (block++, block_start = block_end, | ||
| 1854 | (bh = bh->b_this_page) != head); | ||
| 1855 | |||
| 1856 | /* Release the lock if we took it. */ | ||
| 1857 | if (rl) { | ||
| 1858 | up_read(&ni->runlist.lock); | ||
| 1859 | rl = NULL; | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | /* If we issued read requests, let them complete. */ | ||
| 1863 | while (wait_bh > wait) { | ||
| 1864 | wait_on_buffer(*--wait_bh); | ||
| 1865 | if (!buffer_uptodate(*wait_bh)) | ||
| 1866 | return -EIO; | ||
| 1867 | } | ||
| 1868 | |||
| 1869 | ntfs_debug("Done."); | ||
| 1870 | return 0; | ||
| 1871 | err_out: | ||
| 1872 | /* | ||
| 1873 | * Zero out any newly allocated blocks to avoid exposing stale data. | ||
| 1874 | * If BH_New is set, we know that the block was newly allocated in the | ||
| 1875 | * above loop. | ||
| 1876 | * FIXME: What about initialized_size increments? Have we done all the | ||
| 1877 | * required zeroing above? If not this error handling is broken, and | ||
| 1878 | * in particular the if (block_end <= from) check is completely bogus. | ||
| 1879 | */ | ||
| 1880 | bh = head; | ||
| 1881 | block_start = 0; | ||
| 1882 | is_retry = FALSE; | ||
| 1883 | do { | ||
| 1884 | block_end = block_start + blocksize; | ||
| 1885 | if (block_end <= from) | ||
| 1886 | continue; | ||
| 1887 | if (block_start >= to) | ||
| 1888 | break; | ||
| 1889 | if (buffer_new(bh)) { | ||
| 1890 | void *kaddr; | ||
| 1891 | |||
| 1892 | clear_buffer_new(bh); | ||
| 1893 | kaddr = kmap_atomic(page, KM_USER0); | ||
| 1894 | memset(kaddr + block_start, 0, bh->b_size); | ||
| 1895 | kunmap_atomic(kaddr, KM_USER0); | ||
| 1896 | set_buffer_uptodate(bh); | ||
| 1897 | mark_buffer_dirty(bh); | ||
| 1898 | is_retry = TRUE; | ||
| 1899 | } | ||
| 1900 | } while (block_start = block_end, (bh = bh->b_this_page) != head); | ||
| 1901 | if (is_retry) | ||
| 1902 | flush_dcache_page(page); | ||
| 1903 | if (rl) | ||
| 1904 | up_read(&ni->runlist.lock); | ||
| 1905 | return err; | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | /** | ||
| 1909 | * ntfs_prepare_write - prepare a page for receiving data | ||
| 1910 | * | ||
| 1911 | * This is called from generic_file_write() with i_sem held on the inode | ||
| 1912 | * (@page->mapping->host). The @page is locked but not kmap()ped. The source | ||
| 1913 | * data has not yet been copied into the @page. | ||
| 1914 | * | ||
| 1915 | * Need to extend the attribute/fill in holes if necessary, create blocks and | ||
| 1916 | * make partially overwritten blocks uptodate, | ||
| 1917 | * | ||
| 1918 | * i_size is not to be modified yet. | ||
| 1919 | * | ||
| 1920 | * Return 0 on success or -errno on error. | ||
| 1921 | * | ||
| 1922 | * Should be using block_prepare_write() [support for sparse files] or | ||
| 1923 | * cont_prepare_write() [no support for sparse files]. Cannot do that due to | ||
| 1924 | * ntfs specifics but can look at them for implementation guidance. | ||
| 1925 | * | ||
| 1926 | * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is | ||
| 1927 | * the first byte in the page that will be written to and @to is the first byte | ||
| 1928 | * after the last byte that will be written to. | ||
| 1929 | */ | ||
| 1930 | static int ntfs_prepare_write(struct file *file, struct page *page, | ||
| 1931 | unsigned from, unsigned to) | ||
| 1932 | { | ||
| 1933 | s64 new_size; | ||
| 1934 | loff_t i_size; | ||
| 1935 | struct inode *vi = page->mapping->host; | ||
| 1936 | ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); | ||
| 1937 | ntfs_volume *vol = ni->vol; | ||
| 1938 | ntfs_attr_search_ctx *ctx = NULL; | ||
| 1939 | MFT_RECORD *m = NULL; | ||
| 1940 | ATTR_RECORD *a; | ||
| 1941 | u8 *kaddr; | ||
| 1942 | u32 attr_len; | ||
| 1943 | int err; | ||
| 1944 | |||
| 1945 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " | ||
| 1946 | "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type, | ||
| 1947 | page->index, from, to); | ||
| 1948 | BUG_ON(!PageLocked(page)); | ||
| 1949 | BUG_ON(from > PAGE_CACHE_SIZE); | ||
| 1950 | BUG_ON(to > PAGE_CACHE_SIZE); | ||
| 1951 | BUG_ON(from > to); | ||
| 1952 | BUG_ON(NInoMstProtected(ni)); | ||
| 1953 | /* | ||
| 1954 | * If a previous ntfs_truncate() failed, repeat it and abort if it | ||
| 1955 | * fails again. | ||
| 1956 | */ | ||
| 1957 | if (unlikely(NInoTruncateFailed(ni))) { | ||
| 1958 | down_write(&vi->i_alloc_sem); | ||
| 1959 | err = ntfs_truncate(vi); | ||
| 1960 | up_write(&vi->i_alloc_sem); | ||
| 1961 | if (err || NInoTruncateFailed(ni)) { | ||
| 1962 | if (!err) | ||
| 1963 | err = -EIO; | ||
| 1964 | goto err_out; | ||
| 1965 | } | ||
| 1966 | } | ||
| 1967 | /* If the attribute is not resident, deal with it elsewhere. */ | ||
| 1968 | if (NInoNonResident(ni)) { | ||
| 1969 | /* | ||
| 1970 | * Only unnamed $DATA attributes can be compressed, encrypted, | ||
| 1971 | * and/or sparse. | ||
| 1972 | */ | ||
| 1973 | if (ni->type == AT_DATA && !ni->name_len) { | ||
| 1974 | /* If file is encrypted, deny access, just like NT4. */ | ||
| 1975 | if (NInoEncrypted(ni)) { | ||
| 1976 | ntfs_debug("Denying write access to encrypted " | ||
| 1977 | "file."); | ||
| 1978 | return -EACCES; | ||
| 1979 | } | ||
| 1980 | /* Compressed data streams are handled in compress.c. */ | ||
| 1981 | if (NInoCompressed(ni)) { | ||
| 1982 | // TODO: Implement and replace this check with | ||
| 1983 | // return ntfs_write_compressed_block(page); | ||
| 1984 | ntfs_error(vi->i_sb, "Writing to compressed " | ||
| 1985 | "files is not supported yet. " | ||
| 1986 | "Sorry."); | ||
| 1987 | return -EOPNOTSUPP; | ||
| 1988 | } | ||
| 1989 | // TODO: Implement and remove this check. | ||
| 1990 | if (NInoSparse(ni)) { | ||
| 1991 | ntfs_error(vi->i_sb, "Writing to sparse files " | ||
| 1992 | "is not supported yet. Sorry."); | ||
| 1993 | return -EOPNOTSUPP; | ||
| 1994 | } | ||
| 1995 | } | ||
| 1996 | /* Normal data stream. */ | ||
| 1997 | return ntfs_prepare_nonresident_write(page, from, to); | ||
| 1998 | } | ||
| 1999 | /* | ||
| 2000 | * Attribute is resident, implying it is not compressed, encrypted, or | ||
| 2001 | * sparse. | ||
| 2002 | */ | ||
| 2003 | BUG_ON(page_has_buffers(page)); | ||
| 2004 | new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to; | ||
| 2005 | /* If we do not need to resize the attribute allocation we are done. */ | ||
| 2006 | if (new_size <= i_size_read(vi)) | ||
| 2007 | goto done; | ||
| 2008 | /* Map, pin, and lock the (base) mft record. */ | ||
| 2009 | if (!NInoAttr(ni)) | ||
| 2010 | base_ni = ni; | ||
| 2011 | else | ||
| 2012 | base_ni = ni->ext.base_ntfs_ino; | ||
| 2013 | m = map_mft_record(base_ni); | ||
| 2014 | if (IS_ERR(m)) { | ||
| 2015 | err = PTR_ERR(m); | ||
| 2016 | m = NULL; | ||
| 2017 | ctx = NULL; | ||
| 2018 | goto err_out; | ||
| 2019 | } | ||
| 2020 | ctx = ntfs_attr_get_search_ctx(base_ni, m); | ||
| 2021 | if (unlikely(!ctx)) { | ||
| 2022 | err = -ENOMEM; | ||
| 2023 | goto err_out; | ||
| 2024 | } | ||
| 2025 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, | ||
| 2026 | CASE_SENSITIVE, 0, NULL, 0, ctx); | ||
| 2027 | if (unlikely(err)) { | ||
| 2028 | if (err == -ENOENT) | ||
| 2029 | err = -EIO; | ||
| 2030 | goto err_out; | ||
| 2031 | } | ||
| 2032 | m = ctx->mrec; | ||
| 2033 | a = ctx->attr; | ||
| 2034 | /* The total length of the attribute value. */ | ||
| 2035 | attr_len = le32_to_cpu(a->data.resident.value_length); | ||
| 2036 | /* Fix an eventual previous failure of ntfs_commit_write(). */ | ||
| 2037 | i_size = i_size_read(vi); | ||
| 2038 | if (unlikely(attr_len > i_size)) { | ||
| 2039 | attr_len = i_size; | ||
| 2040 | a->data.resident.value_length = cpu_to_le32(attr_len); | ||
| 2041 | } | ||
| 2042 | /* If we do not need to resize the attribute allocation we are done. */ | ||
| 2043 | if (new_size <= attr_len) | ||
| 2044 | goto done_unm; | ||
| 2045 | /* Check if new size is allowed in $AttrDef. */ | ||
| 2046 | err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); | ||
| 2047 | if (unlikely(err)) { | ||
| 2048 | if (err == -ERANGE) { | ||
| 2049 | ntfs_error(vol->sb, "Write would cause the inode " | ||
| 2050 | "0x%lx to exceed the maximum size for " | ||
| 2051 | "its attribute type (0x%x). Aborting " | ||
| 2052 | "write.", vi->i_ino, | ||
| 2053 | le32_to_cpu(ni->type)); | ||
| 2054 | } else { | ||
| 2055 | ntfs_error(vol->sb, "Inode 0x%lx has unknown " | ||
| 2056 | "attribute type 0x%x. Aborting " | ||
| 2057 | "write.", vi->i_ino, | ||
| 2058 | le32_to_cpu(ni->type)); | ||
| 2059 | err = -EIO; | ||
| 2060 | } | ||
| 2061 | goto err_out2; | ||
| 2062 | } | ||
| 2063 | /* | ||
| 2064 | * Extend the attribute record to be able to store the new attribute | ||
| 2065 | * size. | ||
| 2066 | */ | ||
| 2067 | if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a, | ||
| 2068 | le16_to_cpu(a->data.resident.value_offset) + | ||
| 2069 | new_size)) { | ||
| 2070 | /* Not enough space in the mft record. */ | ||
| 2071 | ntfs_error(vol->sb, "Not enough space in the mft record for " | ||
| 2072 | "the resized attribute value. This is not " | ||
| 2073 | "supported yet. Aborting write."); | ||
| 2074 | err = -EOPNOTSUPP; | ||
| 2075 | goto err_out2; | ||
| 2076 | } | ||
| 2077 | /* | ||
| 2078 | * We have enough space in the mft record to fit the write. This | ||
| 2079 | * implies the attribute is smaller than the mft record and hence the | ||
| 2080 | * attribute must be in a single page and hence page->index must be 0. | ||
| 2081 | */ | ||
| 2082 | BUG_ON(page->index); | ||
| 2083 | /* | ||
| 2084 | * If the beginning of the write is past the old size, enlarge the | ||
| 2085 | * attribute value up to the beginning of the write and fill it with | ||
| 2086 | * zeroes. | ||
| 2087 | */ | ||
| 2088 | if (from > attr_len) { | ||
| 2089 | memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) + | ||
| 2090 | attr_len, 0, from - attr_len); | ||
| 2091 | a->data.resident.value_length = cpu_to_le32(from); | ||
| 2092 | /* Zero the corresponding area in the page as well. */ | ||
| 2093 | if (PageUptodate(page)) { | ||
| 2094 | kaddr = kmap_atomic(page, KM_USER0); | ||
| 2095 | memset(kaddr + attr_len, 0, from - attr_len); | ||
| 2096 | kunmap_atomic(kaddr, KM_USER0); | ||
| 2097 | flush_dcache_page(page); | ||
| 2098 | } | ||
| 2099 | } | ||
| 2100 | flush_dcache_mft_record_page(ctx->ntfs_ino); | ||
| 2101 | mark_mft_record_dirty(ctx->ntfs_ino); | ||
| 2102 | done_unm: | ||
| 2103 | ntfs_attr_put_search_ctx(ctx); | ||
| 2104 | unmap_mft_record(base_ni); | ||
| 2105 | /* | ||
| 2106 | * Because resident attributes are handled by memcpy() to/from the | ||
| 2107 | * corresponding MFT record, and because this form of i/o is byte | ||
| 2108 | * aligned rather than block aligned, there is no need to bring the | ||
| 2109 | * page uptodate here as in the non-resident case where we need to | ||
| 2110 | * bring the buffers straddled by the write uptodate before | ||
| 2111 | * generic_file_write() does the copying from userspace. | ||
| 2112 | * | ||
| 2113 | * We thus defer the uptodate bringing of the page region outside the | ||
| 2114 | * region written to to ntfs_commit_write(), which makes the code | ||
| 2115 | * simpler and saves one atomic kmap which is good. | ||
| 2116 | */ | ||
| 2117 | done: | ||
| 2118 | ntfs_debug("Done."); | ||
| 2119 | return 0; | ||
| 2120 | err_out: | ||
| 2121 | if (err == -ENOMEM) | ||
| 2122 | ntfs_warning(vi->i_sb, "Error allocating memory required to " | ||
| 2123 | "prepare the write."); | ||
| 2124 | else { | ||
| 2125 | ntfs_error(vi->i_sb, "Resident attribute prepare write failed " | ||
| 2126 | "with error %i.", err); | ||
| 2127 | NVolSetErrors(vol); | ||
| 2128 | make_bad_inode(vi); | ||
| 2129 | } | ||
| 2130 | err_out2: | ||
| 2131 | if (ctx) | ||
| 2132 | ntfs_attr_put_search_ctx(ctx); | ||
| 2133 | if (m) | ||
| 2134 | unmap_mft_record(base_ni); | ||
| 2135 | return err; | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | /** | ||
| 2139 | * ntfs_commit_nonresident_write - | ||
| 2140 | * | ||
| 2141 | */ | ||
| 2142 | static int ntfs_commit_nonresident_write(struct page *page, | ||
| 2143 | unsigned from, unsigned to) | ||
| 2144 | { | ||
| 2145 | s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to; | ||
| 2146 | struct inode *vi = page->mapping->host; | ||
| 2147 | struct buffer_head *bh, *head; | ||
| 2148 | unsigned int block_start, block_end, blocksize; | ||
| 2149 | BOOL partial; | ||
| 2150 | |||
| 2151 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " | ||
| 2152 | "0x%lx, from = %u, to = %u.", vi->i_ino, | ||
| 2153 | NTFS_I(vi)->type, page->index, from, to); | ||
| 2154 | blocksize = 1 << vi->i_blkbits; | ||
| 2155 | |||
| 2156 | // FIXME: We need a whole slew of special cases in here for compressed | ||
| 2157 | // files for example... | ||
| 2158 | // For now, we know ntfs_prepare_write() would have failed so we can't | ||
| 2159 | // get here in any of the cases which we have to special case, so we | ||
| 2160 | // are just a ripped off, unrolled generic_commit_write(). | ||
| 2161 | |||
| 2162 | bh = head = page_buffers(page); | ||
| 2163 | block_start = 0; | ||
| 2164 | partial = FALSE; | ||
| 2165 | do { | ||
| 2166 | block_end = block_start + blocksize; | ||
| 2167 | if (block_end <= from || block_start >= to) { | ||
| 2168 | if (!buffer_uptodate(bh)) | ||
| 2169 | partial = TRUE; | ||
| 2170 | } else { | ||
| 2171 | set_buffer_uptodate(bh); | ||
| 2172 | mark_buffer_dirty(bh); | ||
| 2173 | } | ||
| 2174 | } while (block_start = block_end, (bh = bh->b_this_page) != head); | ||
| 2175 | /* | ||
| 2176 | * If this is a partial write which happened to make all buffers | ||
| 2177 | * uptodate then we can optimize away a bogus ->readpage() for the next | ||
| 2178 | * read(). Here we 'discover' whether the page went uptodate as a | ||
| 2179 | * result of this (potentially partial) write. | ||
| 2180 | */ | ||
| 2181 | if (!partial) | ||
| 2182 | SetPageUptodate(page); | ||
| 2183 | /* | ||
| 2184 | * Not convinced about this at all. See disparity comment above. For | ||
| 2185 | * now we know ntfs_prepare_write() would have failed in the write | ||
| 2186 | * exceeds i_size case, so this will never trigger which is fine. | ||
| 2187 | */ | ||
| 2188 | if (pos > i_size_read(vi)) { | ||
| 2189 | ntfs_error(vi->i_sb, "Writing beyond the existing file size is " | ||
| 2190 | "not supported yet. Sorry."); | ||
| 2191 | return -EOPNOTSUPP; | ||
| 2192 | // vi->i_size = pos; | ||
| 2193 | // mark_inode_dirty(vi); | ||
| 2194 | } | ||
| 2195 | ntfs_debug("Done."); | ||
| 2196 | return 0; | ||
| 2197 | } | ||
| 2198 | |||
| 2199 | /** | ||
| 2200 | * ntfs_commit_write - commit the received data | ||
| 2201 | * | ||
| 2202 | * This is called from generic_file_write() with i_sem held on the inode | ||
| 2203 | * (@page->mapping->host). The @page is locked but not kmap()ped. The source | ||
| 2204 | * data has already been copied into the @page. ntfs_prepare_write() has been | ||
| 2205 | * called before the data copied and it returned success so we can take the | ||
| 2206 | * results of various BUG checks and some error handling for granted. | ||
| 2207 | * | ||
| 2208 | * Need to mark modified blocks dirty so they get written out later when | ||
| 2209 | * ntfs_writepage() is invoked by the VM. | ||
| 2210 | * | ||
| 2211 | * Return 0 on success or -errno on error. | ||
| 2212 | * | ||
| 2213 | * Should be using generic_commit_write(). This marks buffers uptodate and | ||
| 2214 | * dirty, sets the page uptodate if all buffers in the page are uptodate, and | ||
| 2215 | * updates i_size if the end of io is beyond i_size. In that case, it also | ||
| 2216 | * marks the inode dirty. | ||
| 2217 | * | ||
| 2218 | * Cannot use generic_commit_write() due to ntfs specialities but can look at | ||
| 2219 | * it for implementation guidance. | ||
| 2220 | * | ||
| 2221 | * If things have gone as outlined in ntfs_prepare_write(), then we do not | ||
| 2222 | * need to do any page content modifications here at all, except in the write | ||
| 2223 | * to resident attribute case, where we need to do the uptodate bringing here | ||
| 2224 | * which we combine with the copying into the mft record which means we save | ||
| 2225 | * one atomic kmap. | ||
| 2226 | */ | ||
| 2227 | static int ntfs_commit_write(struct file *file, struct page *page, | ||
| 2228 | unsigned from, unsigned to) | ||
| 2229 | { | ||
| 2230 | struct inode *vi = page->mapping->host; | ||
| 2231 | ntfs_inode *base_ni, *ni = NTFS_I(vi); | ||
| 2232 | char *kaddr, *kattr; | ||
| 2233 | ntfs_attr_search_ctx *ctx; | ||
| 2234 | MFT_RECORD *m; | ||
| 2235 | ATTR_RECORD *a; | ||
| 2236 | u32 attr_len; | ||
| 2237 | int err; | ||
| 2238 | |||
| 2239 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " | ||
| 2240 | "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type, | ||
| 2241 | page->index, from, to); | ||
| 2242 | /* If the attribute is not resident, deal with it elsewhere. */ | ||
| 2243 | if (NInoNonResident(ni)) { | ||
| 2244 | /* Only unnamed $DATA attributes can be compressed/encrypted. */ | ||
| 2245 | if (ni->type == AT_DATA && !ni->name_len) { | ||
| 2246 | /* Encrypted files need separate handling. */ | ||
| 2247 | if (NInoEncrypted(ni)) { | ||
| 2248 | // We never get here at present! | ||
| 2249 | BUG(); | ||
| 2250 | } | ||
| 2251 | /* Compressed data streams are handled in compress.c. */ | ||
| 2252 | if (NInoCompressed(ni)) { | ||
| 2253 | // TODO: Implement this! | ||
| 2254 | // return ntfs_write_compressed_block(page); | ||
| 2255 | // We never get here at present! | ||
| 2256 | BUG(); | ||
| 2257 | } | ||
| 2258 | } | ||
| 2259 | /* Normal data stream. */ | ||
| 2260 | return ntfs_commit_nonresident_write(page, from, to); | ||
| 2261 | } | ||
| 2262 | /* | ||
| 2263 | * Attribute is resident, implying it is not compressed, encrypted, or | ||
| 2264 | * sparse. | ||
| 2265 | */ | ||
| 2266 | if (!NInoAttr(ni)) | ||
| 2267 | base_ni = ni; | ||
| 2268 | else | ||
| 2269 | base_ni = ni->ext.base_ntfs_ino; | ||
| 2270 | /* Map, pin, and lock the mft record. */ | ||
| 2271 | m = map_mft_record(base_ni); | ||
| 2272 | if (IS_ERR(m)) { | ||
| 2273 | err = PTR_ERR(m); | ||
| 2274 | m = NULL; | ||
| 2275 | ctx = NULL; | ||
| 2276 | goto err_out; | ||
| 2277 | } | ||
| 2278 | ctx = ntfs_attr_get_search_ctx(base_ni, m); | ||
| 2279 | if (unlikely(!ctx)) { | ||
| 2280 | err = -ENOMEM; | ||
| 2281 | goto err_out; | ||
| 2282 | } | ||
| 2283 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, | ||
| 2284 | CASE_SENSITIVE, 0, NULL, 0, ctx); | ||
| 2285 | if (unlikely(err)) { | ||
| 2286 | if (err == -ENOENT) | ||
| 2287 | err = -EIO; | ||
| 2288 | goto err_out; | ||
| 2289 | } | ||
| 2290 | a = ctx->attr; | ||
| 2291 | /* The total length of the attribute value. */ | ||
| 2292 | attr_len = le32_to_cpu(a->data.resident.value_length); | ||
| 2293 | BUG_ON(from > attr_len); | ||
| 2294 | kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset); | ||
| 2295 | kaddr = kmap_atomic(page, KM_USER0); | ||
| 2296 | /* Copy the received data from the page to the mft record. */ | ||
| 2297 | memcpy(kattr + from, kaddr + from, to - from); | ||
| 2298 | /* Update the attribute length if necessary. */ | ||
| 2299 | if (to > attr_len) { | ||
| 2300 | attr_len = to; | ||
| 2301 | a->data.resident.value_length = cpu_to_le32(attr_len); | ||
| 2302 | } | ||
| 2303 | /* | ||
| 2304 | * If the page is not uptodate, bring the out of bounds area(s) | ||
| 2305 | * uptodate by copying data from the mft record to the page. | ||
| 2306 | */ | ||
| 2307 | if (!PageUptodate(page)) { | ||
| 2308 | if (from > 0) | ||
| 2309 | memcpy(kaddr, kattr, from); | ||
| 2310 | if (to < attr_len) | ||
| 2311 | memcpy(kaddr + to, kattr + to, attr_len - to); | ||
| 2312 | /* Zero the region outside the end of the attribute value. */ | ||
| 2313 | if (attr_len < PAGE_CACHE_SIZE) | ||
| 2314 | memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); | ||
| 2315 | /* | ||
| 2316 | * The probability of not having done any of the above is | ||
| 2317 | * extremely small, so we just flush unconditionally. | ||
| 2318 | */ | ||
| 2319 | flush_dcache_page(page); | ||
| 2320 | SetPageUptodate(page); | ||
| 2321 | } | ||
| 2322 | kunmap_atomic(kaddr, KM_USER0); | ||
| 2323 | /* Update i_size if necessary. */ | ||
| 2324 | if (i_size_read(vi) < attr_len) { | ||
| 2325 | unsigned long flags; | ||
| 2326 | |||
| 2327 | write_lock_irqsave(&ni->size_lock, flags); | ||
| 2328 | ni->allocated_size = ni->initialized_size = attr_len; | ||
| 2329 | i_size_write(vi, attr_len); | ||
| 2330 | write_unlock_irqrestore(&ni->size_lock, flags); | ||
| 2331 | } | ||
| 2332 | /* Mark the mft record dirty, so it gets written back. */ | ||
| 2333 | flush_dcache_mft_record_page(ctx->ntfs_ino); | ||
| 2334 | mark_mft_record_dirty(ctx->ntfs_ino); | ||
| 2335 | ntfs_attr_put_search_ctx(ctx); | ||
| 2336 | unmap_mft_record(base_ni); | ||
| 2337 | ntfs_debug("Done."); | ||
| 2338 | return 0; | ||
| 2339 | err_out: | ||
| 2340 | if (err == -ENOMEM) { | ||
| 2341 | ntfs_warning(vi->i_sb, "Error allocating memory required to " | ||
| 2342 | "commit the write."); | ||
| 2343 | if (PageUptodate(page)) { | ||
| 2344 | ntfs_warning(vi->i_sb, "Page is uptodate, setting " | ||
| 2345 | "dirty so the write will be retried " | ||
| 2346 | "later on by the VM."); | ||
| 2347 | /* | ||
| 2348 | * Put the page on mapping->dirty_pages, but leave its | ||
| 2349 | * buffers' dirty state as-is. | ||
| 2350 | */ | ||
| 2351 | __set_page_dirty_nobuffers(page); | ||
| 2352 | err = 0; | ||
| 2353 | } else | ||
| 2354 | ntfs_error(vi->i_sb, "Page is not uptodate. Written " | ||
| 2355 | "data has been lost."); | ||
| 2356 | } else { | ||
| 2357 | ntfs_error(vi->i_sb, "Resident attribute commit write failed " | ||
| 2358 | "with error %i.", err); | ||
| 2359 | NVolSetErrors(ni->vol); | ||
| 2360 | make_bad_inode(vi); | ||
| 2361 | } | ||
| 2362 | if (ctx) | ||
| 2363 | ntfs_attr_put_search_ctx(ctx); | ||
| 2364 | if (m) | ||
| 2365 | unmap_mft_record(base_ni); | ||
| 2366 | return err; | ||
| 2367 | } | ||
| 2368 | |||
| 2369 | #endif /* NTFS_RW */ | 1544 | #endif /* NTFS_RW */ |
| 2370 | 1545 | ||
| 2371 | /** | 1546 | /** |
| @@ -2377,9 +1552,6 @@ struct address_space_operations ntfs_aops = { | |||
| 2377 | disk request queue. */ | 1552 | disk request queue. */ |
| 2378 | #ifdef NTFS_RW | 1553 | #ifdef NTFS_RW |
| 2379 | .writepage = ntfs_writepage, /* Write dirty page to disk. */ | 1554 | .writepage = ntfs_writepage, /* Write dirty page to disk. */ |
| 2380 | .prepare_write = ntfs_prepare_write, /* Prepare page and buffers | ||
| 2381 | ready to receive data. */ | ||
| 2382 | .commit_write = ntfs_commit_write, /* Commit received data. */ | ||
| 2383 | #endif /* NTFS_RW */ | 1555 | #endif /* NTFS_RW */ |
| 2384 | }; | 1556 | }; |
| 2385 | 1557 | ||
