2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2004 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the Linux-NTFS
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/errno.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
28 #include <linux/buffer_head.h>
33 * ntfs_end_buffer_async_read - async io completion for reading attributes
34 * @bh: buffer head on which io is completed
35 * @uptodate: whether @bh is now uptodate or not
37 * Asynchronous I/O completion handler for reading pages belonging to the
38 * attribute address space of an inode. The inodes can either be files or
39 * directories or they can be fake inodes describing some attribute.
41 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
42 * page has been completed and mark the page uptodate or set the error bit on
43 * the page. To determine the size of the records that need fixing up, we cheat
44 * a little bit by setting the index_block_size in ntfs_inode to the ntfs
45 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
48 static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
50 static spinlock_t page_uptodate_lock = SPIN_LOCK_UNLOCKED;
52 struct buffer_head *tmp;
55 int page_uptodate = 1;
58 ni = NTFS_I(page->mapping->host);
60 if (likely(uptodate)) {
63 set_buffer_uptodate(bh);
65 file_ofs = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
66 /* Check for the current buffer head overflowing. */
67 if (file_ofs + bh->b_size > ni->initialized_size) {
71 if (file_ofs < ni->initialized_size)
72 ofs = ni->initialized_size - file_ofs;
73 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
74 memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
75 flush_dcache_page(page);
76 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
79 clear_buffer_uptodate(bh);
80 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
81 (unsigned long long)bh->b_blocknr);
85 spin_lock_irqsave(&page_uptodate_lock, flags);
86 clear_buffer_async_read(bh);
90 if (!buffer_uptodate(tmp))
92 if (buffer_async_read(tmp)) {
93 if (likely(buffer_locked(tmp)))
95 /* Async buffers must be locked. */
98 tmp = tmp->b_this_page;
100 spin_unlock_irqrestore(&page_uptodate_lock, flags);
102 * If none of the buffers had errors then we can set the page uptodate,
103 * but we first have to perform the post read mst fixups, if the
104 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
106 if (!NInoMstProtected(ni)) {
107 if (likely(page_uptodate && !PageError(page)))
108 SetPageUptodate(page);
111 unsigned int i, recs, nr_err;
114 rec_size = ni->itype.index.block_size;
115 recs = PAGE_CACHE_SIZE / rec_size;
116 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
117 for (i = nr_err = 0; i < recs; i++) {
118 if (likely(!post_read_mst_fixup((NTFS_RECORD*)(addr +
119 i * rec_size), rec_size)))
122 ntfs_error(ni->vol->sb, "post_read_mst_fixup() failed, "
123 "corrupt %s record 0x%llx. Run chkdsk.",
124 ni->mft_no ? "index" : "mft",
125 (unsigned long long)(((s64)page->index
126 << PAGE_CACHE_SHIFT >>
127 ni->itype.index.block_size_bits) + i));
129 flush_dcache_page(page);
130 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
131 if (likely(!PageError(page))) {
132 if (likely(!nr_err && recs)) {
133 if (likely(page_uptodate))
134 SetPageUptodate(page);
136 ntfs_error(ni->vol->sb, "Setting page error, "
137 "index 0x%lx.", page->index);
145 spin_unlock_irqrestore(&page_uptodate_lock, flags);
150 * ntfs_read_block - fill a @page of an address space with data
151 * @page: page cache page to fill with data
153 * Fill the page @page of the address space belonging to the @page->host inode.
154 * We read each buffer asynchronously and when all buffers are read in, our io
155 * completion handler ntfs_end_buffer_read_async(), if required, automatically
156 * applies the mst fixups to the page before finally marking it uptodate and
159 * We only enforce allocated_size limit because i_size is checked for in
160 * generic_file_read().
162 * Return 0 on success and -errno on error.
164 * Contains an adapted version of fs/buffer.c::block_read_full_page().
166 static int ntfs_read_block(struct page *page)
172 run_list_element *rl;
173 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
174 sector_t iblock, lblock, zblock;
175 unsigned int blocksize, vcn_ofs;
177 unsigned char blocksize_bits;
179 ni = NTFS_I(page->mapping->host);
182 blocksize_bits = VFS_I(ni)->i_blkbits;
183 blocksize = 1 << blocksize_bits;
185 if (!page_has_buffers(page))
186 create_empty_buffers(page, blocksize, 0);
187 bh = head = page_buffers(page);
193 iblock = page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
194 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
195 zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
198 if (unlikely(!ni->run_list.rl && !ni->mft_no && !NInoAttr(ni)))
199 panic("NTFS: $MFT/$DATA run list has been unmapped! This is a "
200 "very serious bug! Cannot continue...");
203 /* Loop through all the buffers in the page. */
207 if (unlikely(buffer_uptodate(bh)))
209 if (unlikely(buffer_mapped(bh))) {
213 bh->b_bdev = vol->sb->s_bdev;
214 /* Is the block within the allowed limits? */
215 if (iblock < lblock) {
216 BOOL is_retry = FALSE;
218 /* Convert iblock into corresponding vcn and offset. */
219 vcn = (VCN)iblock << blocksize_bits >>
220 vol->cluster_size_bits;
221 vcn_ofs = ((VCN)iblock << blocksize_bits) &
222 vol->cluster_size_mask;
225 down_read(&ni->run_list.lock);
226 rl = ni->run_list.rl;
228 if (likely(rl != NULL)) {
229 /* Seek to element containing target vcn. */
230 while (rl->length && rl[1].vcn <= vcn)
232 lcn = vcn_to_lcn(rl, vcn);
234 lcn = (LCN)LCN_RL_NOT_MAPPED;
235 /* Successful remap. */
237 /* Setup buffer head to correct block. */
238 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
239 + vcn_ofs) >> blocksize_bits;
240 set_buffer_mapped(bh);
241 /* Only read initialized data blocks. */
242 if (iblock < zblock) {
246 /* Fully non-initialized data block, zero it. */
249 /* It is a hole, need to zero it. */
252 /* If first try and run list unmapped, map and retry. */
253 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
256 * Attempt to map run list, dropping lock for
259 up_read(&ni->run_list.lock);
260 if (!map_run_list(ni, vcn))
261 goto lock_retry_remap;
264 /* Hard error, zero out region. */
266 ntfs_error(vol->sb, "vcn_to_lcn(vcn = 0x%llx) failed "
267 "with error code 0x%llx%s.",
268 (unsigned long long)vcn,
269 (unsigned long long)-lcn,
270 is_retry ? " even after retrying" : "");
271 // FIXME: Depending on vol->on_errors, do something.
274 * Either iblock was outside lblock limits or vcn_to_lcn()
275 * returned error. Just zero that portion of the page and set
276 * the buffer uptodate.
279 bh->b_blocknr = -1UL;
280 clear_buffer_mapped(bh);
282 memset(kmap(page) + i * blocksize, 0, blocksize);
283 flush_dcache_page(page);
285 set_buffer_uptodate(bh);
286 } while (i++, iblock++, (bh = bh->b_this_page) != head);
288 /* Release the lock if we took it. */
290 up_read(&ni->run_list.lock);
292 /* Check we have at least one buffer ready for i/o. */
294 struct buffer_head *tbh;
296 /* Lock the buffers. */
297 for (i = 0; i < nr; i++) {
300 tbh->b_end_io = ntfs_end_buffer_async_read;
301 set_buffer_async_read(tbh);
303 /* Finally, start i/o on the buffers. */
304 for (i = 0; i < nr; i++) {
306 if (likely(!buffer_uptodate(tbh)))
307 submit_bh(READ, tbh);
309 ntfs_end_buffer_async_read(tbh, 1);
313 /* No i/o was scheduled on any of the buffers. */
314 if (likely(!PageError(page)))
315 SetPageUptodate(page);
316 else /* Signal synchronous i/o error. */
323 * ntfs_readpage - fill a @page of a @file with data from the device
324 * @file: open file to which the page @page belongs or NULL
325 * @page: page cache page to fill with data
327 * For non-resident attributes, ntfs_readpage() fills the @page of the open
328 * file @file by calling the ntfs version of the generic block_read_full_page()
329 * function, ntfs_read_block(), which in turn creates and reads in the buffers
330 * associated with the page asynchronously.
332 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
333 * data from the mft record (which at this stage is most likely in memory) and
334 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
335 * even if the mft record is not cached at this point in time, we need to wait
336 * for it to be read in before we can do the copy.
338 * Return 0 on success and -errno on error.
340 * WARNING: Do not make this function static! It is used by mft.c!
342 int ntfs_readpage(struct file *file, struct page *page)
345 ntfs_inode *ni, *base_ni;
347 attr_search_context *ctx;
352 BUG_ON(!PageLocked(page));
355 * This can potentially happen because we clear PageUptodate() during
356 * ntfs_writepage() of MstProtected() attributes.
358 if (PageUptodate(page)) {
363 ni = NTFS_I(page->mapping->host);
365 if (NInoNonResident(ni)) {
367 * Only unnamed $DATA attributes can be compressed or
370 if (ni->type == AT_DATA && !ni->name_len) {
371 /* If file is encrypted, deny access, just like NT4. */
372 if (NInoEncrypted(ni)) {
376 /* Compressed data streams are handled in compress.c. */
377 if (NInoCompressed(ni))
378 return ntfs_read_compressed_block(page);
380 /* Normal data stream. */
381 return ntfs_read_block(page);
383 /* Attribute is resident, implying it is not compressed or encrypted. */
387 base_ni = ni->ext.base_ntfs_ino;
389 /* Map, pin, and lock the mft record. */
390 mrec = map_mft_record(base_ni);
391 if (unlikely(IS_ERR(mrec))) {
395 ctx = get_attr_search_ctx(base_ni, mrec);
396 if (unlikely(!ctx)) {
400 if (unlikely(!lookup_attr(ni->type, ni->name, ni->name_len,
401 IGNORE_CASE, 0, NULL, 0, ctx))) {
403 goto put_unm_err_out;
406 /* Starting position of the page within the attribute value. */
407 attr_pos = page->index << PAGE_CACHE_SHIFT;
409 /* The total length of the attribute value. */
410 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
413 /* Copy over in bounds data, zeroing the remainder of the page. */
414 if (attr_pos < attr_len) {
415 u32 bytes = attr_len - attr_pos;
416 if (bytes > PAGE_CACHE_SIZE)
417 bytes = PAGE_CACHE_SIZE;
418 else if (bytes < PAGE_CACHE_SIZE)
419 memset(addr + bytes, 0, PAGE_CACHE_SIZE - bytes);
420 /* Copy the data to the page. */
421 memcpy(addr, attr_pos + (char*)ctx->attr +
423 ctx->attr->data.resident.value_offset), bytes);
425 memset(addr, 0, PAGE_CACHE_SIZE);
426 flush_dcache_page(page);
429 SetPageUptodate(page);
431 put_attr_search_ctx(ctx);
433 unmap_mft_record(base_ni);
442 * ntfs_write_block - write a @page to the backing store
443 * @page: page cache page to write out
445 * This function is for writing pages belonging to non-resident, non-mst
446 * protected attributes to their backing store.
448 * For a page with buffers, map and write the dirty buffers asynchronously
449 * under page writeback. For a page without buffers, create buffers for the
450 * page, then proceed as above.
452 * If a page doesn't have buffers the page dirty state is definitive. If a page
453 * does have buffers, the page dirty state is just a hint, and the buffer dirty
454 * state is definitive. (A hint which has rules: dirty buffers against a clean
455 * page is illegal. Other combinations are legal and need to be handled. In
456 * particular a dirty page containing clean buffers for example.)
458 * Return 0 on success and -errno on error.
460 * Based on ntfs_read_block() and __block_write_full_page().
462 static int ntfs_write_block(struct writeback_control *wbc, struct page *page)
466 sector_t block, dblock, iblock;
470 run_list_element *rl;
471 struct buffer_head *bh, *head;
472 unsigned int blocksize, vcn_ofs;
474 BOOL need_end_writeback;
475 unsigned char blocksize_bits;
477 vi = page->mapping->host;
481 ntfs_debug("Entering for inode %li, attribute type 0x%x, page index "
482 "0x%lx.\n", vi->i_ino, ni->type, page->index);
484 BUG_ON(!NInoNonResident(ni));
485 BUG_ON(NInoMstProtected(ni));
487 blocksize_bits = vi->i_blkbits;
488 blocksize = 1 << blocksize_bits;
490 if (!page_has_buffers(page)) {
491 BUG_ON(!PageUptodate(page));
492 create_empty_buffers(page, blocksize,
493 (1 << BH_Uptodate) | (1 << BH_Dirty));
495 bh = head = page_buffers(page);
497 ntfs_warning(vol->sb, "Error allocating page buffers. "
498 "Redirtying page so we try again later.");
500 * Put the page back on mapping->dirty_pages, but leave its
501 * buffer's dirty state as-is.
503 redirty_page_for_writepage(wbc, page);
508 /* NOTE: Different naming scheme to ntfs_read_block()! */
510 /* The first block in the page. */
511 block = page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
513 /* The first out of bounds block for the data size. */
514 dblock = (vi->i_size + blocksize - 1) >> blocksize_bits;
516 /* The last (fully or partially) initialized block. */
517 iblock = ni->initialized_size >> blocksize_bits;
520 * Be very careful. We have no exclusion from __set_page_dirty_buffers
521 * here, and the (potentially unmapped) buffers may become dirty at
522 * any time. If a buffer becomes dirty here after we've inspected it
523 * then we just miss that fact, and the page stays dirty.
525 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
526 * handle that here by just cleaning them.
530 * Loop through all the buffers in the page, mapping all the dirty
531 * buffers to disk addresses and handling any aliases from the
532 * underlying block device's mapping.
537 BOOL is_retry = FALSE;
539 if (unlikely(block >= dblock)) {
541 * Mapped buffers outside i_size will occur, because
542 * this page can be outside i_size when there is a
543 * truncate in progress. The contents of such buffers
544 * were zeroed by ntfs_writepage().
546 * FIXME: What about the small race window where
547 * ntfs_writepage() has not done any clearing because
548 * the page was within i_size but before we get here,
549 * vmtruncate() modifies i_size?
551 clear_buffer_dirty(bh);
552 set_buffer_uptodate(bh);
556 /* Clean buffers are not written out, so no need to map them. */
557 if (!buffer_dirty(bh))
560 /* Make sure we have enough initialized size. */
561 if (unlikely((block >= iblock) &&
562 (ni->initialized_size < vi->i_size))) {
564 * If this page is fully outside initialized size, zero
565 * out all pages between the current initialized size
566 * and the current page. Just use ntfs_readpage() to do
567 * the zeroing transparently.
569 if (block > iblock) {
572 // - read_cache_page()
573 // Again for each page do:
574 // - wait_on_page_locked()
575 // - Check (PageUptodate(page) &&
577 // Update initialized size in the attribute and
579 // Again, for each page do:
580 // __set_page_dirty_buffers();
581 // page_cache_release()
582 // We don't need to wait on the writes.
586 * The current page straddles initialized size. Zero
587 * all non-uptodate buffers and set them uptodate (and
588 * dirty?). Note, there aren't any non-uptodate buffers
589 * if the page is uptodate.
590 * FIXME: For an uptodate page, the buffers may need to
591 * be written out because they were not initialized on
594 if (!PageUptodate(page)) {
596 // Zero any non-uptodate buffers up to i_size.
597 // Set them uptodate and dirty.
600 // Update initialized size in the attribute and in the
601 // inode (up to i_size).
603 // FIXME: This is inefficient. Try to batch the two
604 // size changes to happen in one go.
605 ntfs_error(vol->sb, "Writing beyond initialized size "
606 "is not supported yet. Sorry.");
609 // Do NOT set_buffer_new() BUT DO clear buffer range
610 // outside write request range.
611 // set_buffer_uptodate() on complete buffers as well as
612 // set_buffer_dirty().
615 /* No need to map buffers that are already mapped. */
616 if (buffer_mapped(bh))
619 /* Unmapped, dirty buffer. Need to map it. */
620 bh->b_bdev = vol->sb->s_bdev;
622 /* Convert block into corresponding vcn and offset. */
623 vcn = (VCN)block << blocksize_bits >> vol->cluster_size_bits;
624 vcn_ofs = ((VCN)block << blocksize_bits) &
625 vol->cluster_size_mask;
628 down_read(&ni->run_list.lock);
629 rl = ni->run_list.rl;
631 if (likely(rl != NULL)) {
632 /* Seek to element containing target vcn. */
633 while (rl->length && rl[1].vcn <= vcn)
635 lcn = vcn_to_lcn(rl, vcn);
637 lcn = (LCN)LCN_RL_NOT_MAPPED;
638 /* Successful remap. */
640 /* Setup buffer head to point to correct block. */
641 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
642 vcn_ofs) >> blocksize_bits;
643 set_buffer_mapped(bh);
646 /* It is a hole, need to instantiate it. */
647 if (lcn == LCN_HOLE) {
648 // TODO: Instantiate the hole.
649 // clear_buffer_new(bh);
650 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
651 ntfs_error(vol->sb, "Writing into sparse regions is "
652 "not supported yet. Sorry.");
656 /* If first try and run list unmapped, map and retry. */
657 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
660 * Attempt to map run list, dropping lock for
663 up_read(&ni->run_list.lock);
664 err = map_run_list(ni, vcn);
666 goto lock_retry_remap;
669 /* Failed to map the buffer, even after retrying. */
670 bh->b_blocknr = -1UL;
671 ntfs_error(vol->sb, "vcn_to_lcn(vcn = 0x%llx) failed "
672 "with error code 0x%llx%s.",
673 (unsigned long long)vcn,
674 (unsigned long long)-lcn,
675 is_retry ? " even after retrying" : "");
676 // FIXME: Depending on vol->on_errors, do something.
680 } while (block++, (bh = bh->b_this_page) != head);
682 /* Release the lock if we took it. */
684 up_read(&ni->run_list.lock);
686 /* For the error case, need to reset bh to the beginning. */
689 /* Just an optimization, so ->readpage() isn't called later. */
690 if (unlikely(!PageUptodate(page))) {
693 if (!buffer_uptodate(bh)) {
698 } while ((bh = bh->b_this_page) != head);
700 SetPageUptodate(page);
703 /* Setup all mapped, dirty buffers for async write i/o. */
706 if (buffer_mapped(bh) && buffer_dirty(bh)) {
708 if (test_clear_buffer_dirty(bh)) {
709 BUG_ON(!buffer_uptodate(bh));
710 mark_buffer_async_write(bh);
713 } else if (unlikely(err)) {
715 * For the error case. The buffer may have been set
716 * dirty during attachment to a dirty page.
719 clear_buffer_dirty(bh);
721 } while ((bh = bh->b_this_page) != head);
724 // TODO: Remove the -EOPNOTSUPP check later on...
725 if (unlikely(err == -EOPNOTSUPP))
727 else if (err == -ENOMEM) {
728 ntfs_warning(vol->sb, "Error allocating memory. "
729 "Redirtying page so we try again "
732 * Put the page back on mapping->dirty_pages, but
733 * leave its buffer's dirty state as-is.
735 redirty_page_for_writepage(wbc, page);
741 BUG_ON(PageWriteback(page));
742 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
746 * Submit the prepared buffers for i/o. Note the page is unlocked,
747 * and the async write i/o completion handler can end_page_writeback()
748 * at any time after the *first* submit_bh(). So the buffers can then
751 need_end_writeback = TRUE;
753 struct buffer_head *next = bh->b_this_page;
754 if (buffer_async_write(bh)) {
755 submit_bh(WRITE, bh);
756 need_end_writeback = FALSE;
760 } while (bh != head);
762 /* If no i/o was started, need to end_page_writeback(). */
763 if (unlikely(need_end_writeback))
764 end_page_writeback(page);
771 * ntfs_writepage - write a @page to the backing store
772 * @page: page cache page to write out
774 * For non-resident attributes, ntfs_writepage() writes the @page by calling
775 * the ntfs version of the generic block_write_full_page() function,
776 * ntfs_write_block(), which in turn if necessary creates and writes the
777 * buffers associated with the page asynchronously.
779 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
780 * the data to the mft record (which at this stage is most likely in memory).
781 * Thus, in this case, I/O is synchronous, as even if the mft record is not
782 * cached at this point in time, we need to wait for it to be read in before we
785 * Note the caller clears the page dirty flag before calling ntfs_writepage().
787 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
789 * Return 0 on success and -errno on error.
791 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
795 ntfs_inode *ni, *base_ni;
797 attr_search_context *ctx;
802 BUG_ON(!PageLocked(page));
804 vi = page->mapping->host;
806 /* Is the page fully outside i_size? (truncate in progress) */
807 if (unlikely(page->index >= (vi->i_size + PAGE_CACHE_SIZE - 1) >>
810 ntfs_debug("Write outside i_size - truncated?");
816 if (NInoNonResident(ni)) {
818 * Only unnamed $DATA attributes can be compressed, encrypted,
821 if (ni->type == AT_DATA && !ni->name_len) {
822 /* If file is encrypted, deny access, just like NT4. */
823 if (NInoEncrypted(ni)) {
825 ntfs_debug("Denying write access to encrypted "
829 /* Compressed data streams are handled in compress.c. */
830 if (NInoCompressed(ni)) {
831 // TODO: Implement and replace this check with
832 // return ntfs_write_compressed_block(page);
834 ntfs_error(vi->i_sb, "Writing to compressed "
835 "files is not supported yet. "
839 // TODO: Implement and remove this check.
840 if (NInoSparse(ni)) {
842 ntfs_error(vi->i_sb, "Writing to sparse files "
843 "is not supported yet. Sorry.");
848 /* We have to zero every time due to mmap-at-end-of-file. */
849 if (page->index >= (vi->i_size >> PAGE_CACHE_SHIFT)) {
850 /* The page straddles i_size. */
851 unsigned int ofs = vi->i_size & ~PAGE_CACHE_MASK;
852 kaddr = kmap_atomic(page, KM_USER0);
853 memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
854 flush_dcache_page(page);
855 kunmap_atomic(kaddr, KM_USER0);
858 // TODO: Implement and remove this check.
859 if (NInoMstProtected(ni)) {
861 ntfs_error(vi->i_sb, "Writing to MST protected "
862 "attributes is not supported yet. "
867 /* Normal data stream. */
868 return ntfs_write_block(wbc, page);
872 * Attribute is resident, implying it is not compressed, encrypted, or
875 BUG_ON(page_has_buffers(page));
876 BUG_ON(!PageUptodate(page));
878 // TODO: Consider using PageWriteback() + unlock_page() in 2.5 once the
879 // "VM fiddling has ended". Note, don't forget to replace all the
880 // unlock_page() calls further below with end_page_writeback() ones.
881 // FIXME: Make sure it is ok to SetPageError() on unlocked page under
882 // writeback before doing the change!
884 set_page_writeback(page);
891 base_ni = ni->ext.base_ntfs_ino;
893 /* Map, pin, and lock the mft record. */
894 m = map_mft_record(base_ni);
895 if (unlikely(IS_ERR(m))) {
901 ctx = get_attr_search_ctx(base_ni, m);
902 if (unlikely(!ctx)) {
906 if (unlikely(!lookup_attr(ni->type, ni->name, ni->name_len,
907 IGNORE_CASE, 0, NULL, 0, ctx))) {
912 /* Starting position of the page within the attribute value. */
913 attr_pos = page->index << PAGE_CACHE_SHIFT;
915 /* The total length of the attribute value. */
916 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
918 if (unlikely(vi->i_size != attr_len)) {
919 ntfs_error(vi->i_sb, "BUG()! i_size (0x%llx) doesn't match "
920 "attr_len (0x%x). Aborting write.", vi->i_size,
925 if (unlikely(attr_pos >= attr_len)) {
926 ntfs_error(vi->i_sb, "BUG()! attr_pos (0x%llx) > attr_len "
927 "(0x%x). Aborting write.",
928 (unsigned long long)attr_pos, attr_len);
933 bytes = attr_len - attr_pos;
934 if (unlikely(bytes > PAGE_CACHE_SIZE))
935 bytes = PAGE_CACHE_SIZE;
938 * Here, we don't need to zero the out of bounds area everytime because
939 * the below memcpy() already takes care of the mmap-at-end-of-file
940 * requirements. If the file is converted to a non-resident one, then
941 * the code path use is switched to the non-resident one where the
942 * zeroing happens on each ntfs_writepage() invocation.
944 * The above also applies nicely when i_size is decreased.
946 * When i_size is increased, the memory between the old and new i_size
947 * _must_ be zeroed (or overwritten with new data). Otherwise we will
948 * expose data to userspace/disk which should never have been exposed.
950 * FIXME: Ensure that i_size increases do the zeroing/overwriting and
951 * if we cannot guarantee that, then enable the zeroing below.
954 kaddr = kmap_atomic(page, KM_USER0);
955 /* Copy the data from the page to the mft record. */
956 memcpy((u8*)ctx->attr + le16_to_cpu(
957 ctx->attr->data.resident.value_offset) + attr_pos,
959 flush_dcache_mft_record_page(ctx->ntfs_ino);
961 /* Zero out of bounds area. */
962 if (likely(bytes < PAGE_CACHE_SIZE)) {
963 memset(kaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
964 flush_dcache_page(page);
967 kunmap_atomic(kaddr, KM_USER0);
971 // TODO: Mark mft record dirty so it gets written back.
972 ntfs_error(vi->i_sb, "Writing to resident files is not supported yet. "
973 "Wrote to memory only...");
975 put_attr_search_ctx(ctx);
976 unmap_mft_record(base_ni);
979 if (err == -ENOMEM) {
980 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
981 "page so we try again later.");
983 * Put the page back on mapping->dirty_pages, but leave its
984 * buffer's dirty state as-is.
986 redirty_page_for_writepage(wbc, page);
989 ntfs_error(vi->i_sb, "Resident attribute write failed with "
990 "error %i. Setting page error flag.", -err);
995 put_attr_search_ctx(ctx);
997 unmap_mft_record(base_ni);
1002 * ntfs_prepare_nonresident_write -
1005 static int ntfs_prepare_nonresident_write(struct page *page,
1006 unsigned from, unsigned to)
1010 sector_t block, ablock, iblock;
1014 run_list_element *rl;
1015 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1016 unsigned int vcn_ofs, block_start, block_end, blocksize;
1019 unsigned char blocksize_bits;
1021 vi = page->mapping->host;
1025 ntfs_debug("Entering for inode %li, attribute type 0x%x, page index "
1026 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1027 page->index, from, to);
1029 BUG_ON(!NInoNonResident(ni));
1030 BUG_ON(NInoMstProtected(ni));
1032 blocksize_bits = vi->i_blkbits;
1033 blocksize = 1 << blocksize_bits;
1036 * create_empty_buffers() will create uptodate/dirty buffers if the
1037 * page is uptodate/dirty.
1039 if (!page_has_buffers(page))
1040 create_empty_buffers(page, blocksize, 0);
1041 bh = head = page_buffers(page);
1045 /* The first block in the page. */
1046 block = page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1049 * The first out of bounds block for the allocated size. No need to
1050 * round up as allocated_size is in multiples of cluster size and the
1051 * minimum cluster size is 512 bytes, which is equal to the smallest
1054 ablock = ni->allocated_size >> blocksize_bits;
1056 /* The last (fully or partially) initialized block. */
1057 iblock = ni->initialized_size >> blocksize_bits;
1059 /* Loop through all the buffers in the page. */
1064 block_end = block_start + blocksize;
1066 * If buffer @bh is outside the write, just mark it uptodate
1067 * if the page is uptodate and continue with the next buffer.
1069 if (block_end <= from || block_start >= to) {
1070 if (PageUptodate(page)) {
1071 if (!buffer_uptodate(bh))
1072 set_buffer_uptodate(bh);
1077 * @bh is at least partially being written to.
1078 * Make sure it is not marked as new.
1080 //if (buffer_new(bh))
1081 // clear_buffer_new(bh);
1083 if (block >= ablock) {
1084 // TODO: block is above allocated_size, need to
1085 // allocate it. Best done in one go to accommodate not
1086 // only block but all above blocks up to and including:
1087 // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1088 // - 1) >> blobksize_bits. Obviously will need to round
1089 // up to next cluster boundary, too. This should be
1090 // done with a helper function, so it can be reused.
1091 ntfs_error(vol->sb, "Writing beyond allocated size "
1092 "is not supported yet. Sorry.");
1095 // Need to update ablock.
1096 // Need to set_buffer_new() on all block bhs that are
1100 * Now we have enough allocated size to fulfill the whole
1101 * request, i.e. block < ablock is true.
1103 if (unlikely((block >= iblock) &&
1104 (ni->initialized_size < vi->i_size))) {
1106 * If this page is fully outside initialized size, zero
1107 * out all pages between the current initialized size
1108 * and the current page. Just use ntfs_readpage() to do
1109 * the zeroing transparently.
1111 if (block > iblock) {
1113 // For each page do:
1114 // - read_cache_page()
1115 // Again for each page do:
1116 // - wait_on_page_locked()
1117 // - Check (PageUptodate(page) &&
1118 // !PageError(page))
1119 // Update initialized size in the attribute and
1121 // Again, for each page do:
1122 // __set_page_dirty_buffers();
1123 // page_cache_release()
1124 // We don't need to wait on the writes.
1128 * The current page straddles initialized size. Zero
1129 * all non-uptodate buffers and set them uptodate (and
1130 * dirty?). Note, there aren't any non-uptodate buffers
1131 * if the page is uptodate.
1132 * FIXME: For an uptodate page, the buffers may need to
1133 * be written out because they were not initialized on
1136 if (!PageUptodate(page)) {
1138 // Zero any non-uptodate buffers up to i_size.
1139 // Set them uptodate and dirty.
1142 // Update initialized size in the attribute and in the
1143 // inode (up to i_size).
1145 // FIXME: This is inefficient. Try to batch the two
1146 // size changes to happen in one go.
1147 ntfs_error(vol->sb, "Writing beyond initialized size "
1148 "is not supported yet. Sorry.");
1151 // Do NOT set_buffer_new() BUT DO clear buffer range
1152 // outside write request range.
1153 // set_buffer_uptodate() on complete buffers as well as
1154 // set_buffer_dirty().
1157 /* Need to map unmapped buffers. */
1158 if (!buffer_mapped(bh)) {
1159 /* Unmapped buffer. Need to map it. */
1160 bh->b_bdev = vol->sb->s_bdev;
1162 /* Convert block into corresponding vcn and offset. */
1163 vcn = (VCN)block << blocksize_bits >>
1164 vol->cluster_size_bits;
1165 vcn_ofs = ((VCN)block << blocksize_bits) &
1166 vol->cluster_size_mask;
1171 down_read(&ni->run_list.lock);
1172 rl = ni->run_list.rl;
1174 if (likely(rl != NULL)) {
1175 /* Seek to element containing target vcn. */
1176 while (rl->length && rl[1].vcn <= vcn)
1178 lcn = vcn_to_lcn(rl, vcn);
1180 lcn = (LCN)LCN_RL_NOT_MAPPED;
1181 if (unlikely(lcn < 0)) {
1183 * We extended the attribute allocation above.
1184 * If we hit an ENOENT here it means that the
1185 * allocation was insufficient which is a bug.
1187 BUG_ON(lcn == LCN_ENOENT);
1189 /* It is a hole, need to instantiate it. */
1190 if (lcn == LCN_HOLE) {
1191 // TODO: Instantiate the hole.
1192 // clear_buffer_new(bh);
1193 // unmap_underlying_metadata(bh->b_bdev,
1195 // For non-uptodate buffers, need to
1196 // zero out the region outside the
1197 // request in this bh or all bhs,
1198 // depending on what we implemented
1200 // Need to flush_dcache_page().
1201 // Or could use set_buffer_new()
1203 ntfs_error(vol->sb, "Writing into "
1204 "sparse regions is "
1205 "not supported yet. "
1209 } else if (!is_retry &&
1210 lcn == LCN_RL_NOT_MAPPED) {
1213 * Attempt to map run list, dropping
1214 * lock for the duration.
1216 up_read(&ni->run_list.lock);
1217 err = map_run_list(ni, vcn);
1219 goto lock_retry_remap;
1223 * Failed to map the buffer, even after
1226 bh->b_blocknr = -1UL;
1227 ntfs_error(vol->sb, "vcn_to_lcn(vcn = 0x%llx) "
1228 "failed with error code "
1230 (unsigned long long)vcn,
1231 (unsigned long long)-lcn,
1232 is_retry ? " even after "
1234 // FIXME: Depending on vol->on_errors, do
1240 /* We now have a successful remap, i.e. lcn >= 0. */
1242 /* Setup buffer head to correct block. */
1243 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
1244 + vcn_ofs) >> blocksize_bits;
1245 set_buffer_mapped(bh);
1247 // FIXME: Something analogous to this is needed for
1248 // each newly allocated block, i.e. BH_New.
1249 // FIXME: Might need to take this out of the
1250 // if (!buffer_mapped(bh)) {}, depending on how we
1251 // implement things during the allocated_size and
1252 // initialized_size extension code above.
1253 if (buffer_new(bh)) {
1254 clear_buffer_new(bh);
1255 unmap_underlying_metadata(bh->b_bdev,
1257 if (PageUptodate(page)) {
1258 set_buffer_uptodate(bh);
1262 * Page is _not_ uptodate, zero surrounding
1263 * region. NOTE: This is how we decide if to
1266 if (block_end > to || block_start < from) {
1269 kaddr = kmap_atomic(page, KM_USER0);
1271 memset(kaddr + to, 0,
1273 if (block_start < from)
1274 memset(kaddr + block_start, 0,
1277 flush_dcache_page(page);
1278 kunmap_atomic(kaddr, KM_USER0);
1283 /* @bh is mapped, set it uptodate if the page is uptodate. */
1284 if (PageUptodate(page)) {
1285 if (!buffer_uptodate(bh))
1286 set_buffer_uptodate(bh);
1290 * The page is not uptodate. The buffer is mapped. If it is not
1291 * uptodate, and it is only partially being written to, we need
1292 * to read the buffer in before the write, i.e. right now.
1294 if (!buffer_uptodate(bh) &&
1295 (block_start < from || block_end > to)) {
1296 ll_rw_block(READ, 1, &bh);
1299 } while (block++, block_start = block_end,
1300 (bh = bh->b_this_page) != head);
1302 /* Release the lock if we took it. */
1304 up_read(&ni->run_list.lock);
1308 /* If we issued read requests, let them complete. */
1309 while (wait_bh > wait) {
1310 wait_on_buffer(*--wait_bh);
1311 if (!buffer_uptodate(*wait_bh))
1315 ntfs_debug("Done.");
1319 * Zero out any newly allocated blocks to avoid exposing stale data.
1320 * If BH_New is set, we know that the block was newly allocated in the
1322 * FIXME: What about initialized_size increments? Have we done all the
1323 * required zeroing above? If not this error handling is broken, and
1324 * in particular the if (block_end <= from) check is completely bogus.
1330 block_end = block_start + blocksize;
1331 if (block_end <= from)
1333 if (block_start >= to)
1335 if (buffer_new(bh)) {
1338 clear_buffer_new(bh);
1339 kaddr = kmap_atomic(page, KM_USER0);
1340 memset(kaddr + block_start, 0, bh->b_size);
1341 kunmap_atomic(kaddr, KM_USER0);
1342 set_buffer_uptodate(bh);
1343 mark_buffer_dirty(bh);
1346 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1348 flush_dcache_page(page);
1350 up_read(&ni->run_list.lock);
1355 * ntfs_prepare_write - prepare a page for receiving data
1357 * This is called from generic_file_write() with i_sem held on the inode
1358 * (@page->mapping->host). The @page is locked and kmap()ped so page_address()
1359 * can simply be used. The source data has not yet been copied into the @page.
1361 * Need to extend the attribute/fill in holes if necessary, create blocks and
1362 * make partially overwritten blocks uptodate,
1364 * i_size is not to be modified yet.
1366 * Return 0 on success or -errno on error.
1368 * Should be using block_prepare_write() [support for sparse files] or
1369 * cont_prepare_write() [no support for sparse files]. Can't do that due to
1370 * ntfs specifics but can look at them for implementation guidancea.
1372 * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1373 * the first byte in the page that will be written to and @to is the first byte
1374 * after the last byte that will be written to.
1376 static int ntfs_prepare_write(struct file *file, struct page *page,
1377 unsigned from, unsigned to)
1379 struct inode *vi = page->mapping->host;
1380 ntfs_inode *ni = NTFS_I(vi);
1382 ntfs_debug("Entering for inode %li, attribute type 0x%x, page index "
1383 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1384 page->index, from, to);
1386 BUG_ON(!PageLocked(page));
1387 BUG_ON(from > PAGE_CACHE_SIZE);
1388 BUG_ON(to > PAGE_CACHE_SIZE);
1391 if (NInoNonResident(ni)) {
1393 * Only unnamed $DATA attributes can be compressed, encrypted,
1396 if (ni->type == AT_DATA && !ni->name_len) {
1397 /* If file is encrypted, deny access, just like NT4. */
1398 if (NInoEncrypted(ni)) {
1399 ntfs_debug("Denying write access to encrypted "
1403 /* Compressed data streams are handled in compress.c. */
1404 if (NInoCompressed(ni)) {
1405 // TODO: Implement and replace this check with
1406 // return ntfs_write_compressed_block(page);
1407 ntfs_error(vi->i_sb, "Writing to compressed "
1408 "files is not supported yet. "
1412 // TODO: Implement and remove this check.
1413 if (NInoSparse(ni)) {
1414 ntfs_error(vi->i_sb, "Writing to sparse files "
1415 "is not supported yet. Sorry.");
1420 // TODO: Implement and remove this check.
1421 if (NInoMstProtected(ni)) {
1422 ntfs_error(vi->i_sb, "Writing to MST protected "
1423 "attributes is not supported yet. "
1428 /* Normal data stream. */
1429 return ntfs_prepare_nonresident_write(page, from, to);
1433 * Attribute is resident, implying it is not compressed, encrypted, or
1436 BUG_ON(page_has_buffers(page));
1438 /* Do we need to resize the attribute? */
1439 if (((s64)page->index << PAGE_CACHE_SHIFT) + to > vi->i_size) {
1440 // TODO: Implement resize...
1441 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1442 "not supported yet. Sorry.");
1447 * Because resident attributes are handled by memcpy() to/from the
1448 * corresponding MFT record, and because this form of i/o is byte
1449 * aligned rather than block aligned, there is no need to bring the
1450 * page uptodate here as in the non-resident case where we need to
1451 * bring the buffers straddled by the write uptodate before
1452 * generic_file_write() does the copying from userspace.
1454 * We thus defer the uptodate bringing of the page region outside the
1455 * region written to to ntfs_commit_write(). The reason for doing this
1456 * is that we save one round of:
1457 * map_mft_record(), get_attr_search_ctx(), lookup_attr(),
1458 * kmap_atomic(), kunmap_atomic(), put_attr_search_ctx(),
1459 * unmap_mft_record().
1460 * Which is obviously a very worthwhile save.
1462 * Thus we just return success now...
1464 ntfs_debug("Done.");
1469 * NOTES: There is a disparity between the apparent need to extend the
1470 * attribute in prepare write but to update i_size only in commit write.
1471 * Need to make sure i_sem protection is sufficient. And if not will need to
1472 * handle this in some way or another.
1476 * ntfs_commit_nonresident_write -
1479 static int ntfs_commit_nonresident_write(struct page *page,
1480 unsigned from, unsigned to)
1482 s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1484 struct buffer_head *bh, *head;
1485 unsigned int block_start, block_end, blocksize;
1488 vi = page->mapping->host;
1490 ntfs_debug("Entering for inode %li, attribute type 0x%x, page index "
1491 "0x%lx, from = %u, to = %u.", vi->i_ino,
1492 NTFS_I(vi)->type, page->index, from, to);
1494 blocksize = 1 << vi->i_blkbits;
1496 // FIXME: We need a whole slew of special cases in here for MST
1497 // protected attributes for example. For compressed files, too...
1498 // For now, we know ntfs_prepare_write() would have failed so we can't
1499 // get here in any of the cases which we have to special case, so we
1500 // are just a ripped off unrolled generic_commit_write() at present.
1502 bh = head = page_buffers(page);
1506 block_end = block_start + blocksize;
1507 if (block_end <= from || block_start >= to) {
1508 if (!buffer_uptodate(bh))
1511 set_buffer_uptodate(bh);
1512 mark_buffer_dirty(bh);
1514 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1517 * If this is a partial write which happened to make all buffers
1518 * uptodate then we can optimize away a bogus ->readpage() for the next
1519 * read(). Here we 'discover' whether the page went uptodate as a
1520 * result of this (potentially partial) write.
1523 SetPageUptodate(page);
1526 * Not convinced about this at all. See disparity comment above. For
1527 * now we know ntfs_prepare_write() would have failed in the write
1528 * exceeds i_size case, so this will never trigger which is fine.
1530 if (pos > vi->i_size) {
1531 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1532 "not supported yet. Sorry.");
1534 // vi->i_size = pos;
1535 // mark_inode_dirty(vi);
1537 ntfs_debug("Done.");
1542 * ntfs_commit_write - commit the received data
1544 * This is called from generic_file_write() with i_sem held on the inode
1545 * (@page->mapping->host). The @page is locked and kmap()ped so page_address()
1546 * can simply be used. The source data has already been copied into the @page.
1548 * Need to mark modified blocks dirty so they get written out later when
1549 * ntfs_writepage() is invoked by the VM.
1551 * Return 0 on success or -errno on error.
1553 * Should be using generic_commit_write(). This marks buffers uptodate and
1554 * dirty, sets the page uptodate if all buffers in the page are uptodate, and
1555 * updates i_size if the end of io is beyond i_size. In that case, it also
1556 * marks the inode dirty. - We could still use this (obviously except for
1557 * NInoMstProtected() attributes, where we will need to duplicate the core code
1558 * because we need our own async_io completion handler) but we could just do
1559 * the i_size update in prepare write, when we resize the attribute. Then
1560 * we would avoid the i_size update and mark_inode_dirty() happening here.
1562 * Can't use generic_commit_write() due to ntfs specialities but can look at
1563 * it for implementation guidance.
1565 * If things have gone as outlined in ntfs_prepare_write(), then we do not
1566 * need to do any page content modifications here at all, except in the write
1567 * to resident attribute case, where we need to do the uptodate bringing here
1568 * which we combine with the copying into the mft record which means we only
1569 * need to map the mft record and find the attribute record in it only once.
1571 static int ntfs_commit_write(struct file *file, struct page *page,
1572 unsigned from, unsigned to)
1576 ntfs_inode *ni, *base_ni;
1577 char *kaddr, *kattr;
1578 attr_search_context *ctx;
1580 u32 attr_len, bytes;
1583 vi = page->mapping->host;
1586 ntfs_debug("Entering for inode %li, attribute type 0x%x, page index "
1587 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1588 page->index, from, to);
1590 if (NInoNonResident(ni)) {
1592 * Only unnamed $DATA attributes can be compressed, encrypted,
1595 if (ni->type == AT_DATA && !ni->name_len) {
1596 /* If file is encrypted, deny access, just like NT4. */
1597 if (NInoEncrypted(ni)) {
1598 // Should never get here!
1599 ntfs_debug("Denying write access to encrypted "
1603 /* Compressed data streams are handled in compress.c. */
1604 if (NInoCompressed(ni)) {
1605 // TODO: Implement and replace this check with
1606 // return ntfs_write_compressed_block(page);
1607 // Should never get here!
1608 ntfs_error(vi->i_sb, "Writing to compressed "
1609 "files is not supported yet. "
1613 // TODO: Implement and remove this check.
1614 if (NInoSparse(ni)) {
1615 // Should never get here!
1616 ntfs_error(vi->i_sb, "Writing to sparse files "
1617 "is not supported yet. Sorry.");
1622 // TODO: Implement and remove this check.
1623 if (NInoMstProtected(ni)) {
1624 // Should never get here!
1625 ntfs_error(vi->i_sb, "Writing to MST protected "
1626 "attributes is not supported yet. "
1631 /* Normal data stream. */
1632 return ntfs_commit_nonresident_write(page, from, to);
1636 * Attribute is resident, implying it is not compressed, encrypted, or
1640 /* Do we need to resize the attribute? */
1641 if (((s64)page->index << PAGE_CACHE_SHIFT) + to > vi->i_size) {
1642 // TODO: Implement resize...
1643 // pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1644 // vi->i_size = pos;
1645 // mark_inode_dirty(vi);
1646 // Should never get here!
1647 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1648 "not supported yet. Sorry.");
1655 base_ni = ni->ext.base_ntfs_ino;
1657 /* Map, pin, and lock the mft record. */
1658 m = map_mft_record(base_ni);
1659 if (unlikely(IS_ERR(m))) {
1665 ctx = get_attr_search_ctx(base_ni, m);
1666 if (unlikely(!ctx)) {
1670 if (unlikely(!lookup_attr(ni->type, ni->name, ni->name_len,
1671 IGNORE_CASE, 0, NULL, 0, ctx))) {
1676 /* Starting position of the page within the attribute value. */
1677 attr_pos = page->index << PAGE_CACHE_SHIFT;
1679 /* The total length of the attribute value. */
1680 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1682 if (unlikely(vi->i_size != attr_len)) {
1683 ntfs_error(vi->i_sb, "BUG()! i_size (0x%llx) doesn't match "
1684 "attr_len (0x%x). Aborting write.", vi->i_size,
1689 if (unlikely(attr_pos >= attr_len)) {
1690 ntfs_error(vi->i_sb, "BUG()! attr_pos (0x%llx) > attr_len "
1691 "(0x%x). Aborting write.",
1692 (unsigned long long)attr_pos, attr_len);
1697 bytes = attr_len - attr_pos;
1698 if (unlikely(bytes > PAGE_CACHE_SIZE))
1699 bytes = PAGE_CACHE_SIZE;
1702 * Calculate the address of the attribute value corresponding to the
1703 * beginning of the current data @page.
1705 kattr = (u8*)ctx->attr + le16_to_cpu(
1706 ctx->attr->data.resident.value_offset) + attr_pos;
1708 kaddr = kmap_atomic(page, KM_USER0);
1710 /* Copy the received data from the page to the mft record. */
1711 memcpy(kattr + from, kaddr + from, to - from);
1712 flush_dcache_mft_record_page(ctx->ntfs_ino);
1714 if (!PageUptodate(page)) {
1716 * Bring the out of bounds area(s) uptodate by copying data
1717 * from the mft record to the page.
1720 memcpy(kaddr, kattr, from);
1722 memcpy(kaddr + to, kattr + to, bytes - to);
1724 /* Zero the region outside the end of the attribute value. */
1725 if (likely(bytes < PAGE_CACHE_SIZE))
1726 memset(kaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1729 * The probability of not having done any of the above is
1730 * extremely small, so we just flush unconditionally.
1732 flush_dcache_page(page);
1733 SetPageUptodate(page);
1735 kunmap_atomic(kaddr, KM_USER0);
1737 // TODO: Mark mft record dirty so it gets written back.
1738 ntfs_error(vi->i_sb, "Writing to resident files is not supported yet. "
1739 "Wrote to memory only...");
1741 put_attr_search_ctx(ctx);
1742 unmap_mft_record(base_ni);
1743 ntfs_debug("Done.");
1746 if (err == -ENOMEM) {
1747 ntfs_warning(vi->i_sb, "Error allocating memory required to "
1748 "commit the write.");
1749 if (PageUptodate(page)) {
1750 ntfs_warning(vi->i_sb, "Page is uptodate, setting "
1751 "dirty so the write will be retried "
1752 "later on by the VM.");
1754 * Put the page on mapping->dirty_pages, but leave its
1755 * buffer's dirty state as-is.
1757 __set_page_dirty_nobuffers(page);
1760 ntfs_error(vi->i_sb, "Page is not uptodate. Written "
1761 "data has been lost. )-:");
1763 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1764 "error %i. Setting page error flag.", -err);
1768 put_attr_search_ctx(ctx);
1770 unmap_mft_record(base_ni);
1774 #endif /* NTFS_RW */
1777 * ntfs_aops - general address space operations for inodes and attributes
1779 struct address_space_operations ntfs_aops = {
1780 .readpage = ntfs_readpage, /* Fill page with data. */
1781 .sync_page = block_sync_page, /* Currently, just unplugs the
1782 disk request queue. */
1784 .writepage = ntfs_writepage, /* Write dirty page to disk. */
1785 .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
1786 ready to receive data. */
1787 .commit_write = ntfs_commit_write, /* Commit received data. */