1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * aops.c - NTFS kernel address space operations and page cache handling.
5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
6 * Copyright (c) 2002 Richard Russon
9 #include <linux/errno.h>
11 #include <linux/gfp.h>
13 #include <linux/pagemap.h>
14 #include <linux/swap.h>
15 #include <linux/buffer_head.h>
16 #include <linux/writeback.h>
17 #include <linux/bit_spinlock.h>
18 #include <linux/bio.h>
30 * ntfs_end_buffer_async_read - async io completion for reading attributes
31 * @bh: buffer head on which io is completed
32 * @uptodate: whether @bh is now uptodate or not
34 * Asynchronous I/O completion handler for reading pages belonging to the
35 * attribute address space of an inode. The inodes can either be files or
36 * directories or they can be fake inodes describing some attribute.
38 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
39 * page has been completed and mark the page uptodate or set the error bit on
40 * the page. To determine the size of the records that need fixing up, we
41 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
42 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
45 static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
48 struct buffer_head *first, *tmp;
52 int page_uptodate = 1;
55 vi = page->mapping->host;
58 if (likely(uptodate)) {
60 s64 file_ofs, init_size;
62 set_buffer_uptodate(bh);
64 file_ofs = ((s64)page->index << PAGE_SHIFT) +
66 read_lock_irqsave(&ni->size_lock, flags);
67 init_size = ni->initialized_size;
68 i_size = i_size_read(vi);
69 read_unlock_irqrestore(&ni->size_lock, flags);
70 if (unlikely(init_size > i_size)) {
71 /* Race with shrinking truncate. */
74 /* Check for the current buffer head overflowing. */
75 if (unlikely(file_ofs + bh->b_size > init_size)) {
80 if (file_ofs < init_size)
81 ofs = init_size - file_ofs;
82 kaddr = kmap_atomic(page);
83 memset(kaddr + bh_offset(bh) + ofs, 0,
85 flush_dcache_page(page);
89 clear_buffer_uptodate(bh);
91 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
92 "0x%llx.", (unsigned long long)bh->b_blocknr);
94 first = page_buffers(page);
95 spin_lock_irqsave(&first->b_uptodate_lock, flags);
96 clear_buffer_async_read(bh);
100 if (!buffer_uptodate(tmp))
102 if (buffer_async_read(tmp)) {
103 if (likely(buffer_locked(tmp)))
105 /* Async buffers must be locked. */
108 tmp = tmp->b_this_page;
110 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
112 * If none of the buffers had errors then we can set the page uptodate,
113 * but we first have to perform the post read mst fixups, if the
114 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
115 * Note we ignore fixup errors as those are detected when
116 * map_mft_record() is called which gives us per record granularity
117 * rather than per page granularity.
119 if (!NInoMstProtected(ni)) {
120 if (likely(page_uptodate && !PageError(page)))
121 SetPageUptodate(page);
124 unsigned int i, recs;
127 rec_size = ni->itype.index.block_size;
128 recs = PAGE_SIZE / rec_size;
129 /* Should have been verified before we got here... */
131 kaddr = kmap_atomic(page);
132 for (i = 0; i < recs; i++)
133 post_read_mst_fixup((NTFS_RECORD*)(kaddr +
134 i * rec_size), rec_size);
135 kunmap_atomic(kaddr);
136 flush_dcache_page(page);
137 if (likely(page_uptodate && !PageError(page)))
138 SetPageUptodate(page);
143 spin_unlock_irqrestore(&first->b_uptodate_lock, flags);
148 * ntfs_read_block - fill a @page of an address space with data
149 * @page: page cache page to fill with data
151 * Fill the page @page of the address space belonging to the @page->host inode.
152 * We read each buffer asynchronously and when all buffers are read in, our io
153 * completion handler ntfs_end_buffer_read_async(), if required, automatically
154 * applies the mst fixups to the page before finally marking it uptodate and
157 * We only enforce allocated_size limit because i_size is checked for in
158 * generic_file_read().
160 * Return 0 on success and -errno on error.
162 * Contains an adapted version of fs/buffer.c::block_read_full_page().
164 static int ntfs_read_block(struct page *page)
174 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
175 sector_t iblock, lblock, zblock;
177 unsigned int blocksize, vcn_ofs;
179 unsigned char blocksize_bits;
181 vi = page->mapping->host;
185 /* $MFT/$DATA must have its complete runlist in memory at all times. */
186 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
188 blocksize = vol->sb->s_blocksize;
189 blocksize_bits = vol->sb->s_blocksize_bits;
191 if (!page_has_buffers(page)) {
192 create_empty_buffers(page, blocksize, 0);
193 if (unlikely(!page_has_buffers(page))) {
198 bh = head = page_buffers(page);
202 * We may be racing with truncate. To avoid some of the problems we
203 * now take a snapshot of the various sizes and use those for the whole
204 * of the function. In case of an extending truncate it just means we
205 * may leave some buffers unmapped which are now allocated. This is
206 * not a problem since these buffers will just get mapped when a write
207 * occurs. In case of a shrinking truncate, we will detect this later
208 * on due to the runlist being incomplete and if the page is being
209 * fully truncated, truncate will throw it away as soon as we unlock
210 * it so no need to worry what we do with it.
212 iblock = (s64)page->index << (PAGE_SHIFT - blocksize_bits);
213 read_lock_irqsave(&ni->size_lock, flags);
214 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
215 init_size = ni->initialized_size;
216 i_size = i_size_read(vi);
217 read_unlock_irqrestore(&ni->size_lock, flags);
218 if (unlikely(init_size > i_size)) {
219 /* Race with shrinking truncate. */
222 zblock = (init_size + blocksize - 1) >> blocksize_bits;
224 /* Loop through all the buffers in the page. */
230 if (unlikely(buffer_uptodate(bh)))
232 if (unlikely(buffer_mapped(bh))) {
236 bh->b_bdev = vol->sb->s_bdev;
237 /* Is the block within the allowed limits? */
238 if (iblock < lblock) {
239 bool is_retry = false;
241 /* Convert iblock into corresponding vcn and offset. */
242 vcn = (VCN)iblock << blocksize_bits >>
243 vol->cluster_size_bits;
244 vcn_ofs = ((VCN)iblock << blocksize_bits) &
245 vol->cluster_size_mask;
248 down_read(&ni->runlist.lock);
251 if (likely(rl != NULL)) {
252 /* Seek to element containing target vcn. */
253 while (rl->length && rl[1].vcn <= vcn)
255 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
257 lcn = LCN_RL_NOT_MAPPED;
258 /* Successful remap. */
260 /* Setup buffer head to correct block. */
261 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
262 + vcn_ofs) >> blocksize_bits;
263 set_buffer_mapped(bh);
264 /* Only read initialized data blocks. */
265 if (iblock < zblock) {
269 /* Fully non-initialized data block, zero it. */
272 /* It is a hole, need to zero it. */
275 /* If first try and runlist unmapped, map and retry. */
276 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
279 * Attempt to map runlist, dropping lock for
282 up_read(&ni->runlist.lock);
283 err = ntfs_map_runlist(ni, vcn);
285 goto lock_retry_remap;
288 up_read(&ni->runlist.lock);
290 * If buffer is outside the runlist, treat it as a
291 * hole. This can happen due to concurrent truncate
294 if (err == -ENOENT || lcn == LCN_ENOENT) {
298 /* Hard error, zero out region. */
303 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
304 "attribute type 0x%x, vcn 0x%llx, "
305 "offset 0x%x because its location on "
306 "disk could not be determined%s "
307 "(error code %i).", ni->mft_no,
308 ni->type, (unsigned long long)vcn,
309 vcn_ofs, is_retry ? " even after "
310 "retrying" : "", err);
313 * Either iblock was outside lblock limits or
314 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
315 * of the page and set the buffer uptodate.
318 bh->b_blocknr = -1UL;
319 clear_buffer_mapped(bh);
321 zero_user(page, i * blocksize, blocksize);
323 set_buffer_uptodate(bh);
324 } while (i++, iblock++, (bh = bh->b_this_page) != head);
326 /* Release the lock if we took it. */
328 up_read(&ni->runlist.lock);
330 /* Check we have at least one buffer ready for i/o. */
332 struct buffer_head *tbh;
334 /* Lock the buffers. */
335 for (i = 0; i < nr; i++) {
338 tbh->b_end_io = ntfs_end_buffer_async_read;
339 set_buffer_async_read(tbh);
341 /* Finally, start i/o on the buffers. */
342 for (i = 0; i < nr; i++) {
344 if (likely(!buffer_uptodate(tbh)))
345 submit_bh(REQ_OP_READ, 0, tbh);
347 ntfs_end_buffer_async_read(tbh, 1);
351 /* No i/o was scheduled on any of the buffers. */
352 if (likely(!PageError(page)))
353 SetPageUptodate(page);
354 else /* Signal synchronous i/o error. */
361 * ntfs_readpage - fill a @page of a @file with data from the device
362 * @file: open file to which the page @page belongs or NULL
363 * @page: page cache page to fill with data
365 * For non-resident attributes, ntfs_readpage() fills the @page of the open
366 * file @file by calling the ntfs version of the generic block_read_full_page()
367 * function, ntfs_read_block(), which in turn creates and reads in the buffers
368 * associated with the page asynchronously.
370 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
371 * data from the mft record (which at this stage is most likely in memory) and
372 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
373 * even if the mft record is not cached at this point in time, we need to wait
374 * for it to be read in before we can do the copy.
376 * Return 0 on success and -errno on error.
378 static int ntfs_readpage(struct file *file, struct page *page)
382 ntfs_inode *ni, *base_ni;
384 ntfs_attr_search_ctx *ctx;
391 BUG_ON(!PageLocked(page));
392 vi = page->mapping->host;
393 i_size = i_size_read(vi);
394 /* Is the page fully outside i_size? (truncate in progress) */
395 if (unlikely(page->index >= (i_size + PAGE_SIZE - 1) >>
397 zero_user(page, 0, PAGE_SIZE);
398 ntfs_debug("Read outside i_size - truncated?");
402 * This can potentially happen because we clear PageUptodate() during
403 * ntfs_writepage() of MstProtected() attributes.
405 if (PageUptodate(page)) {
411 * Only $DATA attributes can be encrypted and only unnamed $DATA
412 * attributes can be compressed. Index root can have the flags set but
413 * this means to create compressed/encrypted files, not that the
414 * attribute is compressed/encrypted. Note we need to check for
415 * AT_INDEX_ALLOCATION since this is the type of both directory and
418 if (ni->type != AT_INDEX_ALLOCATION) {
419 /* If attribute is encrypted, deny access, just like NT4. */
420 if (NInoEncrypted(ni)) {
421 BUG_ON(ni->type != AT_DATA);
425 /* Compressed data streams are handled in compress.c. */
426 if (NInoNonResident(ni) && NInoCompressed(ni)) {
427 BUG_ON(ni->type != AT_DATA);
428 BUG_ON(ni->name_len);
429 return ntfs_read_compressed_block(page);
432 /* NInoNonResident() == NInoIndexAllocPresent() */
433 if (NInoNonResident(ni)) {
434 /* Normal, non-resident data stream. */
435 return ntfs_read_block(page);
438 * Attribute is resident, implying it is not compressed or encrypted.
439 * This also means the attribute is smaller than an mft record and
440 * hence smaller than a page, so can simply zero out any pages with
441 * index above 0. Note the attribute can actually be marked compressed
442 * but if it is resident the actual data is not compressed so we are
443 * ok to ignore the compressed flag here.
445 if (unlikely(page->index > 0)) {
446 zero_user(page, 0, PAGE_SIZE);
452 base_ni = ni->ext.base_ntfs_ino;
453 /* Map, pin, and lock the mft record. */
454 mrec = map_mft_record(base_ni);
460 * If a parallel write made the attribute non-resident, drop the mft
461 * record and retry the readpage.
463 if (unlikely(NInoNonResident(ni))) {
464 unmap_mft_record(base_ni);
467 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
468 if (unlikely(!ctx)) {
472 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
473 CASE_SENSITIVE, 0, NULL, 0, ctx);
475 goto put_unm_err_out;
476 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
477 read_lock_irqsave(&ni->size_lock, flags);
478 if (unlikely(attr_len > ni->initialized_size))
479 attr_len = ni->initialized_size;
480 i_size = i_size_read(vi);
481 read_unlock_irqrestore(&ni->size_lock, flags);
482 if (unlikely(attr_len > i_size)) {
483 /* Race with shrinking truncate. */
486 addr = kmap_atomic(page);
487 /* Copy the data to the page. */
488 memcpy(addr, (u8*)ctx->attr +
489 le16_to_cpu(ctx->attr->data.resident.value_offset),
491 /* Zero the remainder of the page. */
492 memset(addr + attr_len, 0, PAGE_SIZE - attr_len);
493 flush_dcache_page(page);
496 ntfs_attr_put_search_ctx(ctx);
498 unmap_mft_record(base_ni);
500 SetPageUptodate(page);
509 * ntfs_write_block - write a @page to the backing store
510 * @page: page cache page to write out
511 * @wbc: writeback control structure
513 * This function is for writing pages belonging to non-resident, non-mst
514 * protected attributes to their backing store.
516 * For a page with buffers, map and write the dirty buffers asynchronously
517 * under page writeback. For a page without buffers, create buffers for the
518 * page, then proceed as above.
520 * If a page doesn't have buffers the page dirty state is definitive. If a page
521 * does have buffers, the page dirty state is just a hint, and the buffer dirty
522 * state is definitive. (A hint which has rules: dirty buffers against a clean
523 * page is illegal. Other combinations are legal and need to be handled. In
524 * particular a dirty page containing clean buffers for example.)
526 * Return 0 on success and -errno on error.
528 * Based on ntfs_read_block() and __block_write_full_page().
530 static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
534 s64 initialized_size;
536 sector_t block, dblock, iblock;
541 struct buffer_head *bh, *head;
543 unsigned int blocksize, vcn_ofs;
545 bool need_end_writeback;
546 unsigned char blocksize_bits;
548 vi = page->mapping->host;
552 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
553 "0x%lx.", ni->mft_no, ni->type, page->index);
555 BUG_ON(!NInoNonResident(ni));
556 BUG_ON(NInoMstProtected(ni));
557 blocksize = vol->sb->s_blocksize;
558 blocksize_bits = vol->sb->s_blocksize_bits;
559 if (!page_has_buffers(page)) {
560 BUG_ON(!PageUptodate(page));
561 create_empty_buffers(page, blocksize,
562 (1 << BH_Uptodate) | (1 << BH_Dirty));
563 if (unlikely(!page_has_buffers(page))) {
564 ntfs_warning(vol->sb, "Error allocating page "
565 "buffers. Redirtying page so we try "
568 * Put the page back on mapping->dirty_pages, but leave
569 * its buffers' dirty state as-is.
571 redirty_page_for_writepage(wbc, page);
576 bh = head = page_buffers(page);
579 /* NOTE: Different naming scheme to ntfs_read_block()! */
581 /* The first block in the page. */
582 block = (s64)page->index << (PAGE_SHIFT - blocksize_bits);
584 read_lock_irqsave(&ni->size_lock, flags);
585 i_size = i_size_read(vi);
586 initialized_size = ni->initialized_size;
587 read_unlock_irqrestore(&ni->size_lock, flags);
589 /* The first out of bounds block for the data size. */
590 dblock = (i_size + blocksize - 1) >> blocksize_bits;
592 /* The last (fully or partially) initialized block. */
593 iblock = initialized_size >> blocksize_bits;
596 * Be very careful. We have no exclusion from block_dirty_folio
597 * here, and the (potentially unmapped) buffers may become dirty at
598 * any time. If a buffer becomes dirty here after we've inspected it
599 * then we just miss that fact, and the page stays dirty.
601 * Buffers outside i_size may be dirtied by block_dirty_folio;
602 * handle that here by just cleaning them.
606 * Loop through all the buffers in the page, mapping all the dirty
607 * buffers to disk addresses and handling any aliases from the
608 * underlying block device's mapping.
613 bool is_retry = false;
615 if (unlikely(block >= dblock)) {
617 * Mapped buffers outside i_size will occur, because
618 * this page can be outside i_size when there is a
619 * truncate in progress. The contents of such buffers
620 * were zeroed by ntfs_writepage().
622 * FIXME: What about the small race window where
623 * ntfs_writepage() has not done any clearing because
624 * the page was within i_size but before we get here,
625 * vmtruncate() modifies i_size?
627 clear_buffer_dirty(bh);
628 set_buffer_uptodate(bh);
632 /* Clean buffers are not written out, so no need to map them. */
633 if (!buffer_dirty(bh))
636 /* Make sure we have enough initialized size. */
637 if (unlikely((block >= iblock) &&
638 (initialized_size < i_size))) {
640 * If this page is fully outside initialized size, zero
641 * out all pages between the current initialized size
642 * and the current page. Just use ntfs_readpage() to do
643 * the zeroing transparently.
645 if (block > iblock) {
648 // - read_cache_page()
649 // Again for each page do:
650 // - wait_on_page_locked()
651 // - Check (PageUptodate(page) &&
653 // Update initialized size in the attribute and
655 // Again, for each page do:
656 // block_dirty_folio();
658 // We don't need to wait on the writes.
662 * The current page straddles initialized size. Zero
663 * all non-uptodate buffers and set them uptodate (and
664 * dirty?). Note, there aren't any non-uptodate buffers
665 * if the page is uptodate.
666 * FIXME: For an uptodate page, the buffers may need to
667 * be written out because they were not initialized on
670 if (!PageUptodate(page)) {
672 // Zero any non-uptodate buffers up to i_size.
673 // Set them uptodate and dirty.
676 // Update initialized size in the attribute and in the
677 // inode (up to i_size).
679 // FIXME: This is inefficient. Try to batch the two
680 // size changes to happen in one go.
681 ntfs_error(vol->sb, "Writing beyond initialized size "
682 "is not supported yet. Sorry.");
685 // Do NOT set_buffer_new() BUT DO clear buffer range
686 // outside write request range.
687 // set_buffer_uptodate() on complete buffers as well as
688 // set_buffer_dirty().
691 /* No need to map buffers that are already mapped. */
692 if (buffer_mapped(bh))
695 /* Unmapped, dirty buffer. Need to map it. */
696 bh->b_bdev = vol->sb->s_bdev;
698 /* Convert block into corresponding vcn and offset. */
699 vcn = (VCN)block << blocksize_bits;
700 vcn_ofs = vcn & vol->cluster_size_mask;
701 vcn >>= vol->cluster_size_bits;
704 down_read(&ni->runlist.lock);
707 if (likely(rl != NULL)) {
708 /* Seek to element containing target vcn. */
709 while (rl->length && rl[1].vcn <= vcn)
711 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
713 lcn = LCN_RL_NOT_MAPPED;
714 /* Successful remap. */
716 /* Setup buffer head to point to correct block. */
717 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
718 vcn_ofs) >> blocksize_bits;
719 set_buffer_mapped(bh);
722 /* It is a hole, need to instantiate it. */
723 if (lcn == LCN_HOLE) {
725 unsigned long *bpos, *bend;
727 /* Check if the buffer is zero. */
728 kaddr = kmap_atomic(page);
729 bpos = (unsigned long *)(kaddr + bh_offset(bh));
730 bend = (unsigned long *)((u8*)bpos + blocksize);
734 } while (likely(++bpos < bend));
735 kunmap_atomic(kaddr);
738 * Buffer is zero and sparse, no need to write
742 clear_buffer_dirty(bh);
745 // TODO: Instantiate the hole.
746 // clear_buffer_new(bh);
747 // clean_bdev_bh_alias(bh);
748 ntfs_error(vol->sb, "Writing into sparse regions is "
749 "not supported yet. Sorry.");
753 /* If first try and runlist unmapped, map and retry. */
754 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
757 * Attempt to map runlist, dropping lock for
760 up_read(&ni->runlist.lock);
761 err = ntfs_map_runlist(ni, vcn);
763 goto lock_retry_remap;
766 up_read(&ni->runlist.lock);
768 * If buffer is outside the runlist, truncate has cut it out
769 * of the runlist. Just clean and clear the buffer and set it
770 * uptodate so it can get discarded by the VM.
772 if (err == -ENOENT || lcn == LCN_ENOENT) {
774 clear_buffer_dirty(bh);
775 zero_user(page, bh_offset(bh), blocksize);
776 set_buffer_uptodate(bh);
780 /* Failed to map the buffer, even after retrying. */
784 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
785 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
786 "because its location on disk could not be "
787 "determined%s (error code %i).", ni->mft_no,
788 ni->type, (unsigned long long)vcn,
789 vcn_ofs, is_retry ? " even after "
790 "retrying" : "", err);
792 } while (block++, (bh = bh->b_this_page) != head);
794 /* Release the lock if we took it. */
796 up_read(&ni->runlist.lock);
798 /* For the error case, need to reset bh to the beginning. */
801 /* Just an optimization, so ->readpage() is not called later. */
802 if (unlikely(!PageUptodate(page))) {
805 if (!buffer_uptodate(bh)) {
810 } while ((bh = bh->b_this_page) != head);
812 SetPageUptodate(page);
815 /* Setup all mapped, dirty buffers for async write i/o. */
817 if (buffer_mapped(bh) && buffer_dirty(bh)) {
819 if (test_clear_buffer_dirty(bh)) {
820 BUG_ON(!buffer_uptodate(bh));
821 mark_buffer_async_write(bh);
824 } else if (unlikely(err)) {
826 * For the error case. The buffer may have been set
827 * dirty during attachment to a dirty page.
830 clear_buffer_dirty(bh);
832 } while ((bh = bh->b_this_page) != head);
835 // TODO: Remove the -EOPNOTSUPP check later on...
836 if (unlikely(err == -EOPNOTSUPP))
838 else if (err == -ENOMEM) {
839 ntfs_warning(vol->sb, "Error allocating memory. "
840 "Redirtying page so we try again "
843 * Put the page back on mapping->dirty_pages, but
844 * leave its buffer's dirty state as-is.
846 redirty_page_for_writepage(wbc, page);
852 BUG_ON(PageWriteback(page));
853 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
855 /* Submit the prepared buffers for i/o. */
856 need_end_writeback = true;
858 struct buffer_head *next = bh->b_this_page;
859 if (buffer_async_write(bh)) {
860 submit_bh(REQ_OP_WRITE, 0, bh);
861 need_end_writeback = false;
864 } while (bh != head);
867 /* If no i/o was started, need to end_page_writeback(). */
868 if (unlikely(need_end_writeback))
869 end_page_writeback(page);
876 * ntfs_write_mst_block - write a @page to the backing store
877 * @page: page cache page to write out
878 * @wbc: writeback control structure
880 * This function is for writing pages belonging to non-resident, mst protected
881 * attributes to their backing store. The only supported attributes are index
882 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
883 * supported for the index allocation case.
885 * The page must remain locked for the duration of the write because we apply
886 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
887 * page before undoing the fixups, any other user of the page will see the
888 * page contents as corrupt.
890 * We clear the page uptodate flag for the duration of the function to ensure
891 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
892 * are about to apply the mst fixups to.
894 * Return 0 on success and -errno on error.
896 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
897 * write_mft_record_nolock().
899 static int ntfs_write_mst_block(struct page *page,
900 struct writeback_control *wbc)
902 sector_t block, dblock, rec_block;
903 struct inode *vi = page->mapping->host;
904 ntfs_inode *ni = NTFS_I(vi);
905 ntfs_volume *vol = ni->vol;
907 unsigned int rec_size = ni->itype.index.block_size;
908 ntfs_inode *locked_nis[PAGE_SIZE / NTFS_BLOCK_SIZE];
909 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
910 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
912 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
913 unsigned bh_size, rec_size_bits;
914 bool sync, is_mft, page_is_dirty, rec_is_dirty;
915 unsigned char bh_size_bits;
917 if (WARN_ON(rec_size < NTFS_BLOCK_SIZE))
920 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
921 "0x%lx.", vi->i_ino, ni->type, page->index);
922 BUG_ON(!NInoNonResident(ni));
923 BUG_ON(!NInoMstProtected(ni));
924 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
926 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
927 * in its page cache were to be marked dirty. However this should
928 * never happen with the current driver and considering we do not
929 * handle this case here we do want to BUG(), at least for now.
931 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
932 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
933 bh_size = vol->sb->s_blocksize;
934 bh_size_bits = vol->sb->s_blocksize_bits;
935 max_bhs = PAGE_SIZE / bh_size;
937 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
939 /* Were we called for sync purposes? */
940 sync = (wbc->sync_mode == WB_SYNC_ALL);
942 /* Make sure we have mapped buffers. */
943 bh = head = page_buffers(page);
946 rec_size_bits = ni->itype.index.block_size_bits;
947 BUG_ON(!(PAGE_SIZE >> rec_size_bits));
948 bhs_per_rec = rec_size >> bh_size_bits;
949 BUG_ON(!bhs_per_rec);
951 /* The first block in the page. */
952 rec_block = block = (sector_t)page->index <<
953 (PAGE_SHIFT - bh_size_bits);
955 /* The first out of bounds block for the data size. */
956 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
959 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
960 page_is_dirty = rec_is_dirty = false;
963 bool is_retry = false;
965 if (likely(block < rec_block)) {
966 if (unlikely(block >= dblock)) {
967 clear_buffer_dirty(bh);
968 set_buffer_uptodate(bh);
972 * This block is not the first one in the record. We
973 * ignore the buffer's dirty state because we could
974 * have raced with a parallel mark_ntfs_record_dirty().
978 if (unlikely(err2)) {
980 clear_buffer_dirty(bh);
983 } else /* if (block == rec_block) */ {
984 BUG_ON(block > rec_block);
985 /* This block is the first one in the record. */
986 rec_block += bhs_per_rec;
988 if (unlikely(block >= dblock)) {
989 clear_buffer_dirty(bh);
992 if (!buffer_dirty(bh)) {
993 /* Clean records are not written out. */
994 rec_is_dirty = false;
1000 /* Need to map the buffer if it is not mapped already. */
1001 if (unlikely(!buffer_mapped(bh))) {
1004 unsigned int vcn_ofs;
1006 bh->b_bdev = vol->sb->s_bdev;
1007 /* Obtain the vcn and offset of the current block. */
1008 vcn = (VCN)block << bh_size_bits;
1009 vcn_ofs = vcn & vol->cluster_size_mask;
1010 vcn >>= vol->cluster_size_bits;
1013 down_read(&ni->runlist.lock);
1014 rl = ni->runlist.rl;
1016 if (likely(rl != NULL)) {
1017 /* Seek to element containing target vcn. */
1018 while (rl->length && rl[1].vcn <= vcn)
1020 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1022 lcn = LCN_RL_NOT_MAPPED;
1023 /* Successful remap. */
1024 if (likely(lcn >= 0)) {
1025 /* Setup buffer head to correct block. */
1026 bh->b_blocknr = ((lcn <<
1027 vol->cluster_size_bits) +
1028 vcn_ofs) >> bh_size_bits;
1029 set_buffer_mapped(bh);
1032 * Remap failed. Retry to map the runlist once
1033 * unless we are working on $MFT which always
1034 * has the whole of its runlist in memory.
1036 if (!is_mft && !is_retry &&
1037 lcn == LCN_RL_NOT_MAPPED) {
1040 * Attempt to map runlist, dropping
1041 * lock for the duration.
1043 up_read(&ni->runlist.lock);
1044 err2 = ntfs_map_runlist(ni, vcn);
1046 goto lock_retry_remap;
1047 if (err2 == -ENOMEM)
1048 page_is_dirty = true;
1053 up_read(&ni->runlist.lock);
1055 /* Hard error. Abort writing this record. */
1056 if (!err || err == -ENOMEM)
1059 ntfs_error(vol->sb, "Cannot write ntfs record "
1060 "0x%llx (inode 0x%lx, "
1061 "attribute type 0x%x) because "
1062 "its location on disk could "
1063 "not be determined (error "
1067 vol->mft_record_size_bits,
1068 ni->mft_no, ni->type,
1071 * If this is not the first buffer, remove the
1072 * buffers in this record from the list of
1073 * buffers to write and clear their dirty bit
1074 * if not error -ENOMEM.
1076 if (rec_start_bh != bh) {
1077 while (bhs[--nr_bhs] != rec_start_bh)
1079 if (err2 != -ENOMEM) {
1083 } while ((rec_start_bh =
1092 BUG_ON(!buffer_uptodate(bh));
1093 BUG_ON(nr_bhs >= max_bhs);
1095 } while (block++, (bh = bh->b_this_page) != head);
1097 up_read(&ni->runlist.lock);
1098 /* If there were no dirty buffers, we are done. */
1101 /* Map the page so we can access its contents. */
1103 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1104 BUG_ON(!PageUptodate(page));
1105 ClearPageUptodate(page);
1106 for (i = 0; i < nr_bhs; i++) {
1109 /* Skip buffers which are not at the beginning of records. */
1110 if (i % bhs_per_rec)
1113 ofs = bh_offset(tbh);
1116 unsigned long mft_no;
1118 /* Get the mft record number. */
1119 mft_no = (((s64)page->index << PAGE_SHIFT) + ofs)
1121 /* Check whether to write this mft record. */
1123 if (!ntfs_may_write_mft_record(vol, mft_no,
1124 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1126 * The record should not be written. This
1127 * means we need to redirty the page before
1130 page_is_dirty = true;
1132 * Remove the buffers in this mft record from
1133 * the list of buffers to write.
1137 } while (++i % bhs_per_rec);
1141 * The record should be written. If a locked ntfs
1142 * inode was returned, add it to the array of locked
1146 locked_nis[nr_locked_nis++] = tni;
1148 /* Apply the mst protection fixups. */
1149 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1151 if (unlikely(err2)) {
1152 if (!err || err == -ENOMEM)
1154 ntfs_error(vol->sb, "Failed to apply mst fixups "
1155 "(inode 0x%lx, attribute type 0x%x, "
1156 "page index 0x%lx, page offset 0x%x)!"
1157 " Unmount and run chkdsk.", vi->i_ino,
1158 ni->type, page->index, ofs);
1160 * Mark all the buffers in this record clean as we do
1161 * not want to write corrupt data to disk.
1164 clear_buffer_dirty(bhs[i]);
1166 } while (++i % bhs_per_rec);
1171 /* If no records are to be written out, we are done. */
1174 flush_dcache_page(page);
1175 /* Lock buffers and start synchronous write i/o on them. */
1176 for (i = 0; i < nr_bhs; i++) {
1180 if (!trylock_buffer(tbh))
1182 /* The buffer dirty state is now irrelevant, just clean it. */
1183 clear_buffer_dirty(tbh);
1184 BUG_ON(!buffer_uptodate(tbh));
1185 BUG_ON(!buffer_mapped(tbh));
1187 tbh->b_end_io = end_buffer_write_sync;
1188 submit_bh(REQ_OP_WRITE, 0, tbh);
1190 /* Synchronize the mft mirror now if not @sync. */
1191 if (is_mft && !sync)
1194 /* Wait on i/o completion of buffers. */
1195 for (i = 0; i < nr_bhs; i++) {
1199 wait_on_buffer(tbh);
1200 if (unlikely(!buffer_uptodate(tbh))) {
1201 ntfs_error(vol->sb, "I/O error while writing ntfs "
1202 "record buffer (inode 0x%lx, "
1203 "attribute type 0x%x, page index "
1204 "0x%lx, page offset 0x%lx)! Unmount "
1205 "and run chkdsk.", vi->i_ino, ni->type,
1206 page->index, bh_offset(tbh));
1207 if (!err || err == -ENOMEM)
1210 * Set the buffer uptodate so the page and buffer
1211 * states do not become out of sync.
1213 set_buffer_uptodate(tbh);
1216 /* If @sync, now synchronize the mft mirror. */
1217 if (is_mft && sync) {
1219 for (i = 0; i < nr_bhs; i++) {
1220 unsigned long mft_no;
1224 * Skip buffers which are not at the beginning of
1227 if (i % bhs_per_rec)
1230 /* Skip removed buffers (and hence records). */
1233 ofs = bh_offset(tbh);
1234 /* Get the mft record number. */
1235 mft_no = (((s64)page->index << PAGE_SHIFT) + ofs)
1237 if (mft_no < vol->mftmirr_size)
1238 ntfs_sync_mft_mirror(vol, mft_no,
1239 (MFT_RECORD*)(kaddr + ofs),
1245 /* Remove the mst protection fixups again. */
1246 for (i = 0; i < nr_bhs; i++) {
1247 if (!(i % bhs_per_rec)) {
1251 post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1255 flush_dcache_page(page);
1257 /* Unlock any locked inodes. */
1258 while (nr_locked_nis-- > 0) {
1259 ntfs_inode *tni, *base_tni;
1261 tni = locked_nis[nr_locked_nis];
1262 /* Get the base inode. */
1263 mutex_lock(&tni->extent_lock);
1264 if (tni->nr_extents >= 0)
1267 base_tni = tni->ext.base_ntfs_ino;
1270 mutex_unlock(&tni->extent_lock);
1271 ntfs_debug("Unlocking %s inode 0x%lx.",
1272 tni == base_tni ? "base" : "extent",
1274 mutex_unlock(&tni->mrec_lock);
1275 atomic_dec(&tni->count);
1276 iput(VFS_I(base_tni));
1278 SetPageUptodate(page);
1281 if (unlikely(err && err != -ENOMEM)) {
1283 * Set page error if there is only one ntfs record in the page.
1284 * Otherwise we would loose per-record granularity.
1286 if (ni->itype.index.block_size == PAGE_SIZE)
1290 if (page_is_dirty) {
1291 ntfs_debug("Page still contains one or more dirty ntfs "
1292 "records. Redirtying the page starting at "
1293 "record 0x%lx.", page->index <<
1294 (PAGE_SHIFT - rec_size_bits));
1295 redirty_page_for_writepage(wbc, page);
1299 * Keep the VM happy. This must be done otherwise the
1300 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1301 * the page is clean.
1303 BUG_ON(PageWriteback(page));
1304 set_page_writeback(page);
1306 end_page_writeback(page);
1309 ntfs_debug("Done.");
1314 * ntfs_writepage - write a @page to the backing store
1315 * @page: page cache page to write out
1316 * @wbc: writeback control structure
1318 * This is called from the VM when it wants to have a dirty ntfs page cache
1319 * page cleaned. The VM has already locked the page and marked it clean.
1321 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1322 * the ntfs version of the generic block_write_full_page() function,
1323 * ntfs_write_block(), which in turn if necessary creates and writes the
1324 * buffers associated with the page asynchronously.
1326 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1327 * the data to the mft record (which at this stage is most likely in memory).
1328 * The mft record is then marked dirty and written out asynchronously via the
1329 * vfs inode dirty code path for the inode the mft record belongs to or via the
1330 * vm page dirty code path for the page the mft record is in.
1332 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1334 * Return 0 on success and -errno on error.
1336 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1339 struct inode *vi = page->mapping->host;
1340 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1342 ntfs_attr_search_ctx *ctx = NULL;
1343 MFT_RECORD *m = NULL;
1348 BUG_ON(!PageLocked(page));
1349 i_size = i_size_read(vi);
1350 /* Is the page fully outside i_size? (truncate in progress) */
1351 if (unlikely(page->index >= (i_size + PAGE_SIZE - 1) >>
1353 struct folio *folio = page_folio(page);
1355 * The page may have dirty, unmapped buffers. Make them
1356 * freeable here, so the page does not leak.
1358 block_invalidate_folio(folio, 0, folio_size(folio));
1359 folio_unlock(folio);
1360 ntfs_debug("Write outside i_size - truncated?");
1364 * Only $DATA attributes can be encrypted and only unnamed $DATA
1365 * attributes can be compressed. Index root can have the flags set but
1366 * this means to create compressed/encrypted files, not that the
1367 * attribute is compressed/encrypted. Note we need to check for
1368 * AT_INDEX_ALLOCATION since this is the type of both directory and
1371 if (ni->type != AT_INDEX_ALLOCATION) {
1372 /* If file is encrypted, deny access, just like NT4. */
1373 if (NInoEncrypted(ni)) {
1375 BUG_ON(ni->type != AT_DATA);
1376 ntfs_debug("Denying write access to encrypted file.");
1379 /* Compressed data streams are handled in compress.c. */
1380 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1381 BUG_ON(ni->type != AT_DATA);
1382 BUG_ON(ni->name_len);
1383 // TODO: Implement and replace this with
1384 // return ntfs_write_compressed_block(page);
1386 ntfs_error(vi->i_sb, "Writing to compressed files is "
1387 "not supported yet. Sorry.");
1390 // TODO: Implement and remove this check.
1391 if (NInoNonResident(ni) && NInoSparse(ni)) {
1393 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1394 "supported yet. Sorry.");
1398 /* NInoNonResident() == NInoIndexAllocPresent() */
1399 if (NInoNonResident(ni)) {
1400 /* We have to zero every time due to mmap-at-end-of-file. */
1401 if (page->index >= (i_size >> PAGE_SHIFT)) {
1402 /* The page straddles i_size. */
1403 unsigned int ofs = i_size & ~PAGE_MASK;
1404 zero_user_segment(page, ofs, PAGE_SIZE);
1406 /* Handle mst protected attributes. */
1407 if (NInoMstProtected(ni))
1408 return ntfs_write_mst_block(page, wbc);
1409 /* Normal, non-resident data stream. */
1410 return ntfs_write_block(page, wbc);
1413 * Attribute is resident, implying it is not compressed, encrypted, or
1414 * mst protected. This also means the attribute is smaller than an mft
1415 * record and hence smaller than a page, so can simply return error on
1416 * any pages with index above 0. Note the attribute can actually be
1417 * marked compressed but if it is resident the actual data is not
1418 * compressed so we are ok to ignore the compressed flag here.
1420 BUG_ON(page_has_buffers(page));
1421 BUG_ON(!PageUptodate(page));
1422 if (unlikely(page->index > 0)) {
1423 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
1424 "Aborting write.", page->index);
1425 BUG_ON(PageWriteback(page));
1426 set_page_writeback(page);
1428 end_page_writeback(page);
1434 base_ni = ni->ext.base_ntfs_ino;
1435 /* Map, pin, and lock the mft record. */
1436 m = map_mft_record(base_ni);
1444 * If a parallel write made the attribute non-resident, drop the mft
1445 * record and retry the writepage.
1447 if (unlikely(NInoNonResident(ni))) {
1448 unmap_mft_record(base_ni);
1449 goto retry_writepage;
1451 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1452 if (unlikely(!ctx)) {
1456 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1457 CASE_SENSITIVE, 0, NULL, 0, ctx);
1461 * Keep the VM happy. This must be done otherwise the radix-tree tag
1462 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1464 BUG_ON(PageWriteback(page));
1465 set_page_writeback(page);
1467 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1468 i_size = i_size_read(vi);
1469 if (unlikely(attr_len > i_size)) {
1470 /* Race with shrinking truncate or a failed truncate. */
1473 * If the truncate failed, fix it up now. If a concurrent
1474 * truncate, we do its job, so it does not have to do anything.
1476 err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1478 /* Shrinking cannot fail. */
1481 addr = kmap_atomic(page);
1482 /* Copy the data from the page to the mft record. */
1483 memcpy((u8*)ctx->attr +
1484 le16_to_cpu(ctx->attr->data.resident.value_offset),
1486 /* Zero out of bounds area in the page cache page. */
1487 memset(addr + attr_len, 0, PAGE_SIZE - attr_len);
1488 kunmap_atomic(addr);
1489 flush_dcache_page(page);
1490 flush_dcache_mft_record_page(ctx->ntfs_ino);
1491 /* We are done with the page. */
1492 end_page_writeback(page);
1493 /* Finally, mark the mft record dirty, so it gets written back. */
1494 mark_mft_record_dirty(ctx->ntfs_ino);
1495 ntfs_attr_put_search_ctx(ctx);
1496 unmap_mft_record(base_ni);
1499 if (err == -ENOMEM) {
1500 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1501 "page so we try again later.");
1503 * Put the page back on mapping->dirty_pages, but leave its
1504 * buffers' dirty state as-is.
1506 redirty_page_for_writepage(wbc, page);
1509 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1512 NVolSetErrors(ni->vol);
1516 ntfs_attr_put_search_ctx(ctx);
1518 unmap_mft_record(base_ni);
1522 #endif /* NTFS_RW */
1525 * ntfs_bmap - map logical file block to physical device block
1526 * @mapping: address space mapping to which the block to be mapped belongs
1527 * @block: logical block to map to its physical device block
1529 * For regular, non-resident files (i.e. not compressed and not encrypted), map
1530 * the logical @block belonging to the file described by the address space
1531 * mapping @mapping to its physical device block.
1533 * The size of the block is equal to the @s_blocksize field of the super block
1534 * of the mounted file system which is guaranteed to be smaller than or equal
1535 * to the cluster size thus the block is guaranteed to fit entirely inside the
1536 * cluster which means we do not need to care how many contiguous bytes are
1537 * available after the beginning of the block.
1539 * Return the physical device block if the mapping succeeded or 0 if the block
1540 * is sparse or there was an error.
1542 * Note: This is a problem if someone tries to run bmap() on $Boot system file
1543 * as that really is in block zero but there is nothing we can do. bmap() is
1544 * just broken in that respect (just like it cannot distinguish sparse from
1545 * not available or error).
1547 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
1552 unsigned long blocksize, flags;
1553 ntfs_inode *ni = NTFS_I(mapping->host);
1554 ntfs_volume *vol = ni->vol;
1556 unsigned char blocksize_bits, cluster_size_shift;
1558 ntfs_debug("Entering for mft_no 0x%lx, logical block 0x%llx.",
1559 ni->mft_no, (unsigned long long)block);
1560 if (ni->type != AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni)) {
1561 ntfs_error(vol->sb, "BMAP does not make sense for %s "
1562 "attributes, returning 0.",
1563 (ni->type != AT_DATA) ? "non-data" :
1564 (!NInoNonResident(ni) ? "resident" :
1568 /* None of these can happen. */
1569 BUG_ON(NInoCompressed(ni));
1570 BUG_ON(NInoMstProtected(ni));
1571 blocksize = vol->sb->s_blocksize;
1572 blocksize_bits = vol->sb->s_blocksize_bits;
1573 ofs = (s64)block << blocksize_bits;
1574 read_lock_irqsave(&ni->size_lock, flags);
1575 size = ni->initialized_size;
1576 i_size = i_size_read(VFS_I(ni));
1577 read_unlock_irqrestore(&ni->size_lock, flags);
1579 * If the offset is outside the initialized size or the block straddles
1580 * the initialized size then pretend it is a hole unless the
1581 * initialized size equals the file size.
1583 if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size)))
1585 cluster_size_shift = vol->cluster_size_bits;
1586 down_read(&ni->runlist.lock);
1587 lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ofs >> cluster_size_shift, false);
1588 up_read(&ni->runlist.lock);
1589 if (unlikely(lcn < LCN_HOLE)) {
1591 * Step down to an integer to avoid gcc doing a long long
1592 * comparision in the switch when we know @lcn is between
1593 * LCN_HOLE and LCN_EIO (i.e. -1 to -5).
1595 * Otherwise older gcc (at least on some architectures) will
1596 * try to use __cmpdi2() which is of course not available in
1602 * If the offset is out of bounds then pretend it is a
1607 ntfs_error(vol->sb, "Not enough memory to complete "
1608 "mapping for inode 0x%lx. "
1609 "Returning 0.", ni->mft_no);
1612 ntfs_error(vol->sb, "Failed to complete mapping for "
1613 "inode 0x%lx. Run chkdsk. "
1614 "Returning 0.", ni->mft_no);
1622 ntfs_debug("Done (returning hole).");
1626 * The block is really allocated and fullfils all our criteria.
1627 * Convert the cluster to units of block size and return the result.
1629 delta = ofs & vol->cluster_size_mask;
1630 if (unlikely(sizeof(block) < sizeof(lcn))) {
1631 block = lcn = ((lcn << cluster_size_shift) + delta) >>
1633 /* If the block number was truncated return 0. */
1634 if (unlikely(block != lcn)) {
1635 ntfs_error(vol->sb, "Physical block 0x%llx is too "
1636 "large to be returned, returning 0.",
1641 block = ((lcn << cluster_size_shift) + delta) >>
1643 ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn);
1648 * ntfs_normal_aops - address space operations for normal inodes and attributes
1650 * Note these are not used for compressed or mst protected inodes and
1653 const struct address_space_operations ntfs_normal_aops = {
1654 .readpage = ntfs_readpage,
1656 .writepage = ntfs_writepage,
1657 .dirty_folio = block_dirty_folio,
1658 #endif /* NTFS_RW */
1660 .migratepage = buffer_migrate_page,
1661 .is_partially_uptodate = block_is_partially_uptodate,
1662 .error_remove_page = generic_error_remove_page,
1666 * ntfs_compressed_aops - address space operations for compressed inodes
1668 const struct address_space_operations ntfs_compressed_aops = {
1669 .readpage = ntfs_readpage,
1671 .writepage = ntfs_writepage,
1672 .dirty_folio = block_dirty_folio,
1673 #endif /* NTFS_RW */
1674 .migratepage = buffer_migrate_page,
1675 .is_partially_uptodate = block_is_partially_uptodate,
1676 .error_remove_page = generic_error_remove_page,
1680 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1683 const struct address_space_operations ntfs_mst_aops = {
1684 .readpage = ntfs_readpage, /* Fill page with data. */
1686 .writepage = ntfs_writepage, /* Write dirty page to disk. */
1687 .dirty_folio = filemap_dirty_folio,
1688 #endif /* NTFS_RW */
1689 .migratepage = buffer_migrate_page,
1690 .is_partially_uptodate = block_is_partially_uptodate,
1691 .error_remove_page = generic_error_remove_page,
1697 * mark_ntfs_record_dirty - mark an ntfs record dirty
1698 * @page: page containing the ntfs record to mark dirty
1699 * @ofs: byte offset within @page at which the ntfs record begins
1701 * Set the buffers and the page in which the ntfs record is located dirty.
1703 * The latter also marks the vfs inode the ntfs record belongs to dirty
1704 * (I_DIRTY_PAGES only).
1706 * If the page does not have buffers, we create them and set them uptodate.
1707 * The page may not be locked which is why we need to handle the buffers under
1708 * the mapping->private_lock. Once the buffers are marked dirty we no longer
1709 * need the lock since try_to_free_buffers() does not free dirty buffers.
1711 void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
1712 struct address_space *mapping = page->mapping;
1713 ntfs_inode *ni = NTFS_I(mapping->host);
1714 struct buffer_head *bh, *head, *buffers_to_free = NULL;
1715 unsigned int end, bh_size, bh_ofs;
1717 BUG_ON(!PageUptodate(page));
1718 end = ofs + ni->itype.index.block_size;
1719 bh_size = VFS_I(ni)->i_sb->s_blocksize;
1720 spin_lock(&mapping->private_lock);
1721 if (unlikely(!page_has_buffers(page))) {
1722 spin_unlock(&mapping->private_lock);
1723 bh = head = alloc_page_buffers(page, bh_size, true);
1724 spin_lock(&mapping->private_lock);
1725 if (likely(!page_has_buffers(page))) {
1726 struct buffer_head *tail;
1729 set_buffer_uptodate(bh);
1731 bh = bh->b_this_page;
1733 tail->b_this_page = head;
1734 attach_page_private(page, head);
1736 buffers_to_free = bh;
1738 bh = head = page_buffers(page);
1741 bh_ofs = bh_offset(bh);
1742 if (bh_ofs + bh_size <= ofs)
1744 if (unlikely(bh_ofs >= end))
1746 set_buffer_dirty(bh);
1747 } while ((bh = bh->b_this_page) != head);
1748 spin_unlock(&mapping->private_lock);
1749 filemap_dirty_folio(mapping, page_folio(page));
1750 if (unlikely(buffers_to_free)) {
1752 bh = buffers_to_free->b_this_page;
1753 free_buffer_head(buffers_to_free);
1754 buffers_to_free = bh;
1755 } while (buffers_to_free);
1759 #endif /* NTFS_RW */