2 * file.c - NTFS kernel file operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2005 Anton Altaparmakov
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/buffer_head.h>
23 #include <linux/pagemap.h>
24 #include <linux/pagevec.h>
25 #include <linux/sched.h>
26 #include <linux/swap.h>
27 #include <linux/uio.h>
28 #include <linux/writeback.h>
31 #include <asm/uaccess.h>
43 * ntfs_file_open - called when an inode is about to be opened
44 * @vi: inode to be opened
45 * @filp: file structure describing the inode
47 * Limit file size to the page cache limit on architectures where unsigned long
48 * is 32-bits. This is the most we can do for now without overflowing the page
49 * cache page index. Doing it this way means we don't run into problems because
50 * of existing too large files. It would be better to allow the user to read
51 * the beginning of the file but I doubt very much anyone is going to hit this
52 * check on a 32-bit architecture, so there is no point in adding the extra
53 * complexity required to support this.
55 * On 64-bit architectures, the check is hopefully optimized away by the
58 * After the check passes, just call generic_file_open() to do its work.
60 static int ntfs_file_open(struct inode
*vi
, struct file
*filp
)
62 if (sizeof(unsigned long) < 8) {
63 if (i_size_read(vi
) > MAX_LFS_FILESIZE
)
66 return generic_file_open(vi
, filp
);
72 * ntfs_attr_extend_initialized - extend the initialized size of an attribute
73 * @ni: ntfs inode of the attribute to extend
74 * @new_init_size: requested new initialized size in bytes
75 * @cached_page: store any allocated but unused page here
76 * @lru_pvec: lru-buffering pagevec of the caller
78 * Extend the initialized size of an attribute described by the ntfs inode @ni
79 * to @new_init_size bytes. This involves zeroing any non-sparse space between
80 * the old initialized size and @new_init_size both in the page cache and on
81 * disk (if relevant complete pages are zeroed in the page cache then these may
82 * simply be marked dirty for later writeout). There is one caveat and that is
83 * that if any uptodate page cache pages between the old initialized size and
84 * the smaller of @new_init_size and the file size (vfs inode->i_size) are in
85 * memory, these need to be marked dirty without being zeroed since they could
86 * be non-zero due to mmap() based writes.
88 * As a side-effect, the file size (vfs inode->i_size) may be incremented as,
89 * in the resident attribute case, it is tied to the initialized size and, in
90 * the non-resident attribute case, it may not fall below the initialized size.
92 * Note that if the attribute is resident, we do not need to touch the page
93 * cache at all. This is because if the page cache page is not uptodate we
94 * bring it uptodate later, when doing the write to the mft record since we
95 * then already have the page mapped. And if the page is uptodate, the
96 * non-initialized region will already have been zeroed when the page was
97 * brought uptodate and the region may in fact already have been overwritten
98 * with new data via mmap() based writes, so we cannot just zero it. And since
99 * POSIX specifies that the behaviour of resizing a file whilst it is mmap()ped
100 * is unspecified, we choose not to do zeroing and thus we do not need to touch
101 * the page at all. For a more detailed explanation see ntfs_truncate() which
102 * is in fs/ntfs/inode.c.
104 * @cached_page and @lru_pvec are just optimisations for dealing with multiple
107 * Return 0 on success and -errno on error. In the case that an error is
108 * encountered it is possible that the initialized size will already have been
109 * incremented some way towards @new_init_size but it is guaranteed that if
110 * this is the case, the necessary zeroing will also have happened and that all
111 * metadata is self-consistent.
113 * Locking: This function locks the mft record of the base ntfs inode and
114 * maintains the lock throughout execution of the function. This is required
115 * so that the initialized size of the attribute can be modified safely.
117 static int ntfs_attr_extend_initialized(ntfs_inode
*ni
, const s64 new_init_size
,
118 struct page
**cached_page
, struct pagevec
*lru_pvec
)
122 pgoff_t index
, end_index
;
124 struct inode
*vi
= VFS_I(ni
);
126 MFT_RECORD
*m
= NULL
;
128 ntfs_attr_search_ctx
*ctx
= NULL
;
129 struct address_space
*mapping
;
130 struct page
*page
= NULL
;
135 read_lock_irqsave(&ni
->size_lock
, flags
);
136 old_init_size
= ni
->initialized_size
;
137 old_i_size
= i_size_read(vi
);
138 BUG_ON(new_init_size
> ni
->allocated_size
);
139 read_unlock_irqrestore(&ni
->size_lock
, flags
);
140 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
141 "old_initialized_size 0x%llx, "
142 "new_initialized_size 0x%llx, i_size 0x%llx.",
143 vi
->i_ino
, (unsigned)le32_to_cpu(ni
->type
),
144 (unsigned long long)old_init_size
,
145 (unsigned long long)new_init_size
, old_i_size
);
149 base_ni
= ni
->ext
.base_ntfs_ino
;
150 /* Use goto to reduce indentation and we need the label below anyway. */
151 if (NInoNonResident(ni
))
152 goto do_non_resident_extend
;
153 BUG_ON(old_init_size
!= old_i_size
);
154 m
= map_mft_record(base_ni
);
160 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
161 if (unlikely(!ctx
)) {
165 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
166 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
174 BUG_ON(a
->non_resident
);
175 /* The total length of the attribute value. */
176 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
177 BUG_ON(old_i_size
!= (loff_t
)attr_len
);
179 * Do the zeroing in the mft record and update the attribute size in
182 kattr
= (u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
);
183 memset(kattr
+ attr_len
, 0, new_init_size
- attr_len
);
184 a
->data
.resident
.value_length
= cpu_to_le32((u32
)new_init_size
);
185 /* Finally, update the sizes in the vfs and ntfs inodes. */
186 write_lock_irqsave(&ni
->size_lock
, flags
);
187 i_size_write(vi
, new_init_size
);
188 ni
->initialized_size
= new_init_size
;
189 write_unlock_irqrestore(&ni
->size_lock
, flags
);
191 do_non_resident_extend
:
193 * If the new initialized size @new_init_size exceeds the current file
194 * size (vfs inode->i_size), we need to extend the file size to the
195 * new initialized size.
197 if (new_init_size
> old_i_size
) {
198 m
= map_mft_record(base_ni
);
204 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
205 if (unlikely(!ctx
)) {
209 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
210 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
218 BUG_ON(!a
->non_resident
);
219 BUG_ON(old_i_size
!= (loff_t
)
220 sle64_to_cpu(a
->data
.non_resident
.data_size
));
221 a
->data
.non_resident
.data_size
= cpu_to_sle64(new_init_size
);
222 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
223 mark_mft_record_dirty(ctx
->ntfs_ino
);
224 /* Update the file size in the vfs inode. */
225 i_size_write(vi
, new_init_size
);
226 ntfs_attr_put_search_ctx(ctx
);
228 unmap_mft_record(base_ni
);
231 mapping
= vi
->i_mapping
;
232 index
= old_init_size
>> PAGE_CACHE_SHIFT
;
233 end_index
= (new_init_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
236 * Read the page. If the page is not present, this will zero
237 * the uninitialized regions for us.
239 page
= read_cache_page(mapping
, index
,
240 (filler_t
*)mapping
->a_ops
->readpage
, NULL
);
245 wait_on_page_locked(page
);
246 if (unlikely(!PageUptodate(page
) || PageError(page
))) {
247 page_cache_release(page
);
252 * Update the initialized size in the ntfs inode. This is
253 * enough to make ntfs_writepage() work.
255 write_lock_irqsave(&ni
->size_lock
, flags
);
256 ni
->initialized_size
= (index
+ 1) << PAGE_CACHE_SHIFT
;
257 if (ni
->initialized_size
> new_init_size
)
258 ni
->initialized_size
= new_init_size
;
259 write_unlock_irqrestore(&ni
->size_lock
, flags
);
260 /* Set the page dirty so it gets written out. */
261 set_page_dirty(page
);
262 page_cache_release(page
);
264 * Play nice with the vm and the rest of the system. This is
265 * very much needed as we can potentially be modifying the
266 * initialised size from a very small value to a really huge
268 * f = open(somefile, O_TRUNC);
269 * truncate(f, 10GiB);
272 * And this would mean we would be marking dirty hundreds of
273 * thousands of pages or as in the above example more than
274 * two and a half million pages!
276 * TODO: For sparse pages could optimize this workload by using
277 * the FsMisc / MiscFs page bit as a "PageIsSparse" bit. This
278 * would be set in readpage for sparse pages and here we would
279 * not need to mark dirty any pages which have this bit set.
280 * The only caveat is that we have to clear the bit everywhere
281 * where we allocate any clusters that lie in the page or that
284 * TODO: An even greater optimization would be for us to only
285 * call readpage() on pages which are not in sparse regions as
286 * determined from the runlist. This would greatly reduce the
287 * number of pages we read and make dirty in the case of sparse
290 balance_dirty_pages_ratelimited(mapping
);
292 } while (++index
< end_index
);
293 read_lock_irqsave(&ni
->size_lock
, flags
);
294 BUG_ON(ni
->initialized_size
!= new_init_size
);
295 read_unlock_irqrestore(&ni
->size_lock
, flags
);
296 /* Now bring in sync the initialized_size in the mft record. */
297 m
= map_mft_record(base_ni
);
303 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
304 if (unlikely(!ctx
)) {
308 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
309 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
317 BUG_ON(!a
->non_resident
);
318 a
->data
.non_resident
.initialized_size
= cpu_to_sle64(new_init_size
);
320 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
321 mark_mft_record_dirty(ctx
->ntfs_ino
);
323 ntfs_attr_put_search_ctx(ctx
);
325 unmap_mft_record(base_ni
);
326 ntfs_debug("Done, initialized_size 0x%llx, i_size 0x%llx.",
327 (unsigned long long)new_init_size
, i_size_read(vi
));
330 write_lock_irqsave(&ni
->size_lock
, flags
);
331 ni
->initialized_size
= old_init_size
;
332 write_unlock_irqrestore(&ni
->size_lock
, flags
);
335 ntfs_attr_put_search_ctx(ctx
);
337 unmap_mft_record(base_ni
);
338 ntfs_debug("Failed. Returning error code %i.", err
);
343 * ntfs_fault_in_pages_readable -
345 * Fault a number of userspace pages into pagetables.
347 * Unlike include/linux/pagemap.h::fault_in_pages_readable(), this one copes
348 * with more than two userspace pages as well as handling the single page case
351 * If you find this difficult to understand, then think of the while loop being
352 * the following code, except that we do without the integer variable ret:
355 * ret = __get_user(c, uaddr);
356 * uaddr += PAGE_SIZE;
357 * } while (!ret && uaddr < end);
359 * Note, the final __get_user() may well run out-of-bounds of the user buffer,
360 * but _not_ out-of-bounds of the page the user buffer belongs to, and since
361 * this is only a read and not a write, and since it is still in the same page,
362 * it should not matter and this makes the code much simpler.
364 static inline void ntfs_fault_in_pages_readable(const char __user
*uaddr
,
367 const char __user
*end
;
370 /* Set @end to the first byte outside the last page we care about. */
371 end
= (const char __user
*)PAGE_ALIGN((ptrdiff_t __user
)uaddr
+ bytes
);
373 while (!__get_user(c
, uaddr
) && (uaddr
+= PAGE_SIZE
, uaddr
< end
))
378 * ntfs_fault_in_pages_readable_iovec -
380 * Same as ntfs_fault_in_pages_readable() but operates on an array of iovecs.
382 static inline void ntfs_fault_in_pages_readable_iovec(const struct iovec
*iov
,
383 size_t iov_ofs
, int bytes
)
386 const char __user
*buf
;
389 buf
= iov
->iov_base
+ iov_ofs
;
390 len
= iov
->iov_len
- iov_ofs
;
393 ntfs_fault_in_pages_readable(buf
, len
);
401 * __ntfs_grab_cache_pages - obtain a number of locked pages
402 * @mapping: address space mapping from which to obtain page cache pages
403 * @index: starting index in @mapping at which to begin obtaining pages
404 * @nr_pages: number of page cache pages to obtain
405 * @pages: array of pages in which to return the obtained page cache pages
406 * @cached_page: allocated but as yet unused page
407 * @lru_pvec: lru-buffering pagevec of caller
409 * Obtain @nr_pages locked page cache pages from the mapping @maping and
410 * starting at index @index.
412 * If a page is newly created, increment its refcount and add it to the
413 * caller's lru-buffering pagevec @lru_pvec.
415 * This is the same as mm/filemap.c::__grab_cache_page(), except that @nr_pages
416 * are obtained at once instead of just one page and that 0 is returned on
417 * success and -errno on error.
419 * Note, the page locks are obtained in ascending page index order.
421 static inline int __ntfs_grab_cache_pages(struct address_space
*mapping
,
422 pgoff_t index
, const unsigned nr_pages
, struct page
**pages
,
423 struct page
**cached_page
, struct pagevec
*lru_pvec
)
430 pages
[nr
] = find_lock_page(mapping
, index
);
433 *cached_page
= page_cache_alloc(mapping
);
434 if (unlikely(!*cached_page
)) {
439 err
= add_to_page_cache(*cached_page
, mapping
, index
,
446 pages
[nr
] = *cached_page
;
447 page_cache_get(*cached_page
);
448 if (unlikely(!pagevec_add(lru_pvec
, *cached_page
)))
449 __pagevec_lru_add(lru_pvec
);
454 } while (nr
< nr_pages
);
459 unlock_page(pages
[--nr
]);
460 page_cache_release(pages
[nr
]);
465 static inline int ntfs_submit_bh_for_read(struct buffer_head
*bh
)
469 bh
->b_end_io
= end_buffer_read_sync
;
470 return submit_bh(READ
, bh
);
474 * ntfs_prepare_pages_for_non_resident_write - prepare pages for receiving data
475 * @pages: array of destination pages
476 * @nr_pages: number of pages in @pages
477 * @pos: byte position in file at which the write begins
478 * @bytes: number of bytes to be written
480 * This is called for non-resident attributes from ntfs_file_buffered_write()
481 * with i_sem held on the inode (@pages[0]->mapping->host). There are
482 * @nr_pages pages in @pages which are locked but not kmap()ped. The source
483 * data has not yet been copied into the @pages.
485 * Need to fill any holes with actual clusters, allocate buffers if necessary,
486 * ensure all the buffers are mapped, and bring uptodate any buffers that are
487 * only partially being written to.
489 * If @nr_pages is greater than one, we are guaranteed that the cluster size is
490 * greater than PAGE_CACHE_SIZE, that all pages in @pages are entirely inside
491 * the same cluster and that they are the entirety of that cluster, and that
492 * the cluster is sparse, i.e. we need to allocate a cluster to fill the hole.
494 * i_size is not to be modified yet.
496 * Return 0 on success or -errno on error.
498 static int ntfs_prepare_pages_for_non_resident_write(struct page
**pages
,
499 unsigned nr_pages
, s64 pos
, size_t bytes
)
501 VCN vcn
, highest_vcn
= 0, cpos
, cend
, bh_cpos
, bh_cend
;
503 s64 bh_pos
, vcn_len
, end
, initialized_size
;
507 ntfs_inode
*ni
, *base_ni
= NULL
;
509 runlist_element
*rl
, *rl2
;
510 struct buffer_head
*bh
, *head
, *wait
[2], **wait_bh
= wait
;
511 ntfs_attr_search_ctx
*ctx
= NULL
;
512 MFT_RECORD
*m
= NULL
;
513 ATTR_RECORD
*a
= NULL
;
515 u32 attr_rec_len
= 0;
516 unsigned blocksize
, u
;
518 BOOL rl_write_locked
, was_hole
, is_retry
;
519 unsigned char blocksize_bits
;
522 u8 mft_attr_mapped
:1;
525 } status
= { 0, 0, 0, 0 };
530 vi
= pages
[0]->mapping
->host
;
533 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
534 "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%x.",
535 vi
->i_ino
, ni
->type
, pages
[0]->index
, nr_pages
,
536 (long long)pos
, bytes
);
537 blocksize_bits
= vi
->i_blkbits
;
538 blocksize
= 1 << blocksize_bits
;
541 struct page
*page
= pages
[u
];
543 * create_empty_buffers() will create uptodate/dirty buffers if
544 * the page is uptodate/dirty.
546 if (!page_has_buffers(page
)) {
547 create_empty_buffers(page
, blocksize
, 0);
548 if (unlikely(!page_has_buffers(page
)))
551 } while (++u
< nr_pages
);
552 rl_write_locked
= FALSE
;
559 cpos
= pos
>> vol
->cluster_size_bits
;
561 cend
= (end
+ vol
->cluster_size
- 1) >> vol
->cluster_size_bits
;
563 * Loop over each page and for each page over each buffer. Use goto to
564 * reduce indentation.
569 bh_pos
= (s64
)page
->index
<< PAGE_CACHE_SHIFT
;
570 bh
= head
= page_buffers(page
);
576 /* Clear buffer_new on all buffers to reinitialise state. */
578 clear_buffer_new(bh
);
579 bh_end
= bh_pos
+ blocksize
;
580 bh_cpos
= bh_pos
>> vol
->cluster_size_bits
;
581 bh_cofs
= bh_pos
& vol
->cluster_size_mask
;
582 if (buffer_mapped(bh
)) {
584 * The buffer is already mapped. If it is uptodate,
587 if (buffer_uptodate(bh
))
590 * The buffer is not uptodate. If the page is uptodate
591 * set the buffer uptodate and otherwise ignore it.
593 if (PageUptodate(page
)) {
594 set_buffer_uptodate(bh
);
598 * Neither the page nor the buffer are uptodate. If
599 * the buffer is only partially being written to, we
600 * need to read it in before the write, i.e. now.
602 if ((bh_pos
< pos
&& bh_end
> pos
) ||
603 (bh_pos
< end
&& bh_end
> end
)) {
605 * If the buffer is fully or partially within
606 * the initialized size, do an actual read.
607 * Otherwise, simply zero the buffer.
609 read_lock_irqsave(&ni
->size_lock
, flags
);
610 initialized_size
= ni
->initialized_size
;
611 read_unlock_irqrestore(&ni
->size_lock
, flags
);
612 if (bh_pos
< initialized_size
) {
613 ntfs_submit_bh_for_read(bh
);
616 u8
*kaddr
= kmap_atomic(page
, KM_USER0
);
617 memset(kaddr
+ bh_offset(bh
), 0,
619 kunmap_atomic(kaddr
, KM_USER0
);
620 flush_dcache_page(page
);
621 set_buffer_uptodate(bh
);
626 /* Unmapped buffer. Need to map it. */
627 bh
->b_bdev
= vol
->sb
->s_bdev
;
629 * If the current buffer is in the same clusters as the map
630 * cache, there is no need to check the runlist again. The
631 * map cache is made up of @vcn, which is the first cached file
632 * cluster, @vcn_len which is the number of cached file
633 * clusters, @lcn is the device cluster corresponding to @vcn,
634 * and @lcn_block is the block number corresponding to @lcn.
636 cdelta
= bh_cpos
- vcn
;
637 if (likely(!cdelta
|| (cdelta
> 0 && cdelta
< vcn_len
))) {
640 bh
->b_blocknr
= lcn_block
+
641 (cdelta
<< (vol
->cluster_size_bits
-
643 (bh_cofs
>> blocksize_bits
);
644 set_buffer_mapped(bh
);
646 * If the page is uptodate so is the buffer. If the
647 * buffer is fully outside the write, we ignore it if
648 * it was already allocated and we mark it dirty so it
649 * gets written out if we allocated it. On the other
650 * hand, if we allocated the buffer but we are not
651 * marking it dirty we set buffer_new so we can do
654 if (PageUptodate(page
)) {
655 if (!buffer_uptodate(bh
))
656 set_buffer_uptodate(bh
);
657 if (unlikely(was_hole
)) {
658 /* We allocated the buffer. */
659 unmap_underlying_metadata(bh
->b_bdev
,
661 if (bh_end
<= pos
|| bh_pos
>= end
)
662 mark_buffer_dirty(bh
);
668 /* Page is _not_ uptodate. */
669 if (likely(!was_hole
)) {
671 * Buffer was already allocated. If it is not
672 * uptodate and is only partially being written
673 * to, we need to read it in before the write,
676 if (!buffer_uptodate(bh
) && ((bh_pos
< pos
&&
681 * If the buffer is fully or partially
682 * within the initialized size, do an
683 * actual read. Otherwise, simply zero
686 read_lock_irqsave(&ni
->size_lock
,
688 initialized_size
= ni
->initialized_size
;
689 read_unlock_irqrestore(&ni
->size_lock
,
691 if (bh_pos
< initialized_size
) {
692 ntfs_submit_bh_for_read(bh
);
695 u8
*kaddr
= kmap_atomic(page
,
697 memset(kaddr
+ bh_offset(bh
),
699 kunmap_atomic(kaddr
, KM_USER0
);
700 flush_dcache_page(page
);
701 set_buffer_uptodate(bh
);
706 /* We allocated the buffer. */
707 unmap_underlying_metadata(bh
->b_bdev
, bh
->b_blocknr
);
709 * If the buffer is fully outside the write, zero it,
710 * set it uptodate, and mark it dirty so it gets
711 * written out. If it is partially being written to,
712 * zero region surrounding the write but leave it to
713 * commit write to do anything else. Finally, if the
714 * buffer is fully being overwritten, do nothing.
716 if (bh_end
<= pos
|| bh_pos
>= end
) {
717 if (!buffer_uptodate(bh
)) {
718 u8
*kaddr
= kmap_atomic(page
, KM_USER0
);
719 memset(kaddr
+ bh_offset(bh
), 0,
721 kunmap_atomic(kaddr
, KM_USER0
);
722 flush_dcache_page(page
);
723 set_buffer_uptodate(bh
);
725 mark_buffer_dirty(bh
);
729 if (!buffer_uptodate(bh
) &&
730 (bh_pos
< pos
|| bh_end
> end
)) {
734 kaddr
= kmap_atomic(page
, KM_USER0
);
736 pofs
= bh_pos
& ~PAGE_CACHE_MASK
;
737 memset(kaddr
+ pofs
, 0, pos
- bh_pos
);
740 pofs
= end
& ~PAGE_CACHE_MASK
;
741 memset(kaddr
+ pofs
, 0, bh_end
- end
);
743 kunmap_atomic(kaddr
, KM_USER0
);
744 flush_dcache_page(page
);
749 * Slow path: this is the first buffer in the cluster. If it
750 * is outside allocated size and is not uptodate, zero it and
753 read_lock_irqsave(&ni
->size_lock
, flags
);
754 initialized_size
= ni
->allocated_size
;
755 read_unlock_irqrestore(&ni
->size_lock
, flags
);
756 if (bh_pos
> initialized_size
) {
757 if (PageUptodate(page
)) {
758 if (!buffer_uptodate(bh
))
759 set_buffer_uptodate(bh
);
760 } else if (!buffer_uptodate(bh
)) {
761 u8
*kaddr
= kmap_atomic(page
, KM_USER0
);
762 memset(kaddr
+ bh_offset(bh
), 0, blocksize
);
763 kunmap_atomic(kaddr
, KM_USER0
);
764 flush_dcache_page(page
);
765 set_buffer_uptodate(bh
);
771 down_read(&ni
->runlist
.lock
);
775 if (likely(rl
!= NULL
)) {
776 /* Seek to element containing target cluster. */
777 while (rl
->length
&& rl
[1].vcn
<= bh_cpos
)
779 lcn
= ntfs_rl_vcn_to_lcn(rl
, bh_cpos
);
780 if (likely(lcn
>= 0)) {
782 * Successful remap, setup the map cache and
783 * use that to deal with the buffer.
787 vcn_len
= rl
[1].vcn
- vcn
;
788 lcn_block
= lcn
<< (vol
->cluster_size_bits
-
791 * If the number of remaining clusters in the
792 * @pages is smaller or equal to the number of
793 * cached clusters, unlock the runlist as the
794 * map cache will be used from now on.
796 if (likely(vcn
+ vcn_len
>= cend
)) {
797 if (rl_write_locked
) {
798 up_write(&ni
->runlist
.lock
);
799 rl_write_locked
= FALSE
;
801 up_read(&ni
->runlist
.lock
);
804 goto map_buffer_cached
;
807 lcn
= LCN_RL_NOT_MAPPED
;
809 * If it is not a hole and not out of bounds, the runlist is
810 * probably unmapped so try to map it now.
812 if (unlikely(lcn
!= LCN_HOLE
&& lcn
!= LCN_ENOENT
)) {
813 if (likely(!is_retry
&& lcn
== LCN_RL_NOT_MAPPED
)) {
814 /* Attempt to map runlist. */
815 if (!rl_write_locked
) {
817 * We need the runlist locked for
818 * writing, so if it is locked for
819 * reading relock it now and retry in
820 * case it changed whilst we dropped
823 up_read(&ni
->runlist
.lock
);
824 down_write(&ni
->runlist
.lock
);
825 rl_write_locked
= TRUE
;
828 err
= ntfs_map_runlist_nolock(ni
, bh_cpos
,
835 * If @vcn is out of bounds, pretend @lcn is
836 * LCN_ENOENT. As long as the buffer is out
837 * of bounds this will work fine.
839 if (err
== -ENOENT
) {
842 goto rl_not_mapped_enoent
;
846 /* Failed to map the buffer, even after retrying. */
848 ntfs_error(vol
->sb
, "Failed to write to inode 0x%lx, "
849 "attribute type 0x%x, vcn 0x%llx, "
850 "vcn offset 0x%x, because its "
851 "location on disk could not be "
852 "determined%s (error code %i).",
853 ni
->mft_no
, ni
->type
,
854 (unsigned long long)bh_cpos
,
856 vol
->cluster_size_mask
,
857 is_retry
? " even after retrying" : "",
861 rl_not_mapped_enoent
:
863 * The buffer is in a hole or out of bounds. We need to fill
864 * the hole, unless the buffer is in a cluster which is not
865 * touched by the write, in which case we just leave the buffer
866 * unmapped. This can only happen when the cluster size is
867 * less than the page cache size.
869 if (unlikely(vol
->cluster_size
< PAGE_CACHE_SIZE
)) {
870 bh_cend
= (bh_end
+ vol
->cluster_size
- 1) >>
871 vol
->cluster_size_bits
;
872 if ((bh_cend
<= cpos
|| bh_cpos
>= cend
)) {
875 * If the buffer is uptodate we skip it. If it
876 * is not but the page is uptodate, we can set
877 * the buffer uptodate. If the page is not
878 * uptodate, we can clear the buffer and set it
879 * uptodate. Whether this is worthwhile is
880 * debatable and this could be removed.
882 if (PageUptodate(page
)) {
883 if (!buffer_uptodate(bh
))
884 set_buffer_uptodate(bh
);
885 } else if (!buffer_uptodate(bh
)) {
886 u8
*kaddr
= kmap_atomic(page
, KM_USER0
);
887 memset(kaddr
+ bh_offset(bh
), 0,
889 kunmap_atomic(kaddr
, KM_USER0
);
890 flush_dcache_page(page
);
891 set_buffer_uptodate(bh
);
897 * Out of bounds buffer is invalid if it was not really out of
900 BUG_ON(lcn
!= LCN_HOLE
);
902 * We need the runlist locked for writing, so if it is locked
903 * for reading relock it now and retry in case it changed
904 * whilst we dropped the lock.
907 if (!rl_write_locked
) {
908 up_read(&ni
->runlist
.lock
);
909 down_write(&ni
->runlist
.lock
);
910 rl_write_locked
= TRUE
;
913 /* Find the previous last allocated cluster. */
914 BUG_ON(rl
->lcn
!= LCN_HOLE
);
917 while (--rl2
>= ni
->runlist
.rl
) {
919 lcn
= rl2
->lcn
+ rl2
->length
;
923 rl2
= ntfs_cluster_alloc(vol
, bh_cpos
, 1, lcn
, DATA_ZONE
,
927 ntfs_debug("Failed to allocate cluster, error code %i.",
932 rl
= ntfs_runlists_merge(ni
->runlist
.rl
, rl2
);
937 if (ntfs_cluster_free_from_rl(vol
, rl2
)) {
938 ntfs_error(vol
->sb
, "Failed to release "
939 "allocated cluster in error "
940 "code path. Run chkdsk to "
941 "recover the lost cluster.");
948 status
.runlist_merged
= 1;
949 ntfs_debug("Allocated cluster, lcn 0x%llx.", lcn
);
950 /* Map and lock the mft record and get the attribute record. */
954 base_ni
= ni
->ext
.base_ntfs_ino
;
955 m
= map_mft_record(base_ni
);
960 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
961 if (unlikely(!ctx
)) {
963 unmap_mft_record(base_ni
);
966 status
.mft_attr_mapped
= 1;
967 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
968 CASE_SENSITIVE
, bh_cpos
, NULL
, 0, ctx
);
977 * Find the runlist element with which the attribute extent
978 * starts. Note, we cannot use the _attr_ version because we
979 * have mapped the mft record. That is ok because we know the
980 * runlist fragment must be mapped already to have ever gotten
981 * here, so we can just use the _rl_ version.
983 vcn
= sle64_to_cpu(a
->data
.non_resident
.lowest_vcn
);
984 rl2
= ntfs_rl_find_vcn_nolock(rl
, vcn
);
986 BUG_ON(!rl2
->length
);
987 BUG_ON(rl2
->lcn
< LCN_HOLE
);
988 highest_vcn
= sle64_to_cpu(a
->data
.non_resident
.highest_vcn
);
990 * If @highest_vcn is zero, calculate the real highest_vcn
991 * (which can really be zero).
994 highest_vcn
= (sle64_to_cpu(
995 a
->data
.non_resident
.allocated_size
) >>
996 vol
->cluster_size_bits
) - 1;
998 * Determine the size of the mapping pairs array for the new
999 * extent, i.e. the old extent with the hole filled.
1001 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, vcn
,
1003 if (unlikely(mp_size
<= 0)) {
1004 if (!(err
= mp_size
))
1006 ntfs_debug("Failed to get size for mapping pairs "
1007 "array, error code %i.", err
);
1011 * Resize the attribute record to fit the new mapping pairs
1014 attr_rec_len
= le32_to_cpu(a
->length
);
1015 err
= ntfs_attr_record_resize(m
, a
, mp_size
+ le16_to_cpu(
1016 a
->data
.non_resident
.mapping_pairs_offset
));
1017 if (unlikely(err
)) {
1018 BUG_ON(err
!= -ENOSPC
);
1019 // TODO: Deal with this by using the current attribute
1020 // and fill it with as much of the mapping pairs
1021 // array as possible. Then loop over each attribute
1022 // extent rewriting the mapping pairs arrays as we go
1023 // along and if when we reach the end we have not
1024 // enough space, try to resize the last attribute
1025 // extent and if even that fails, add a new attribute
1027 // We could also try to resize at each step in the hope
1028 // that we will not need to rewrite every single extent.
1029 // Note, we may need to decompress some extents to fill
1030 // the runlist as we are walking the extents...
1031 ntfs_error(vol
->sb
, "Not enough space in the mft "
1032 "record for the extended attribute "
1033 "record. This case is not "
1034 "implemented yet.");
1038 status
.mp_rebuilt
= 1;
1040 * Generate the mapping pairs array directly into the attribute
1043 err
= ntfs_mapping_pairs_build(vol
, (u8
*)a
+ le16_to_cpu(
1044 a
->data
.non_resident
.mapping_pairs_offset
),
1045 mp_size
, rl2
, vcn
, highest_vcn
, NULL
);
1046 if (unlikely(err
)) {
1047 ntfs_error(vol
->sb
, "Cannot fill hole in inode 0x%lx, "
1048 "attribute type 0x%x, because building "
1049 "the mapping pairs failed with error "
1050 "code %i.", vi
->i_ino
,
1051 (unsigned)le32_to_cpu(ni
->type
), err
);
1055 /* Update the highest_vcn but only if it was not set. */
1056 if (unlikely(!a
->data
.non_resident
.highest_vcn
))
1057 a
->data
.non_resident
.highest_vcn
=
1058 cpu_to_sle64(highest_vcn
);
1060 * If the attribute is sparse/compressed, update the compressed
1061 * size in the ntfs_inode structure and the attribute record.
1063 if (likely(NInoSparse(ni
) || NInoCompressed(ni
))) {
1065 * If we are not in the first attribute extent, switch
1066 * to it, but first ensure the changes will make it to
1069 if (a
->data
.non_resident
.lowest_vcn
) {
1070 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1071 mark_mft_record_dirty(ctx
->ntfs_ino
);
1072 ntfs_attr_reinit_search_ctx(ctx
);
1073 err
= ntfs_attr_lookup(ni
->type
, ni
->name
,
1074 ni
->name_len
, CASE_SENSITIVE
,
1076 if (unlikely(err
)) {
1077 status
.attr_switched
= 1;
1080 /* @m is not used any more so do not set it. */
1083 write_lock_irqsave(&ni
->size_lock
, flags
);
1084 ni
->itype
.compressed
.size
+= vol
->cluster_size
;
1085 a
->data
.non_resident
.compressed_size
=
1086 cpu_to_sle64(ni
->itype
.compressed
.size
);
1087 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1089 /* Ensure the changes make it to disk. */
1090 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1091 mark_mft_record_dirty(ctx
->ntfs_ino
);
1092 ntfs_attr_put_search_ctx(ctx
);
1093 unmap_mft_record(base_ni
);
1094 /* Successfully filled the hole. */
1095 status
.runlist_merged
= 0;
1096 status
.mft_attr_mapped
= 0;
1097 status
.mp_rebuilt
= 0;
1098 /* Setup the map cache and use that to deal with the buffer. */
1102 lcn_block
= lcn
<< (vol
->cluster_size_bits
- blocksize_bits
);
1105 * If the number of remaining clusters in the @pages is smaller
1106 * or equal to the number of cached clusters, unlock the
1107 * runlist as the map cache will be used from now on.
1109 if (likely(vcn
+ vcn_len
>= cend
)) {
1110 up_write(&ni
->runlist
.lock
);
1111 rl_write_locked
= FALSE
;
1114 goto map_buffer_cached
;
1115 } while (bh_pos
+= blocksize
, (bh
= bh
->b_this_page
) != head
);
1116 /* If there are no errors, do the next page. */
1117 if (likely(!err
&& ++u
< nr_pages
))
1119 /* If there are no errors, release the runlist lock if we took it. */
1121 if (unlikely(rl_write_locked
)) {
1122 up_write(&ni
->runlist
.lock
);
1123 rl_write_locked
= FALSE
;
1124 } else if (unlikely(rl
))
1125 up_read(&ni
->runlist
.lock
);
1128 /* If we issued read requests, let them complete. */
1129 read_lock_irqsave(&ni
->size_lock
, flags
);
1130 initialized_size
= ni
->initialized_size
;
1131 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1132 while (wait_bh
> wait
) {
1135 if (likely(buffer_uptodate(bh
))) {
1137 bh_pos
= ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1140 * If the buffer overflows the initialized size, need
1141 * to zero the overflowing region.
1143 if (unlikely(bh_pos
+ blocksize
> initialized_size
)) {
1147 if (likely(bh_pos
< initialized_size
))
1148 ofs
= initialized_size
- bh_pos
;
1149 kaddr
= kmap_atomic(page
, KM_USER0
);
1150 memset(kaddr
+ bh_offset(bh
) + ofs
, 0,
1152 kunmap_atomic(kaddr
, KM_USER0
);
1153 flush_dcache_page(page
);
1155 } else /* if (unlikely(!buffer_uptodate(bh))) */
1159 /* Clear buffer_new on all buffers. */
1162 bh
= head
= page_buffers(pages
[u
]);
1165 clear_buffer_new(bh
);
1166 } while ((bh
= bh
->b_this_page
) != head
);
1167 } while (++u
< nr_pages
);
1168 ntfs_debug("Done.");
1171 if (status
.attr_switched
) {
1172 /* Get back to the attribute extent we modified. */
1173 ntfs_attr_reinit_search_ctx(ctx
);
1174 if (ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1175 CASE_SENSITIVE
, bh_cpos
, NULL
, 0, ctx
)) {
1176 ntfs_error(vol
->sb
, "Failed to find required "
1177 "attribute extent of attribute in "
1178 "error code path. Run chkdsk to "
1180 write_lock_irqsave(&ni
->size_lock
, flags
);
1181 ni
->itype
.compressed
.size
+= vol
->cluster_size
;
1182 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1183 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1184 mark_mft_record_dirty(ctx
->ntfs_ino
);
1186 * The only thing that is now wrong is the compressed
1187 * size of the base attribute extent which chkdsk
1188 * should be able to fix.
1194 status
.attr_switched
= 0;
1198 * If the runlist has been modified, need to restore it by punching a
1199 * hole into it and we then need to deallocate the on-disk cluster as
1200 * well. Note, we only modify the runlist if we are able to generate a
1201 * new mapping pairs array, i.e. only when the mapped attribute extent
1204 if (status
.runlist_merged
&& !status
.attr_switched
) {
1205 BUG_ON(!rl_write_locked
);
1206 /* Make the file cluster we allocated sparse in the runlist. */
1207 if (ntfs_rl_punch_nolock(vol
, &ni
->runlist
, bh_cpos
, 1)) {
1208 ntfs_error(vol
->sb
, "Failed to punch hole into "
1209 "attribute runlist in error code "
1210 "path. Run chkdsk to recover the "
1213 make_bad_inode(VFS_I(base_ni
));
1215 } else /* if (success) */ {
1216 status
.runlist_merged
= 0;
1218 * Deallocate the on-disk cluster we allocated but only
1219 * if we succeeded in punching its vcn out of the
1222 down_write(&vol
->lcnbmp_lock
);
1223 if (ntfs_bitmap_clear_bit(vol
->lcnbmp_ino
, lcn
)) {
1224 ntfs_error(vol
->sb
, "Failed to release "
1225 "allocated cluster in error "
1226 "code path. Run chkdsk to "
1227 "recover the lost cluster.");
1230 up_write(&vol
->lcnbmp_lock
);
1234 * Resize the attribute record to its old size and rebuild the mapping
1235 * pairs array. Note, we only can do this if the runlist has been
1236 * restored to its old state which also implies that the mapped
1237 * attribute extent is not switched.
1239 if (status
.mp_rebuilt
&& !status
.runlist_merged
) {
1240 if (ntfs_attr_record_resize(m
, a
, attr_rec_len
)) {
1241 ntfs_error(vol
->sb
, "Failed to restore attribute "
1242 "record in error code path. Run "
1243 "chkdsk to recover.");
1245 make_bad_inode(VFS_I(base_ni
));
1247 } else /* if (success) */ {
1248 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+
1249 le16_to_cpu(a
->data
.non_resident
.
1250 mapping_pairs_offset
), attr_rec_len
-
1251 le16_to_cpu(a
->data
.non_resident
.
1252 mapping_pairs_offset
), ni
->runlist
.rl
,
1253 vcn
, highest_vcn
, NULL
)) {
1254 ntfs_error(vol
->sb
, "Failed to restore "
1255 "mapping pairs array in error "
1256 "code path. Run chkdsk to "
1259 make_bad_inode(VFS_I(base_ni
));
1262 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1263 mark_mft_record_dirty(ctx
->ntfs_ino
);
1266 /* Release the mft record and the attribute. */
1267 if (status
.mft_attr_mapped
) {
1268 ntfs_attr_put_search_ctx(ctx
);
1269 unmap_mft_record(base_ni
);
1271 /* Release the runlist lock. */
1272 if (rl_write_locked
)
1273 up_write(&ni
->runlist
.lock
);
1275 up_read(&ni
->runlist
.lock
);
1277 * Zero out any newly allocated blocks to avoid exposing stale data.
1278 * If BH_New is set, we know that the block was newly allocated above
1279 * and that it has not been fully zeroed and marked dirty yet.
1283 end
= bh_cpos
<< vol
->cluster_size_bits
;
1286 bh
= head
= page_buffers(page
);
1288 if (u
== nr_pages
&&
1289 ((s64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1290 bh_offset(bh
) >= end
)
1292 if (!buffer_new(bh
))
1294 clear_buffer_new(bh
);
1295 if (!buffer_uptodate(bh
)) {
1296 if (PageUptodate(page
))
1297 set_buffer_uptodate(bh
);
1299 u8
*kaddr
= kmap_atomic(page
, KM_USER0
);
1300 memset(kaddr
+ bh_offset(bh
), 0,
1302 kunmap_atomic(kaddr
, KM_USER0
);
1303 flush_dcache_page(page
);
1304 set_buffer_uptodate(bh
);
1307 mark_buffer_dirty(bh
);
1308 } while ((bh
= bh
->b_this_page
) != head
);
1309 } while (++u
<= nr_pages
);
1310 ntfs_error(vol
->sb
, "Failed. Returning error code %i.", err
);
1315 * Copy as much as we can into the pages and return the number of bytes which
1316 * were sucessfully copied. If a fault is encountered then clear the pages
1317 * out to (ofs + bytes) and return the number of bytes which were copied.
1319 static inline size_t ntfs_copy_from_user(struct page
**pages
,
1320 unsigned nr_pages
, unsigned ofs
, const char __user
*buf
,
1323 struct page
**last_page
= pages
+ nr_pages
;
1330 len
= PAGE_CACHE_SIZE
- ofs
;
1333 kaddr
= kmap_atomic(*pages
, KM_USER0
);
1334 left
= __copy_from_user_inatomic(kaddr
+ ofs
, buf
, len
);
1335 kunmap_atomic(kaddr
, KM_USER0
);
1336 if (unlikely(left
)) {
1337 /* Do it the slow way. */
1338 kaddr
= kmap(*pages
);
1339 left
= __copy_from_user(kaddr
+ ofs
, buf
, len
);
1350 } while (++pages
< last_page
);
1354 total
+= len
- left
;
1355 /* Zero the rest of the target like __copy_from_user(). */
1356 while (++pages
< last_page
) {
1360 len
= PAGE_CACHE_SIZE
;
1363 kaddr
= kmap_atomic(*pages
, KM_USER0
);
1364 memset(kaddr
, 0, len
);
1365 kunmap_atomic(kaddr
, KM_USER0
);
1370 static size_t __ntfs_copy_from_user_iovec(char *vaddr
,
1371 const struct iovec
*iov
, size_t iov_ofs
, size_t bytes
)
1376 const char __user
*buf
= iov
->iov_base
+ iov_ofs
;
1380 len
= iov
->iov_len
- iov_ofs
;
1383 left
= __copy_from_user_inatomic(vaddr
, buf
, len
);
1387 if (unlikely(left
)) {
1389 * Zero the rest of the target like __copy_from_user().
1391 memset(vaddr
, 0, bytes
);
1403 static inline void ntfs_set_next_iovec(const struct iovec
**iovp
,
1404 size_t *iov_ofsp
, size_t bytes
)
1406 const struct iovec
*iov
= *iovp
;
1407 size_t iov_ofs
= *iov_ofsp
;
1412 len
= iov
->iov_len
- iov_ofs
;
1417 if (iov
->iov_len
== iov_ofs
) {
1423 *iov_ofsp
= iov_ofs
;
1427 * This has the same side-effects and return value as ntfs_copy_from_user().
1428 * The difference is that on a fault we need to memset the remainder of the
1429 * pages (out to offset + bytes), to emulate ntfs_copy_from_user()'s
1430 * single-segment behaviour.
1432 * We call the same helper (__ntfs_copy_from_user_iovec()) both when atomic and
1433 * when not atomic. This is ok because __ntfs_copy_from_user_iovec() calls
1434 * __copy_from_user_inatomic() and it is ok to call this when non-atomic. In
1435 * fact, the only difference between __copy_from_user_inatomic() and
1436 * __copy_from_user() is that the latter calls might_sleep(). And on many
1437 * architectures __copy_from_user_inatomic() is just defined to
1438 * __copy_from_user() so it makes no difference at all on those architectures.
1440 static inline size_t ntfs_copy_from_user_iovec(struct page
**pages
,
1441 unsigned nr_pages
, unsigned ofs
, const struct iovec
**iov
,
1442 size_t *iov_ofs
, size_t bytes
)
1444 struct page
**last_page
= pages
+ nr_pages
;
1446 size_t copied
, len
, total
= 0;
1449 len
= PAGE_CACHE_SIZE
- ofs
;
1452 kaddr
= kmap_atomic(*pages
, KM_USER0
);
1453 copied
= __ntfs_copy_from_user_iovec(kaddr
+ ofs
,
1454 *iov
, *iov_ofs
, len
);
1455 kunmap_atomic(kaddr
, KM_USER0
);
1456 if (unlikely(copied
!= len
)) {
1457 /* Do it the slow way. */
1458 kaddr
= kmap(*pages
);
1459 copied
= __ntfs_copy_from_user_iovec(kaddr
+ ofs
,
1460 *iov
, *iov_ofs
, len
);
1462 if (unlikely(copied
!= len
))
1469 ntfs_set_next_iovec(iov
, iov_ofs
, len
);
1471 } while (++pages
< last_page
);
1476 /* Zero the rest of the target like __copy_from_user(). */
1477 while (++pages
< last_page
) {
1481 len
= PAGE_CACHE_SIZE
;
1484 kaddr
= kmap_atomic(*pages
, KM_USER0
);
1485 memset(kaddr
, 0, len
);
1486 kunmap_atomic(kaddr
, KM_USER0
);
1491 static inline void ntfs_flush_dcache_pages(struct page
**pages
,
1497 * Warning: Do not do the decrement at the same time as the
1498 * call because flush_dcache_page() is a NULL macro on i386
1499 * and hence the decrement never happens.
1501 flush_dcache_page(pages
[nr_pages
]);
1502 } while (--nr_pages
> 0);
1506 * ntfs_commit_pages_after_non_resident_write - commit the received data
1507 * @pages: array of destination pages
1508 * @nr_pages: number of pages in @pages
1509 * @pos: byte position in file at which the write begins
1510 * @bytes: number of bytes to be written
1512 * See description of ntfs_commit_pages_after_write(), below.
1514 static inline int ntfs_commit_pages_after_non_resident_write(
1515 struct page
**pages
, const unsigned nr_pages
,
1516 s64 pos
, size_t bytes
)
1518 s64 end
, initialized_size
;
1520 ntfs_inode
*ni
, *base_ni
;
1521 struct buffer_head
*bh
, *head
;
1522 ntfs_attr_search_ctx
*ctx
;
1525 unsigned long flags
;
1526 unsigned blocksize
, u
;
1529 vi
= pages
[0]->mapping
->host
;
1531 blocksize
= 1 << vi
->i_blkbits
;
1540 bh_pos
= (s64
)page
->index
<< PAGE_CACHE_SHIFT
;
1541 bh
= head
= page_buffers(page
);
1546 bh_end
= bh_pos
+ blocksize
;
1547 if (bh_end
<= pos
|| bh_pos
>= end
) {
1548 if (!buffer_uptodate(bh
))
1551 set_buffer_uptodate(bh
);
1552 mark_buffer_dirty(bh
);
1554 } while (bh_pos
+= blocksize
, (bh
= bh
->b_this_page
) != head
);
1556 * If all buffers are now uptodate but the page is not, set the
1559 if (!partial
&& !PageUptodate(page
))
1560 SetPageUptodate(page
);
1561 } while (++u
< nr_pages
);
1563 * Finally, if we do not need to update initialized_size or i_size we
1566 read_lock_irqsave(&ni
->size_lock
, flags
);
1567 initialized_size
= ni
->initialized_size
;
1568 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1569 if (end
<= initialized_size
) {
1570 ntfs_debug("Done.");
1574 * Update initialized_size/i_size as appropriate, both in the inode and
1580 base_ni
= ni
->ext
.base_ntfs_ino
;
1581 /* Map, pin, and lock the mft record. */
1582 m
= map_mft_record(base_ni
);
1589 BUG_ON(!NInoNonResident(ni
));
1590 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1591 if (unlikely(!ctx
)) {
1595 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1596 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1597 if (unlikely(err
)) {
1603 BUG_ON(!a
->non_resident
);
1604 write_lock_irqsave(&ni
->size_lock
, flags
);
1605 BUG_ON(end
> ni
->allocated_size
);
1606 ni
->initialized_size
= end
;
1607 a
->data
.non_resident
.initialized_size
= cpu_to_sle64(end
);
1608 if (end
> i_size_read(vi
)) {
1609 i_size_write(vi
, end
);
1610 a
->data
.non_resident
.data_size
=
1611 a
->data
.non_resident
.initialized_size
;
1613 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1614 /* Mark the mft record dirty, so it gets written back. */
1615 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1616 mark_mft_record_dirty(ctx
->ntfs_ino
);
1617 ntfs_attr_put_search_ctx(ctx
);
1618 unmap_mft_record(base_ni
);
1619 ntfs_debug("Done.");
1623 ntfs_attr_put_search_ctx(ctx
);
1625 unmap_mft_record(base_ni
);
1626 ntfs_error(vi
->i_sb
, "Failed to update initialized_size/i_size (error "
1628 if (err
!= -ENOMEM
) {
1629 NVolSetErrors(ni
->vol
);
1630 make_bad_inode(VFS_I(base_ni
));
1637 * ntfs_commit_pages_after_write - commit the received data
1638 * @pages: array of destination pages
1639 * @nr_pages: number of pages in @pages
1640 * @pos: byte position in file at which the write begins
1641 * @bytes: number of bytes to be written
1643 * This is called from ntfs_file_buffered_write() with i_sem held on the inode
1644 * (@pages[0]->mapping->host). There are @nr_pages pages in @pages which are
1645 * locked but not kmap()ped. The source data has already been copied into the
1646 * @page. ntfs_prepare_pages_for_non_resident_write() has been called before
1647 * the data was copied (for non-resident attributes only) and it returned
1650 * Need to set uptodate and mark dirty all buffers within the boundary of the
1651 * write. If all buffers in a page are uptodate we set the page uptodate, too.
1653 * Setting the buffers dirty ensures that they get written out later when
1654 * ntfs_writepage() is invoked by the VM.
1656 * Finally, we need to update i_size and initialized_size as appropriate both
1657 * in the inode and the mft record.
1659 * This is modelled after fs/buffer.c::generic_commit_write(), which marks
1660 * buffers uptodate and dirty, sets the page uptodate if all buffers in the
1661 * page are uptodate, and updates i_size if the end of io is beyond i_size. In
1662 * that case, it also marks the inode dirty.
1664 * If things have gone as outlined in
1665 * ntfs_prepare_pages_for_non_resident_write(), we do not need to do any page
1666 * content modifications here for non-resident attributes. For resident
1667 * attributes we need to do the uptodate bringing here which we combine with
1668 * the copying into the mft record which means we save one atomic kmap.
1670 * Return 0 on success or -errno on error.
1672 static int ntfs_commit_pages_after_write(struct page
**pages
,
1673 const unsigned nr_pages
, s64 pos
, size_t bytes
)
1675 s64 end
, initialized_size
;
1678 ntfs_inode
*ni
, *base_ni
;
1680 ntfs_attr_search_ctx
*ctx
;
1683 char *kattr
, *kaddr
;
1684 unsigned long flags
;
1692 vi
= page
->mapping
->host
;
1694 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page "
1695 "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%x.",
1696 vi
->i_ino
, ni
->type
, page
->index
, nr_pages
,
1697 (long long)pos
, bytes
);
1698 if (NInoNonResident(ni
))
1699 return ntfs_commit_pages_after_non_resident_write(pages
,
1700 nr_pages
, pos
, bytes
);
1701 BUG_ON(nr_pages
> 1);
1703 * Attribute is resident, implying it is not compressed, encrypted, or
1709 base_ni
= ni
->ext
.base_ntfs_ino
;
1710 BUG_ON(NInoNonResident(ni
));
1711 /* Map, pin, and lock the mft record. */
1712 m
= map_mft_record(base_ni
);
1719 ctx
= ntfs_attr_get_search_ctx(base_ni
, m
);
1720 if (unlikely(!ctx
)) {
1724 err
= ntfs_attr_lookup(ni
->type
, ni
->name
, ni
->name_len
,
1725 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1726 if (unlikely(err
)) {
1732 BUG_ON(a
->non_resident
);
1733 /* The total length of the attribute value. */
1734 attr_len
= le32_to_cpu(a
->data
.resident
.value_length
);
1735 i_size
= i_size_read(vi
);
1736 BUG_ON(attr_len
!= i_size
);
1737 BUG_ON(pos
> attr_len
);
1739 BUG_ON(end
> le32_to_cpu(a
->length
) -
1740 le16_to_cpu(a
->data
.resident
.value_offset
));
1741 kattr
= (u8
*)a
+ le16_to_cpu(a
->data
.resident
.value_offset
);
1742 kaddr
= kmap_atomic(page
, KM_USER0
);
1743 /* Copy the received data from the page to the mft record. */
1744 memcpy(kattr
+ pos
, kaddr
+ pos
, bytes
);
1745 /* Update the attribute length if necessary. */
1746 if (end
> attr_len
) {
1748 a
->data
.resident
.value_length
= cpu_to_le32(attr_len
);
1751 * If the page is not uptodate, bring the out of bounds area(s)
1752 * uptodate by copying data from the mft record to the page.
1754 if (!PageUptodate(page
)) {
1756 memcpy(kaddr
, kattr
, pos
);
1758 memcpy(kaddr
+ end
, kattr
+ end
, attr_len
- end
);
1759 /* Zero the region outside the end of the attribute value. */
1760 memset(kaddr
+ attr_len
, 0, PAGE_CACHE_SIZE
- attr_len
);
1761 flush_dcache_page(page
);
1762 SetPageUptodate(page
);
1764 kunmap_atomic(kaddr
, KM_USER0
);
1765 /* Update initialized_size/i_size if necessary. */
1766 read_lock_irqsave(&ni
->size_lock
, flags
);
1767 initialized_size
= ni
->initialized_size
;
1768 BUG_ON(end
> ni
->allocated_size
);
1769 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1770 BUG_ON(initialized_size
!= i_size
);
1771 if (end
> initialized_size
) {
1772 unsigned long flags
;
1774 write_lock_irqsave(&ni
->size_lock
, flags
);
1775 ni
->initialized_size
= end
;
1776 i_size_write(vi
, end
);
1777 write_unlock_irqrestore(&ni
->size_lock
, flags
);
1779 /* Mark the mft record dirty, so it gets written back. */
1780 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1781 mark_mft_record_dirty(ctx
->ntfs_ino
);
1782 ntfs_attr_put_search_ctx(ctx
);
1783 unmap_mft_record(base_ni
);
1784 ntfs_debug("Done.");
1787 if (err
== -ENOMEM
) {
1788 ntfs_warning(vi
->i_sb
, "Error allocating memory required to "
1789 "commit the write.");
1790 if (PageUptodate(page
)) {
1791 ntfs_warning(vi
->i_sb
, "Page is uptodate, setting "
1792 "dirty so the write will be retried "
1793 "later on by the VM.");
1795 * Put the page on mapping->dirty_pages, but leave its
1796 * buffers' dirty state as-is.
1798 __set_page_dirty_nobuffers(page
);
1801 ntfs_error(vi
->i_sb
, "Page is not uptodate. Written "
1802 "data has been lost.");
1804 ntfs_error(vi
->i_sb
, "Resident attribute commit write failed "
1805 "with error %i.", err
);
1806 NVolSetErrors(ni
->vol
);
1807 make_bad_inode(VFS_I(base_ni
));
1811 ntfs_attr_put_search_ctx(ctx
);
1813 unmap_mft_record(base_ni
);
1818 * ntfs_file_buffered_write -
1820 * Locking: The vfs is holding ->i_sem on the inode.
1822 static ssize_t
ntfs_file_buffered_write(struct kiocb
*iocb
,
1823 const struct iovec
*iov
, unsigned long nr_segs
,
1824 loff_t pos
, loff_t
*ppos
, size_t count
)
1826 struct file
*file
= iocb
->ki_filp
;
1827 struct address_space
*mapping
= file
->f_mapping
;
1828 struct inode
*vi
= mapping
->host
;
1829 ntfs_inode
*ni
= NTFS_I(vi
);
1830 ntfs_volume
*vol
= ni
->vol
;
1831 struct page
*pages
[NTFS_MAX_PAGES_PER_CLUSTER
];
1832 struct page
*cached_page
= NULL
;
1833 char __user
*buf
= NULL
;
1837 unsigned long flags
;
1838 size_t bytes
, iov_ofs
;
1839 ssize_t status
, written
;
1842 struct pagevec lru_pvec
;
1844 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1845 "pos 0x%llx, count 0x%lx.",
1846 vi
->i_ino
, (unsigned)le32_to_cpu(ni
->type
),
1847 (unsigned long long)pos
, (unsigned long)count
);
1848 if (unlikely(!count
))
1850 BUG_ON(NInoMstProtected(ni
));
1852 * If the attribute is not an index root and it is encrypted or
1853 * compressed, we cannot write to it yet. Note we need to check for
1854 * AT_INDEX_ALLOCATION since this is the type of both directory and
1857 if (ni
->type
!= AT_INDEX_ALLOCATION
) {
1858 /* If file is encrypted, deny access, just like NT4. */
1859 if (NInoEncrypted(ni
)) {
1861 * Reminder for later: Encrypted files are _always_
1862 * non-resident so that the content can always be
1865 ntfs_debug("Denying write access to encrypted file.");
1868 if (NInoCompressed(ni
)) {
1869 /* Only unnamed $DATA attribute can be compressed. */
1870 BUG_ON(ni
->type
!= AT_DATA
);
1871 BUG_ON(ni
->name_len
);
1873 * Reminder for later: If resident, the data is not
1874 * actually compressed. Only on the switch to non-
1875 * resident does compression kick in. This is in
1876 * contrast to encrypted files (see above).
1878 ntfs_error(vi
->i_sb
, "Writing to compressed files is "
1879 "not implemented yet. Sorry.");
1884 * If a previous ntfs_truncate() failed, repeat it and abort if it
1887 if (unlikely(NInoTruncateFailed(ni
))) {
1888 down_write(&vi
->i_alloc_sem
);
1889 err
= ntfs_truncate(vi
);
1890 up_write(&vi
->i_alloc_sem
);
1891 if (err
|| NInoTruncateFailed(ni
)) {
1894 ntfs_error(vol
->sb
, "Cannot perform write to inode "
1895 "0x%lx, attribute type 0x%x, because "
1896 "ntfs_truncate() failed (error code "
1898 (unsigned)le32_to_cpu(ni
->type
), err
);
1902 /* The first byte after the write. */
1905 * If the write goes beyond the allocated size, extend the allocation
1906 * to cover the whole of the write, rounded up to the nearest cluster.
1908 read_lock_irqsave(&ni
->size_lock
, flags
);
1909 ll
= ni
->allocated_size
;
1910 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1912 /* Extend the allocation without changing the data size. */
1913 ll
= ntfs_attr_extend_allocation(ni
, end
, -1, pos
);
1914 if (likely(ll
>= 0)) {
1916 /* If the extension was partial truncate the write. */
1918 ntfs_debug("Truncating write to inode 0x%lx, "
1919 "attribute type 0x%x, because "
1920 "the allocation was only "
1921 "partially extended.",
1922 vi
->i_ino
, (unsigned)
1923 le32_to_cpu(ni
->type
));
1929 read_lock_irqsave(&ni
->size_lock
, flags
);
1930 ll
= ni
->allocated_size
;
1931 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1932 /* Perform a partial write if possible or fail. */
1934 ntfs_debug("Truncating write to inode 0x%lx, "
1935 "attribute type 0x%x, because "
1936 "extending the allocation "
1937 "failed (error code %i).",
1938 vi
->i_ino
, (unsigned)
1939 le32_to_cpu(ni
->type
), err
);
1943 ntfs_error(vol
->sb
, "Cannot perform write to "
1944 "inode 0x%lx, attribute type "
1945 "0x%x, because extending the "
1946 "allocation failed (error "
1947 "code %i).", vi
->i_ino
,
1949 le32_to_cpu(ni
->type
), err
);
1954 pagevec_init(&lru_pvec
, 0);
1957 * If the write starts beyond the initialized size, extend it up to the
1958 * beginning of the write and initialize all non-sparse space between
1959 * the old initialized size and the new one. This automatically also
1960 * increments the vfs inode->i_size to keep it above or equal to the
1963 read_lock_irqsave(&ni
->size_lock
, flags
);
1964 ll
= ni
->initialized_size
;
1965 read_unlock_irqrestore(&ni
->size_lock
, flags
);
1967 err
= ntfs_attr_extend_initialized(ni
, pos
, &cached_page
,
1970 ntfs_error(vol
->sb
, "Cannot perform write to inode "
1971 "0x%lx, attribute type 0x%x, because "
1972 "extending the initialized size "
1973 "failed (error code %i).", vi
->i_ino
,
1974 (unsigned)le32_to_cpu(ni
->type
), err
);
1980 * Determine the number of pages per cluster for non-resident
1984 if (vol
->cluster_size
> PAGE_CACHE_SIZE
&& NInoNonResident(ni
))
1985 nr_pages
= vol
->cluster_size
>> PAGE_CACHE_SHIFT
;
1986 /* Finally, perform the actual write. */
1988 if (likely(nr_segs
== 1))
1989 buf
= iov
->iov_base
;
1991 iov_ofs
= 0; /* Offset in the current iovec. */
1994 pgoff_t idx
, start_idx
;
1995 unsigned ofs
, do_pages
, u
;
1998 start_idx
= idx
= pos
>> PAGE_CACHE_SHIFT
;
1999 ofs
= pos
& ~PAGE_CACHE_MASK
;
2000 bytes
= PAGE_CACHE_SIZE
- ofs
;
2003 vcn
= pos
>> vol
->cluster_size_bits
;
2004 if (vcn
!= last_vcn
) {
2007 * Get the lcn of the vcn the write is in. If
2008 * it is a hole, need to lock down all pages in
2011 down_read(&ni
->runlist
.lock
);
2012 lcn
= ntfs_attr_vcn_to_lcn_nolock(ni
, pos
>>
2013 vol
->cluster_size_bits
, FALSE
);
2014 up_read(&ni
->runlist
.lock
);
2015 if (unlikely(lcn
< LCN_HOLE
)) {
2017 if (lcn
== LCN_ENOMEM
)
2020 ntfs_error(vol
->sb
, "Cannot "
2023 "attribute type 0x%x, "
2024 "because the attribute "
2026 vi
->i_ino
, (unsigned)
2027 le32_to_cpu(ni
->type
));
2030 if (lcn
== LCN_HOLE
) {
2031 start_idx
= (pos
& ~(s64
)
2032 vol
->cluster_size_mask
)
2033 >> PAGE_CACHE_SHIFT
;
2034 bytes
= vol
->cluster_size
- (pos
&
2035 vol
->cluster_size_mask
);
2036 do_pages
= nr_pages
;
2043 * Bring in the user page(s) that we will copy from _first_.
2044 * Otherwise there is a nasty deadlock on copying from the same
2045 * page(s) as we are writing to, without it/them being marked
2046 * up-to-date. Note, at present there is nothing to stop the
2047 * pages being swapped out between us bringing them into memory
2048 * and doing the actual copying.
2050 if (likely(nr_segs
== 1))
2051 ntfs_fault_in_pages_readable(buf
, bytes
);
2053 ntfs_fault_in_pages_readable_iovec(iov
, iov_ofs
, bytes
);
2054 /* Get and lock @do_pages starting at index @start_idx. */
2055 status
= __ntfs_grab_cache_pages(mapping
, start_idx
, do_pages
,
2056 pages
, &cached_page
, &lru_pvec
);
2057 if (unlikely(status
))
2060 * For non-resident attributes, we need to fill any holes with
2061 * actual clusters and ensure all bufferes are mapped. We also
2062 * need to bring uptodate any buffers that are only partially
2065 if (NInoNonResident(ni
)) {
2066 status
= ntfs_prepare_pages_for_non_resident_write(
2067 pages
, do_pages
, pos
, bytes
);
2068 if (unlikely(status
)) {
2072 unlock_page(pages
[--do_pages
]);
2073 page_cache_release(pages
[do_pages
]);
2076 * The write preparation may have instantiated
2077 * allocated space outside i_size. Trim this
2078 * off again. We can ignore any errors in this
2079 * case as we will just be waisting a bit of
2080 * allocated space, which is not a disaster.
2082 i_size
= i_size_read(vi
);
2083 if (pos
+ bytes
> i_size
)
2084 vmtruncate(vi
, i_size
);
2088 u
= (pos
>> PAGE_CACHE_SHIFT
) - pages
[0]->index
;
2089 if (likely(nr_segs
== 1)) {
2090 copied
= ntfs_copy_from_user(pages
+ u
, do_pages
- u
,
2094 copied
= ntfs_copy_from_user_iovec(pages
+ u
,
2095 do_pages
- u
, ofs
, &iov
, &iov_ofs
,
2097 ntfs_flush_dcache_pages(pages
+ u
, do_pages
- u
);
2098 status
= ntfs_commit_pages_after_write(pages
, do_pages
, pos
,
2100 if (likely(!status
)) {
2104 if (unlikely(copied
!= bytes
))
2108 unlock_page(pages
[--do_pages
]);
2109 mark_page_accessed(pages
[do_pages
]);
2110 page_cache_release(pages
[do_pages
]);
2112 if (unlikely(status
))
2114 balance_dirty_pages_ratelimited(mapping
);
2120 page_cache_release(cached_page
);
2121 /* For now, when the user asks for O_SYNC, we actually give O_DSYNC. */
2122 if (likely(!status
)) {
2123 if (unlikely((file
->f_flags
& O_SYNC
) || IS_SYNC(vi
))) {
2124 if (!mapping
->a_ops
->writepage
|| !is_sync_kiocb(iocb
))
2125 status
= generic_osync_inode(vi
, mapping
,
2126 OSYNC_METADATA
|OSYNC_DATA
);
2129 pagevec_lru_add(&lru_pvec
);
2130 ntfs_debug("Done. Returning %s (written 0x%lx, status %li).",
2131 written
? "written" : "status", (unsigned long)written
,
2133 return written
? written
: status
;
2137 * ntfs_file_aio_write_nolock -
2139 static ssize_t
ntfs_file_aio_write_nolock(struct kiocb
*iocb
,
2140 const struct iovec
*iov
, unsigned long nr_segs
, loff_t
*ppos
)
2142 struct file
*file
= iocb
->ki_filp
;
2143 struct address_space
*mapping
= file
->f_mapping
;
2144 struct inode
*inode
= mapping
->host
;
2147 size_t count
; /* after file limit checks */
2148 ssize_t written
, err
;
2151 for (seg
= 0; seg
< nr_segs
; seg
++) {
2152 const struct iovec
*iv
= &iov
[seg
];
2154 * If any segment has a negative length, or the cumulative
2155 * length ever wraps negative then return -EINVAL.
2157 count
+= iv
->iov_len
;
2158 if (unlikely((ssize_t
)(count
|iv
->iov_len
) < 0))
2160 if (access_ok(VERIFY_READ
, iv
->iov_base
, iv
->iov_len
))
2165 count
-= iv
->iov_len
; /* This segment is no good */
2169 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
2170 /* We can write back this queue in page reclaim. */
2171 current
->backing_dev_info
= mapping
->backing_dev_info
;
2173 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
2178 err
= remove_suid(file
->f_dentry
);
2181 inode_update_time(inode
, 1);
2182 written
= ntfs_file_buffered_write(iocb
, iov
, nr_segs
, pos
, ppos
,
2185 current
->backing_dev_info
= NULL
;
2186 return written
? written
: err
;
2190 * ntfs_file_aio_write -
2192 static ssize_t
ntfs_file_aio_write(struct kiocb
*iocb
, const char __user
*buf
,
2193 size_t count
, loff_t pos
)
2195 struct file
*file
= iocb
->ki_filp
;
2196 struct address_space
*mapping
= file
->f_mapping
;
2197 struct inode
*inode
= mapping
->host
;
2199 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2202 BUG_ON(iocb
->ki_pos
!= pos
);
2204 down(&inode
->i_sem
);
2205 ret
= ntfs_file_aio_write_nolock(iocb
, &local_iov
, 1, &iocb
->ki_pos
);
2207 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2208 int err
= sync_page_range(inode
, mapping
, pos
, ret
);
2216 * ntfs_file_writev -
2218 * Basically the same as generic_file_writev() except that it ends up calling
2219 * ntfs_file_aio_write_nolock() instead of __generic_file_aio_write_nolock().
2221 static ssize_t
ntfs_file_writev(struct file
*file
, const struct iovec
*iov
,
2222 unsigned long nr_segs
, loff_t
*ppos
)
2224 struct address_space
*mapping
= file
->f_mapping
;
2225 struct inode
*inode
= mapping
->host
;
2229 down(&inode
->i_sem
);
2230 init_sync_kiocb(&kiocb
, file
);
2231 ret
= ntfs_file_aio_write_nolock(&kiocb
, iov
, nr_segs
, ppos
);
2232 if (ret
== -EIOCBQUEUED
)
2233 ret
= wait_on_sync_kiocb(&kiocb
);
2235 if (ret
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
2236 int err
= sync_page_range(inode
, mapping
, *ppos
- ret
, ret
);
2244 * ntfs_file_write - simple wrapper for ntfs_file_writev()
2246 static ssize_t
ntfs_file_write(struct file
*file
, const char __user
*buf
,
2247 size_t count
, loff_t
*ppos
)
2249 struct iovec local_iov
= { .iov_base
= (void __user
*)buf
,
2252 return ntfs_file_writev(file
, &local_iov
, 1, ppos
);
2256 * ntfs_file_fsync - sync a file to disk
2257 * @filp: file to be synced
2258 * @dentry: dentry describing the file to sync
2259 * @datasync: if non-zero only flush user data and not metadata
2261 * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync
2262 * system calls. This function is inspired by fs/buffer.c::file_fsync().
2264 * If @datasync is false, write the mft record and all associated extent mft
2265 * records as well as the $DATA attribute and then sync the block device.
2267 * If @datasync is true and the attribute is non-resident, we skip the writing
2268 * of the mft record and all associated extent mft records (this might still
2269 * happen due to the write_inode_now() call).
2271 * Also, if @datasync is true, we do not wait on the inode to be written out
2272 * but we always wait on the page cache pages to be written out.
2274 * Note: In the past @filp could be NULL so we ignore it as we don't need it
2277 * Locking: Caller must hold i_sem on the inode.
2279 * TODO: We should probably also write all attribute/index inodes associated
2280 * with this inode but since we have no simple way of getting to them we ignore
2281 * this problem for now.
2283 static int ntfs_file_fsync(struct file
*filp
, struct dentry
*dentry
,
2286 struct inode
*vi
= dentry
->d_inode
;
2289 ntfs_debug("Entering for inode 0x%lx.", vi
->i_ino
);
2290 BUG_ON(S_ISDIR(vi
->i_mode
));
2291 if (!datasync
|| !NInoNonResident(NTFS_I(vi
)))
2292 ret
= ntfs_write_inode(vi
, 1);
2293 write_inode_now(vi
, !datasync
);
2295 * NOTE: If we were to use mapping->private_list (see ext2 and
2296 * fs/buffer.c) for dirty blocks then we could optimize the below to be
2297 * sync_mapping_buffers(vi->i_mapping).
2299 err
= sync_blockdev(vi
->i_sb
->s_bdev
);
2300 if (unlikely(err
&& !ret
))
2303 ntfs_debug("Done.");
2305 ntfs_warning(vi
->i_sb
, "Failed to f%ssync inode 0x%lx. Error "
2306 "%u.", datasync
? "data" : "", vi
->i_ino
, -ret
);
2310 #endif /* NTFS_RW */
2312 struct file_operations ntfs_file_ops
= {
2313 .llseek
= generic_file_llseek
, /* Seek inside file. */
2314 .read
= generic_file_read
, /* Read from file. */
2315 .aio_read
= generic_file_aio_read
, /* Async read from file. */
2316 .readv
= generic_file_readv
, /* Read from file. */
2318 .write
= ntfs_file_write
, /* Write to file. */
2319 .aio_write
= ntfs_file_aio_write
, /* Async write to file. */
2320 .writev
= ntfs_file_writev
, /* Write to file. */
2321 /*.release = ,*/ /* Last file is closed. See
2323 ext2_release_file() for
2324 how to use this to discard
2325 preallocated space for
2326 write opened files. */
2327 .fsync
= ntfs_file_fsync
, /* Sync a file to disk. */
2328 /*.aio_fsync = ,*/ /* Sync all outstanding async
2331 #endif /* NTFS_RW */
2332 /*.ioctl = ,*/ /* Perform function on the
2333 mounted filesystem. */
2334 .mmap
= generic_file_mmap
, /* Mmap file. */
2335 .open
= ntfs_file_open
, /* Open file. */
2336 .sendfile
= generic_file_sendfile
, /* Zero-copy data send with
2337 the data source being on
2338 the ntfs partition. We do
2339 not need to care about the
2340 data destination. */
2341 /*.sendpage = ,*/ /* Zero-copy data send with
2342 the data destination being
2343 on the ntfs partition. We
2344 do not need to care about
2348 struct inode_operations ntfs_file_inode_ops
= {
2350 .truncate
= ntfs_truncate_vfs
,
2351 .setattr
= ntfs_setattr
,
2352 #endif /* NTFS_RW */
2355 struct file_operations ntfs_empty_file_ops
= {};
2357 struct inode_operations ntfs_empty_inode_ops
= {};