2 * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc.
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the Linux-NTFS
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/backing-dev.h>
23 #include <linux/buffer_head.h>
24 #include <linux/gfp.h>
25 #include <linux/pagemap.h>
26 #include <linux/pagevec.h>
27 #include <linux/sched.h>
28 #include <linux/swap.h>
29 #include <linux/uio.h>
30 #include <linux/writeback.h>
31 #include <linux/aio.h>
34 #include <asm/uaccess.h>
46 * ntfs_file_open - called when an inode is about to be opened
47 * @vi: inode to be opened
48 * @filp: file structure describing the inode
50 * Limit file size to the page cache limit on architectures where unsigned long
51 * is 32-bits. This is the most we can do for now without overflowing the page
52 * cache page index. Doing it this way means we don't run into problems because
53 * of existing too large files. It would be better to allow the user to read
54 * the beginning of the file but I doubt very much anyone is going to hit this
55 * check on a 32-bit architecture, so there is no point in adding the extra
56 * complexity required to support this.
58 * On 64-bit architectures, the check is hopefully optimized away by the
61 * After the check passes, just call generic_file_open() to do its work.
63 static int ntfs_file_open(struct inode
*vi
, struct file
*filp
)
65 if (sizeof(unsigned long) < 8) {
66 if (i_size_read(vi
) > MAX_LFS_FILESIZE
)
69 return generic_file_open(vi
, filp
);
75 * ntfs_attr_extend_initialized - extend the initialized size of an attribute
76 * @ni: ntfs inode of the attribute to extend
77 * @new_init_size: requested new initialized size in bytes
79 * Extend the initialized size of an attribute described by the ntfs inode @ni
80 * to @new_init_size bytes. This involves zeroing any non-sparse space between
81 * the old initialized size and @new_init_size both in the page cache and on
82 * disk (if relevant complete pages are already uptodate in the page cache then
83 * these are simply marked dirty).
85 * As a side-effect, the file size (vfs inode->i_size) may be incremented as,
86 * in the resident attribute case, it is tied to the initialized size and, in
87 * the non-resident attribute case, it may not fall below the initialized size.
89 * Note that if the attribute is resident, we do not need to touch the page
90 * cache at all. This is because if the page cache page is not uptodate we
91 * bring it uptodate later, when doing the write to the mft record since we
92 * then already have the page mapped. And if the page is uptodate, the
93 * non-initialized region will already have been zeroed when the page was
94 * brought uptodate and the region may in fact already have been overwritten
95 * with new data via mmap() based writes, so we cannot just zero it. And since
96 * POSIX specifies that the behaviour of resizing a file whilst it is mmap()ped
97 * is unspecified, we choose not to do zeroing and thus we do not need to touch
98 * the page at all. For a more detailed explanation see ntfs_truncate() in
101 * Return 0 on success and -errno on error. In the case that an error is
102 * encountered it is possible that the initialized size will already have been
103 * incremented some way towards @new_init_size but it is guaranteed that if
104 * this is the case, the necessary zeroing will also have happened and that all
105 * metadata is self-consistent.
107 * Locking: i_mutex on the vfs inode corrseponsind to the ntfs inode @ni must be
108 * held by the caller.
110 static int ntfs_attr_extend_initialized(ntfs_inode
*ni
, const s64 new_init_size
)
114 pgoff_t index
, end_index
;
116 struct inode
*vi
= VFS_I(ni
);
118 MFT_RECORD
*m
= NULL
;
120 ntfs_attr_search_ctx
*ctx
= NULL
;
121 struct address_space
*mapping
;
122 struct page
*page
= NULL
;
127 read_lock_irqsave(&ni
->size_lock
, flags
);
128 old_init_size
= ni
->initialized_size
;
129 old_i_size
= i_size_read(vi
);
130 BUG_ON(new_init_size
> ni
->allocated_size
);
131 read_unlock_irqrestore(&ni
->size_lock
, flags
);
132 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
133 "old_initialized_size 0x%llx, "
134 "new_initialized_size 0x%llx, i_size 0x%llx.",
135 vi
->i_ino
, (unsigned)le32_to_cpu(ni
->type
),
136 (unsigned long long)old_init_size
,
137 (unsigned long long)new_init_size
, old_i_size
);
141 base_ni
= ni
->ext
.base_ntfs_ino
;
142 /* Use goto to reduce indentation and we need the label below anyway. */
143 if (NInoNonResident(ni
))
144 goto do_non_resident_extend
;
145 BUG_ON(old_init_size
!= old_i_size
);
146 m
= map_mft_record(base_ni
);
152 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
153 if (unlikely(!ctx
)) {
157 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
158 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
166 BUG_ON(a
->non_resident
);
167 /* The total length of the attribute value. */
168 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
169 BUG_ON(old_i_size
!= (loff_t
)attr_len
);
171 * Do the zeroing in the mft record and update the attribute size in
174 kattr
= (u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
);
175 memset(kattr
+ attr_len
, 0, new_init_size
- attr_len
);
176 a
->data
.resident
.value_length
= cpu_to_le32((u32
)new_init_size
);
177 /* Finally, update the sizes in the vfs and ntfs inodes. */
178 write_lock_irqsave(&ni
->size_lock
, flags
);
179 i_size_write(vi
, new_init_size
);
180 ni
->initialized_size
= new_init_size
;
181 write_unlock_irqrestore(&ni
->size_lock
, flags
);
183 do_non_resident_extend
:
185 * If the new initialized size @new_init_size exceeds the current file
186 * size (vfs inode->i_size), we need to extend the file size to the
187 * new initialized size.
189 if (new_init_size
> old_i_size
) {
190 m
= map_mft_record(base_ni
);
196 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
197 if (unlikely(!ctx
)) {
201 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
202 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
210 BUG_ON(!a
->non_resident
);
211 BUG_ON(old_i_size
!= (loff_t
)
212 sle64_to_cpu(a
->data
.non_resident
.data_size
));
213 a
->data
.non_resident
.data_size
= cpu_to_sle64(new_init_size
);
214 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
215 mark_mft_record_dirty(ctx
->ntfs_ino
);
216 /* Update the file size in the vfs inode. */
217 i_size_write(vi
, new_init_size
);
218 ntfs_attr_put_search_ctx(ctx
);
220 unmap_mft_record(base_ni
);
223 mapping
= vi
->i_mapping
;
224 index
= old_init_size
>> PAGE_CACHE_SHIFT
;
225 end_index
= (new_init_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
228 * Read the page. If the page is not present, this will zero
229 * the uninitialized regions for us.
231 page
= read_mapping_page(mapping
, index
, NULL
);
236 if (unlikely(PageError(page
))) {
237 page_cache_release(page
);
242 * Update the initialized size in the ntfs inode. This is
243 * enough to make ntfs_writepage() work.
245 write_lock_irqsave(&ni
->size_lock
, flags
);
246 ni
->initialized_size
= (s64
)(index
+ 1) << PAGE_CACHE_SHIFT
;
247 if (ni
->initialized_size
> new_init_size
)
248 ni
->initialized_size
= new_init_size
;
249 write_unlock_irqrestore(&ni
->size_lock
, flags
);
250 /* Set the page dirty so it gets written out. */
251 set_page_dirty(page
);
252 page_cache_release(page
);
254 * Play nice with the vm and the rest of the system. This is
255 * very much needed as we can potentially be modifying the
256 * initialised size from a very small value to a really huge
258 * f = open(somefile, O_TRUNC);
259 * truncate(f, 10GiB);
262 * And this would mean we would be marking dirty hundreds of
263 * thousands of pages or as in the above example more than
264 * two and a half million pages!
266 * TODO: For sparse pages could optimize this workload by using
267 * the FsMisc / MiscFs page bit as a "PageIsSparse" bit. This
268 * would be set in readpage for sparse pages and here we would
269 * not need to mark dirty any pages which have this bit set.
270 * The only caveat is that we have to clear the bit everywhere
271 * where we allocate any clusters that lie in the page or that
274 * TODO: An even greater optimization would be for us to only
275 * call readpage() on pages which are not in sparse regions as
276 * determined from the runlist. This would greatly reduce the
277 * number of pages we read and make dirty in the case of sparse
280 balance_dirty_pages_ratelimited(mapping
);
282 } while (++index
< end_index
);
283 read_lock_irqsave(&ni
->size_lock
, flags
);
284 BUG_ON(ni
->initialized_size
!= new_init_size
);
285 read_unlock_irqrestore(&ni
->size_lock
, flags
);
286 /* Now bring in sync the initialized_size in the mft record. */
287 m
= map_mft_record(base_ni
);
293 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
294 if (unlikely(!ctx
)) {
298 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
299 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
307 BUG_ON(!a
->non_resident
);
308 a
->data
.non_resident
.initialized_size
= cpu_to_sle64(new_init_size
);
310 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
311 mark_mft_record_dirty(ctx
->ntfs_ino
);
313 ntfs_attr_put_search_ctx(ctx
);
315 unmap_mft_record(base_ni
);
316 ntfs_debug("Done, initialized_size 0x%llx, i_size 0x%llx.",
317 (unsigned long long)new_init_size
, i_size_read(vi
));
320 write_lock_irqsave(&ni
->size_lock
, flags
);
321 ni
->initialized_size
= old_init_size
;
322 write_unlock_irqrestore(&ni
->size_lock
, flags
);
325 ntfs_attr_put_search_ctx(ctx
);
327 unmap_mft_record(base_ni
);
328 ntfs_debug("Failed. Returning error code %i.", err
);
332 static ssize_t
ntfs_prepare_file_for_write(struct file
*file
, loff_t
*ppos
,
339 struct inode
*vi
= file_inode(file
);
340 ntfs_inode
*base_ni
, *ni
= NTFS_I(vi
);
341 ntfs_volume
*vol
= ni
->vol
;
343 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, pos "
344 "0x%llx, count 0x%lx.", vi
->i_ino
,
345 (unsigned)le32_to_cpu(ni
->type
),
346 (unsigned long long)*ppos
, (unsigned long)*count
);
347 /* We can write back this queue in page reclaim. */
348 current
->backing_dev_info
= inode_to_bdi(vi
);
349 err
= generic_write_checks(file
, ppos
, count
, S_ISBLK(vi
->i_mode
));
353 * All checks have passed. Before we start doing any writing we want
354 * to abort any totally illegal writes.
356 BUG_ON(NInoMstProtected(ni
));
357 BUG_ON(ni
->type
!= AT_DATA
);
358 /* If file is encrypted, deny access, just like NT4. */
359 if (NInoEncrypted(ni
)) {
360 /* Only $DATA attributes can be encrypted. */
362 * Reminder for later: Encrypted files are _always_
363 * non-resident so that the content can always be encrypted.
365 ntfs_debug("Denying write access to encrypted file.");
369 if (NInoCompressed(ni
)) {
370 /* Only unnamed $DATA attribute can be compressed. */
371 BUG_ON(ni
->name_len
);
373 * Reminder for later: If resident, the data is not actually
374 * compressed. Only on the switch to non-resident does
375 * compression kick in. This is in contrast to encrypted files
378 ntfs_error(vi
->i_sb
, "Writing to compressed files is not "
379 "implemented yet. Sorry.");
387 base_ni
= ni
->ext
.base_ntfs_ino
;
388 err
= file_remove_suid(file
);
392 * Our ->update_time method always succeeds thus file_update_time()
393 * cannot fail either so there is no need to check the return code.
395 file_update_time(file
);
397 /* The first byte after the last cluster being written to. */
398 end
= (pos
+ *count
+ vol
->cluster_size_mask
) &
399 ~(u64
)vol
->cluster_size_mask
;
401 * If the write goes beyond the allocated size, extend the allocation
402 * to cover the whole of the write, rounded up to the nearest cluster.
404 read_lock_irqsave(&ni
->size_lock
, flags
);
405 ll
= ni
->allocated_size
;
406 read_unlock_irqrestore(&ni
->size_lock
, flags
);
409 * Extend the allocation without changing the data size.
411 * Note we ensure the allocation is big enough to at least
412 * write some data but we do not require the allocation to be
413 * complete, i.e. it may be partial.
415 ll
= ntfs_attr_extend_allocation(ni
, end
, -1, pos
);
416 if (likely(ll
>= 0)) {
418 /* If the extension was partial truncate the write. */
420 ntfs_debug("Truncating write to inode 0x%lx, "
421 "attribute type 0x%x, because "
422 "the allocation was only "
423 "partially extended.",
424 vi
->i_ino
, (unsigned)
425 le32_to_cpu(ni
->type
));
430 read_lock_irqsave(&ni
->size_lock
, flags
);
431 ll
= ni
->allocated_size
;
432 read_unlock_irqrestore(&ni
->size_lock
, flags
);
433 /* Perform a partial write if possible or fail. */
435 ntfs_debug("Truncating write to inode 0x%lx "
436 "attribute type 0x%x, because "
437 "extending the allocation "
438 "failed (error %d).",
439 vi
->i_ino
, (unsigned)
440 le32_to_cpu(ni
->type
),
445 ntfs_error(vi
->i_sb
, "Cannot perform "
448 "type 0x%x, because "
452 vi
->i_ino
, (unsigned)
453 le32_to_cpu(ni
->type
),
456 ntfs_debug("Cannot perform write to "
458 "attribute type 0x%x, "
459 "because there is not "
461 vi
->i_ino
, (unsigned)
462 le32_to_cpu(ni
->type
));
468 * If the write starts beyond the initialized size, extend it up to the
469 * beginning of the write and initialize all non-sparse space between
470 * the old initialized size and the new one. This automatically also
471 * increments the vfs inode->i_size to keep it above or equal to the
474 read_lock_irqsave(&ni
->size_lock
, flags
);
475 ll
= ni
->initialized_size
;
476 read_unlock_irqrestore(&ni
->size_lock
, flags
);
479 * Wait for ongoing direct i/o to complete before proceeding.
480 * New direct i/o cannot start as we hold i_mutex.
483 err
= ntfs_attr_extend_initialized(ni
, pos
);
484 if (unlikely(err
< 0))
485 ntfs_error(vi
->i_sb
, "Cannot perform write to inode "
486 "0x%lx, attribute type 0x%x, because "
487 "extending the initialized size "
488 "failed (error %d).", vi
->i_ino
,
489 (unsigned)le32_to_cpu(ni
->type
),
497 * __ntfs_grab_cache_pages - obtain a number of locked pages
498 * @mapping: address space mapping from which to obtain page cache pages
499 * @index: starting index in @mapping at which to begin obtaining pages
500 * @nr_pages: number of page cache pages to obtain
501 * @pages: array of pages in which to return the obtained page cache pages
502 * @cached_page: allocated but as yet unused page
504 * Obtain @nr_pages locked page cache pages from the mapping @mapping and
505 * starting at index @index.
507 * If a page is newly created, add it to lru list
509 * Note, the page locks are obtained in ascending page index order.
511 static inline int __ntfs_grab_cache_pages(struct address_space
*mapping
,
512 pgoff_t index
, const unsigned nr_pages
, struct page
**pages
,
513 struct page
**cached_page
)
520 pages
[nr
] = find_get_page_flags(mapping
, index
, FGP_LOCK
|
524 *cached_page
= page_cache_alloc(mapping
);
525 if (unlikely(!*cached_page
)) {
530 err
= add_to_page_cache_lru(*cached_page
, mapping
,
537 pages
[nr
] = *cached_page
;
542 } while (nr
< nr_pages
);
547 unlock_page(pages
[--nr
]);
548 page_cache_release(pages
[nr
]);
553 static inline int ntfs_submit_bh_for_read(struct buffer_head
*bh
)
557 bh
->b_end_io
= end_buffer_read_sync
;
558 return submit_bh(READ
, bh
);
562 * ntfs_prepare_pages_for_non_resident_write - prepare pages for receiving data
563 * @pages: array of destination pages
564 * @nr_pages: number of pages in @pages
565 * @pos: byte position in file at which the write begins
566 * @bytes: number of bytes to be written
568 * This is called for non-resident attributes from ntfs_file_buffered_write()
569 * with i_mutex held on the inode (@pages[0]->mapping->host). There are
570 * @nr_pages pages in @pages which are locked but not kmap()ped. The source
571 * data has not yet been copied into the @pages.
573 * Need to fill any holes with actual clusters, allocate buffers if necessary,
574 * ensure all the buffers are mapped, and bring uptodate any buffers that are
575 * only partially being written to.
577 * If @nr_pages is greater than one, we are guaranteed that the cluster size is
578 * greater than PAGE_CACHE_SIZE, that all pages in @pages are entirely inside
579 * the same cluster and that they are the entirety of that cluster, and that
580 * the cluster is sparse, i.e. we need to allocate a cluster to fill the hole.
582 * i_size is not to be modified yet.
584 * Return 0 on success or -errno on error.
586 static int ntfs_prepare_pages_for_non_resident_write(struct page
**pages
,
587 unsigned nr_pages
, s64 pos
, size_t bytes
)
589 VCN vcn
, highest_vcn
= 0, cpos
, cend
, bh_cpos
, bh_cend
;
591 s64 bh_pos
, vcn_len
, end
, initialized_size
;
595 ntfs_inode
*ni
, *base_ni
= NULL
;
597 runlist_element
*rl
, *rl2
;
598 struct buffer_head
*bh
, *head
, *wait
[2], **wait_bh
= wait
;
599 ntfs_attr_search_ctx
*ctx
= NULL
;
600 MFT_RECORD
*m
= NULL
;
601 ATTR_RECORD
*a
= NULL
;
603 u32 attr_rec_len
= 0;
604 unsigned blocksize
, u
;
606 bool rl_write_locked
, was_hole
, is_retry
;
607 unsigned char blocksize_bits
;
610 u8 mft_attr_mapped
:1;
613 } status
= { 0, 0, 0, 0 };
618 vi
= pages
[0]->mapping
->host
;
621 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
622 "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.",
623 vi
->i_ino
, ni
->type
, pages
[0]->index
, nr_pages
,
624 (long long)pos
, bytes
);
625 blocksize
= vol
->sb
->s_blocksize
;
626 blocksize_bits
= vol
->sb
->s_blocksize_bits
;
632 * create_empty_buffers() will create uptodate/dirty buffers if
633 * the page is uptodate/dirty.
635 if (!page_has_buffers(page
)) {
636 create_empty_buffers(page
, blocksize
, 0);
637 if (unlikely(!page_has_buffers(page
)))
640 } while (++u
< nr_pages
);
641 rl_write_locked
= false;
648 cpos
= pos
>> vol
->cluster_size_bits
;
650 cend
= (end
+ vol
->cluster_size
- 1) >> vol
->cluster_size_bits
;
652 * Loop over each page and for each page over each buffer. Use goto to
653 * reduce indentation.
658 bh_pos
= (s64
)page
->index
<< PAGE_CACHE_SHIFT
;
659 bh
= head
= page_buffers(page
);
665 /* Clear buffer_new on all buffers to reinitialise state. */
667 clear_buffer_new(bh
);
668 bh_end
= bh_pos
+ blocksize
;
669 bh_cpos
= bh_pos
>> vol
->cluster_size_bits
;
670 bh_cofs
= bh_pos
& vol
->cluster_size_mask
;
671 if (buffer_mapped(bh
)) {
673 * The buffer is already mapped. If it is uptodate,
676 if (buffer_uptodate(bh
))
679 * The buffer is not uptodate. If the page is uptodate
680 * set the buffer uptodate and otherwise ignore it.
682 if (PageUptodate(page
)) {
683 set_buffer_uptodate(bh
);
687 * Neither the page nor the buffer are uptodate. If
688 * the buffer is only partially being written to, we
689 * need to read it in before the write, i.e. now.
691 if ((bh_pos
< pos
&& bh_end
> pos
) ||
692 (bh_pos
< end
&& bh_end
> end
)) {
694 * If the buffer is fully or partially within
695 * the initialized size, do an actual read.
696 * Otherwise, simply zero the buffer.
698 read_lock_irqsave(&ni
->size_lock
, flags
);
699 initialized_size
= ni
->initialized_size
;
700 read_unlock_irqrestore(&ni
->size_lock
, flags
);
701 if (bh_pos
< initialized_size
) {
702 ntfs_submit_bh_for_read(bh
);
705 zero_user(page
, bh_offset(bh
),
707 set_buffer_uptodate(bh
);
712 /* Unmapped buffer. Need to map it. */
713 bh
->b_bdev
= vol
->sb
->s_bdev
;
715 * If the current buffer is in the same clusters as the map
716 * cache, there is no need to check the runlist again. The
717 * map cache is made up of @vcn, which is the first cached file
718 * cluster, @vcn_len which is the number of cached file
719 * clusters, @lcn is the device cluster corresponding to @vcn,
720 * and @lcn_block is the block number corresponding to @lcn.
722 cdelta
= bh_cpos
- vcn
;
723 if (likely(!cdelta
|| (cdelta
> 0 && cdelta
< vcn_len
))) {
726 bh
->b_blocknr
= lcn_block
+
727 (cdelta
<< (vol
->cluster_size_bits
-
729 (bh_cofs
>> blocksize_bits
);
730 set_buffer_mapped(bh
);
732 * If the page is uptodate so is the buffer. If the
733 * buffer is fully outside the write, we ignore it if
734 * it was already allocated and we mark it dirty so it
735 * gets written out if we allocated it. On the other
736 * hand, if we allocated the buffer but we are not
737 * marking it dirty we set buffer_new so we can do
740 if (PageUptodate(page
)) {
741 if (!buffer_uptodate(bh
))
742 set_buffer_uptodate(bh
);
743 if (unlikely(was_hole
)) {
744 /* We allocated the buffer. */
745 unmap_underlying_metadata(bh
->b_bdev
,
747 if (bh_end
<= pos
|| bh_pos
>= end
)
748 mark_buffer_dirty(bh
);
754 /* Page is _not_ uptodate. */
755 if (likely(!was_hole
)) {
757 * Buffer was already allocated. If it is not
758 * uptodate and is only partially being written
759 * to, we need to read it in before the write,
762 if (!buffer_uptodate(bh
) && bh_pos
< end
&&
767 * If the buffer is fully or partially
768 * within the initialized size, do an
769 * actual read. Otherwise, simply zero
772 read_lock_irqsave(&ni
->size_lock
,
774 initialized_size
= ni
->initialized_size
;
775 read_unlock_irqrestore(&ni
->size_lock
,
777 if (bh_pos
< initialized_size
) {
778 ntfs_submit_bh_for_read(bh
);
781 zero_user(page
, bh_offset(bh
),
783 set_buffer_uptodate(bh
);
788 /* We allocated the buffer. */
789 unmap_underlying_metadata(bh
->b_bdev
, bh
->b_blocknr
);
791 * If the buffer is fully outside the write, zero it,
792 * set it uptodate, and mark it dirty so it gets
793 * written out. If it is partially being written to,
794 * zero region surrounding the write but leave it to
795 * commit write to do anything else. Finally, if the
796 * buffer is fully being overwritten, do nothing.
798 if (bh_end
<= pos
|| bh_pos
>= end
) {
799 if (!buffer_uptodate(bh
)) {
800 zero_user(page
, bh_offset(bh
),
802 set_buffer_uptodate(bh
);
804 mark_buffer_dirty(bh
);
808 if (!buffer_uptodate(bh
) &&
809 (bh_pos
< pos
|| bh_end
> end
)) {
813 kaddr
= kmap_atomic(page
);
815 pofs
= bh_pos
& ~PAGE_CACHE_MASK
;
816 memset(kaddr
+ pofs
, 0, pos
- bh_pos
);
819 pofs
= end
& ~PAGE_CACHE_MASK
;
820 memset(kaddr
+ pofs
, 0, bh_end
- end
);
822 kunmap_atomic(kaddr
);
823 flush_dcache_page(page
);
828 * Slow path: this is the first buffer in the cluster. If it
829 * is outside allocated size and is not uptodate, zero it and
832 read_lock_irqsave(&ni
->size_lock
, flags
);
833 initialized_size
= ni
->allocated_size
;
834 read_unlock_irqrestore(&ni
->size_lock
, flags
);
835 if (bh_pos
> initialized_size
) {
836 if (PageUptodate(page
)) {
837 if (!buffer_uptodate(bh
))
838 set_buffer_uptodate(bh
);
839 } else if (!buffer_uptodate(bh
)) {
840 zero_user(page
, bh_offset(bh
), blocksize
);
841 set_buffer_uptodate(bh
);
847 down_read(&ni
->runlist
.lock
);
851 if (likely(rl
!= NULL
)) {
852 /* Seek to element containing target cluster. */
853 while (rl
->length
&& rl
[1].vcn
<= bh_cpos
)
855 lcn
= ntfs_rl_vcn_to_lcn(rl
, bh_cpos
);
856 if (likely(lcn
>= 0)) {
858 * Successful remap, setup the map cache and
859 * use that to deal with the buffer.
863 vcn_len
= rl
[1].vcn
- vcn
;
864 lcn_block
= lcn
<< (vol
->cluster_size_bits
-
868 * If the number of remaining clusters touched
869 * by the write is smaller or equal to the
870 * number of cached clusters, unlock the
871 * runlist as the map cache will be used from
874 if (likely(vcn
+ vcn_len
>= cend
)) {
875 if (rl_write_locked
) {
876 up_write(&ni
->runlist
.lock
);
877 rl_write_locked
= false;
879 up_read(&ni
->runlist
.lock
);
882 goto map_buffer_cached
;
885 lcn
= LCN_RL_NOT_MAPPED
;
887 * If it is not a hole and not out of bounds, the runlist is
888 * probably unmapped so try to map it now.
890 if (unlikely(lcn
!= LCN_HOLE
&& lcn
!= LCN_ENOENT
)) {
891 if (likely(!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
)) {
892 /* Attempt to map runlist. */
893 if (!rl_write_locked
) {
895 * We need the runlist locked for
896 * writing, so if it is locked for
897 * reading relock it now and retry in
898 * case it changed whilst we dropped
901 up_read(&ni
->runlist
.lock
);
902 down_write(&ni
->runlist
.lock
);
903 rl_write_locked
= true;
906 err
= ntfs_map_runlist_nolock(ni
, bh_cpos
,
913 * If @vcn is out of bounds, pretend @lcn is
914 * LCN_ENOENT. As long as the buffer is out
915 * of bounds this will work fine.
917 if (err
== -ENOENT
) {
920 goto rl_not_mapped_enoent
;
924 /* Failed to map the buffer, even after retrying. */
926 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
927 "attribute type 0x%x, vcn 0x%llx, "
928 "vcn offset 0x%x, because its "
929 "location on disk could not be "
930 "determined%s (error code %i).",
931 ni
->mft_no
, ni
->type
,
932 (unsigned long long)bh_cpos
,
934 vol
->cluster_size_mask
,
935 is_retry
? " even after retrying" : "",
939 rl_not_mapped_enoent
:
941 * The buffer is in a hole or out of bounds. We need to fill
942 * the hole, unless the buffer is in a cluster which is not
943 * touched by the write, in which case we just leave the buffer
944 * unmapped. This can only happen when the cluster size is
945 * less than the page cache size.
947 if (unlikely(vol
->cluster_size
< PAGE_CACHE_SIZE
)) {
948 bh_cend
= (bh_end
+ vol
->cluster_size
- 1) >>
949 vol
->cluster_size_bits
;
950 if ((bh_cend
<= cpos
|| bh_cpos
>= cend
)) {
953 * If the buffer is uptodate we skip it. If it
954 * is not but the page is uptodate, we can set
955 * the buffer uptodate. If the page is not
956 * uptodate, we can clear the buffer and set it
957 * uptodate. Whether this is worthwhile is
958 * debatable and this could be removed.
960 if (PageUptodate(page
)) {
961 if (!buffer_uptodate(bh
))
962 set_buffer_uptodate(bh
);
963 } else if (!buffer_uptodate(bh
)) {
964 zero_user(page
, bh_offset(bh
),
966 set_buffer_uptodate(bh
);
972 * Out of bounds buffer is invalid if it was not really out of
975 BUG_ON(lcn
!= LCN_HOLE
);
977 * We need the runlist locked for writing, so if it is locked
978 * for reading relock it now and retry in case it changed
979 * whilst we dropped the lock.
982 if (!rl_write_locked
) {
983 up_read(&ni
->runlist
.lock
);
984 down_write(&ni
->runlist
.lock
);
985 rl_write_locked
= true;
988 /* Find the previous last allocated cluster. */
989 BUG_ON(rl
->lcn
!= LCN_HOLE
);
992 while (--rl2
>= ni
->runlist
.rl
) {
994 lcn
= rl2
->lcn
+ rl2
->length
;
998 rl2
= ntfs_cluster_alloc(vol
, bh_cpos
, 1, lcn
, DATA_ZONE
,
1002 ntfs_debug("Failed to allocate cluster, error code %i.",
1007 rl
= ntfs_runlists_merge(ni
->runlist
.rl
, rl2
);
1012 if (ntfs_cluster_free_from_rl(vol
, rl2
)) {
1013 ntfs_error(vol
->sb
, "Failed to release "
1014 "allocated cluster in error "
1015 "code path. Run chkdsk to "
1016 "recover the lost cluster.");
1022 ni
->runlist
.rl
= rl
;
1023 status
.runlist_merged
= 1;
1024 ntfs_debug("Allocated cluster, lcn 0x%llx.",
1025 (unsigned long long)lcn
);
1026 /* Map and lock the mft record and get the attribute record. */
1030 base_ni
= ni
->ext
.base_ntfs_ino
;
1031 m
= map_mft_record(base_ni
);
1036 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1037 if (unlikely(!ctx
)) {
1039 unmap_mft_record(base_ni
);
1042 status
.mft_attr_mapped
= 1;
1043 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1044 CASE_SENSITIVE
, bh_cpos
, NULL
, 0, ctx
);
1045 if (unlikely(err
)) {
1053 * Find the runlist element with which the attribute extent
1054 * starts. Note, we cannot use the _attr_ version because we
1055 * have mapped the mft record. That is ok because we know the
1056 * runlist fragment must be mapped already to have ever gotten
1057 * here, so we can just use the _rl_ version.
1059 vcn
= sle64_to_cpu(a
->data
.non_resident
.lowest_vcn
);
1060 rl2
= ntfs_rl_find_vcn_nolock(rl
, vcn
);
1062 BUG_ON(!rl2
->length
);
1063 BUG_ON(rl2
->lcn
< LCN_HOLE
);
1064 highest_vcn
= sle64_to_cpu(a
->data
.non_resident
.highest_vcn
);
1066 * If @highest_vcn is zero, calculate the real highest_vcn
1067 * (which can really be zero).
1070 highest_vcn
= (sle64_to_cpu(
1071 a
->data
.non_resident
.allocated_size
) >>
1072 vol
->cluster_size_bits
) - 1;
1074 * Determine the size of the mapping pairs array for the new
1075 * extent, i.e. the old extent with the hole filled.
1077 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, vcn
,
1079 if (unlikely(mp_size
<= 0)) {
1080 if (!(err
= mp_size
))
1082 ntfs_debug("Failed to get size for mapping pairs "
1083 "array, error code %i.", err
);
1087 * Resize the attribute record to fit the new mapping pairs
1090 attr_rec_len
= le32_to_cpu(a
->length
);
1091 err
= ntfs_attr_record_resize(m
, a
, mp_size
+ le16_to_cpu(
1092 a
->data
.non_resident
.mapping_pairs_offset
));
1093 if (unlikely(err
)) {
1094 BUG_ON(err
!= -ENOSPC
);
1095 // TODO: Deal with this by using the current attribute
1096 // and fill it with as much of the mapping pairs
1097 // array as possible. Then loop over each attribute
1098 // extent rewriting the mapping pairs arrays as we go
1099 // along and if when we reach the end we have not
1100 // enough space, try to resize the last attribute
1101 // extent and if even that fails, add a new attribute
1103 // We could also try to resize at each step in the hope
1104 // that we will not need to rewrite every single extent.
1105 // Note, we may need to decompress some extents to fill
1106 // the runlist as we are walking the extents...
1107 ntfs_error(vol
->sb
, "Not enough space in the mft "
1108 "record for the extended attribute "
1109 "record. This case is not "
1110 "implemented yet.");
1114 status
.mp_rebuilt
= 1;
1116 * Generate the mapping pairs array directly into the attribute
1119 err
= ntfs_mapping_pairs_build(vol
, (u8
*)a
+ le16_to_cpu(
1120 a
->data
.non_resident
.mapping_pairs_offset
),
1121 mp_size
, rl2
, vcn
, highest_vcn
, NULL
);
1122 if (unlikely(err
)) {
1123 ntfs_error(vol
->sb
, "Cannot fill hole in inode 0x%lx, "
1124 "attribute type 0x%x, because building "
1125 "the mapping pairs failed with error "
1126 "code %i.", vi
->i_ino
,
1127 (unsigned)le32_to_cpu(ni
->type
), err
);
1131 /* Update the highest_vcn but only if it was not set. */
1132 if (unlikely(!a
->data
.non_resident
.highest_vcn
))
1133 a
->data
.non_resident
.highest_vcn
=
1134 cpu_to_sle64(highest_vcn
);
1136 * If the attribute is sparse/compressed, update the compressed
1137 * size in the ntfs_inode structure and the attribute record.
1139 if (likely(NInoSparse(ni
) || NInoCompressed(ni
))) {
1141 * If we are not in the first attribute extent, switch
1142 * to it, but first ensure the changes will make it to
1145 if (a
->data
.non_resident
.lowest_vcn
) {
1146 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1147 mark_mft_record_dirty(ctx
->ntfs_ino
);
1148 ntfs_attr_reinit_search_ctx(ctx
);
1149 err
= ntfs_attr_lookup(ni
->type
, ni
->name
,
1150 ni
->name_len
, CASE_SENSITIVE
,
1152 if (unlikely(err
)) {
1153 status
.attr_switched
= 1;
1156 /* @m is not used any more so do not set it. */
1159 write_lock_irqsave(&ni
->size_lock
, flags
);
1160 ni
->itype
.compressed
.size
+= vol
->cluster_size
;
1161 a
->data
.non_resident
.compressed_size
=
1162 cpu_to_sle64(ni
->itype
.compressed
.size
);
1163 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1165 /* Ensure the changes make it to disk. */
1166 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1167 mark_mft_record_dirty(ctx
->ntfs_ino
);
1168 ntfs_attr_put_search_ctx(ctx
);
1169 unmap_mft_record(base_ni
);
1170 /* Successfully filled the hole. */
1171 status
.runlist_merged
= 0;
1172 status
.mft_attr_mapped
= 0;
1173 status
.mp_rebuilt
= 0;
1174 /* Setup the map cache and use that to deal with the buffer. */
1178 lcn_block
= lcn
<< (vol
->cluster_size_bits
- blocksize_bits
);
1181 * If the number of remaining clusters in the @pages is smaller
1182 * or equal to the number of cached clusters, unlock the
1183 * runlist as the map cache will be used from now on.
1185 if (likely(vcn
+ vcn_len
>= cend
)) {
1186 up_write(&ni
->runlist
.lock
);
1187 rl_write_locked
= false;
1190 goto map_buffer_cached
;
1191 } while (bh_pos
+= blocksize
, (bh
= bh
->b_this_page
) != head
);
1192 /* If there are no errors, do the next page. */
1193 if (likely(!err
&& ++u
< nr_pages
))
1195 /* If there are no errors, release the runlist lock if we took it. */
1197 if (unlikely(rl_write_locked
)) {
1198 up_write(&ni
->runlist
.lock
);
1199 rl_write_locked
= false;
1200 } else if (unlikely(rl
))
1201 up_read(&ni
->runlist
.lock
);
1204 /* If we issued read requests, let them complete. */
1205 read_lock_irqsave(&ni
->size_lock
, flags
);
1206 initialized_size
= ni
->initialized_size
;
1207 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1208 while (wait_bh
> wait
) {
1211 if (likely(buffer_uptodate(bh
))) {
1213 bh_pos
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1216 * If the buffer overflows the initialized size, need
1217 * to zero the overflowing region.
1219 if (unlikely(bh_pos
+ blocksize
> initialized_size
)) {
1222 if (likely(bh_pos
< initialized_size
))
1223 ofs
= initialized_size
- bh_pos
;
1224 zero_user_segment(page
, bh_offset(bh
) + ofs
,
1227 } else /* if (unlikely(!buffer_uptodate(bh))) */
1231 /* Clear buffer_new on all buffers. */
1234 bh
= head
= page_buffers(pages
[u
]);
1237 clear_buffer_new(bh
);
1238 } while ((bh
= bh
->b_this_page
) != head
);
1239 } while (++u
< nr_pages
);
1240 ntfs_debug("Done.");
1243 if (status
.attr_switched
) {
1244 /* Get back to the attribute extent we modified. */
1245 ntfs_attr_reinit_search_ctx(ctx
);
1246 if (ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1247 CASE_SENSITIVE
, bh_cpos
, NULL
, 0, ctx
)) {
1248 ntfs_error(vol
->sb
, "Failed to find required "
1249 "attribute extent of attribute in "
1250 "error code path. Run chkdsk to "
1252 write_lock_irqsave(&ni
->size_lock
, flags
);
1253 ni
->itype
.compressed
.size
+= vol
->cluster_size
;
1254 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1255 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1256 mark_mft_record_dirty(ctx
->ntfs_ino
);
1258 * The only thing that is now wrong is the compressed
1259 * size of the base attribute extent which chkdsk
1260 * should be able to fix.
1266 status
.attr_switched
= 0;
1270 * If the runlist has been modified, need to restore it by punching a
1271 * hole into it and we then need to deallocate the on-disk cluster as
1272 * well. Note, we only modify the runlist if we are able to generate a
1273 * new mapping pairs array, i.e. only when the mapped attribute extent
1276 if (status
.runlist_merged
&& !status
.attr_switched
) {
1277 BUG_ON(!rl_write_locked
);
1278 /* Make the file cluster we allocated sparse in the runlist. */
1279 if (ntfs_rl_punch_nolock(vol
, &ni
->runlist
, bh_cpos
, 1)) {
1280 ntfs_error(vol
->sb
, "Failed to punch hole into "
1281 "attribute runlist in error code "
1282 "path. Run chkdsk to recover the "
1285 } else /* if (success) */ {
1286 status
.runlist_merged
= 0;
1288 * Deallocate the on-disk cluster we allocated but only
1289 * if we succeeded in punching its vcn out of the
1292 down_write(&vol
->lcnbmp_lock
);
1293 if (ntfs_bitmap_clear_bit(vol
->lcnbmp_ino
, lcn
)) {
1294 ntfs_error(vol
->sb
, "Failed to release "
1295 "allocated cluster in error "
1296 "code path. Run chkdsk to "
1297 "recover the lost cluster.");
1300 up_write(&vol
->lcnbmp_lock
);
1304 * Resize the attribute record to its old size and rebuild the mapping
1305 * pairs array. Note, we only can do this if the runlist has been
1306 * restored to its old state which also implies that the mapped
1307 * attribute extent is not switched.
1309 if (status
.mp_rebuilt
&& !status
.runlist_merged
) {
1310 if (ntfs_attr_record_resize(m
, a
, attr_rec_len
)) {
1311 ntfs_error(vol
->sb
, "Failed to restore attribute "
1312 "record in error code path. Run "
1313 "chkdsk to recover.");
1315 } else /* if (success) */ {
1316 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+
1317 le16_to_cpu(a
->data
.non_resident
.
1318 mapping_pairs_offset
), attr_rec_len
-
1319 le16_to_cpu(a
->data
.non_resident
.
1320 mapping_pairs_offset
), ni
->runlist
.rl
,
1321 vcn
, highest_vcn
, NULL
)) {
1322 ntfs_error(vol
->sb
, "Failed to restore "
1323 "mapping pairs array in error "
1324 "code path. Run chkdsk to "
1328 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1329 mark_mft_record_dirty(ctx
->ntfs_ino
);
1332 /* Release the mft record and the attribute. */
1333 if (status
.mft_attr_mapped
) {
1334 ntfs_attr_put_search_ctx(ctx
);
1335 unmap_mft_record(base_ni
);
1337 /* Release the runlist lock. */
1338 if (rl_write_locked
)
1339 up_write(&ni
->runlist
.lock
);
1341 up_read(&ni
->runlist
.lock
);
1343 * Zero out any newly allocated blocks to avoid exposing stale data.
1344 * If BH_New is set, we know that the block was newly allocated above
1345 * and that it has not been fully zeroed and marked dirty yet.
1349 end
= bh_cpos
<< vol
->cluster_size_bits
;
1352 bh
= head
= page_buffers(page
);
1354 if (u
== nr_pages
&&
1355 ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1356 bh_offset(bh
) >= end
)
1358 if (!buffer_new(bh
))
1360 clear_buffer_new(bh
);
1361 if (!buffer_uptodate(bh
)) {
1362 if (PageUptodate(page
))
1363 set_buffer_uptodate(bh
);
1365 zero_user(page
, bh_offset(bh
),
1367 set_buffer_uptodate(bh
);
1370 mark_buffer_dirty(bh
);
1371 } while ((bh
= bh
->b_this_page
) != head
);
1372 } while (++u
<= nr_pages
);
1373 ntfs_error(vol
->sb
, "Failed. Returning error code %i.", err
);
1377 static inline void ntfs_flush_dcache_pages(struct page
**pages
,
1382 * Warning: Do not do the decrement at the same time as the call to
1383 * flush_dcache_page() because it is a NULL macro on i386 and hence the
1384 * decrement never happens so the loop never terminates.
1388 flush_dcache_page(pages
[nr_pages
]);
1389 } while (nr_pages
> 0);
1393 * ntfs_commit_pages_after_non_resident_write - commit the received data
1394 * @pages: array of destination pages
1395 * @nr_pages: number of pages in @pages
1396 * @pos: byte position in file at which the write begins
1397 * @bytes: number of bytes to be written
1399 * See description of ntfs_commit_pages_after_write(), below.
1401 static inline int ntfs_commit_pages_after_non_resident_write(
1402 struct page
**pages
, const unsigned nr_pages
,
1403 s64 pos
, size_t bytes
)
1405 s64 end
, initialized_size
;
1407 ntfs_inode
*ni
, *base_ni
;
1408 struct buffer_head
*bh
, *head
;
1409 ntfs_attr_search_ctx
*ctx
;
1412 unsigned long flags
;
1413 unsigned blocksize
, u
;
1416 vi
= pages
[0]->mapping
->host
;
1418 blocksize
= vi
->i_sb
->s_blocksize
;
1427 bh_pos
= (s64
)page
->index
<< PAGE_CACHE_SHIFT
;
1428 bh
= head
= page_buffers(page
);
1433 bh_end
= bh_pos
+ blocksize
;
1434 if (bh_end
<= pos
|| bh_pos
>= end
) {
1435 if (!buffer_uptodate(bh
))
1438 set_buffer_uptodate(bh
);
1439 mark_buffer_dirty(bh
);
1441 } while (bh_pos
+= blocksize
, (bh
= bh
->b_this_page
) != head
);
1443 * If all buffers are now uptodate but the page is not, set the
1446 if (!partial
&& !PageUptodate(page
))
1447 SetPageUptodate(page
);
1448 } while (++u
< nr_pages
);
1450 * Finally, if we do not need to update initialized_size or i_size we
1453 read_lock_irqsave(&ni
->size_lock
, flags
);
1454 initialized_size
= ni
->initialized_size
;
1455 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1456 if (end
<= initialized_size
) {
1457 ntfs_debug("Done.");
1461 * Update initialized_size/i_size as appropriate, both in the inode and
1467 base_ni
= ni
->ext
.base_ntfs_ino
;
1468 /* Map, pin, and lock the mft record. */
1469 m
= map_mft_record(base_ni
);
1476 BUG_ON(!NInoNonResident(ni
));
1477 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1478 if (unlikely(!ctx
)) {
1482 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1483 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1484 if (unlikely(err
)) {
1490 BUG_ON(!a
->non_resident
);
1491 write_lock_irqsave(&ni
->size_lock
, flags
);
1492 BUG_ON(end
> ni
->allocated_size
);
1493 ni
->initialized_size
= end
;
1494 a
->data
.non_resident
.initialized_size
= cpu_to_sle64(end
);
1495 if (end
> i_size_read(vi
)) {
1496 i_size_write(vi
, end
);
1497 a
->data
.non_resident
.data_size
=
1498 a
->data
.non_resident
.initialized_size
;
1500 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1501 /* Mark the mft record dirty, so it gets written back. */
1502 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1503 mark_mft_record_dirty(ctx
->ntfs_ino
);
1504 ntfs_attr_put_search_ctx(ctx
);
1505 unmap_mft_record(base_ni
);
1506 ntfs_debug("Done.");
1510 ntfs_attr_put_search_ctx(ctx
);
1512 unmap_mft_record(base_ni
);
1513 ntfs_error(vi
->i_sb
, "Failed to update initialized_size/i_size (error "
1516 NVolSetErrors(ni
->vol
);
1521 * ntfs_commit_pages_after_write - commit the received data
1522 * @pages: array of destination pages
1523 * @nr_pages: number of pages in @pages
1524 * @pos: byte position in file at which the write begins
1525 * @bytes: number of bytes to be written
1527 * This is called from ntfs_file_buffered_write() with i_mutex held on the inode
1528 * (@pages[0]->mapping->host). There are @nr_pages pages in @pages which are
1529 * locked but not kmap()ped. The source data has already been copied into the
1530 * @page. ntfs_prepare_pages_for_non_resident_write() has been called before
1531 * the data was copied (for non-resident attributes only) and it returned
1534 * Need to set uptodate and mark dirty all buffers within the boundary of the
1535 * write. If all buffers in a page are uptodate we set the page uptodate, too.
1537 * Setting the buffers dirty ensures that they get written out later when
1538 * ntfs_writepage() is invoked by the VM.
1540 * Finally, we need to update i_size and initialized_size as appropriate both
1541 * in the inode and the mft record.
1543 * This is modelled after fs/buffer.c::generic_commit_write(), which marks
1544 * buffers uptodate and dirty, sets the page uptodate if all buffers in the
1545 * page are uptodate, and updates i_size if the end of io is beyond i_size. In
1546 * that case, it also marks the inode dirty.
1548 * If things have gone as outlined in
1549 * ntfs_prepare_pages_for_non_resident_write(), we do not need to do any page
1550 * content modifications here for non-resident attributes. For resident
1551 * attributes we need to do the uptodate bringing here which we combine with
1552 * the copying into the mft record which means we save one atomic kmap.
1554 * Return 0 on success or -errno on error.
1556 static int ntfs_commit_pages_after_write(struct page
**pages
,
1557 const unsigned nr_pages
, s64 pos
, size_t bytes
)
1559 s64 end
, initialized_size
;
1562 ntfs_inode
*ni
, *base_ni
;
1564 ntfs_attr_search_ctx
*ctx
;
1567 char *kattr
, *kaddr
;
1568 unsigned long flags
;
1576 vi
= page
->mapping
->host
;
1578 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
1579 "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.",
1580 vi
->i_ino
, ni
->type
, page
->index
, nr_pages
,
1581 (long long)pos
, bytes
);
1582 if (NInoNonResident(ni
))
1583 return ntfs_commit_pages_after_non_resident_write(pages
,
1584 nr_pages
, pos
, bytes
);
1585 BUG_ON(nr_pages
> 1);
1587 * Attribute is resident, implying it is not compressed, encrypted, or
1593 base_ni
= ni
->ext
.base_ntfs_ino
;
1594 BUG_ON(NInoNonResident(ni
));
1595 /* Map, pin, and lock the mft record. */
1596 m
= map_mft_record(base_ni
);
1603 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1604 if (unlikely(!ctx
)) {
1608 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1609 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1610 if (unlikely(err
)) {
1616 BUG_ON(a
->non_resident
);
1617 /* The total length of the attribute value. */
1618 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
1619 i_size
= i_size_read(vi
);
1620 BUG_ON(attr_len
!= i_size
);
1621 BUG_ON(pos
> attr_len
);
1623 BUG_ON(end
> le32_to_cpu(a
->length
) -
1624 le16_to_cpu(a
->data
.resident
.value_offset
));
1625 kattr
= (u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
);
1626 kaddr
= kmap_atomic(page
);
1627 /* Copy the received data from the page to the mft record. */
1628 memcpy(kattr
+ pos
, kaddr
+ pos
, bytes
);
1629 /* Update the attribute length if necessary. */
1630 if (end
> attr_len
) {
1632 a
->data
.resident
.value_length
= cpu_to_le32(attr_len
);
1635 * If the page is not uptodate, bring the out of bounds area(s)
1636 * uptodate by copying data from the mft record to the page.
1638 if (!PageUptodate(page
)) {
1640 memcpy(kaddr
, kattr
, pos
);
1642 memcpy(kaddr
+ end
, kattr
+ end
, attr_len
- end
);
1643 /* Zero the region outside the end of the attribute value. */
1644 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
1645 flush_dcache_page(page
);
1646 SetPageUptodate(page
);
1648 kunmap_atomic(kaddr
);
1649 /* Update initialized_size/i_size if necessary. */
1650 read_lock_irqsave(&ni
->size_lock
, flags
);
1651 initialized_size
= ni
->initialized_size
;
1652 BUG_ON(end
> ni
->allocated_size
);
1653 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1654 BUG_ON(initialized_size
!= i_size
);
1655 if (end
> initialized_size
) {
1656 write_lock_irqsave(&ni
->size_lock
, flags
);
1657 ni
->initialized_size
= end
;
1658 i_size_write(vi
, end
);
1659 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1661 /* Mark the mft record dirty, so it gets written back. */
1662 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1663 mark_mft_record_dirty(ctx
->ntfs_ino
);
1664 ntfs_attr_put_search_ctx(ctx
);
1665 unmap_mft_record(base_ni
);
1666 ntfs_debug("Done.");
1669 if (err
== -ENOMEM
) {
1670 ntfs_warning(vi
->i_sb
, "Error allocating memory required to "
1671 "commit the write.");
1672 if (PageUptodate(page
)) {
1673 ntfs_warning(vi
->i_sb
, "Page is uptodate, setting "
1674 "dirty so the write will be retried "
1675 "later on by the VM.");
1677 * Put the page on mapping->dirty_pages, but leave its
1678 * buffers' dirty state as-is.
1680 __set_page_dirty_nobuffers(page
);
1683 ntfs_error(vi
->i_sb
, "Page is not uptodate. Written "
1684 "data has been lost.");
1686 ntfs_error(vi
->i_sb
, "Resident attribute commit write failed "
1687 "with error %i.", err
);
1688 NVolSetErrors(ni
->vol
);
1691 ntfs_attr_put_search_ctx(ctx
);
1693 unmap_mft_record(base_ni
);
1698 * Copy as much as we can into the pages and return the number of bytes which
1699 * were successfully copied. If a fault is encountered then clear the pages
1700 * out to (ofs + bytes) and return the number of bytes which were copied.
1702 static size_t ntfs_copy_from_user_iter(struct page
**pages
, unsigned nr_pages
,
1703 unsigned ofs
, struct iov_iter
*i
, size_t bytes
)
1705 struct page
**last_page
= pages
+ nr_pages
;
1707 struct iov_iter data
= *i
;
1708 unsigned len
, copied
;
1711 len
= PAGE_CACHE_SIZE
- ofs
;
1714 copied
= iov_iter_copy_from_user_atomic(*pages
, &data
, ofs
,
1720 iov_iter_advance(&data
, copied
);
1724 } while (++pages
< last_page
);
1728 /* Zero the rest of the target like __copy_from_user(). */
1729 len
= PAGE_CACHE_SIZE
- copied
;
1733 zero_user(*pages
, copied
, len
);
1736 len
= PAGE_CACHE_SIZE
;
1737 } while (++pages
< last_page
);
1742 * ntfs_perform_write - perform buffered write to a file
1743 * @file: file to write to
1744 * @i: iov_iter with data to write
1745 * @pos: byte offset in file at which to begin writing to
1747 static ssize_t
ntfs_perform_write(struct file
*file
, struct iov_iter
*i
,
1750 struct address_space
*mapping
= file
->f_mapping
;
1751 struct inode
*vi
= mapping
->host
;
1752 ntfs_inode
*ni
= NTFS_I(vi
);
1753 ntfs_volume
*vol
= ni
->vol
;
1754 struct page
*pages
[NTFS_MAX_PAGES_PER_CLUSTER
];
1755 struct page
*cached_page
= NULL
;
1759 ssize_t status
, written
= 0;
1762 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, pos "
1763 "0x%llx, count 0x%lx.", vi
->i_ino
,
1764 (unsigned)le32_to_cpu(ni
->type
),
1765 (unsigned long long)pos
,
1766 (unsigned long)iov_iter_count(i
));
1768 * If a previous ntfs_truncate() failed, repeat it and abort if it
1771 if (unlikely(NInoTruncateFailed(ni
))) {
1775 err
= ntfs_truncate(vi
);
1776 if (err
|| NInoTruncateFailed(ni
)) {
1779 ntfs_error(vol
->sb
, "Cannot perform write to inode "
1780 "0x%lx, attribute type 0x%x, because "
1781 "ntfs_truncate() failed (error code "
1783 (unsigned)le32_to_cpu(ni
->type
), err
);
1788 * Determine the number of pages per cluster for non-resident
1792 if (vol
->cluster_size
> PAGE_CACHE_SIZE
&& NInoNonResident(ni
))
1793 nr_pages
= vol
->cluster_size
>> PAGE_CACHE_SHIFT
;
1797 pgoff_t idx
, start_idx
;
1798 unsigned ofs
, do_pages
, u
;
1801 start_idx
= idx
= pos
>> PAGE_CACHE_SHIFT
;
1802 ofs
= pos
& ~PAGE_CACHE_MASK
;
1803 bytes
= PAGE_CACHE_SIZE
- ofs
;
1806 vcn
= pos
>> vol
->cluster_size_bits
;
1807 if (vcn
!= last_vcn
) {
1810 * Get the lcn of the vcn the write is in. If
1811 * it is a hole, need to lock down all pages in
1814 down_read(&ni
->runlist
.lock
);
1815 lcn
= ntfs_attr_vcn_to_lcn_nolock(ni
, pos
>>
1816 vol
->cluster_size_bits
, false);
1817 up_read(&ni
->runlist
.lock
);
1818 if (unlikely(lcn
< LCN_HOLE
)) {
1819 if (lcn
== LCN_ENOMEM
)
1823 ntfs_error(vol
->sb
, "Cannot "
1826 "attribute type 0x%x, "
1827 "because the attribute "
1829 vi
->i_ino
, (unsigned)
1830 le32_to_cpu(ni
->type
));
1834 if (lcn
== LCN_HOLE
) {
1835 start_idx
= (pos
& ~(s64
)
1836 vol
->cluster_size_mask
)
1837 >> PAGE_CACHE_SHIFT
;
1838 bytes
= vol
->cluster_size
- (pos
&
1839 vol
->cluster_size_mask
);
1840 do_pages
= nr_pages
;
1844 if (bytes
> iov_iter_count(i
))
1845 bytes
= iov_iter_count(i
);
1848 * Bring in the user page(s) that we will copy from _first_.
1849 * Otherwise there is a nasty deadlock on copying from the same
1850 * page(s) as we are writing to, without it/them being marked
1851 * up-to-date. Note, at present there is nothing to stop the
1852 * pages being swapped out between us bringing them into memory
1853 * and doing the actual copying.
1855 if (unlikely(iov_iter_fault_in_multipages_readable(i
, bytes
))) {
1859 /* Get and lock @do_pages starting at index @start_idx. */
1860 status
= __ntfs_grab_cache_pages(mapping
, start_idx
, do_pages
,
1861 pages
, &cached_page
);
1862 if (unlikely(status
))
1865 * For non-resident attributes, we need to fill any holes with
1866 * actual clusters and ensure all bufferes are mapped. We also
1867 * need to bring uptodate any buffers that are only partially
1870 if (NInoNonResident(ni
)) {
1871 status
= ntfs_prepare_pages_for_non_resident_write(
1872 pages
, do_pages
, pos
, bytes
);
1873 if (unlikely(status
)) {
1875 unlock_page(pages
[--do_pages
]);
1876 page_cache_release(pages
[do_pages
]);
1881 u
= (pos
>> PAGE_CACHE_SHIFT
) - pages
[0]->index
;
1882 copied
= ntfs_copy_from_user_iter(pages
+ u
, do_pages
- u
, ofs
,
1884 ntfs_flush_dcache_pages(pages
+ u
, do_pages
- u
);
1886 if (likely(copied
== bytes
)) {
1887 status
= ntfs_commit_pages_after_write(pages
, do_pages
,
1893 unlock_page(pages
[--do_pages
]);
1894 page_cache_release(pages
[do_pages
]);
1896 if (unlikely(status
< 0))
1900 if (unlikely(!copied
)) {
1904 * We failed to copy anything. Fall back to single
1905 * segment length write.
1907 * This is needed to avoid possible livelock in the
1908 * case that all segments in the iov cannot be copied
1909 * at once without a pagefault.
1911 sc
= iov_iter_single_seg_count(i
);
1916 iov_iter_advance(i
, copied
);
1919 balance_dirty_pages_ratelimited(mapping
);
1920 if (fatal_signal_pending(current
)) {
1924 } while (iov_iter_count(i
));
1926 page_cache_release(cached_page
);
1927 ntfs_debug("Done. Returning %s (written 0x%lx, status %li).",
1928 written
? "written" : "status", (unsigned long)written
,
1930 return written
? written
: status
;
1934 * ntfs_file_write_iter_nolock - write data to a file
1935 * @iocb: IO state structure (file, offset, etc.)
1936 * @from: iov_iter with data to write
1938 * Basically the same as __generic_file_write_iter() except that it ends
1939 * up calling ntfs_perform_write() instead of generic_perform_write() and that
1940 * O_DIRECT is not implemented.
1942 static ssize_t
ntfs_file_write_iter_nolock(struct kiocb
*iocb
,
1943 struct iov_iter
*from
)
1945 struct file
*file
= iocb
->ki_filp
;
1946 loff_t pos
= iocb
->ki_pos
;
1947 ssize_t written
= 0;
1949 size_t count
= iov_iter_count(from
);
1951 err
= ntfs_prepare_file_for_write(file
, &pos
, &count
);
1952 if (count
&& !err
) {
1953 iov_iter_truncate(from
, count
);
1954 written
= ntfs_perform_write(file
, from
, pos
);
1955 if (likely(written
>= 0))
1956 iocb
->ki_pos
= pos
+ written
;
1958 current
->backing_dev_info
= NULL
;
1959 return written
? written
: err
;
1963 * ntfs_file_write_iter - simple wrapper for ntfs_file_write_iter_nolock()
1964 * @iocb: IO state structure
1965 * @from: iov_iter with data to write
1967 * Basically the same as generic_file_write_iter() except that it ends up
1968 * calling ntfs_file_write_iter_nolock() instead of
1969 * __generic_file_write_iter().
1971 static ssize_t
ntfs_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1973 struct file
*file
= iocb
->ki_filp
;
1974 struct inode
*vi
= file_inode(file
);
1977 mutex_lock(&vi
->i_mutex
);
1978 ret
= ntfs_file_write_iter_nolock(iocb
, from
);
1979 mutex_unlock(&vi
->i_mutex
);
1983 err
= generic_write_sync(file
, iocb
->ki_pos
- ret
, ret
);
1991 * ntfs_file_fsync - sync a file to disk
1992 * @filp: file to be synced
1993 * @datasync: if non-zero only flush user data and not metadata
1995 * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
1996 * system calls. This function is inspired by fs/buffer.c::file_fsync().
1998 * If @datasync is false, write the mft record and all associated extent mft
1999 * records as well as the $DATA attribute and then sync the block device.
2001 * If @datasync is true and the attribute is non-resident, we skip the writing
2002 * of the mft record and all associated extent mft records (this might still
2003 * happen due to the write_inode_now() call).
2005 * Also, if @datasync is true, we do not wait on the inode to be written out
2006 * but we always wait on the page cache pages to be written out.
2008 * Locking: Caller must hold i_mutex on the inode.
2010 * TODO: We should probably also write all attribute/index inodes associated
2011 * with this inode but since we have no simple way of getting to them we ignore
2012 * this problem for now.
2014 static int ntfs_file_fsync(struct file
*filp
, loff_t start
, loff_t end
,
2017 struct inode
*vi
= filp
->f_mapping
->host
;
2020 ntfs_debug("Entering for inode 0x%lx.", vi
->i_ino
);
2022 err
= filemap_write_and_wait_range(vi
->i_mapping
, start
, end
);
2025 mutex_lock(&vi
->i_mutex
);
2027 BUG_ON(S_ISDIR(vi
->i_mode
));
2028 if (!datasync
|| !NInoNonResident(NTFS_I(vi
)))
2029 ret
= __ntfs_write_inode(vi
, 1);
2030 write_inode_now(vi
, !datasync
);
2032 * NOTE: If we were to use mapping->private_list (see ext2 and
2033 * fs/buffer.c) for dirty blocks then we could optimize the below to be
2034 * sync_mapping_buffers(vi->i_mapping).
2036 err
= sync_blockdev(vi
->i_sb
->s_bdev
);
2037 if (unlikely(err
&& !ret
))
2040 ntfs_debug("Done.");
2042 ntfs_warning(vi
->i_sb
, "Failed to f%ssync inode 0x%lx. Error "
2043 "%u.", datasync
? "data" : "", vi
->i_ino
, -ret
);
2044 mutex_unlock(&vi
->i_mutex
);
2048 #endif /* NTFS_RW */
2050 const struct file_operations ntfs_file_ops
= {
2051 .llseek
= generic_file_llseek
,
2052 .read
= new_sync_read
,
2053 .read_iter
= generic_file_read_iter
,
2055 .write
= new_sync_write
,
2056 .write_iter
= ntfs_file_write_iter
,
2057 .fsync
= ntfs_file_fsync
,
2058 #endif /* NTFS_RW */
2059 .mmap
= generic_file_mmap
,
2060 .open
= ntfs_file_open
,
2061 .splice_read
= generic_file_splice_read
,
2064 const struct inode_operations ntfs_file_inode_ops
= {
2066 .setattr
= ntfs_setattr
,
2067 #endif /* NTFS_RW */
2070 const struct file_operations ntfs_empty_file_ops
= {};
2072 const struct inode_operations ntfs_empty_inode_ops
= {};