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 0x%lx, attribute type 0x%x, page index "
482 "0x%lx.", 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 * The mft record is then marked dirty and written out asynchronously via the
782 * vfs inode dirty code path.
784 * Note the caller clears the page dirty flag before calling ntfs_writepage().
786 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
788 * Return 0 on success and -errno on error.
790 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
794 ntfs_inode *ni, *base_ni;
796 attr_search_context *ctx;
801 BUG_ON(!PageLocked(page));
803 vi = page->mapping->host;
805 /* Is the page fully outside i_size? (truncate in progress) */
806 if (unlikely(page->index >= (vi->i_size + PAGE_CACHE_SIZE - 1) >>
809 ntfs_debug("Write outside i_size - truncated?");
815 if (NInoNonResident(ni)) {
817 * Only unnamed $DATA attributes can be compressed, encrypted,
820 if (ni->type == AT_DATA && !ni->name_len) {
821 /* If file is encrypted, deny access, just like NT4. */
822 if (NInoEncrypted(ni)) {
824 ntfs_debug("Denying write access to encrypted "
828 /* Compressed data streams are handled in compress.c. */
829 if (NInoCompressed(ni)) {
830 // TODO: Implement and replace this check with
831 // return ntfs_write_compressed_block(page);
833 ntfs_error(vi->i_sb, "Writing to compressed "
834 "files is not supported yet. "
838 // TODO: Implement and remove this check.
839 if (NInoSparse(ni)) {
841 ntfs_error(vi->i_sb, "Writing to sparse files "
842 "is not supported yet. Sorry.");
847 /* We have to zero every time due to mmap-at-end-of-file. */
848 if (page->index >= (vi->i_size >> PAGE_CACHE_SHIFT)) {
849 /* The page straddles i_size. */
850 unsigned int ofs = vi->i_size & ~PAGE_CACHE_MASK;
851 kaddr = kmap_atomic(page, KM_USER0);
852 memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
853 flush_dcache_page(page);
854 kunmap_atomic(kaddr, KM_USER0);
857 // TODO: Implement and remove this check.
858 if (NInoMstProtected(ni)) {
860 ntfs_error(vi->i_sb, "Writing to MST protected "
861 "attributes is not supported yet. "
866 /* Normal data stream. */
867 return ntfs_write_block(wbc, page);
871 * Attribute is resident, implying it is not compressed, encrypted, or
874 BUG_ON(page_has_buffers(page));
875 BUG_ON(!PageUptodate(page));
880 base_ni = ni->ext.base_ntfs_ino;
882 /* Map, pin, and lock the mft record. */
883 m = map_mft_record(base_ni);
884 if (unlikely(IS_ERR(m))) {
890 ctx = get_attr_search_ctx(base_ni, m);
891 if (unlikely(!ctx)) {
895 if (unlikely(!lookup_attr(ni->type, ni->name, ni->name_len,
896 IGNORE_CASE, 0, NULL, 0, ctx))) {
901 /* Starting position of the page within the attribute value. */
902 attr_pos = page->index << PAGE_CACHE_SHIFT;
904 /* The total length of the attribute value. */
905 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
907 if (unlikely(vi->i_size != attr_len)) {
908 ntfs_error(vi->i_sb, "BUG()! i_size (0x%llx) doesn't match "
909 "attr_len (0x%x). Aborting write.", vi->i_size,
914 if (unlikely(attr_pos >= attr_len)) {
915 ntfs_error(vi->i_sb, "BUG()! attr_pos (0x%llx) > attr_len "
916 "(0x%x). Aborting write.",
917 (unsigned long long)attr_pos, attr_len);
922 bytes = attr_len - attr_pos;
923 if (unlikely(bytes > PAGE_CACHE_SIZE))
924 bytes = PAGE_CACHE_SIZE;
927 * Keep the VM happy. This must be done otherwise the radix-tree tag
928 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
930 BUG_ON(PageWriteback(page));
931 set_page_writeback(page);
935 * Here, we don't need to zero the out of bounds area everytime because
936 * the below memcpy() already takes care of the mmap-at-end-of-file
937 * requirements. If the file is converted to a non-resident one, then
938 * the code path use is switched to the non-resident one where the
939 * zeroing happens on each ntfs_writepage() invocation.
941 * The above also applies nicely when i_size is decreased.
943 * When i_size is increased, the memory between the old and new i_size
944 * _must_ be zeroed (or overwritten with new data). Otherwise we will
945 * expose data to userspace/disk which should never have been exposed.
947 * FIXME: Ensure that i_size increases do the zeroing/overwriting and
948 * if we cannot guarantee that, then enable the zeroing below. If the
949 * zeroing below is enabled, we MUST move the unlock_page() from above
950 * to after the kunmap_atomic(), i.e. just before the
951 * end_page_writeback().
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);
969 end_page_writeback(page);
971 /* Mark the mft record dirty, so it gets written back. */
972 mark_mft_record_dirty(ctx->ntfs_ino);
974 put_attr_search_ctx(ctx);
975 unmap_mft_record(base_ni);
978 if (err == -ENOMEM) {
979 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
980 "page so we try again later.");
982 * Put the page back on mapping->dirty_pages, but leave its
983 * buffer's dirty state as-is.
985 redirty_page_for_writepage(wbc, page);
988 ntfs_error(vi->i_sb, "Resident attribute write failed with "
989 "error %i. Setting page error flag.", -err);
994 put_attr_search_ctx(ctx);
996 unmap_mft_record(base_ni);
1001 * ntfs_prepare_nonresident_write -
1004 static int ntfs_prepare_nonresident_write(struct page *page,
1005 unsigned from, unsigned to)
1009 sector_t block, ablock, iblock;
1013 run_list_element *rl;
1014 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1015 unsigned int vcn_ofs, block_start, block_end, blocksize;
1018 unsigned char blocksize_bits;
1020 vi = page->mapping->host;
1024 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1025 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1026 page->index, from, to);
1028 BUG_ON(!NInoNonResident(ni));
1029 BUG_ON(NInoMstProtected(ni));
1031 blocksize_bits = vi->i_blkbits;
1032 blocksize = 1 << blocksize_bits;
1035 * create_empty_buffers() will create uptodate/dirty buffers if the
1036 * page is uptodate/dirty.
1038 if (!page_has_buffers(page))
1039 create_empty_buffers(page, blocksize, 0);
1040 bh = head = page_buffers(page);
1044 /* The first block in the page. */
1045 block = page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1048 * The first out of bounds block for the allocated size. No need to
1049 * round up as allocated_size is in multiples of cluster size and the
1050 * minimum cluster size is 512 bytes, which is equal to the smallest
1053 ablock = ni->allocated_size >> blocksize_bits;
1055 /* The last (fully or partially) initialized block. */
1056 iblock = ni->initialized_size >> blocksize_bits;
1058 /* Loop through all the buffers in the page. */
1063 block_end = block_start + blocksize;
1065 * If buffer @bh is outside the write, just mark it uptodate
1066 * if the page is uptodate and continue with the next buffer.
1068 if (block_end <= from || block_start >= to) {
1069 if (PageUptodate(page)) {
1070 if (!buffer_uptodate(bh))
1071 set_buffer_uptodate(bh);
1076 * @bh is at least partially being written to.
1077 * Make sure it is not marked as new.
1079 //if (buffer_new(bh))
1080 // clear_buffer_new(bh);
1082 if (block >= ablock) {
1083 // TODO: block is above allocated_size, need to
1084 // allocate it. Best done in one go to accommodate not
1085 // only block but all above blocks up to and including:
1086 // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1087 // - 1) >> blobksize_bits. Obviously will need to round
1088 // up to next cluster boundary, too. This should be
1089 // done with a helper function, so it can be reused.
1090 ntfs_error(vol->sb, "Writing beyond allocated size "
1091 "is not supported yet. Sorry.");
1094 // Need to update ablock.
1095 // Need to set_buffer_new() on all block bhs that are
1099 * Now we have enough allocated size to fulfill the whole
1100 * request, i.e. block < ablock is true.
1102 if (unlikely((block >= iblock) &&
1103 (ni->initialized_size < vi->i_size))) {
1105 * If this page is fully outside initialized size, zero
1106 * out all pages between the current initialized size
1107 * and the current page. Just use ntfs_readpage() to do
1108 * the zeroing transparently.
1110 if (block > iblock) {
1112 // For each page do:
1113 // - read_cache_page()
1114 // Again for each page do:
1115 // - wait_on_page_locked()
1116 // - Check (PageUptodate(page) &&
1117 // !PageError(page))
1118 // Update initialized size in the attribute and
1120 // Again, for each page do:
1121 // __set_page_dirty_buffers();
1122 // page_cache_release()
1123 // We don't need to wait on the writes.
1127 * The current page straddles initialized size. Zero
1128 * all non-uptodate buffers and set them uptodate (and
1129 * dirty?). Note, there aren't any non-uptodate buffers
1130 * if the page is uptodate.
1131 * FIXME: For an uptodate page, the buffers may need to
1132 * be written out because they were not initialized on
1135 if (!PageUptodate(page)) {
1137 // Zero any non-uptodate buffers up to i_size.
1138 // Set them uptodate and dirty.
1141 // Update initialized size in the attribute and in the
1142 // inode (up to i_size).
1144 // FIXME: This is inefficient. Try to batch the two
1145 // size changes to happen in one go.
1146 ntfs_error(vol->sb, "Writing beyond initialized size "
1147 "is not supported yet. Sorry.");
1150 // Do NOT set_buffer_new() BUT DO clear buffer range
1151 // outside write request range.
1152 // set_buffer_uptodate() on complete buffers as well as
1153 // set_buffer_dirty().
1156 /* Need to map unmapped buffers. */
1157 if (!buffer_mapped(bh)) {
1158 /* Unmapped buffer. Need to map it. */
1159 bh->b_bdev = vol->sb->s_bdev;
1161 /* Convert block into corresponding vcn and offset. */
1162 vcn = (VCN)block << blocksize_bits >>
1163 vol->cluster_size_bits;
1164 vcn_ofs = ((VCN)block << blocksize_bits) &
1165 vol->cluster_size_mask;
1170 down_read(&ni->run_list.lock);
1171 rl = ni->run_list.rl;
1173 if (likely(rl != NULL)) {
1174 /* Seek to element containing target vcn. */
1175 while (rl->length && rl[1].vcn <= vcn)
1177 lcn = vcn_to_lcn(rl, vcn);
1179 lcn = (LCN)LCN_RL_NOT_MAPPED;
1180 if (unlikely(lcn < 0)) {
1182 * We extended the attribute allocation above.
1183 * If we hit an ENOENT here it means that the
1184 * allocation was insufficient which is a bug.
1186 BUG_ON(lcn == LCN_ENOENT);
1188 /* It is a hole, need to instantiate it. */
1189 if (lcn == LCN_HOLE) {
1190 // TODO: Instantiate the hole.
1191 // clear_buffer_new(bh);
1192 // unmap_underlying_metadata(bh->b_bdev,
1194 // For non-uptodate buffers, need to
1195 // zero out the region outside the
1196 // request in this bh or all bhs,
1197 // depending on what we implemented
1199 // Need to flush_dcache_page().
1200 // Or could use set_buffer_new()
1202 ntfs_error(vol->sb, "Writing into "
1203 "sparse regions is "
1204 "not supported yet. "
1208 } else if (!is_retry &&
1209 lcn == LCN_RL_NOT_MAPPED) {
1212 * Attempt to map run list, dropping
1213 * lock for the duration.
1215 up_read(&ni->run_list.lock);
1216 err = map_run_list(ni, vcn);
1218 goto lock_retry_remap;
1222 * Failed to map the buffer, even after
1225 bh->b_blocknr = -1UL;
1226 ntfs_error(vol->sb, "vcn_to_lcn(vcn = 0x%llx) "
1227 "failed with error code "
1229 (unsigned long long)vcn,
1230 (unsigned long long)-lcn,
1231 is_retry ? " even after "
1233 // FIXME: Depending on vol->on_errors, do
1239 /* We now have a successful remap, i.e. lcn >= 0. */
1241 /* Setup buffer head to correct block. */
1242 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
1243 + vcn_ofs) >> blocksize_bits;
1244 set_buffer_mapped(bh);
1246 // FIXME: Something analogous to this is needed for
1247 // each newly allocated block, i.e. BH_New.
1248 // FIXME: Might need to take this out of the
1249 // if (!buffer_mapped(bh)) {}, depending on how we
1250 // implement things during the allocated_size and
1251 // initialized_size extension code above.
1252 if (buffer_new(bh)) {
1253 clear_buffer_new(bh);
1254 unmap_underlying_metadata(bh->b_bdev,
1256 if (PageUptodate(page)) {
1257 set_buffer_uptodate(bh);
1261 * Page is _not_ uptodate, zero surrounding
1262 * region. NOTE: This is how we decide if to
1265 if (block_end > to || block_start < from) {
1268 kaddr = kmap_atomic(page, KM_USER0);
1270 memset(kaddr + to, 0,
1272 if (block_start < from)
1273 memset(kaddr + block_start, 0,
1276 flush_dcache_page(page);
1277 kunmap_atomic(kaddr, KM_USER0);
1282 /* @bh is mapped, set it uptodate if the page is uptodate. */
1283 if (PageUptodate(page)) {
1284 if (!buffer_uptodate(bh))
1285 set_buffer_uptodate(bh);
1289 * The page is not uptodate. The buffer is mapped. If it is not
1290 * uptodate, and it is only partially being written to, we need
1291 * to read the buffer in before the write, i.e. right now.
1293 if (!buffer_uptodate(bh) &&
1294 (block_start < from || block_end > to)) {
1295 ll_rw_block(READ, 1, &bh);
1298 } while (block++, block_start = block_end,
1299 (bh = bh->b_this_page) != head);
1301 /* Release the lock if we took it. */
1303 up_read(&ni->run_list.lock);
1307 /* If we issued read requests, let them complete. */
1308 while (wait_bh > wait) {
1309 wait_on_buffer(*--wait_bh);
1310 if (!buffer_uptodate(*wait_bh))
1314 ntfs_debug("Done.");
1318 * Zero out any newly allocated blocks to avoid exposing stale data.
1319 * If BH_New is set, we know that the block was newly allocated in the
1321 * FIXME: What about initialized_size increments? Have we done all the
1322 * required zeroing above? If not this error handling is broken, and
1323 * in particular the if (block_end <= from) check is completely bogus.
1329 block_end = block_start + blocksize;
1330 if (block_end <= from)
1332 if (block_start >= to)
1334 if (buffer_new(bh)) {
1337 clear_buffer_new(bh);
1338 kaddr = kmap_atomic(page, KM_USER0);
1339 memset(kaddr + block_start, 0, bh->b_size);
1340 kunmap_atomic(kaddr, KM_USER0);
1341 set_buffer_uptodate(bh);
1342 mark_buffer_dirty(bh);
1345 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1347 flush_dcache_page(page);
1349 up_read(&ni->run_list.lock);
1354 * ntfs_prepare_write - prepare a page for receiving data
1356 * This is called from generic_file_write() with i_sem held on the inode
1357 * (@page->mapping->host). The @page is locked and kmap()ped so page_address()
1358 * can simply be used. The source data has not yet been copied into the @page.
1360 * Need to extend the attribute/fill in holes if necessary, create blocks and
1361 * make partially overwritten blocks uptodate,
1363 * i_size is not to be modified yet.
1365 * Return 0 on success or -errno on error.
1367 * Should be using block_prepare_write() [support for sparse files] or
1368 * cont_prepare_write() [no support for sparse files]. Can't do that due to
1369 * ntfs specifics but can look at them for implementation guidancea.
1371 * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1372 * the first byte in the page that will be written to and @to is the first byte
1373 * after the last byte that will be written to.
1375 static int ntfs_prepare_write(struct file *file, struct page *page,
1376 unsigned from, unsigned to)
1378 struct inode *vi = page->mapping->host;
1379 ntfs_inode *ni = NTFS_I(vi);
1381 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1382 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1383 page->index, from, to);
1385 BUG_ON(!PageLocked(page));
1386 BUG_ON(from > PAGE_CACHE_SIZE);
1387 BUG_ON(to > PAGE_CACHE_SIZE);
1390 if (NInoNonResident(ni)) {
1392 * Only unnamed $DATA attributes can be compressed, encrypted,
1395 if (ni->type == AT_DATA && !ni->name_len) {
1396 /* If file is encrypted, deny access, just like NT4. */
1397 if (NInoEncrypted(ni)) {
1398 ntfs_debug("Denying write access to encrypted "
1402 /* Compressed data streams are handled in compress.c. */
1403 if (NInoCompressed(ni)) {
1404 // TODO: Implement and replace this check with
1405 // return ntfs_write_compressed_block(page);
1406 ntfs_error(vi->i_sb, "Writing to compressed "
1407 "files is not supported yet. "
1411 // TODO: Implement and remove this check.
1412 if (NInoSparse(ni)) {
1413 ntfs_error(vi->i_sb, "Writing to sparse files "
1414 "is not supported yet. Sorry.");
1419 // TODO: Implement and remove this check.
1420 if (NInoMstProtected(ni)) {
1421 ntfs_error(vi->i_sb, "Writing to MST protected "
1422 "attributes is not supported yet. "
1427 /* Normal data stream. */
1428 return ntfs_prepare_nonresident_write(page, from, to);
1432 * Attribute is resident, implying it is not compressed, encrypted, or
1435 BUG_ON(page_has_buffers(page));
1437 /* Do we need to resize the attribute? */
1438 if (((s64)page->index << PAGE_CACHE_SHIFT) + to > vi->i_size) {
1439 // TODO: Implement resize...
1440 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1441 "not supported yet. Sorry.");
1446 * Because resident attributes are handled by memcpy() to/from the
1447 * corresponding MFT record, and because this form of i/o is byte
1448 * aligned rather than block aligned, there is no need to bring the
1449 * page uptodate here as in the non-resident case where we need to
1450 * bring the buffers straddled by the write uptodate before
1451 * generic_file_write() does the copying from userspace.
1453 * We thus defer the uptodate bringing of the page region outside the
1454 * region written to to ntfs_commit_write(). The reason for doing this
1455 * is that we save one round of:
1456 * map_mft_record(), get_attr_search_ctx(), lookup_attr(),
1457 * kmap_atomic(), kunmap_atomic(), put_attr_search_ctx(),
1458 * unmap_mft_record().
1459 * Which is obviously a very worthwhile save.
1461 * Thus we just return success now...
1463 ntfs_debug("Done.");
1468 * NOTES: There is a disparity between the apparent need to extend the
1469 * attribute in prepare write but to update i_size only in commit write.
1470 * Need to make sure i_sem protection is sufficient. And if not will need to
1471 * handle this in some way or another.
1475 * ntfs_commit_nonresident_write -
1478 static int ntfs_commit_nonresident_write(struct page *page,
1479 unsigned from, unsigned to)
1481 s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1483 struct buffer_head *bh, *head;
1484 unsigned int block_start, block_end, blocksize;
1487 vi = page->mapping->host;
1489 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1490 "0x%lx, from = %u, to = %u.", vi->i_ino,
1491 NTFS_I(vi)->type, page->index, from, to);
1493 blocksize = 1 << vi->i_blkbits;
1495 // FIXME: We need a whole slew of special cases in here for MST
1496 // protected attributes for example. For compressed files, too...
1497 // For now, we know ntfs_prepare_write() would have failed so we can't
1498 // get here in any of the cases which we have to special case, so we
1499 // are just a ripped off unrolled generic_commit_write() at present.
1501 bh = head = page_buffers(page);
1505 block_end = block_start + blocksize;
1506 if (block_end <= from || block_start >= to) {
1507 if (!buffer_uptodate(bh))
1510 set_buffer_uptodate(bh);
1511 mark_buffer_dirty(bh);
1513 } while (block_start = block_end, (bh = bh->b_this_page) != head);
1516 * If this is a partial write which happened to make all buffers
1517 * uptodate then we can optimize away a bogus ->readpage() for the next
1518 * read(). Here we 'discover' whether the page went uptodate as a
1519 * result of this (potentially partial) write.
1522 SetPageUptodate(page);
1525 * Not convinced about this at all. See disparity comment above. For
1526 * now we know ntfs_prepare_write() would have failed in the write
1527 * exceeds i_size case, so this will never trigger which is fine.
1529 if (pos > vi->i_size) {
1530 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1531 "not supported yet. Sorry.");
1533 // vi->i_size = pos;
1534 // mark_inode_dirty(vi);
1536 ntfs_debug("Done.");
1541 * ntfs_commit_write - commit the received data
1543 * This is called from generic_file_write() with i_sem held on the inode
1544 * (@page->mapping->host). The @page is locked and kmap()ped so page_address()
1545 * can simply be used. The source data has already been copied into the @page.
1547 * Need to mark modified blocks dirty so they get written out later when
1548 * ntfs_writepage() is invoked by the VM.
1550 * Return 0 on success or -errno on error.
1552 * Should be using generic_commit_write(). This marks buffers uptodate and
1553 * dirty, sets the page uptodate if all buffers in the page are uptodate, and
1554 * updates i_size if the end of io is beyond i_size. In that case, it also
1555 * marks the inode dirty. - We could still use this (obviously except for
1556 * NInoMstProtected() attributes, where we will need to duplicate the core code
1557 * because we need our own async_io completion handler) but we could just do
1558 * the i_size update in prepare write, when we resize the attribute. Then
1559 * we would avoid the i_size update and mark_inode_dirty() happening here.
1561 * Can't use generic_commit_write() due to ntfs specialities but can look at
1562 * it for implementation guidance.
1564 * If things have gone as outlined in ntfs_prepare_write(), then we do not
1565 * need to do any page content modifications here at all, except in the write
1566 * to resident attribute case, where we need to do the uptodate bringing here
1567 * which we combine with the copying into the mft record which means we only
1568 * need to map the mft record and find the attribute record in it only once.
1570 static int ntfs_commit_write(struct file *file, struct page *page,
1571 unsigned from, unsigned to)
1575 ntfs_inode *ni, *base_ni;
1576 char *kaddr, *kattr;
1577 attr_search_context *ctx;
1579 u32 attr_len, bytes;
1582 vi = page->mapping->host;
1585 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1586 "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1587 page->index, from, to);
1589 if (NInoNonResident(ni)) {
1591 * Only unnamed $DATA attributes can be compressed, encrypted,
1594 if (ni->type == AT_DATA && !ni->name_len) {
1595 /* If file is encrypted, deny access, just like NT4. */
1596 if (NInoEncrypted(ni)) {
1597 // Should never get here!
1598 ntfs_debug("Denying write access to encrypted "
1602 /* Compressed data streams are handled in compress.c. */
1603 if (NInoCompressed(ni)) {
1604 // TODO: Implement and replace this check with
1605 // return ntfs_write_compressed_block(page);
1606 // Should never get here!
1607 ntfs_error(vi->i_sb, "Writing to compressed "
1608 "files is not supported yet. "
1612 // TODO: Implement and remove this check.
1613 if (NInoSparse(ni)) {
1614 // Should never get here!
1615 ntfs_error(vi->i_sb, "Writing to sparse files "
1616 "is not supported yet. Sorry.");
1621 // TODO: Implement and remove this check.
1622 if (NInoMstProtected(ni)) {
1623 // Should never get here!
1624 ntfs_error(vi->i_sb, "Writing to MST protected "
1625 "attributes is not supported yet. "
1630 /* Normal data stream. */
1631 return ntfs_commit_nonresident_write(page, from, to);
1635 * Attribute is resident, implying it is not compressed, encrypted, or
1639 /* Do we need to resize the attribute? */
1640 if (((s64)page->index << PAGE_CACHE_SHIFT) + to > vi->i_size) {
1641 // TODO: Implement resize...
1642 // pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1643 // vi->i_size = pos;
1644 // mark_inode_dirty(vi);
1645 // Should never get here!
1646 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
1647 "not supported yet. Sorry.");
1654 base_ni = ni->ext.base_ntfs_ino;
1656 /* Map, pin, and lock the mft record. */
1657 m = map_mft_record(base_ni);
1658 if (unlikely(IS_ERR(m))) {
1664 ctx = get_attr_search_ctx(base_ni, m);
1665 if (unlikely(!ctx)) {
1669 if (unlikely(!lookup_attr(ni->type, ni->name, ni->name_len,
1670 IGNORE_CASE, 0, NULL, 0, ctx))) {
1675 /* Starting position of the page within the attribute value. */
1676 attr_pos = page->index << PAGE_CACHE_SHIFT;
1678 /* The total length of the attribute value. */
1679 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1681 if (unlikely(vi->i_size != attr_len)) {
1682 ntfs_error(vi->i_sb, "BUG()! i_size (0x%llx) doesn't match "
1683 "attr_len (0x%x). Aborting write.", vi->i_size,
1688 if (unlikely(attr_pos >= attr_len)) {
1689 ntfs_error(vi->i_sb, "BUG()! attr_pos (0x%llx) > attr_len "
1690 "(0x%x). Aborting write.",
1691 (unsigned long long)attr_pos, attr_len);
1696 bytes = attr_len - attr_pos;
1697 if (unlikely(bytes > PAGE_CACHE_SIZE))
1698 bytes = PAGE_CACHE_SIZE;
1701 * Calculate the address of the attribute value corresponding to the
1702 * beginning of the current data @page.
1704 kattr = (u8*)ctx->attr + le16_to_cpu(
1705 ctx->attr->data.resident.value_offset) + attr_pos;
1707 kaddr = kmap_atomic(page, KM_USER0);
1709 /* Copy the received data from the page to the mft record. */
1710 memcpy(kattr + from, kaddr + from, to - from);
1711 flush_dcache_mft_record_page(ctx->ntfs_ino);
1713 if (!PageUptodate(page)) {
1715 * Bring the out of bounds area(s) uptodate by copying data
1716 * from the mft record to the page.
1719 memcpy(kaddr, kattr, from);
1721 memcpy(kaddr + to, kattr + to, bytes - to);
1723 /* Zero the region outside the end of the attribute value. */
1724 if (likely(bytes < PAGE_CACHE_SIZE))
1725 memset(kaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1728 * The probability of not having done any of the above is
1729 * extremely small, so we just flush unconditionally.
1731 flush_dcache_page(page);
1732 SetPageUptodate(page);
1734 kunmap_atomic(kaddr, KM_USER0);
1736 /* Mark the mft record dirty, so it gets written back. */
1737 mark_mft_record_dirty(ctx->ntfs_ino);
1739 put_attr_search_ctx(ctx);
1740 unmap_mft_record(base_ni);
1741 ntfs_debug("Done.");
1744 if (err == -ENOMEM) {
1745 ntfs_warning(vi->i_sb, "Error allocating memory required to "
1746 "commit the write.");
1747 if (PageUptodate(page)) {
1748 ntfs_warning(vi->i_sb, "Page is uptodate, setting "
1749 "dirty so the write will be retried "
1750 "later on by the VM.");
1752 * Put the page on mapping->dirty_pages, but leave its
1753 * buffer's dirty state as-is.
1755 __set_page_dirty_nobuffers(page);
1758 ntfs_error(vi->i_sb, "Page is not uptodate. Written "
1759 "data has been lost. )-:");
1761 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1762 "error %i. Setting page error flag.", -err);
1766 put_attr_search_ctx(ctx);
1768 unmap_mft_record(base_ni);
1772 #endif /* NTFS_RW */
1775 * ntfs_aops - general address space operations for inodes and attributes
1777 struct address_space_operations ntfs_aops = {
1778 .readpage = ntfs_readpage, /* Fill page with data. */
1779 .sync_page = block_sync_page, /* Currently, just unplugs the
1780 disk request queue. */
1782 .writepage = ntfs_writepage, /* Write dirty page to disk. */
1783 .prepare_write = ntfs_prepare_write, /* Prepare page and buffers
1784 ready to receive data. */
1785 .commit_write = ntfs_commit_write, /* Commit received data. */
1790 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1793 struct address_space_operations ntfs_mst_aops = {
1794 .readpage = ntfs_readpage, /* Fill page with data. */
1795 .sync_page = block_sync_page, /* Currently, just unplugs the
1796 disk request queue. */