|
@@ -1542,830 +1542,6 @@ err_out:
|
|
|
return err;
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
- * ntfs_prepare_nonresident_write -
|
|
|
- *
|
|
|
- */
|
|
|
-static int ntfs_prepare_nonresident_write(struct page *page,
|
|
|
- unsigned from, unsigned to)
|
|
|
-{
|
|
|
- VCN vcn;
|
|
|
- LCN lcn;
|
|
|
- s64 initialized_size;
|
|
|
- loff_t i_size;
|
|
|
- sector_t block, ablock, iblock;
|
|
|
- struct inode *vi;
|
|
|
- ntfs_inode *ni;
|
|
|
- ntfs_volume *vol;
|
|
|
- runlist_element *rl;
|
|
|
- struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
|
|
|
- unsigned long flags;
|
|
|
- unsigned int vcn_ofs, block_start, block_end, blocksize;
|
|
|
- int err;
|
|
|
- BOOL is_retry;
|
|
|
- unsigned char blocksize_bits;
|
|
|
-
|
|
|
- vi = page->mapping->host;
|
|
|
- ni = NTFS_I(vi);
|
|
|
- vol = ni->vol;
|
|
|
-
|
|
|
- ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
|
|
|
- "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
|
|
|
- page->index, from, to);
|
|
|
-
|
|
|
- BUG_ON(!NInoNonResident(ni));
|
|
|
-
|
|
|
- blocksize_bits = vi->i_blkbits;
|
|
|
- blocksize = 1 << blocksize_bits;
|
|
|
-
|
|
|
- /*
|
|
|
- * create_empty_buffers() will create uptodate/dirty buffers if the
|
|
|
- * page is uptodate/dirty.
|
|
|
- */
|
|
|
- if (!page_has_buffers(page))
|
|
|
- create_empty_buffers(page, blocksize, 0);
|
|
|
- bh = head = page_buffers(page);
|
|
|
- if (unlikely(!bh))
|
|
|
- return -ENOMEM;
|
|
|
-
|
|
|
- /* The first block in the page. */
|
|
|
- block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
|
|
|
-
|
|
|
- read_lock_irqsave(&ni->size_lock, flags);
|
|
|
- /*
|
|
|
- * The first out of bounds block for the allocated size. No need to
|
|
|
- * round up as allocated_size is in multiples of cluster size and the
|
|
|
- * minimum cluster size is 512 bytes, which is equal to the smallest
|
|
|
- * blocksize.
|
|
|
- */
|
|
|
- ablock = ni->allocated_size >> blocksize_bits;
|
|
|
- i_size = i_size_read(vi);
|
|
|
- initialized_size = ni->initialized_size;
|
|
|
- read_unlock_irqrestore(&ni->size_lock, flags);
|
|
|
-
|
|
|
- /* The last (fully or partially) initialized block. */
|
|
|
- iblock = initialized_size >> blocksize_bits;
|
|
|
-
|
|
|
- /* Loop through all the buffers in the page. */
|
|
|
- block_start = 0;
|
|
|
- rl = NULL;
|
|
|
- err = 0;
|
|
|
- do {
|
|
|
- block_end = block_start + blocksize;
|
|
|
- /*
|
|
|
- * If buffer @bh is outside the write, just mark it uptodate
|
|
|
- * if the page is uptodate and continue with the next buffer.
|
|
|
- */
|
|
|
- if (block_end <= from || block_start >= to) {
|
|
|
- if (PageUptodate(page)) {
|
|
|
- if (!buffer_uptodate(bh))
|
|
|
- set_buffer_uptodate(bh);
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
- /*
|
|
|
- * @bh is at least partially being written to.
|
|
|
- * Make sure it is not marked as new.
|
|
|
- */
|
|
|
- //if (buffer_new(bh))
|
|
|
- // clear_buffer_new(bh);
|
|
|
-
|
|
|
- if (block >= ablock) {
|
|
|
- // TODO: block is above allocated_size, need to
|
|
|
- // allocate it. Best done in one go to accommodate not
|
|
|
- // only block but all above blocks up to and including:
|
|
|
- // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
|
|
|
- // - 1) >> blobksize_bits. Obviously will need to round
|
|
|
- // up to next cluster boundary, too. This should be
|
|
|
- // done with a helper function, so it can be reused.
|
|
|
- ntfs_error(vol->sb, "Writing beyond allocated size "
|
|
|
- "is not supported yet. Sorry.");
|
|
|
- err = -EOPNOTSUPP;
|
|
|
- goto err_out;
|
|
|
- // Need to update ablock.
|
|
|
- // Need to set_buffer_new() on all block bhs that are
|
|
|
- // newly allocated.
|
|
|
- }
|
|
|
- /*
|
|
|
- * Now we have enough allocated size to fulfill the whole
|
|
|
- * request, i.e. block < ablock is true.
|
|
|
- */
|
|
|
- if (unlikely((block >= iblock) &&
|
|
|
- (initialized_size < i_size))) {
|
|
|
- /*
|
|
|
- * If this page is fully outside initialized size, zero
|
|
|
- * out all pages between the current initialized size
|
|
|
- * and the current page. Just use ntfs_readpage() to do
|
|
|
- * the zeroing transparently.
|
|
|
- */
|
|
|
- if (block > iblock) {
|
|
|
- // TODO:
|
|
|
- // For each page do:
|
|
|
- // - read_cache_page()
|
|
|
- // Again for each page do:
|
|
|
- // - wait_on_page_locked()
|
|
|
- // - Check (PageUptodate(page) &&
|
|
|
- // !PageError(page))
|
|
|
- // Update initialized size in the attribute and
|
|
|
- // in the inode.
|
|
|
- // Again, for each page do:
|
|
|
- // __set_page_dirty_buffers();
|
|
|
- // page_cache_release()
|
|
|
- // We don't need to wait on the writes.
|
|
|
- // Update iblock.
|
|
|
- }
|
|
|
- /*
|
|
|
- * The current page straddles initialized size. Zero
|
|
|
- * all non-uptodate buffers and set them uptodate (and
|
|
|
- * dirty?). Note, there aren't any non-uptodate buffers
|
|
|
- * if the page is uptodate.
|
|
|
- * FIXME: For an uptodate page, the buffers may need to
|
|
|
- * be written out because they were not initialized on
|
|
|
- * disk before.
|
|
|
- */
|
|
|
- if (!PageUptodate(page)) {
|
|
|
- // TODO:
|
|
|
- // Zero any non-uptodate buffers up to i_size.
|
|
|
- // Set them uptodate and dirty.
|
|
|
- }
|
|
|
- // TODO:
|
|
|
- // Update initialized size in the attribute and in the
|
|
|
- // inode (up to i_size).
|
|
|
- // Update iblock.
|
|
|
- // FIXME: This is inefficient. Try to batch the two
|
|
|
- // size changes to happen in one go.
|
|
|
- ntfs_error(vol->sb, "Writing beyond initialized size "
|
|
|
- "is not supported yet. Sorry.");
|
|
|
- err = -EOPNOTSUPP;
|
|
|
- goto err_out;
|
|
|
- // Do NOT set_buffer_new() BUT DO clear buffer range
|
|
|
- // outside write request range.
|
|
|
- // set_buffer_uptodate() on complete buffers as well as
|
|
|
- // set_buffer_dirty().
|
|
|
- }
|
|
|
-
|
|
|
- /* Need to map unmapped buffers. */
|
|
|
- if (!buffer_mapped(bh)) {
|
|
|
- /* Unmapped buffer. Need to map it. */
|
|
|
- bh->b_bdev = vol->sb->s_bdev;
|
|
|
-
|
|
|
- /* Convert block into corresponding vcn and offset. */
|
|
|
- vcn = (VCN)block << blocksize_bits >>
|
|
|
- vol->cluster_size_bits;
|
|
|
- vcn_ofs = ((VCN)block << blocksize_bits) &
|
|
|
- vol->cluster_size_mask;
|
|
|
-
|
|
|
- is_retry = FALSE;
|
|
|
- if (!rl) {
|
|
|
-lock_retry_remap:
|
|
|
- down_read(&ni->runlist.lock);
|
|
|
- rl = ni->runlist.rl;
|
|
|
- }
|
|
|
- if (likely(rl != NULL)) {
|
|
|
- /* Seek to element containing target vcn. */
|
|
|
- while (rl->length && rl[1].vcn <= vcn)
|
|
|
- rl++;
|
|
|
- lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
|
|
|
- } else
|
|
|
- lcn = LCN_RL_NOT_MAPPED;
|
|
|
- if (unlikely(lcn < 0)) {
|
|
|
- /*
|
|
|
- * We extended the attribute allocation above.
|
|
|
- * If we hit an ENOENT here it means that the
|
|
|
- * allocation was insufficient which is a bug.
|
|
|
- */
|
|
|
- BUG_ON(lcn == LCN_ENOENT);
|
|
|
-
|
|
|
- /* It is a hole, need to instantiate it. */
|
|
|
- if (lcn == LCN_HOLE) {
|
|
|
- // TODO: Instantiate the hole.
|
|
|
- // clear_buffer_new(bh);
|
|
|
- // unmap_underlying_metadata(bh->b_bdev,
|
|
|
- // bh->b_blocknr);
|
|
|
- // For non-uptodate buffers, need to
|
|
|
- // zero out the region outside the
|
|
|
- // request in this bh or all bhs,
|
|
|
- // depending on what we implemented
|
|
|
- // above.
|
|
|
- // Need to flush_dcache_page().
|
|
|
- // Or could use set_buffer_new()
|
|
|
- // instead?
|
|
|
- ntfs_error(vol->sb, "Writing into "
|
|
|
- "sparse regions is "
|
|
|
- "not supported yet. "
|
|
|
- "Sorry.");
|
|
|
- err = -EOPNOTSUPP;
|
|
|
- if (!rl)
|
|
|
- up_read(&ni->runlist.lock);
|
|
|
- goto err_out;
|
|
|
- } else if (!is_retry &&
|
|
|
- lcn == LCN_RL_NOT_MAPPED) {
|
|
|
- is_retry = TRUE;
|
|
|
- /*
|
|
|
- * Attempt to map runlist, dropping
|
|
|
- * lock for the duration.
|
|
|
- */
|
|
|
- up_read(&ni->runlist.lock);
|
|
|
- err = ntfs_map_runlist(ni, vcn);
|
|
|
- if (likely(!err))
|
|
|
- goto lock_retry_remap;
|
|
|
- rl = NULL;
|
|
|
- } else if (!rl)
|
|
|
- up_read(&ni->runlist.lock);
|
|
|
- /*
|
|
|
- * Failed to map the buffer, even after
|
|
|
- * retrying.
|
|
|
- */
|
|
|
- if (!err)
|
|
|
- err = -EIO;
|
|
|
- bh->b_blocknr = -1;
|
|
|
- ntfs_error(vol->sb, "Failed to write to inode "
|
|
|
- "0x%lx, attribute type 0x%x, "
|
|
|
- "vcn 0x%llx, offset 0x%x "
|
|
|
- "because its location on disk "
|
|
|
- "could not be determined%s "
|
|
|
- "(error code %i).",
|
|
|
- ni->mft_no, ni->type,
|
|
|
- (unsigned long long)vcn,
|
|
|
- vcn_ofs, is_retry ? " even "
|
|
|
- "after retrying" : "", err);
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- /* We now have a successful remap, i.e. lcn >= 0. */
|
|
|
-
|
|
|
- /* Setup buffer head to correct block. */
|
|
|
- bh->b_blocknr = ((lcn << vol->cluster_size_bits)
|
|
|
- + vcn_ofs) >> blocksize_bits;
|
|
|
- set_buffer_mapped(bh);
|
|
|
-
|
|
|
- // FIXME: Something analogous to this is needed for
|
|
|
- // each newly allocated block, i.e. BH_New.
|
|
|
- // FIXME: Might need to take this out of the
|
|
|
- // if (!buffer_mapped(bh)) {}, depending on how we
|
|
|
- // implement things during the allocated_size and
|
|
|
- // initialized_size extension code above.
|
|
|
- if (buffer_new(bh)) {
|
|
|
- clear_buffer_new(bh);
|
|
|
- unmap_underlying_metadata(bh->b_bdev,
|
|
|
- bh->b_blocknr);
|
|
|
- if (PageUptodate(page)) {
|
|
|
- set_buffer_uptodate(bh);
|
|
|
- continue;
|
|
|
- }
|
|
|
- /*
|
|
|
- * Page is _not_ uptodate, zero surrounding
|
|
|
- * region. NOTE: This is how we decide if to
|
|
|
- * zero or not!
|
|
|
- */
|
|
|
- if (block_end > to || block_start < from) {
|
|
|
- void *kaddr;
|
|
|
-
|
|
|
- kaddr = kmap_atomic(page, KM_USER0);
|
|
|
- if (block_end > to)
|
|
|
- memset(kaddr + to, 0,
|
|
|
- block_end - to);
|
|
|
- if (block_start < from)
|
|
|
- memset(kaddr + block_start, 0,
|
|
|
- from -
|
|
|
- block_start);
|
|
|
- flush_dcache_page(page);
|
|
|
- kunmap_atomic(kaddr, KM_USER0);
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- /* @bh is mapped, set it uptodate if the page is uptodate. */
|
|
|
- if (PageUptodate(page)) {
|
|
|
- if (!buffer_uptodate(bh))
|
|
|
- set_buffer_uptodate(bh);
|
|
|
- continue;
|
|
|
- }
|
|
|
- /*
|
|
|
- * The page is not uptodate. The buffer is mapped. If it is not
|
|
|
- * uptodate, and it is only partially being written to, we need
|
|
|
- * to read the buffer in before the write, i.e. right now.
|
|
|
- */
|
|
|
- if (!buffer_uptodate(bh) &&
|
|
|
- (block_start < from || block_end > to)) {
|
|
|
- ll_rw_block(READ, 1, &bh);
|
|
|
- *wait_bh++ = bh;
|
|
|
- }
|
|
|
- } while (block++, block_start = block_end,
|
|
|
- (bh = bh->b_this_page) != head);
|
|
|
-
|
|
|
- /* Release the lock if we took it. */
|
|
|
- if (rl) {
|
|
|
- up_read(&ni->runlist.lock);
|
|
|
- rl = NULL;
|
|
|
- }
|
|
|
-
|
|
|
- /* If we issued read requests, let them complete. */
|
|
|
- while (wait_bh > wait) {
|
|
|
- wait_on_buffer(*--wait_bh);
|
|
|
- if (!buffer_uptodate(*wait_bh))
|
|
|
- return -EIO;
|
|
|
- }
|
|
|
-
|
|
|
- ntfs_debug("Done.");
|
|
|
- return 0;
|
|
|
-err_out:
|
|
|
- /*
|
|
|
- * Zero out any newly allocated blocks to avoid exposing stale data.
|
|
|
- * If BH_New is set, we know that the block was newly allocated in the
|
|
|
- * above loop.
|
|
|
- * FIXME: What about initialized_size increments? Have we done all the
|
|
|
- * required zeroing above? If not this error handling is broken, and
|
|
|
- * in particular the if (block_end <= from) check is completely bogus.
|
|
|
- */
|
|
|
- bh = head;
|
|
|
- block_start = 0;
|
|
|
- is_retry = FALSE;
|
|
|
- do {
|
|
|
- block_end = block_start + blocksize;
|
|
|
- if (block_end <= from)
|
|
|
- continue;
|
|
|
- if (block_start >= to)
|
|
|
- break;
|
|
|
- if (buffer_new(bh)) {
|
|
|
- void *kaddr;
|
|
|
-
|
|
|
- clear_buffer_new(bh);
|
|
|
- kaddr = kmap_atomic(page, KM_USER0);
|
|
|
- memset(kaddr + block_start, 0, bh->b_size);
|
|
|
- kunmap_atomic(kaddr, KM_USER0);
|
|
|
- set_buffer_uptodate(bh);
|
|
|
- mark_buffer_dirty(bh);
|
|
|
- is_retry = TRUE;
|
|
|
- }
|
|
|
- } while (block_start = block_end, (bh = bh->b_this_page) != head);
|
|
|
- if (is_retry)
|
|
|
- flush_dcache_page(page);
|
|
|
- if (rl)
|
|
|
- up_read(&ni->runlist.lock);
|
|
|
- return err;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * ntfs_prepare_write - prepare a page for receiving data
|
|
|
- *
|
|
|
- * This is called from generic_file_write() with i_sem held on the inode
|
|
|
- * (@page->mapping->host). The @page is locked but not kmap()ped. The source
|
|
|
- * data has not yet been copied into the @page.
|
|
|
- *
|
|
|
- * Need to extend the attribute/fill in holes if necessary, create blocks and
|
|
|
- * make partially overwritten blocks uptodate,
|
|
|
- *
|
|
|
- * i_size is not to be modified yet.
|
|
|
- *
|
|
|
- * Return 0 on success or -errno on error.
|
|
|
- *
|
|
|
- * Should be using block_prepare_write() [support for sparse files] or
|
|
|
- * cont_prepare_write() [no support for sparse files]. Cannot do that due to
|
|
|
- * ntfs specifics but can look at them for implementation guidance.
|
|
|
- *
|
|
|
- * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
|
|
|
- * the first byte in the page that will be written to and @to is the first byte
|
|
|
- * after the last byte that will be written to.
|
|
|
- */
|
|
|
-static int ntfs_prepare_write(struct file *file, struct page *page,
|
|
|
- unsigned from, unsigned to)
|
|
|
-{
|
|
|
- s64 new_size;
|
|
|
- loff_t i_size;
|
|
|
- struct inode *vi = page->mapping->host;
|
|
|
- ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
|
|
|
- ntfs_volume *vol = ni->vol;
|
|
|
- ntfs_attr_search_ctx *ctx = NULL;
|
|
|
- MFT_RECORD *m = NULL;
|
|
|
- ATTR_RECORD *a;
|
|
|
- u8 *kaddr;
|
|
|
- u32 attr_len;
|
|
|
- int err;
|
|
|
-
|
|
|
- ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
|
|
|
- "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
|
|
|
- page->index, from, to);
|
|
|
- BUG_ON(!PageLocked(page));
|
|
|
- BUG_ON(from > PAGE_CACHE_SIZE);
|
|
|
- BUG_ON(to > PAGE_CACHE_SIZE);
|
|
|
- BUG_ON(from > to);
|
|
|
- BUG_ON(NInoMstProtected(ni));
|
|
|
- /*
|
|
|
- * If a previous ntfs_truncate() failed, repeat it and abort if it
|
|
|
- * fails again.
|
|
|
- */
|
|
|
- if (unlikely(NInoTruncateFailed(ni))) {
|
|
|
- down_write(&vi->i_alloc_sem);
|
|
|
- err = ntfs_truncate(vi);
|
|
|
- up_write(&vi->i_alloc_sem);
|
|
|
- if (err || NInoTruncateFailed(ni)) {
|
|
|
- if (!err)
|
|
|
- err = -EIO;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- }
|
|
|
- /* If the attribute is not resident, deal with it elsewhere. */
|
|
|
- if (NInoNonResident(ni)) {
|
|
|
- /*
|
|
|
- * Only unnamed $DATA attributes can be compressed, encrypted,
|
|
|
- * and/or sparse.
|
|
|
- */
|
|
|
- if (ni->type == AT_DATA && !ni->name_len) {
|
|
|
- /* If file is encrypted, deny access, just like NT4. */
|
|
|
- if (NInoEncrypted(ni)) {
|
|
|
- ntfs_debug("Denying write access to encrypted "
|
|
|
- "file.");
|
|
|
- return -EACCES;
|
|
|
- }
|
|
|
- /* Compressed data streams are handled in compress.c. */
|
|
|
- if (NInoCompressed(ni)) {
|
|
|
- // TODO: Implement and replace this check with
|
|
|
- // return ntfs_write_compressed_block(page);
|
|
|
- ntfs_error(vi->i_sb, "Writing to compressed "
|
|
|
- "files is not supported yet. "
|
|
|
- "Sorry.");
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
- // TODO: Implement and remove this check.
|
|
|
- if (NInoSparse(ni)) {
|
|
|
- ntfs_error(vi->i_sb, "Writing to sparse files "
|
|
|
- "is not supported yet. Sorry.");
|
|
|
- return -EOPNOTSUPP;
|
|
|
- }
|
|
|
- }
|
|
|
- /* Normal data stream. */
|
|
|
- return ntfs_prepare_nonresident_write(page, from, to);
|
|
|
- }
|
|
|
- /*
|
|
|
- * Attribute is resident, implying it is not compressed, encrypted, or
|
|
|
- * sparse.
|
|
|
- */
|
|
|
- BUG_ON(page_has_buffers(page));
|
|
|
- new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
|
|
|
- /* If we do not need to resize the attribute allocation we are done. */
|
|
|
- if (new_size <= i_size_read(vi))
|
|
|
- goto done;
|
|
|
- /* Map, pin, and lock the (base) mft record. */
|
|
|
- if (!NInoAttr(ni))
|
|
|
- base_ni = ni;
|
|
|
- else
|
|
|
- base_ni = ni->ext.base_ntfs_ino;
|
|
|
- m = map_mft_record(base_ni);
|
|
|
- if (IS_ERR(m)) {
|
|
|
- err = PTR_ERR(m);
|
|
|
- m = NULL;
|
|
|
- ctx = NULL;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- ctx = ntfs_attr_get_search_ctx(base_ni, m);
|
|
|
- if (unlikely(!ctx)) {
|
|
|
- err = -ENOMEM;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
|
|
|
- CASE_SENSITIVE, 0, NULL, 0, ctx);
|
|
|
- if (unlikely(err)) {
|
|
|
- if (err == -ENOENT)
|
|
|
- err = -EIO;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- m = ctx->mrec;
|
|
|
- a = ctx->attr;
|
|
|
- /* The total length of the attribute value. */
|
|
|
- attr_len = le32_to_cpu(a->data.resident.value_length);
|
|
|
- /* Fix an eventual previous failure of ntfs_commit_write(). */
|
|
|
- i_size = i_size_read(vi);
|
|
|
- if (unlikely(attr_len > i_size)) {
|
|
|
- attr_len = i_size;
|
|
|
- a->data.resident.value_length = cpu_to_le32(attr_len);
|
|
|
- }
|
|
|
- /* If we do not need to resize the attribute allocation we are done. */
|
|
|
- if (new_size <= attr_len)
|
|
|
- goto done_unm;
|
|
|
- /* Check if new size is allowed in $AttrDef. */
|
|
|
- err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
|
|
|
- if (unlikely(err)) {
|
|
|
- if (err == -ERANGE) {
|
|
|
- ntfs_error(vol->sb, "Write would cause the inode "
|
|
|
- "0x%lx to exceed the maximum size for "
|
|
|
- "its attribute type (0x%x). Aborting "
|
|
|
- "write.", vi->i_ino,
|
|
|
- le32_to_cpu(ni->type));
|
|
|
- } else {
|
|
|
- ntfs_error(vol->sb, "Inode 0x%lx has unknown "
|
|
|
- "attribute type 0x%x. Aborting "
|
|
|
- "write.", vi->i_ino,
|
|
|
- le32_to_cpu(ni->type));
|
|
|
- err = -EIO;
|
|
|
- }
|
|
|
- goto err_out2;
|
|
|
- }
|
|
|
- /*
|
|
|
- * Extend the attribute record to be able to store the new attribute
|
|
|
- * size.
|
|
|
- */
|
|
|
- if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
|
|
|
- le16_to_cpu(a->data.resident.value_offset) +
|
|
|
- new_size)) {
|
|
|
- /* Not enough space in the mft record. */
|
|
|
- ntfs_error(vol->sb, "Not enough space in the mft record for "
|
|
|
- "the resized attribute value. This is not "
|
|
|
- "supported yet. Aborting write.");
|
|
|
- err = -EOPNOTSUPP;
|
|
|
- goto err_out2;
|
|
|
- }
|
|
|
- /*
|
|
|
- * We have enough space in the mft record to fit the write. This
|
|
|
- * implies the attribute is smaller than the mft record and hence the
|
|
|
- * attribute must be in a single page and hence page->index must be 0.
|
|
|
- */
|
|
|
- BUG_ON(page->index);
|
|
|
- /*
|
|
|
- * If the beginning of the write is past the old size, enlarge the
|
|
|
- * attribute value up to the beginning of the write and fill it with
|
|
|
- * zeroes.
|
|
|
- */
|
|
|
- if (from > attr_len) {
|
|
|
- memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
|
|
|
- attr_len, 0, from - attr_len);
|
|
|
- a->data.resident.value_length = cpu_to_le32(from);
|
|
|
- /* Zero the corresponding area in the page as well. */
|
|
|
- if (PageUptodate(page)) {
|
|
|
- kaddr = kmap_atomic(page, KM_USER0);
|
|
|
- memset(kaddr + attr_len, 0, from - attr_len);
|
|
|
- kunmap_atomic(kaddr, KM_USER0);
|
|
|
- flush_dcache_page(page);
|
|
|
- }
|
|
|
- }
|
|
|
- flush_dcache_mft_record_page(ctx->ntfs_ino);
|
|
|
- mark_mft_record_dirty(ctx->ntfs_ino);
|
|
|
-done_unm:
|
|
|
- ntfs_attr_put_search_ctx(ctx);
|
|
|
- unmap_mft_record(base_ni);
|
|
|
- /*
|
|
|
- * Because resident attributes are handled by memcpy() to/from the
|
|
|
- * corresponding MFT record, and because this form of i/o is byte
|
|
|
- * aligned rather than block aligned, there is no need to bring the
|
|
|
- * page uptodate here as in the non-resident case where we need to
|
|
|
- * bring the buffers straddled by the write uptodate before
|
|
|
- * generic_file_write() does the copying from userspace.
|
|
|
- *
|
|
|
- * We thus defer the uptodate bringing of the page region outside the
|
|
|
- * region written to to ntfs_commit_write(), which makes the code
|
|
|
- * simpler and saves one atomic kmap which is good.
|
|
|
- */
|
|
|
-done:
|
|
|
- ntfs_debug("Done.");
|
|
|
- return 0;
|
|
|
-err_out:
|
|
|
- if (err == -ENOMEM)
|
|
|
- ntfs_warning(vi->i_sb, "Error allocating memory required to "
|
|
|
- "prepare the write.");
|
|
|
- else {
|
|
|
- ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
|
|
|
- "with error %i.", err);
|
|
|
- NVolSetErrors(vol);
|
|
|
- make_bad_inode(vi);
|
|
|
- }
|
|
|
-err_out2:
|
|
|
- if (ctx)
|
|
|
- ntfs_attr_put_search_ctx(ctx);
|
|
|
- if (m)
|
|
|
- unmap_mft_record(base_ni);
|
|
|
- return err;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * ntfs_commit_nonresident_write -
|
|
|
- *
|
|
|
- */
|
|
|
-static int ntfs_commit_nonresident_write(struct page *page,
|
|
|
- unsigned from, unsigned to)
|
|
|
-{
|
|
|
- s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
|
|
|
- struct inode *vi = page->mapping->host;
|
|
|
- struct buffer_head *bh, *head;
|
|
|
- unsigned int block_start, block_end, blocksize;
|
|
|
- BOOL partial;
|
|
|
-
|
|
|
- ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
|
|
|
- "0x%lx, from = %u, to = %u.", vi->i_ino,
|
|
|
- NTFS_I(vi)->type, page->index, from, to);
|
|
|
- blocksize = 1 << vi->i_blkbits;
|
|
|
-
|
|
|
- // FIXME: We need a whole slew of special cases in here for compressed
|
|
|
- // files for example...
|
|
|
- // For now, we know ntfs_prepare_write() would have failed so we can't
|
|
|
- // get here in any of the cases which we have to special case, so we
|
|
|
- // are just a ripped off, unrolled generic_commit_write().
|
|
|
-
|
|
|
- bh = head = page_buffers(page);
|
|
|
- block_start = 0;
|
|
|
- partial = FALSE;
|
|
|
- do {
|
|
|
- block_end = block_start + blocksize;
|
|
|
- if (block_end <= from || block_start >= to) {
|
|
|
- if (!buffer_uptodate(bh))
|
|
|
- partial = TRUE;
|
|
|
- } else {
|
|
|
- set_buffer_uptodate(bh);
|
|
|
- mark_buffer_dirty(bh);
|
|
|
- }
|
|
|
- } while (block_start = block_end, (bh = bh->b_this_page) != head);
|
|
|
- /*
|
|
|
- * If this is a partial write which happened to make all buffers
|
|
|
- * uptodate then we can optimize away a bogus ->readpage() for the next
|
|
|
- * read(). Here we 'discover' whether the page went uptodate as a
|
|
|
- * result of this (potentially partial) write.
|
|
|
- */
|
|
|
- if (!partial)
|
|
|
- SetPageUptodate(page);
|
|
|
- /*
|
|
|
- * Not convinced about this at all. See disparity comment above. For
|
|
|
- * now we know ntfs_prepare_write() would have failed in the write
|
|
|
- * exceeds i_size case, so this will never trigger which is fine.
|
|
|
- */
|
|
|
- if (pos > i_size_read(vi)) {
|
|
|
- ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
|
|
|
- "not supported yet. Sorry.");
|
|
|
- return -EOPNOTSUPP;
|
|
|
- // vi->i_size = pos;
|
|
|
- // mark_inode_dirty(vi);
|
|
|
- }
|
|
|
- ntfs_debug("Done.");
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * ntfs_commit_write - commit the received data
|
|
|
- *
|
|
|
- * This is called from generic_file_write() with i_sem held on the inode
|
|
|
- * (@page->mapping->host). The @page is locked but not kmap()ped. The source
|
|
|
- * data has already been copied into the @page. ntfs_prepare_write() has been
|
|
|
- * called before the data copied and it returned success so we can take the
|
|
|
- * results of various BUG checks and some error handling for granted.
|
|
|
- *
|
|
|
- * Need to mark modified blocks dirty so they get written out later when
|
|
|
- * ntfs_writepage() is invoked by the VM.
|
|
|
- *
|
|
|
- * Return 0 on success or -errno on error.
|
|
|
- *
|
|
|
- * Should be using generic_commit_write(). This marks buffers uptodate and
|
|
|
- * dirty, sets the page uptodate if all buffers in the page are uptodate, and
|
|
|
- * updates i_size if the end of io is beyond i_size. In that case, it also
|
|
|
- * marks the inode dirty.
|
|
|
- *
|
|
|
- * Cannot use generic_commit_write() due to ntfs specialities but can look at
|
|
|
- * it for implementation guidance.
|
|
|
- *
|
|
|
- * If things have gone as outlined in ntfs_prepare_write(), then we do not
|
|
|
- * need to do any page content modifications here at all, except in the write
|
|
|
- * to resident attribute case, where we need to do the uptodate bringing here
|
|
|
- * which we combine with the copying into the mft record which means we save
|
|
|
- * one atomic kmap.
|
|
|
- */
|
|
|
-static int ntfs_commit_write(struct file *file, struct page *page,
|
|
|
- unsigned from, unsigned to)
|
|
|
-{
|
|
|
- struct inode *vi = page->mapping->host;
|
|
|
- ntfs_inode *base_ni, *ni = NTFS_I(vi);
|
|
|
- char *kaddr, *kattr;
|
|
|
- ntfs_attr_search_ctx *ctx;
|
|
|
- MFT_RECORD *m;
|
|
|
- ATTR_RECORD *a;
|
|
|
- u32 attr_len;
|
|
|
- int err;
|
|
|
-
|
|
|
- ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
|
|
|
- "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
|
|
|
- page->index, from, to);
|
|
|
- /* If the attribute is not resident, deal with it elsewhere. */
|
|
|
- if (NInoNonResident(ni)) {
|
|
|
- /* Only unnamed $DATA attributes can be compressed/encrypted. */
|
|
|
- if (ni->type == AT_DATA && !ni->name_len) {
|
|
|
- /* Encrypted files need separate handling. */
|
|
|
- if (NInoEncrypted(ni)) {
|
|
|
- // We never get here at present!
|
|
|
- BUG();
|
|
|
- }
|
|
|
- /* Compressed data streams are handled in compress.c. */
|
|
|
- if (NInoCompressed(ni)) {
|
|
|
- // TODO: Implement this!
|
|
|
- // return ntfs_write_compressed_block(page);
|
|
|
- // We never get here at present!
|
|
|
- BUG();
|
|
|
- }
|
|
|
- }
|
|
|
- /* Normal data stream. */
|
|
|
- return ntfs_commit_nonresident_write(page, from, to);
|
|
|
- }
|
|
|
- /*
|
|
|
- * Attribute is resident, implying it is not compressed, encrypted, or
|
|
|
- * sparse.
|
|
|
- */
|
|
|
- if (!NInoAttr(ni))
|
|
|
- base_ni = ni;
|
|
|
- else
|
|
|
- base_ni = ni->ext.base_ntfs_ino;
|
|
|
- /* Map, pin, and lock the mft record. */
|
|
|
- m = map_mft_record(base_ni);
|
|
|
- if (IS_ERR(m)) {
|
|
|
- err = PTR_ERR(m);
|
|
|
- m = NULL;
|
|
|
- ctx = NULL;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- ctx = ntfs_attr_get_search_ctx(base_ni, m);
|
|
|
- if (unlikely(!ctx)) {
|
|
|
- err = -ENOMEM;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
|
|
|
- CASE_SENSITIVE, 0, NULL, 0, ctx);
|
|
|
- if (unlikely(err)) {
|
|
|
- if (err == -ENOENT)
|
|
|
- err = -EIO;
|
|
|
- goto err_out;
|
|
|
- }
|
|
|
- a = ctx->attr;
|
|
|
- /* The total length of the attribute value. */
|
|
|
- attr_len = le32_to_cpu(a->data.resident.value_length);
|
|
|
- BUG_ON(from > attr_len);
|
|
|
- kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
|
|
|
- kaddr = kmap_atomic(page, KM_USER0);
|
|
|
- /* Copy the received data from the page to the mft record. */
|
|
|
- memcpy(kattr + from, kaddr + from, to - from);
|
|
|
- /* Update the attribute length if necessary. */
|
|
|
- if (to > attr_len) {
|
|
|
- attr_len = to;
|
|
|
- a->data.resident.value_length = cpu_to_le32(attr_len);
|
|
|
- }
|
|
|
- /*
|
|
|
- * If the page is not uptodate, bring the out of bounds area(s)
|
|
|
- * uptodate by copying data from the mft record to the page.
|
|
|
- */
|
|
|
- if (!PageUptodate(page)) {
|
|
|
- if (from > 0)
|
|
|
- memcpy(kaddr, kattr, from);
|
|
|
- if (to < attr_len)
|
|
|
- memcpy(kaddr + to, kattr + to, attr_len - to);
|
|
|
- /* Zero the region outside the end of the attribute value. */
|
|
|
- if (attr_len < PAGE_CACHE_SIZE)
|
|
|
- memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
|
|
|
- /*
|
|
|
- * The probability of not having done any of the above is
|
|
|
- * extremely small, so we just flush unconditionally.
|
|
|
- */
|
|
|
- flush_dcache_page(page);
|
|
|
- SetPageUptodate(page);
|
|
|
- }
|
|
|
- kunmap_atomic(kaddr, KM_USER0);
|
|
|
- /* Update i_size if necessary. */
|
|
|
- if (i_size_read(vi) < attr_len) {
|
|
|
- unsigned long flags;
|
|
|
-
|
|
|
- write_lock_irqsave(&ni->size_lock, flags);
|
|
|
- ni->allocated_size = ni->initialized_size = attr_len;
|
|
|
- i_size_write(vi, attr_len);
|
|
|
- write_unlock_irqrestore(&ni->size_lock, flags);
|
|
|
- }
|
|
|
- /* Mark the mft record dirty, so it gets written back. */
|
|
|
- flush_dcache_mft_record_page(ctx->ntfs_ino);
|
|
|
- mark_mft_record_dirty(ctx->ntfs_ino);
|
|
|
- ntfs_attr_put_search_ctx(ctx);
|
|
|
- unmap_mft_record(base_ni);
|
|
|
- ntfs_debug("Done.");
|
|
|
- return 0;
|
|
|
-err_out:
|
|
|
- if (err == -ENOMEM) {
|
|
|
- ntfs_warning(vi->i_sb, "Error allocating memory required to "
|
|
|
- "commit the write.");
|
|
|
- if (PageUptodate(page)) {
|
|
|
- ntfs_warning(vi->i_sb, "Page is uptodate, setting "
|
|
|
- "dirty so the write will be retried "
|
|
|
- "later on by the VM.");
|
|
|
- /*
|
|
|
- * Put the page on mapping->dirty_pages, but leave its
|
|
|
- * buffers' dirty state as-is.
|
|
|
- */
|
|
|
- __set_page_dirty_nobuffers(page);
|
|
|
- err = 0;
|
|
|
- } else
|
|
|
- ntfs_error(vi->i_sb, "Page is not uptodate. Written "
|
|
|
- "data has been lost.");
|
|
|
- } else {
|
|
|
- ntfs_error(vi->i_sb, "Resident attribute commit write failed "
|
|
|
- "with error %i.", err);
|
|
|
- NVolSetErrors(ni->vol);
|
|
|
- make_bad_inode(vi);
|
|
|
- }
|
|
|
- if (ctx)
|
|
|
- ntfs_attr_put_search_ctx(ctx);
|
|
|
- if (m)
|
|
|
- unmap_mft_record(base_ni);
|
|
|
- return err;
|
|
|
-}
|
|
|
-
|
|
|
#endif /* NTFS_RW */
|
|
|
|
|
|
/**
|
|
@@ -2377,9 +1553,6 @@ struct address_space_operations ntfs_aops = {
|
|
|
disk request queue. */
|
|
|
#ifdef NTFS_RW
|
|
|
.writepage = ntfs_writepage, /* Write dirty page to disk. */
|
|
|
- .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
|
|
|
- ready to receive data. */
|
|
|
- .commit_write = ntfs_commit_write, /* Commit received data. */
|
|
|
#endif /* NTFS_RW */
|
|
|
};
|
|
|
|