From nobody Sun Feb 8 15:28:51 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DA0819CD0A for ; Sun, 11 Jan 2026 14:14:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768140884; cv=none; b=J35z1emoEDbZYsyCheGlS9jw5RmgvPCIfVTrRGOgBUw6NfljOsm39A8OTiNBTCtBsA0CuGV17GCsWSTom+9fuEAbuZ51+17mosyZBtMwDiP8YSN6AvksuuRiQhcsX4gOcBkX/l+HY/9KKRZyaP+yByBcQuHChCiQniqKgVFo5VA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768140884; c=relaxed/simple; bh=73qpmsZL0CnJUvSxYIE9j8QWRVTfJ5A04zkQGPMnfl8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mHLjvBTk7mo+IhAKfA9bx8cGsdO/voWOiSg7VAYrfbXJzuZRUBQDBbRs975TVND+W8X+0fEGH+pra+MzOdtBST/og2owQyZLjO8V8zYHaXOcBqFoqe8jjHGrHP0u9Il7U7FX6ywg2nouWjIpBQUyzsq+0y+MbD+1AHLj5rzd/gY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-81e8a9d521dso941468b3a.2 for ; Sun, 11 Jan 2026 06:14:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768140880; x=1768745680; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=rjKsrsLLetIzi7Ltfsf7VOHX7bBdAAdY/Iw5rV914cA=; b=Ng6KaivKrBj1ubUigwfRkWBrANk9R8lg7hWxhHSd5HHPPSJN54IxmfYN8ksc+rSEXN 9+05C7b+fSu+Sqhs5sn4WinHa26ikjhplRgXveZSofkR8H+rlz1ETelLSCxOXF61tOEJ veEYCLHU9nTkaZAIL5c7e3f8BHg6J7A3fUVHkODhrS27HpBMoc1YJq5fsgaTEhpZycKf q7MR44a0AASEr2r7LRiLGDCHPPGY8n7ja/EwcnP7Ys5PxtgbFNnP/YSe4Luj5UqoPu+h 9t+b9yXHqhl8/gysswkKGuwnNy2o0SlEY56FeKTeyc3AMCPl09D26OxqxhZPCFABG+BX OvDg== X-Forwarded-Encrypted: i=1; AJvYcCUCf4kxqRGoX8oLc26uwzXihMxfgQzZx/czcE3S62wnaXuVVRsstGcQhgdH4DH4a8pW53fV4E3x+bDOJeg=@vger.kernel.org X-Gm-Message-State: AOJu0YyaHU8cBJQmjJwI6FAGlTGpB5lIumpmuR788hH2euqgS1nzAsmI kqcWksNnXx8oRkHj5G2unb+YYQO8j/n26vaO6pJ+WwMN10bRTQ6Zg0Z1 X-Gm-Gg: AY/fxX4k4Wifl00nlj+4igk5PaovpnzNvY3mRwXthvlNIJ94Oo8+Me95QZ0cWbc7xt/ NIZAICEXpYMO1KFAsQYOUZDk7xmov1M/03OK/39Qab5kt7i+CLpcymz2nwpR919ohEr8GvMDIam GdtwWQ2Qv9CeCuPG+WEoiZbCA8VW6cKCOxxYwEdWt7rar4JVBewsxWrUrRDmwmkLekQM2fv5Jkj z5JxsRUQ3S65sHA9UQQTznJetu/49uKvNYUlSXBqOeFAhxK6ao8AutHwRKWDdPSVVLZUH4TiwFz LLTu9mLadA3IlqBGQMy/sO/g+WmMj5cfLwPAZOnzOVFftjqDVYWe8WTajyM5aHeMYB4uTu8YzGf QCZjgtlNq5ZObv++1Orl1T28T8LCs+zdGamCMNqMAP3jnvywSdLLgPXairGcpMa1rUiWjNpzNG0 YpgD/EWDfuqo86ivaMyKID1YoApw== X-Google-Smtp-Source: AGHT+IFYyPsjC92hIC7zw8szNYmzYWGerEQc43XBqsyr+zLGhiJuEs8MHSyHONrfqdo6syinA/tpXA== X-Received: by 2002:a05:6a20:7490:b0:389:8f3f:50ce with SMTP id adf61e73a8af0-3898f9af60amr13630477637.56.1768140879688; Sun, 11 Jan 2026 06:14:39 -0800 (PST) Received: from localhost.localdomain ([1.227.206.162]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c4cc02ecfccsm14887077a12.13.2026.01.11.06.14.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Jan 2026 06:14:38 -0800 (PST) From: Namjae Jeon To: viro@zeniv.linux.org.uk, brauner@kernel.org, hch@lst.de, tytso@mit.edu, willy@infradead.org, jack@suse.cz, djwong@kernel.org, josef@toxicpanda.com, sandeen@sandeen.net, rgoldwyn@suse.com, xiang@kernel.org, dsterba@suse.com, pali@kernel.org, ebiggers@kernel.org, neil@brown.name, amir73il@gmail.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, iamjoonsoo.kim@lge.com, cheol.lee@lge.com, jay.sim@lge.com, gunho.lee@lge.com, Namjae Jeon , Hyunchul Lee Subject: [PATCH v5 07/14] ntfs: update iomap and address space operations Date: Sun, 11 Jan 2026 23:03:37 +0900 Message-Id: <20260111140345.3866-8-linkinjeon@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260111140345.3866-1-linkinjeon@kernel.org> References: <20260111140345.3866-1-linkinjeon@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This updates the implementation of iomap and address space operations. Signed-off-by: Hyunchul Lee Signed-off-by: Namjae Jeon --- fs/ntfs/aops.c | 1891 +++++++++-------------------------------------- fs/ntfs/iomap.c | 754 +++++++++++++++++++ 2 files changed, 1102 insertions(+), 1543 deletions(-) create mode 100644 fs/ntfs/iomap.c diff --git a/fs/ntfs/aops.c b/fs/ntfs/aops.c index 2d01517a2d59..3b60797176d6 100644 --- a/fs/ntfs/aops.c +++ b/fs/ntfs/aops.c @@ -1,407 +1,59 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * aops.c - NTFS kernel address space operations and page cache handling. + * NTFS kernel address space operations and page cache handling. * * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. * Copyright (c) 2002 Richard Russon + * Copyright (c) 2025 LG Electronics Co., Ltd. */ =20 -#include -#include -#include -#include -#include -#include -#include #include -#include -#include +#include +#include =20 #include "aops.h" #include "attrib.h" -#include "debug.h" -#include "inode.h" #include "mft.h" -#include "runlist.h" -#include "types.h" #include "ntfs.h" +#include "debug.h" +#include "iomap.h" =20 -/** - * ntfs_end_buffer_async_read - async io completion for reading attributes - * @bh: buffer head on which io is completed - * @uptodate: whether @bh is now uptodate or not - * - * Asynchronous I/O completion handler for reading pages belonging to the - * attribute address space of an inode. The inodes can either be files or - * directories or they can be fake inodes describing some attribute. - * - * If NInoMstProtected(), perform the post read mst fixups when all IO on = the - * page has been completed and mark the page uptodate or set the error bit= on - * the page. To determine the size of the records that need fixing up, we - * cheat a little bit by setting the index_block_size in ntfs_inode to the= ntfs - * record size, and index_block_size_bits, to the log(base 2) of the ntfs - * record size. - */ -static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodat= e) +static s64 lcn_from_index(struct ntfs_volume *vol, struct ntfs_inode *ni, + unsigned long index) { - unsigned long flags; - struct buffer_head *first, *tmp; - struct page *page; - struct inode *vi; - ntfs_inode *ni; - int page_uptodate =3D 1; - - page =3D bh->b_page; - vi =3D page->mapping->host; - ni =3D NTFS_I(vi); - - if (likely(uptodate)) { - loff_t i_size; - s64 file_ofs, init_size; - - set_buffer_uptodate(bh); - - file_ofs =3D ((s64)page->index << PAGE_SHIFT) + - bh_offset(bh); - read_lock_irqsave(&ni->size_lock, flags); - init_size =3D ni->initialized_size; - i_size =3D i_size_read(vi); - read_unlock_irqrestore(&ni->size_lock, flags); - if (unlikely(init_size > i_size)) { - /* Race with shrinking truncate. */ - init_size =3D i_size; - } - /* Check for the current buffer head overflowing. */ - if (unlikely(file_ofs + bh->b_size > init_size)) { - int ofs; - void *kaddr; - - ofs =3D 0; - if (file_ofs < init_size) - ofs =3D init_size - file_ofs; - kaddr =3D kmap_atomic(page); - memset(kaddr + bh_offset(bh) + ofs, 0, - bh->b_size - ofs); - flush_dcache_page(page); - kunmap_atomic(kaddr); - } - } else { - clear_buffer_uptodate(bh); - SetPageError(page); - ntfs_error(ni->vol->sb, "Buffer I/O error, logical block " - "0x%llx.", (unsigned long long)bh->b_blocknr); - } - first =3D page_buffers(page); - spin_lock_irqsave(&first->b_uptodate_lock, flags); - clear_buffer_async_read(bh); - unlock_buffer(bh); - tmp =3D bh; - do { - if (!buffer_uptodate(tmp)) - page_uptodate =3D 0; - if (buffer_async_read(tmp)) { - if (likely(buffer_locked(tmp))) - goto still_busy; - /* Async buffers must be locked. */ - BUG(); - } - tmp =3D tmp->b_this_page; - } while (tmp !=3D bh); - spin_unlock_irqrestore(&first->b_uptodate_lock, flags); - /* - * If none of the buffers had errors then we can set the page uptodate, - * but we first have to perform the post read mst fixups, if the - * attribute is mst protected, i.e. if NInoMstProteced(ni) is true. - * Note we ignore fixup errors as those are detected when - * map_mft_record() is called which gives us per record granularity - * rather than per page granularity. - */ - if (!NInoMstProtected(ni)) { - if (likely(page_uptodate && !PageError(page))) - SetPageUptodate(page); - } else { - u8 *kaddr; - unsigned int i, recs; - u32 rec_size; - - rec_size =3D ni->itype.index.block_size; - recs =3D PAGE_SIZE / rec_size; - /* Should have been verified before we got here... */ - BUG_ON(!recs); - kaddr =3D kmap_atomic(page); - for (i =3D 0; i < recs; i++) - post_read_mst_fixup((NTFS_RECORD*)(kaddr + - i * rec_size), rec_size); - kunmap_atomic(kaddr); - flush_dcache_page(page); - if (likely(page_uptodate && !PageError(page))) - SetPageUptodate(page); - } - unlock_page(page); - return; -still_busy: - spin_unlock_irqrestore(&first->b_uptodate_lock, flags); - return; -} + s64 vcn; + s64 lcn; =20 -/** - * ntfs_read_block - fill a @folio of an address space with data - * @folio: page cache folio to fill with data - * - * We read each buffer asynchronously and when all buffers are read in, ou= r io - * completion handler ntfs_end_buffer_read_async(), if required, automatic= ally - * applies the mst fixups to the folio before finally marking it uptodate = and - * unlocking it. - * - * We only enforce allocated_size limit because i_size is checked for in - * generic_file_read(). - * - * Return 0 on success and -errno on error. - * - * Contains an adapted version of fs/buffer.c::block_read_full_folio(). - */ -static int ntfs_read_block(struct folio *folio) -{ - loff_t i_size; - VCN vcn; - LCN lcn; - s64 init_size; - struct inode *vi; - ntfs_inode *ni; - ntfs_volume *vol; - runlist_element *rl; - struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; - sector_t iblock, lblock, zblock; - unsigned long flags; - unsigned int blocksize, vcn_ofs; - int i, nr; - unsigned char blocksize_bits; + vcn =3D NTFS_PIDX_TO_CLU(vol, index); =20 - vi =3D folio->mapping->host; - ni =3D NTFS_I(vi); - vol =3D ni->vol; - - /* $MFT/$DATA must have its complete runlist in memory at all times. */ - BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni)); - - blocksize =3D vol->sb->s_blocksize; - blocksize_bits =3D vol->sb->s_blocksize_bits; - - head =3D folio_buffers(folio); - if (!head) - head =3D create_empty_buffers(folio, blocksize, 0); - bh =3D head; + down_read(&ni->runlist.lock); + lcn =3D ntfs_attr_vcn_to_lcn_nolock(ni, vcn, false); + up_read(&ni->runlist.lock); =20 - /* - * We may be racing with truncate. To avoid some of the problems we - * now take a snapshot of the various sizes and use those for the whole - * of the function. In case of an extending truncate it just means we - * may leave some buffers unmapped which are now allocated. This is - * not a problem since these buffers will just get mapped when a write - * occurs. In case of a shrinking truncate, we will detect this later - * on due to the runlist being incomplete and if the folio is being - * fully truncated, truncate will throw it away as soon as we unlock - * it so no need to worry what we do with it. - */ - iblock =3D (s64)folio->index << (PAGE_SHIFT - blocksize_bits); - read_lock_irqsave(&ni->size_lock, flags); - lblock =3D (ni->allocated_size + blocksize - 1) >> blocksize_bits; - init_size =3D ni->initialized_size; - i_size =3D i_size_read(vi); - read_unlock_irqrestore(&ni->size_lock, flags); - if (unlikely(init_size > i_size)) { - /* Race with shrinking truncate. */ - init_size =3D i_size; - } - zblock =3D (init_size + blocksize - 1) >> blocksize_bits; - - /* Loop through all the buffers in the folio. */ - rl =3D NULL; - nr =3D i =3D 0; - do { - int err =3D 0; - - if (unlikely(buffer_uptodate(bh))) - continue; - if (unlikely(buffer_mapped(bh))) { - arr[nr++] =3D bh; - continue; - } - bh->b_bdev =3D vol->sb->s_bdev; - /* Is the block within the allowed limits? */ - if (iblock < lblock) { - bool is_retry =3D false; - - /* Convert iblock into corresponding vcn and offset. */ - vcn =3D (VCN)iblock << blocksize_bits >> - vol->cluster_size_bits; - vcn_ofs =3D ((VCN)iblock << blocksize_bits) & - vol->cluster_size_mask; - if (!rl) { -lock_retry_remap: - down_read(&ni->runlist.lock); - rl =3D ni->runlist.rl; - } - if (likely(rl !=3D NULL)) { - /* Seek to element containing target vcn. */ - while (rl->length && rl[1].vcn <=3D vcn) - rl++; - lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); - } else - lcn =3D LCN_RL_NOT_MAPPED; - /* Successful remap. */ - if (lcn >=3D 0) { - /* Setup buffer head to correct block. */ - bh->b_blocknr =3D ((lcn << vol->cluster_size_bits) - + vcn_ofs) >> blocksize_bits; - set_buffer_mapped(bh); - /* Only read initialized data blocks. */ - if (iblock < zblock) { - arr[nr++] =3D bh; - continue; - } - /* Fully non-initialized data block, zero it. */ - goto handle_zblock; - } - /* It is a hole, need to zero it. */ - if (lcn =3D=3D LCN_HOLE) - goto handle_hole; - /* If first try and runlist unmapped, map and retry. */ - if (!is_retry && lcn =3D=3D LCN_RL_NOT_MAPPED) { - is_retry =3D true; - /* - * Attempt to map runlist, dropping lock for - * the duration. - */ - up_read(&ni->runlist.lock); - err =3D ntfs_map_runlist(ni, vcn); - if (likely(!err)) - goto lock_retry_remap; - rl =3D NULL; - } else if (!rl) - up_read(&ni->runlist.lock); - /* - * If buffer is outside the runlist, treat it as a - * hole. This can happen due to concurrent truncate - * for example. - */ - if (err =3D=3D -ENOENT || lcn =3D=3D LCN_ENOENT) { - err =3D 0; - goto handle_hole; - } - /* Hard error, zero out region. */ - if (!err) - err =3D -EIO; - bh->b_blocknr =3D -1; - folio_set_error(folio); - ntfs_error(vol->sb, "Failed to read from inode 0x%lx, " - "attribute type 0x%x, vcn 0x%llx, " - "offset 0x%x because its location on " - "disk could not be determined%s " - "(error code %i).", ni->mft_no, - ni->type, (unsigned long long)vcn, - vcn_ofs, is_retry ? " even after " - "retrying" : "", err); - } - /* - * Either iblock was outside lblock limits or - * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion - * of the folio and set the buffer uptodate. - */ -handle_hole: - bh->b_blocknr =3D -1UL; - clear_buffer_mapped(bh); -handle_zblock: - folio_zero_range(folio, i * blocksize, blocksize); - if (likely(!err)) - set_buffer_uptodate(bh); - } while (i++, iblock++, (bh =3D bh->b_this_page) !=3D head); - - /* Release the lock if we took it. */ - if (rl) - up_read(&ni->runlist.lock); - - /* Check we have at least one buffer ready for i/o. */ - if (nr) { - struct buffer_head *tbh; - - /* Lock the buffers. */ - for (i =3D 0; i < nr; i++) { - tbh =3D arr[i]; - lock_buffer(tbh); - tbh->b_end_io =3D ntfs_end_buffer_async_read; - set_buffer_async_read(tbh); - } - /* Finally, start i/o on the buffers. */ - for (i =3D 0; i < nr; i++) { - tbh =3D arr[i]; - if (likely(!buffer_uptodate(tbh))) - submit_bh(REQ_OP_READ, tbh); - else - ntfs_end_buffer_async_read(tbh, 1); - } - return 0; - } - /* No i/o was scheduled on any of the buffers. */ - if (likely(!folio_test_error(folio))) - folio_mark_uptodate(folio); - else /* Signal synchronous i/o error. */ - nr =3D -EIO; - folio_unlock(folio); - return nr; + return lcn; } =20 /** - * ntfs_read_folio - fill a @folio of a @file with data from the device + * ntfs_read_folio - Read data for a folio from the device * @file: open file to which the folio @folio belongs or NULL * @folio: page cache folio to fill with data * - * For non-resident attributes, ntfs_read_folio() fills the @folio of the = open - * file @file by calling the ntfs version of the generic block_read_full_f= olio() - * function, ntfs_read_block(), which in turn creates and reads in the buf= fers - * associated with the folio asynchronously. + * This function handles reading data into the page cache. It first checks + * for specific ntfs attribute type like encryption and compression. * - * For resident attributes, OTOH, ntfs_read_folio() fills @folio by copyin= g the - * data from the mft record (which at this stage is most likely in memory)= and - * fills the remainder with zeroes. Thus, in this case, I/O is synchronous= , as - * even if the mft record is not cached at this point in time, we need to = wait - * for it to be read in before we can do the copy. + * - If the attribute is encrypted, access is denied (-EACCES) because + * decryption is not supported in this path. + * - If the attribute is non-resident and compressed, the read operation is + * delegated to ntfs_read_compressed_block(). + * - For normal resident or non-resident attribute, it utilizes the generic + * iomap infrastructure via iomap_bio_read_folio() to perform the I/O. * - * Return 0 on success and -errno on error. + * Return: 0 on success, or -errno on error. */ static int ntfs_read_folio(struct file *file, struct folio *folio) { - struct page *page =3D &folio->page; - loff_t i_size; - struct inode *vi; - ntfs_inode *ni, *base_ni; - u8 *addr; - ntfs_attr_search_ctx *ctx; - MFT_RECORD *mrec; - unsigned long flags; - u32 attr_len; - int err =3D 0; - -retry_readpage: - BUG_ON(!PageLocked(page)); - vi =3D page->mapping->host; - i_size =3D i_size_read(vi); - /* Is the page fully outside i_size? (truncate in progress) */ - if (unlikely(page->index >=3D (i_size + PAGE_SIZE - 1) >> - PAGE_SHIFT)) { - zero_user(page, 0, PAGE_SIZE); - ntfs_debug("Read outside i_size - truncated?"); - goto done; - } - /* - * This can potentially happen because we clear PageUptodate() during - * ntfs_writepage() of MstProtected() attributes. - */ - if (PageUptodate(page)) { - unlock_page(page); - return 0; - } - ni =3D NTFS_I(vi); + struct ntfs_inode *ni =3D NTFS_I(folio->mapping->host); + /* * Only $DATA attributes can be encrypted and only unnamed $DATA * attributes can be compressed. Index root can have the flags set but @@ -413,1099 +65,201 @@ static int ntfs_read_folio(struct file *file, struc= t folio *folio) if (ni->type !=3D AT_INDEX_ALLOCATION) { /* If attribute is encrypted, deny access, just like NT4. */ if (NInoEncrypted(ni)) { - BUG_ON(ni->type !=3D AT_DATA); - err =3D -EACCES; - goto err_out; + folio_unlock(folio); + return -EACCES; } /* Compressed data streams are handled in compress.c. */ - if (NInoNonResident(ni) && NInoCompressed(ni)) { - BUG_ON(ni->type !=3D AT_DATA); - BUG_ON(ni->name_len); - return ntfs_read_compressed_block(page); - } - } - /* NInoNonResident() =3D=3D NInoIndexAllocPresent() */ - if (NInoNonResident(ni)) { - /* Normal, non-resident data stream. */ - return ntfs_read_block(folio); - } - /* - * Attribute is resident, implying it is not compressed or encrypted. - * This also means the attribute is smaller than an mft record and - * hence smaller than a page, so can simply zero out any pages with - * index above 0. Note the attribute can actually be marked compressed - * but if it is resident the actual data is not compressed so we are - * ok to ignore the compressed flag here. - */ - if (unlikely(page->index > 0)) { - zero_user(page, 0, PAGE_SIZE); - goto done; + if (NInoNonResident(ni) && NInoCompressed(ni)) + return ntfs_read_compressed_block(folio); } - if (!NInoAttr(ni)) - base_ni =3D ni; - else - base_ni =3D ni->ext.base_ntfs_ino; - /* Map, pin, and lock the mft record. */ - mrec =3D map_mft_record(base_ni); - if (IS_ERR(mrec)) { - err =3D PTR_ERR(mrec); - goto err_out; - } - /* - * If a parallel write made the attribute non-resident, drop the mft - * record and retry the read_folio. - */ - if (unlikely(NInoNonResident(ni))) { - unmap_mft_record(base_ni); - goto retry_readpage; - } - ctx =3D ntfs_attr_get_search_ctx(base_ni, mrec); - if (unlikely(!ctx)) { - err =3D -ENOMEM; - goto unm_err_out; - } - err =3D ntfs_attr_lookup(ni->type, ni->name, ni->name_len, - CASE_SENSITIVE, 0, NULL, 0, ctx); - if (unlikely(err)) - goto put_unm_err_out; - attr_len =3D le32_to_cpu(ctx->attr->data.resident.value_length); - read_lock_irqsave(&ni->size_lock, flags); - if (unlikely(attr_len > ni->initialized_size)) - attr_len =3D ni->initialized_size; - i_size =3D i_size_read(vi); - read_unlock_irqrestore(&ni->size_lock, flags); - if (unlikely(attr_len > i_size)) { - /* Race with shrinking truncate. */ - attr_len =3D i_size; - } - addr =3D kmap_atomic(page); - /* Copy the data to the page. */ - memcpy(addr, (u8*)ctx->attr + - le16_to_cpu(ctx->attr->data.resident.value_offset), - attr_len); - /* Zero the remainder of the page. */ - memset(addr + attr_len, 0, PAGE_SIZE - attr_len); - flush_dcache_page(page); - kunmap_atomic(addr); -put_unm_err_out: - ntfs_attr_put_search_ctx(ctx); -unm_err_out: - unmap_mft_record(base_ni); -done: - SetPageUptodate(page); -err_out: - unlock_page(page); - return err; -} =20 -#ifdef NTFS_RW + iomap_bio_read_folio(folio, &ntfs_read_iomap_ops); + return 0; +} =20 -/** - * ntfs_write_block - write a @folio to the backing store - * @folio: page cache folio to write out - * @wbc: writeback control structure - * - * This function is for writing folios belonging to non-resident, non-mst - * protected attributes to their backing store. - * - * For a folio with buffers, map and write the dirty buffers asynchronously - * under folio writeback. For a folio without buffers, create buffers for = the - * folio, then proceed as above. - * - * If a folio doesn't have buffers the folio dirty state is definitive. If - * a folio does have buffers, the folio dirty state is just a hint, - * and the buffer dirty state is definitive. (A hint which has rules: - * dirty buffers against a clean folio is illegal. Other combinations are - * legal and need to be handled. In particular a dirty folio containing - * clean buffers for example.) - * - * Return 0 on success and -errno on error. - * - * Based on ntfs_read_block() and __block_write_full_folio(). - */ -static int ntfs_write_block(struct folio *folio, struct writeback_control = *wbc) +void ntfs_bio_end_io(struct bio *bio) { - VCN vcn; - LCN lcn; - s64 initialized_size; - loff_t i_size; - sector_t block, dblock, iblock; - struct inode *vi; - ntfs_inode *ni; - ntfs_volume *vol; - runlist_element *rl; - struct buffer_head *bh, *head; - unsigned long flags; - unsigned int blocksize, vcn_ofs; - int err; - bool need_end_writeback; - unsigned char blocksize_bits; - - vi =3D folio->mapping->host; - ni =3D NTFS_I(vi); - vol =3D ni->vol; - - ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " - "0x%lx.", ni->mft_no, ni->type, folio->index); + if (bio->bi_private) + folio_put((struct folio *)bio->bi_private); + bio_put(bio); +} =20 - BUG_ON(!NInoNonResident(ni)); - BUG_ON(NInoMstProtected(ni)); - blocksize =3D vol->sb->s_blocksize; - blocksize_bits =3D vol->sb->s_blocksize_bits; - head =3D folio_buffers(folio); - if (!head) { - BUG_ON(!folio_test_uptodate(folio)); - head =3D create_empty_buffers(folio, blocksize, - (1 << BH_Uptodate) | (1 << BH_Dirty)); +static int ntfs_write_mft_block(struct ntfs_inode *ni, struct folio *folio, + struct writeback_control *wbc) +{ + struct inode *vi =3D VFS_I(ni); + struct ntfs_volume *vol =3D ni->vol; + u8 *kaddr; + struct ntfs_inode *locked_nis[PAGE_SIZE / NTFS_BLOCK_SIZE]; + int nr_locked_nis =3D 0, err =3D 0, mft_ofs, prev_mft_ofs; + struct bio *bio =3D NULL; + unsigned long mft_no; + struct ntfs_inode *tni; + s64 lcn; + s64 vcn =3D NTFS_PIDX_TO_CLU(vol, folio->index); + s64 end_vcn =3D NTFS_B_TO_CLU(vol, ni->allocated_size); + unsigned int folio_sz; + struct runlist_element *rl; + + ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, folio index 0x= %lx.", + vi->i_ino, ni->type, folio->index); + + lcn =3D lcn_from_index(vol, ni, folio->index); + if (lcn <=3D LCN_HOLE) { + folio_start_writeback(folio); + folio_unlock(folio); + folio_end_writeback(folio); + return -EIO; } - bh =3D head; - - /* NOTE: Different naming scheme to ntfs_read_block()! */ - - /* The first block in the folio. */ - block =3D (s64)folio->index << (PAGE_SHIFT - blocksize_bits); - - read_lock_irqsave(&ni->size_lock, flags); - i_size =3D i_size_read(vi); - initialized_size =3D ni->initialized_size; - read_unlock_irqrestore(&ni->size_lock, flags); - - /* The first out of bounds block for the data size. */ - dblock =3D (i_size + blocksize - 1) >> blocksize_bits; - - /* The last (fully or partially) initialized block. */ - iblock =3D initialized_size >> blocksize_bits; - - /* - * Be very careful. We have no exclusion from block_dirty_folio - * here, and the (potentially unmapped) buffers may become dirty at - * any time. If a buffer becomes dirty here after we've inspected it - * then we just miss that fact, and the folio stays dirty. - * - * Buffers outside i_size may be dirtied by block_dirty_folio; - * handle that here by just cleaning them. - */ - - /* - * Loop through all the buffers in the folio, mapping all the dirty - * buffers to disk addresses and handling any aliases from the - * underlying block device's mapping. - */ - rl =3D NULL; - err =3D 0; - do { - bool is_retry =3D false; - - if (unlikely(block >=3D dblock)) { - /* - * Mapped buffers outside i_size will occur, because - * this folio can be outside i_size when there is a - * truncate in progress. The contents of such buffers - * were zeroed by ntfs_writepage(). - * - * FIXME: What about the small race window where - * ntfs_writepage() has not done any clearing because - * the folio was within i_size but before we get here, - * vmtruncate() modifies i_size? - */ - clear_buffer_dirty(bh); - set_buffer_uptodate(bh); - continue; - } =20 - /* Clean buffers are not written out, so no need to map them. */ - if (!buffer_dirty(bh)) - continue; + /* Map folio so we can access its contents. */ + kaddr =3D kmap_local_folio(folio, 0); + /* Clear the page uptodate flag whilst the mst fixups are applied. */ + folio_clear_uptodate(folio); + + for (mft_ofs =3D 0; mft_ofs < PAGE_SIZE && vcn < end_vcn; + mft_ofs +=3D vol->mft_record_size) { + /* Get the mft record number. */ + mft_no =3D (((s64)folio->index << PAGE_SHIFT) + mft_ofs) >> + vol->mft_record_size_bits; + vcn =3D NTFS_MFT_NR_TO_CLU(vol, mft_no); + /* Check whether to write this mft record. */ + tni =3D NULL; + if (ntfs_may_write_mft_record(vol, mft_no, + (struct mft_record *)(kaddr + mft_ofs), &tni)) { + unsigned int mft_record_off =3D 0; + s64 vcn_off =3D vcn; =20 - /* Make sure we have enough initialized size. */ - if (unlikely((block >=3D iblock) && - (initialized_size < i_size))) { - /* - * If this folio is fully outside initialized - * size, zero out all folios between the current - * initialized size and the current folio. Just - * use ntfs_read_folio() to do the zeroing - * transparently. - */ - if (block > iblock) { - // TODO: - // For each folio do: - // - read_cache_folio() - // Again for each folio do: - // - wait_on_folio_locked() - // - Check (folio_test_uptodate(folio) && - // !folio_test_error(folio)) - // Update initialized size in the attribute and - // in the inode. - // Again, for each folio do: - // block_dirty_folio(); - // folio_put() - // We don't need to wait on the writes. - // Update iblock. - } /* - * The current folio straddles initialized size. Zero - * all non-uptodate buffers and set them uptodate (and - * dirty?). Note, there aren't any non-uptodate buffers - * if the folio is uptodate. - * FIXME: For an uptodate folio, the buffers may need to - * be written out because they were not initialized on - * disk before. + * Skip $MFT extent mft records and let them being written + * by writeback to avioid deadlocks. the $MFT runlist + * lock must be taken before $MFT extent mrec_lock is taken. */ - if (!folio_test_uptodate(folio)) { - // TODO: - // Zero any non-uptodate buffers up to i_size. - // Set them uptodate and dirty. - } - // TODO: - // Update initialized size in the attribute and in the - // inode (up to i_size). - // Update iblock. - // FIXME: This is inefficient. Try to batch the two - // size changes to happen in one go. - ntfs_error(vol->sb, "Writing beyond initialized size " - "is not supported yet. Sorry."); - err =3D -EOPNOTSUPP; - break; - // Do NOT set_buffer_new() BUT DO clear buffer range - // outside write request range. - // set_buffer_uptodate() on complete buffers as well as - // set_buffer_dirty(). - } - - /* No need to map buffers that are already mapped. */ - if (buffer_mapped(bh)) - continue; - - /* Unmapped, dirty buffer. Need to map it. */ - bh->b_bdev =3D vol->sb->s_bdev; - - /* Convert block into corresponding vcn and offset. */ - vcn =3D (VCN)block << blocksize_bits; - vcn_ofs =3D vcn & vol->cluster_size_mask; - vcn >>=3D vol->cluster_size_bits; - if (!rl) { -lock_retry_remap: - down_read(&ni->runlist.lock); - rl =3D ni->runlist.rl; - } - if (likely(rl !=3D NULL)) { - /* Seek to element containing target vcn. */ - while (rl->length && rl[1].vcn <=3D vcn) - rl++; - lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); - } else - lcn =3D LCN_RL_NOT_MAPPED; - /* Successful remap. */ - if (lcn >=3D 0) { - /* Setup buffer head to point to correct block. */ - bh->b_blocknr =3D ((lcn << vol->cluster_size_bits) + - vcn_ofs) >> blocksize_bits; - set_buffer_mapped(bh); - continue; - } - /* It is a hole, need to instantiate it. */ - if (lcn =3D=3D LCN_HOLE) { - u8 *kaddr; - unsigned long *bpos, *bend; - - /* Check if the buffer is zero. */ - kaddr =3D kmap_local_folio(folio, bh_offset(bh)); - bpos =3D (unsigned long *)kaddr; - bend =3D (unsigned long *)(kaddr + blocksize); - do { - if (unlikely(*bpos)) - break; - } while (likely(++bpos < bend)); - kunmap_local(kaddr); - if (bpos =3D=3D bend) { - /* - * Buffer is zero and sparse, no need to write - * it. - */ - bh->b_blocknr =3D -1; - clear_buffer_dirty(bh); + if (tni && tni->nr_extents < 0 && + tni->ext.base_ntfs_ino =3D=3D NTFS_I(vol->mft_ino)) { + mutex_unlock(&tni->mrec_lock); + atomic_dec(&tni->count); + iput(vol->mft_ino); continue; } - // TODO: Instantiate the hole. - // clear_buffer_new(bh); - // clean_bdev_bh_alias(bh); - ntfs_error(vol->sb, "Writing into sparse regions is " - "not supported yet. Sorry."); - err =3D -EOPNOTSUPP; - break; - } - /* If first try and runlist unmapped, map and retry. */ - if (!is_retry && lcn =3D=3D LCN_RL_NOT_MAPPED) { - is_retry =3D true; - /* - * Attempt to map runlist, dropping lock for - * the duration. - */ - up_read(&ni->runlist.lock); - err =3D ntfs_map_runlist(ni, vcn); - if (likely(!err)) - goto lock_retry_remap; - rl =3D NULL; - } else if (!rl) - up_read(&ni->runlist.lock); - /* - * If buffer is outside the runlist, truncate has cut it out - * of the runlist. Just clean and clear the buffer and set it - * uptodate so it can get discarded by the VM. - */ - if (err =3D=3D -ENOENT || lcn =3D=3D LCN_ENOENT) { - bh->b_blocknr =3D -1; - clear_buffer_dirty(bh); - folio_zero_range(folio, bh_offset(bh), blocksize); - set_buffer_uptodate(bh); - err =3D 0; - continue; - } - /* Failed to map the buffer, even after retrying. */ - if (!err) - err =3D -EIO; - bh->b_blocknr =3D -1; - ntfs_error(vol->sb, "Failed to write to inode 0x%lx, " - "attribute type 0x%x, vcn 0x%llx, offset 0x%x " - "because its location on disk could not be " - "determined%s (error code %i).", ni->mft_no, - ni->type, (unsigned long long)vcn, - vcn_ofs, is_retry ? " even after " - "retrying" : "", err); - break; - } while (block++, (bh =3D bh->b_this_page) !=3D head); - - /* Release the lock if we took it. */ - if (rl) - up_read(&ni->runlist.lock); - - /* For the error case, need to reset bh to the beginning. */ - bh =3D head; - - /* Just an optimization, so ->read_folio() is not called later. */ - if (unlikely(!folio_test_uptodate(folio))) { - int uptodate =3D 1; - do { - if (!buffer_uptodate(bh)) { - uptodate =3D 0; - bh =3D head; - break; - } - } while ((bh =3D bh->b_this_page) !=3D head); - if (uptodate) - folio_mark_uptodate(folio); - } =20 - /* Setup all mapped, dirty buffers for async write i/o. */ - do { - if (buffer_mapped(bh) && buffer_dirty(bh)) { - lock_buffer(bh); - if (test_clear_buffer_dirty(bh)) { - BUG_ON(!buffer_uptodate(bh)); - mark_buffer_async_write(bh); - } else - unlock_buffer(bh); - } else if (unlikely(err)) { - /* - * For the error case. The buffer may have been set - * dirty during attachment to a dirty folio. - */ - if (err !=3D -ENOMEM) - clear_buffer_dirty(bh); - } - } while ((bh =3D bh->b_this_page) !=3D head); - - if (unlikely(err)) { - // TODO: Remove the -EOPNOTSUPP check later on... - if (unlikely(err =3D=3D -EOPNOTSUPP)) - err =3D 0; - else if (err =3D=3D -ENOMEM) { - ntfs_warning(vol->sb, "Error allocating memory. " - "Redirtying folio so we try again " - "later."); /* - * Put the folio back on mapping->dirty_pages, but - * leave its buffer's dirty state as-is. + * The record should be written. If a locked ntfs + * inode was returned, add it to the array of locked + * ntfs inodes. */ - folio_redirty_for_writepage(wbc, folio); - err =3D 0; - } else - folio_set_error(folio); - } + if (tni) + locked_nis[nr_locked_nis++] =3D tni; =20 - BUG_ON(folio_test_writeback(folio)); - folio_start_writeback(folio); /* Keeps try_to_free_buffers() away. */ + if (bio && (mft_ofs !=3D prev_mft_ofs + vol->mft_record_size)) { +flush_bio: + flush_dcache_folio(folio); + bio->bi_end_io =3D ntfs_bio_end_io; + submit_bio(bio); + bio =3D NULL; + } =20 - /* Submit the prepared buffers for i/o. */ - need_end_writeback =3D true; - do { - struct buffer_head *next =3D bh->b_this_page; - if (buffer_async_write(bh)) { - submit_bh(REQ_OP_WRITE, bh); - need_end_writeback =3D false; - } - bh =3D next; - } while (bh !=3D head); - folio_unlock(folio); + if (vol->cluster_size < folio_size(folio)) { + down_write(&ni->runlist.lock); + rl =3D ntfs_attr_vcn_to_rl(ni, vcn_off, &lcn); + up_write(&ni->runlist.lock); + if (IS_ERR(rl) || lcn < 0) { + err =3D -EIO; + goto unm_done; + } =20 - /* If no i/o was started, need to end writeback here. */ - if (unlikely(need_end_writeback)) - folio_end_writeback(folio); + if (bio && + (bio_end_sector(bio) >> (vol->cluster_size_bits - 9)) !=3D + lcn) { + flush_dcache_folio(folio); + bio->bi_end_io =3D ntfs_bio_end_io; + submit_bio(bio); + bio =3D NULL; + } + } =20 - ntfs_debug("Done."); - return err; -} + if (!bio) { + unsigned int off; =20 -/** - * ntfs_write_mst_block - write a @page to the backing store - * @page: page cache page to write out - * @wbc: writeback control structure - * - * This function is for writing pages belonging to non-resident, mst prote= cted - * attributes to their backing store. The only supported attributes are i= ndex - * allocation and $MFT/$DATA. Both directory inodes and index inodes are - * supported for the index allocation case. - * - * The page must remain locked for the duration of the write because we ap= ply - * the mst fixups, write, and then undo the fixups, so if we were to unloc= k the - * page before undoing the fixups, any other user of the page will see the - * page contents as corrupt. - * - * We clear the page uptodate flag for the duration of the function to ens= ure - * exclusion for the $MFT/$DATA case against someone mapping an mft record= we - * are about to apply the mst fixups to. - * - * Return 0 on success and -errno on error. - * - * Based on ntfs_write_block(), ntfs_mft_writepage(), and - * write_mft_record_nolock(). - */ -static int ntfs_write_mst_block(struct page *page, - struct writeback_control *wbc) -{ - sector_t block, dblock, rec_block; - struct inode *vi =3D page->mapping->host; - ntfs_inode *ni =3D NTFS_I(vi); - ntfs_volume *vol =3D ni->vol; - u8 *kaddr; - unsigned int rec_size =3D ni->itype.index.block_size; - ntfs_inode *locked_nis[PAGE_SIZE / NTFS_BLOCK_SIZE]; - struct buffer_head *bh, *head, *tbh, *rec_start_bh; - struct buffer_head *bhs[MAX_BUF_PER_PAGE]; - runlist_element *rl; - int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2; - unsigned bh_size, rec_size_bits; - bool sync, is_mft, page_is_dirty, rec_is_dirty; - unsigned char bh_size_bits; - - if (WARN_ON(rec_size < NTFS_BLOCK_SIZE)) - return -EINVAL; - - ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " - "0x%lx.", vi->i_ino, ni->type, page->index); - BUG_ON(!NInoNonResident(ni)); - BUG_ON(!NInoMstProtected(ni)); - is_mft =3D (S_ISREG(vi->i_mode) && !vi->i_ino); - /* - * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page - * in its page cache were to be marked dirty. However this should - * never happen with the current driver and considering we do not - * handle this case here we do want to BUG(), at least for now. - */ - BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) || - (NInoAttr(ni) && ni->type =3D=3D AT_INDEX_ALLOCATION))); - bh_size =3D vol->sb->s_blocksize; - bh_size_bits =3D vol->sb->s_blocksize_bits; - max_bhs =3D PAGE_SIZE / bh_size; - BUG_ON(!max_bhs); - BUG_ON(max_bhs > MAX_BUF_PER_PAGE); - - /* Were we called for sync purposes? */ - sync =3D (wbc->sync_mode =3D=3D WB_SYNC_ALL); - - /* Make sure we have mapped buffers. */ - bh =3D head =3D page_buffers(page); - BUG_ON(!bh); - - rec_size_bits =3D ni->itype.index.block_size_bits; - BUG_ON(!(PAGE_SIZE >> rec_size_bits)); - bhs_per_rec =3D rec_size >> bh_size_bits; - BUG_ON(!bhs_per_rec); - - /* The first block in the page. */ - rec_block =3D block =3D (sector_t)page->index << - (PAGE_SHIFT - bh_size_bits); - - /* The first out of bounds block for the data size. */ - dblock =3D (i_size_read(vi) + bh_size - 1) >> bh_size_bits; - - rl =3D NULL; - err =3D err2 =3D nr_bhs =3D nr_recs =3D nr_locked_nis =3D 0; - page_is_dirty =3D rec_is_dirty =3D false; - rec_start_bh =3D NULL; - do { - bool is_retry =3D false; - - if (likely(block < rec_block)) { - if (unlikely(block >=3D dblock)) { - clear_buffer_dirty(bh); - set_buffer_uptodate(bh); - continue; - } - /* - * This block is not the first one in the record. We - * ignore the buffer's dirty state because we could - * have raced with a parallel mark_ntfs_record_dirty(). - */ - if (!rec_is_dirty) - continue; - if (unlikely(err2)) { - if (err2 !=3D -ENOMEM) - clear_buffer_dirty(bh); - continue; - } - } else /* if (block =3D=3D rec_block) */ { - BUG_ON(block > rec_block); - /* This block is the first one in the record. */ - rec_block +=3D bhs_per_rec; - err2 =3D 0; - if (unlikely(block >=3D dblock)) { - clear_buffer_dirty(bh); - continue; - } - if (!buffer_dirty(bh)) { - /* Clean records are not written out. */ - rec_is_dirty =3D false; - continue; - } - rec_is_dirty =3D true; - rec_start_bh =3D bh; - } - /* Need to map the buffer if it is not mapped already. */ - if (unlikely(!buffer_mapped(bh))) { - VCN vcn; - LCN lcn; - unsigned int vcn_ofs; - - bh->b_bdev =3D vol->sb->s_bdev; - /* Obtain the vcn and offset of the current block. */ - vcn =3D (VCN)block << bh_size_bits; - vcn_ofs =3D vcn & vol->cluster_size_mask; - vcn >>=3D vol->cluster_size_bits; - if (!rl) { -lock_retry_remap: - down_read(&ni->runlist.lock); - rl =3D ni->runlist.rl; + off =3D ((mft_no << vol->mft_record_size_bits) + + mft_record_off) & vol->cluster_size_mask; + + bio =3D bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, + GFP_NOIO); + bio->bi_iter.bi_sector =3D + NTFS_B_TO_SECTOR(vol, NTFS_CLU_TO_B(vol, lcn) + off); } - if (likely(rl !=3D NULL)) { - /* Seek to element containing target vcn. */ - while (rl->length && rl[1].vcn <=3D vcn) - rl++; - lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); - } else - lcn =3D LCN_RL_NOT_MAPPED; - /* Successful remap. */ - if (likely(lcn >=3D 0)) { - /* Setup buffer head to correct block. */ - bh->b_blocknr =3D ((lcn << - vol->cluster_size_bits) + - vcn_ofs) >> bh_size_bits; - set_buffer_mapped(bh); - } else { - /* - * Remap failed. Retry to map the runlist once - * unless we are working on $MFT which always - * has the whole of its runlist in memory. - */ - if (!is_mft && !is_retry && - lcn =3D=3D LCN_RL_NOT_MAPPED) { - is_retry =3D true; - /* - * Attempt to map runlist, dropping - * lock for the duration. - */ - up_read(&ni->runlist.lock); - err2 =3D ntfs_map_runlist(ni, vcn); - if (likely(!err2)) - goto lock_retry_remap; - if (err2 =3D=3D -ENOMEM) - page_is_dirty =3D true; - lcn =3D err2; - } else { - err2 =3D -EIO; - if (!rl) - up_read(&ni->runlist.lock); - } - /* Hard error. Abort writing this record. */ - if (!err || err =3D=3D -ENOMEM) - err =3D err2; - bh->b_blocknr =3D -1; - ntfs_error(vol->sb, "Cannot write ntfs record " - "0x%llx (inode 0x%lx, " - "attribute type 0x%x) because " - "its location on disk could " - "not be determined (error " - "code %lli).", - (long long)block << - bh_size_bits >> - vol->mft_record_size_bits, - ni->mft_no, ni->type, - (long long)lcn); - /* - * If this is not the first buffer, remove the - * buffers in this record from the list of - * buffers to write and clear their dirty bit - * if not error -ENOMEM. - */ - if (rec_start_bh !=3D bh) { - while (bhs[--nr_bhs] !=3D rec_start_bh) - ; - if (err2 !=3D -ENOMEM) { - do { - clear_buffer_dirty( - rec_start_bh); - } while ((rec_start_bh =3D - rec_start_bh-> - b_this_page) !=3D - bh); - } - } - continue; + + if (vol->cluster_size =3D=3D NTFS_BLOCK_SIZE && + (mft_record_off || + rl->length - (vcn_off - rl->vcn) =3D=3D 1 || + mft_ofs + NTFS_BLOCK_SIZE >=3D PAGE_SIZE)) + folio_sz =3D NTFS_BLOCK_SIZE; + else + folio_sz =3D vol->mft_record_size; + if (!bio_add_folio(bio, folio, folio_sz, + mft_ofs + mft_record_off)) { + err =3D -EIO; + bio_put(bio); + goto unm_done; } - } - BUG_ON(!buffer_uptodate(bh)); - BUG_ON(nr_bhs >=3D max_bhs); - bhs[nr_bhs++] =3D bh; - } while (block++, (bh =3D bh->b_this_page) !=3D head); - if (unlikely(rl)) - up_read(&ni->runlist.lock); - /* If there were no dirty buffers, we are done. */ - if (!nr_bhs) - goto done; - /* Map the page so we can access its contents. */ - kaddr =3D kmap(page); - /* Clear the page uptodate flag whilst the mst fixups are applied. */ - BUG_ON(!PageUptodate(page)); - ClearPageUptodate(page); - for (i =3D 0; i < nr_bhs; i++) { - unsigned int ofs; - - /* Skip buffers which are not at the beginning of records. */ - if (i % bhs_per_rec) - continue; - tbh =3D bhs[i]; - ofs =3D bh_offset(tbh); - if (is_mft) { - ntfs_inode *tni; - unsigned long mft_no; - - /* Get the mft record number. */ - mft_no =3D (((s64)page->index << PAGE_SHIFT) + ofs) - >> rec_size_bits; - /* Check whether to write this mft record. */ - tni =3D NULL; - if (!ntfs_may_write_mft_record(vol, mft_no, - (MFT_RECORD*)(kaddr + ofs), &tni)) { - /* - * The record should not be written. This - * means we need to redirty the page before - * returning. - */ - page_is_dirty =3D true; - /* - * Remove the buffers in this mft record from - * the list of buffers to write. - */ - do { - bhs[i] =3D NULL; - } while (++i % bhs_per_rec); - continue; + mft_record_off +=3D folio_sz; + + if (mft_record_off !=3D vol->mft_record_size) { + vcn_off++; + goto flush_bio; } - /* - * The record should be written. If a locked ntfs - * inode was returned, add it to the array of locked - * ntfs inodes. - */ - if (tni) - locked_nis[nr_locked_nis++] =3D tni; - } - /* Apply the mst protection fixups. */ - err2 =3D pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs), - rec_size); - if (unlikely(err2)) { - if (!err || err =3D=3D -ENOMEM) - err =3D -EIO; - ntfs_error(vol->sb, "Failed to apply mst fixups " - "(inode 0x%lx, attribute type 0x%x, " - "page index 0x%lx, page offset 0x%x)!" - " Unmount and run chkdsk.", vi->i_ino, - ni->type, page->index, ofs); - /* - * Mark all the buffers in this record clean as we do - * not want to write corrupt data to disk. - */ - do { - clear_buffer_dirty(bhs[i]); - bhs[i] =3D NULL; - } while (++i % bhs_per_rec); - continue; - } - nr_recs++; - } - /* If no records are to be written out, we are done. */ - if (!nr_recs) - goto unm_done; - flush_dcache_page(page); - /* Lock buffers and start synchronous write i/o on them. */ - for (i =3D 0; i < nr_bhs; i++) { - tbh =3D bhs[i]; - if (!tbh) - continue; - if (!trylock_buffer(tbh)) - BUG(); - /* The buffer dirty state is now irrelevant, just clean it. */ - clear_buffer_dirty(tbh); - BUG_ON(!buffer_uptodate(tbh)); - BUG_ON(!buffer_mapped(tbh)); - get_bh(tbh); - tbh->b_end_io =3D end_buffer_write_sync; - submit_bh(REQ_OP_WRITE, tbh); - } - /* Synchronize the mft mirror now if not @sync. */ - if (is_mft && !sync) - goto do_mirror; -do_wait: - /* Wait on i/o completion of buffers. */ - for (i =3D 0; i < nr_bhs; i++) { - tbh =3D bhs[i]; - if (!tbh) - continue; - wait_on_buffer(tbh); - if (unlikely(!buffer_uptodate(tbh))) { - ntfs_error(vol->sb, "I/O error while writing ntfs " - "record buffer (inode 0x%lx, " - "attribute type 0x%x, page index " - "0x%lx, page offset 0x%lx)! Unmount " - "and run chkdsk.", vi->i_ino, ni->type, - page->index, bh_offset(tbh)); - if (!err || err =3D=3D -ENOMEM) - err =3D -EIO; - /* - * Set the buffer uptodate so the page and buffer - * states do not become out of sync. - */ - set_buffer_uptodate(tbh); - } - } - /* If @sync, now synchronize the mft mirror. */ - if (is_mft && sync) { -do_mirror: - for (i =3D 0; i < nr_bhs; i++) { - unsigned long mft_no; - unsigned int ofs; + prev_mft_ofs =3D mft_ofs; =20 - /* - * Skip buffers which are not at the beginning of - * records. - */ - if (i % bhs_per_rec) - continue; - tbh =3D bhs[i]; - /* Skip removed buffers (and hence records). */ - if (!tbh) - continue; - ofs =3D bh_offset(tbh); - /* Get the mft record number. */ - mft_no =3D (((s64)page->index << PAGE_SHIFT) + ofs) - >> rec_size_bits; if (mft_no < vol->mftmirr_size) ntfs_sync_mft_mirror(vol, mft_no, - (MFT_RECORD*)(kaddr + ofs), - sync); + (struct mft_record *)(kaddr + mft_ofs)); } - if (!sync) - goto do_wait; + } - /* Remove the mst protection fixups again. */ - for (i =3D 0; i < nr_bhs; i++) { - if (!(i % bhs_per_rec)) { - tbh =3D bhs[i]; - if (!tbh) - continue; - post_write_mst_fixup((NTFS_RECORD*)(kaddr + - bh_offset(tbh))); - } + + if (bio) { + flush_dcache_folio(folio); + bio->bi_end_io =3D ntfs_bio_end_io; + submit_bio(bio); } - flush_dcache_page(page); + flush_dcache_folio(folio); unm_done: + folio_mark_uptodate(folio); + kunmap_local(kaddr); + + folio_start_writeback(folio); + folio_unlock(folio); + folio_end_writeback(folio); + /* Unlock any locked inodes. */ while (nr_locked_nis-- > 0) { - ntfs_inode *tni, *base_tni; - =09 + struct ntfs_inode *base_tni; + tni =3D locked_nis[nr_locked_nis]; + mutex_unlock(&tni->mrec_lock); + /* Get the base inode. */ mutex_lock(&tni->extent_lock); if (tni->nr_extents >=3D 0) base_tni =3D tni; - else { + else base_tni =3D tni->ext.base_ntfs_ino; - BUG_ON(!base_tni); - } mutex_unlock(&tni->extent_lock); ntfs_debug("Unlocking %s inode 0x%lx.", tni =3D=3D base_tni ? "base" : "extent", tni->mft_no); - mutex_unlock(&tni->mrec_lock); atomic_dec(&tni->count); iput(VFS_I(base_tni)); } - SetPageUptodate(page); - kunmap(page); -done: - if (unlikely(err && err !=3D -ENOMEM)) { - /* - * Set page error if there is only one ntfs record in the page. - * Otherwise we would loose per-record granularity. - */ - if (ni->itype.index.block_size =3D=3D PAGE_SIZE) - SetPageError(page); + + if (unlikely(err && err !=3D -ENOMEM)) NVolSetErrors(vol); - } - if (page_is_dirty) { - ntfs_debug("Page still contains one or more dirty ntfs " - "records. Redirtying the page starting at " - "record 0x%lx.", page->index << - (PAGE_SHIFT - rec_size_bits)); - redirty_page_for_writepage(wbc, page); - unlock_page(page); - } else { - /* - * Keep the VM happy. This must be done otherwise the - * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though - * the page is clean. - */ - BUG_ON(PageWriteback(page)); - set_page_writeback(page); - unlock_page(page); - end_page_writeback(page); - } if (likely(!err)) ntfs_debug("Done."); return err; } =20 -/** - * ntfs_writepage - write a @page to the backing store - * @page: page cache page to write out - * @wbc: writeback control structure - * - * This is called from the VM when it wants to have a dirty ntfs page cache - * page cleaned. The VM has already locked the page and marked it clean. - * - * For non-resident attributes, ntfs_writepage() writes the @page by calli= ng - * the ntfs version of the generic block_write_full_folio() function, - * ntfs_write_block(), which in turn if necessary creates and writes the - * buffers associated with the page asynchronously. - * - * For resident attributes, OTOH, ntfs_writepage() writes the @page by cop= ying - * the data to the mft record (which at this stage is most likely in memor= y). - * The mft record is then marked dirty and written out asynchronously via = the - * vfs inode dirty code path for the inode the mft record belongs to or vi= a the - * vm page dirty code path for the page the mft record is in. - * - * Based on ntfs_read_folio() and fs/buffer.c::block_write_full_folio(). - * - * Return 0 on success and -errno on error. - */ -static int ntfs_writepage(struct page *page, struct writeback_control *wbc) -{ - struct folio *folio =3D page_folio(page); - loff_t i_size; - struct inode *vi =3D folio->mapping->host; - ntfs_inode *base_ni =3D NULL, *ni =3D NTFS_I(vi); - char *addr; - ntfs_attr_search_ctx *ctx =3D NULL; - MFT_RECORD *m =3D NULL; - u32 attr_len; - int err; - -retry_writepage: - BUG_ON(!folio_test_locked(folio)); - i_size =3D i_size_read(vi); - /* Is the folio fully outside i_size? (truncate in progress) */ - if (unlikely(folio->index >=3D (i_size + PAGE_SIZE - 1) >> - PAGE_SHIFT)) { - /* - * The folio may have dirty, unmapped buffers. Make them - * freeable here, so the page does not leak. - */ - block_invalidate_folio(folio, 0, folio_size(folio)); - folio_unlock(folio); - ntfs_debug("Write outside i_size - truncated?"); - return 0; - } - /* - * Only $DATA attributes can be encrypted and only unnamed $DATA - * attributes can be compressed. Index root can have the flags set but - * this means to create compressed/encrypted files, not that the - * attribute is compressed/encrypted. Note we need to check for - * AT_INDEX_ALLOCATION since this is the type of both directory and - * index inodes. - */ - if (ni->type !=3D AT_INDEX_ALLOCATION) { - /* If file is encrypted, deny access, just like NT4. */ - if (NInoEncrypted(ni)) { - folio_unlock(folio); - BUG_ON(ni->type !=3D AT_DATA); - ntfs_debug("Denying write access to encrypted file."); - return -EACCES; - } - /* Compressed data streams are handled in compress.c. */ - if (NInoNonResident(ni) && NInoCompressed(ni)) { - BUG_ON(ni->type !=3D AT_DATA); - BUG_ON(ni->name_len); - // TODO: Implement and replace this with - // return ntfs_write_compressed_block(page); - folio_unlock(folio); - ntfs_error(vi->i_sb, "Writing to compressed files is " - "not supported yet. Sorry."); - return -EOPNOTSUPP; - } - // TODO: Implement and remove this check. - if (NInoNonResident(ni) && NInoSparse(ni)) { - folio_unlock(folio); - ntfs_error(vi->i_sb, "Writing to sparse files is not " - "supported yet. Sorry."); - return -EOPNOTSUPP; - } - } - /* NInoNonResident() =3D=3D NInoIndexAllocPresent() */ - if (NInoNonResident(ni)) { - /* We have to zero every time due to mmap-at-end-of-file. */ - if (folio->index >=3D (i_size >> PAGE_SHIFT)) { - /* The folio straddles i_size. */ - unsigned int ofs =3D i_size & (folio_size(folio) - 1); - folio_zero_segment(folio, ofs, folio_size(folio)); - } - /* Handle mst protected attributes. */ - if (NInoMstProtected(ni)) - return ntfs_write_mst_block(page, wbc); - /* Normal, non-resident data stream. */ - return ntfs_write_block(folio, wbc); - } - /* - * Attribute is resident, implying it is not compressed, encrypted, or - * mst protected. This also means the attribute is smaller than an mft - * record and hence smaller than a folio, so can simply return error on - * any folios with index above 0. Note the attribute can actually be - * marked compressed but if it is resident the actual data is not - * compressed so we are ok to ignore the compressed flag here. - */ - BUG_ON(folio_buffers(folio)); - BUG_ON(!folio_test_uptodate(folio)); - if (unlikely(folio->index > 0)) { - ntfs_error(vi->i_sb, "BUG()! folio->index (0x%lx) > 0. " - "Aborting write.", folio->index); - BUG_ON(folio_test_writeback(folio)); - folio_start_writeback(folio); - folio_unlock(folio); - folio_end_writeback(folio); - return -EIO; - } - if (!NInoAttr(ni)) - base_ni =3D ni; - else - base_ni =3D ni->ext.base_ntfs_ino; - /* Map, pin, and lock the mft record. */ - m =3D map_mft_record(base_ni); - if (IS_ERR(m)) { - err =3D PTR_ERR(m); - m =3D NULL; - ctx =3D NULL; - goto err_out; - } - /* - * If a parallel write made the attribute non-resident, drop the mft - * record and retry the writepage. - */ - if (unlikely(NInoNonResident(ni))) { - unmap_mft_record(base_ni); - goto retry_writepage; - } - ctx =3D ntfs_attr_get_search_ctx(base_ni, m); - if (unlikely(!ctx)) { - err =3D -ENOMEM; - goto err_out; - } - err =3D ntfs_attr_lookup(ni->type, ni->name, ni->name_len, - CASE_SENSITIVE, 0, NULL, 0, ctx); - if (unlikely(err)) - goto err_out; - /* - * Keep the VM happy. This must be done otherwise - * PAGECACHE_TAG_DIRTY remains set even though the folio is clean. - */ - BUG_ON(folio_test_writeback(folio)); - folio_start_writeback(folio); - folio_unlock(folio); - attr_len =3D le32_to_cpu(ctx->attr->data.resident.value_length); - i_size =3D i_size_read(vi); - if (unlikely(attr_len > i_size)) { - /* Race with shrinking truncate or a failed truncate. */ - attr_len =3D i_size; - /* - * If the truncate failed, fix it up now. If a concurrent - * truncate, we do its job, so it does not have to do anything. - */ - err =3D ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, - attr_len); - /* Shrinking cannot fail. */ - BUG_ON(err); - } - addr =3D kmap_local_folio(folio, 0); - /* Copy the data from the folio to the mft record. */ - memcpy((u8*)ctx->attr + - le16_to_cpu(ctx->attr->data.resident.value_offset), - addr, attr_len); - /* Zero out of bounds area in the page cache folio. */ - memset(addr + attr_len, 0, folio_size(folio) - attr_len); - kunmap_local(addr); - flush_dcache_folio(folio); - flush_dcache_mft_record_page(ctx->ntfs_ino); - /* We are done with the folio. */ - folio_end_writeback(folio); - /* Finally, mark the mft record dirty, so it gets written back. */ - mark_mft_record_dirty(ctx->ntfs_ino); - ntfs_attr_put_search_ctx(ctx); - unmap_mft_record(base_ni); - return 0; -err_out: - if (err =3D=3D -ENOMEM) { - ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying " - "page so we try again later."); - /* - * Put the folio back on mapping->dirty_pages, but leave its - * buffers' dirty state as-is. - */ - folio_redirty_for_writepage(wbc, folio); - err =3D 0; - } else { - ntfs_error(vi->i_sb, "Resident attribute write failed with " - "error %i.", err); - folio_set_error(folio); - NVolSetErrors(ni->vol); - } - folio_unlock(folio); - if (ctx) - ntfs_attr_put_search_ctx(ctx); - if (m) - unmap_mft_record(base_ni); - return err; -} - -#endif /* NTFS_RW */ - /** * ntfs_bmap - map logical file block to physical device block * @mapping: address space mapping to which the block to be mapped belongs @@ -1533,26 +287,24 @@ static sector_t ntfs_bmap(struct address_space *mapp= ing, sector_t block) { s64 ofs, size; loff_t i_size; - LCN lcn; + s64 lcn; unsigned long blocksize, flags; - ntfs_inode *ni =3D NTFS_I(mapping->host); - ntfs_volume *vol =3D ni->vol; - unsigned delta; - unsigned char blocksize_bits, cluster_size_shift; + struct ntfs_inode *ni =3D NTFS_I(mapping->host); + struct ntfs_volume *vol =3D ni->vol; + unsigned int delta; + unsigned char blocksize_bits; =20 ntfs_debug("Entering for mft_no 0x%lx, logical block 0x%llx.", ni->mft_no, (unsigned long long)block); - if (ni->type !=3D AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni)) { - ntfs_error(vol->sb, "BMAP does not make sense for %s " - "attributes, returning 0.", + if (ni->type !=3D AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni) || + NInoMstProtected(ni)) { + ntfs_error(vol->sb, "BMAP does not make sense for %s attributes, returni= ng 0.", (ni->type !=3D AT_DATA) ? "non-data" : (!NInoNonResident(ni) ? "resident" : "encrypted")); return 0; } /* None of these can happen. */ - BUG_ON(NInoCompressed(ni)); - BUG_ON(NInoMstProtected(ni)); blocksize =3D vol->sb->s_blocksize; blocksize_bits =3D vol->sb->s_blocksize_bits; ofs =3D (s64)block << blocksize_bits; @@ -1567,9 +319,8 @@ static sector_t ntfs_bmap(struct address_space *mappin= g, sector_t block) */ if (unlikely(ofs >=3D size || (ofs + blocksize > size && size < i_size))) goto hole; - cluster_size_shift =3D vol->cluster_size_bits; down_read(&ni->runlist.lock); - lcn =3D ntfs_attr_vcn_to_lcn_nolock(ni, ofs >> cluster_size_shift, false); + lcn =3D ntfs_attr_vcn_to_lcn_nolock(ni, NTFS_B_TO_CLU(vol, ofs), false); up_read(&ni->runlist.lock); if (unlikely(lcn < LCN_HOLE)) { /* @@ -1589,14 +340,14 @@ static sector_t ntfs_bmap(struct address_space *mapp= ing, sector_t block) */ goto hole; case LCN_ENOMEM: - ntfs_error(vol->sb, "Not enough memory to complete " - "mapping for inode 0x%lx. " - "Returning 0.", ni->mft_no); + ntfs_error(vol->sb, + "Not enough memory to complete mapping for inode 0x%lx. Returning 0.", + ni->mft_no); break; default: - ntfs_error(vol->sb, "Failed to complete mapping for " - "inode 0x%lx. Run chkdsk. " - "Returning 0.", ni->mft_no); + ntfs_error(vol->sb, + "Failed to complete mapping for inode 0x%lx. Run chkdsk. Returning 0.= ", + ni->mft_no); break; } return 0; @@ -1613,132 +364,186 @@ static sector_t ntfs_bmap(struct address_space *ma= pping, sector_t block) */ delta =3D ofs & vol->cluster_size_mask; if (unlikely(sizeof(block) < sizeof(lcn))) { - block =3D lcn =3D ((lcn << cluster_size_shift) + delta) >> + block =3D lcn =3D (NTFS_CLU_TO_B(vol, lcn) + delta) >> blocksize_bits; /* If the block number was truncated return 0. */ if (unlikely(block !=3D lcn)) { - ntfs_error(vol->sb, "Physical block 0x%llx is too " - "large to be returned, returning 0.", - (long long)lcn); + ntfs_error(vol->sb, + "Physical block 0x%llx is too large to be returned, returning 0.", + (long long)lcn); return 0; } } else - block =3D ((lcn << cluster_size_shift) + delta) >> + block =3D (NTFS_CLU_TO_B(vol, lcn) + delta) >> blocksize_bits; ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn); return block; } =20 -/* - * ntfs_normal_aops - address space operations for normal inodes and attri= butes - * - * Note these are not used for compressed or mst protected inodes and - * attributes. - */ -const struct address_space_operations ntfs_normal_aops =3D { - .read_folio =3D ntfs_read_folio, -#ifdef NTFS_RW - .writepage =3D ntfs_writepage, - .dirty_folio =3D block_dirty_folio, -#endif /* NTFS_RW */ - .bmap =3D ntfs_bmap, - .migrate_folio =3D buffer_migrate_folio, - .is_partially_uptodate =3D block_is_partially_uptodate, - .error_remove_folio =3D generic_error_remove_folio, -}; +static void ntfs_readahead(struct readahead_control *rac) +{ + struct address_space *mapping =3D rac->mapping; + struct inode *inode =3D mapping->host; + struct ntfs_inode *ni =3D NTFS_I(inode); =20 -/* - * ntfs_compressed_aops - address space operations for compressed inodes - */ -const struct address_space_operations ntfs_compressed_aops =3D { - .read_folio =3D ntfs_read_folio, -#ifdef NTFS_RW - .writepage =3D ntfs_writepage, - .dirty_folio =3D block_dirty_folio, -#endif /* NTFS_RW */ - .migrate_folio =3D buffer_migrate_folio, - .is_partially_uptodate =3D block_is_partially_uptodate, - .error_remove_folio =3D generic_error_remove_folio, -}; + if (!NInoNonResident(ni) || NInoCompressed(ni)) { + /* No readahead for resident and compressed. */ + return; + } =20 -/* - * ntfs_mst_aops - general address space operations for mst protecteed ino= des - * and attributes - */ -const struct address_space_operations ntfs_mst_aops =3D { - .read_folio =3D ntfs_read_folio, /* Fill page with data. */ -#ifdef NTFS_RW - .writepage =3D ntfs_writepage, /* Write dirty page to disk. */ - .dirty_folio =3D filemap_dirty_folio, -#endif /* NTFS_RW */ - .migrate_folio =3D buffer_migrate_folio, - .is_partially_uptodate =3D block_is_partially_uptodate, - .error_remove_folio =3D generic_error_remove_folio, -}; + iomap_bio_readahead(rac, &ntfs_read_iomap_ops); +} =20 -#ifdef NTFS_RW +static int ntfs_mft_writepage(struct folio *folio, struct writeback_contro= l *wbc) +{ + struct address_space *mapping =3D folio->mapping; + struct inode *vi =3D mapping->host; + struct ntfs_inode *ni =3D NTFS_I(vi); + loff_t i_size; + int ret; =20 -/** - * mark_ntfs_record_dirty - mark an ntfs record dirty - * @page: page containing the ntfs record to mark dirty - * @ofs: byte offset within @page at which the ntfs record begins - * - * Set the buffers and the page in which the ntfs record is located dirty. - * - * The latter also marks the vfs inode the ntfs record belongs to dirty - * (I_DIRTY_PAGES only). - * - * If the page does not have buffers, we create them and set them uptodate. - * The page may not be locked which is why we need to handle the buffers u= nder - * the mapping->i_private_lock. Once the buffers are marked dirty we no l= onger - * need the lock since try_to_free_buffers() does not free dirty buffers. - */ -void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) { - struct address_space *mapping =3D page->mapping; - ntfs_inode *ni =3D NTFS_I(mapping->host); - struct buffer_head *bh, *head, *buffers_to_free =3D NULL; - unsigned int end, bh_size, bh_ofs; - - BUG_ON(!PageUptodate(page)); - end =3D ofs + ni->itype.index.block_size; - bh_size =3D VFS_I(ni)->i_sb->s_blocksize; - spin_lock(&mapping->i_private_lock); - if (unlikely(!page_has_buffers(page))) { - spin_unlock(&mapping->i_private_lock); - bh =3D head =3D alloc_page_buffers(page, bh_size, true); - spin_lock(&mapping->i_private_lock); - if (likely(!page_has_buffers(page))) { - struct buffer_head *tail; - - do { - set_buffer_uptodate(bh); - tail =3D bh; - bh =3D bh->b_this_page; - } while (bh); - tail->b_this_page =3D head; - attach_page_private(page, head); - } else - buffers_to_free =3D bh; + i_size =3D i_size_read(vi); + + /* We have to zero every time due to mmap-at-end-of-file. */ + if (folio->index >=3D (i_size >> PAGE_SHIFT)) { + /* The page straddles i_size. */ + unsigned int ofs =3D i_size & ~PAGE_MASK; + + folio_zero_segment(folio, ofs, PAGE_SIZE); } - bh =3D head =3D page_buffers(page); - BUG_ON(!bh); - do { - bh_ofs =3D bh_offset(bh); - if (bh_ofs + bh_size <=3D ofs) - continue; - if (unlikely(bh_ofs >=3D end)) - break; - set_buffer_dirty(bh); - } while ((bh =3D bh->b_this_page) !=3D head); - spin_unlock(&mapping->i_private_lock); - filemap_dirty_folio(mapping, page_folio(page)); - if (unlikely(buffers_to_free)) { - do { - bh =3D buffers_to_free->b_this_page; - free_buffer_head(buffers_to_free); - buffers_to_free =3D bh; - } while (buffers_to_free); + + ret =3D ntfs_write_mft_block(ni, folio, wbc); + mapping_set_error(mapping, ret); + return ret; +} + +static int ntfs_writepages(struct address_space *mapping, + struct writeback_control *wbc) +{ + struct inode *inode =3D mapping->host; + struct ntfs_inode *ni =3D NTFS_I(inode); + struct iomap_writepage_ctx wpc =3D { + .inode =3D mapping->host, + .wbc =3D wbc, + .ops =3D &ntfs_writeback_ops, + }; + + if (NVolShutdown(ni->vol)) + return -EIO; + + if (!NInoNonResident(ni)) + return 0; + + if (NInoMstProtected(ni) && ni->mft_no =3D=3D FILE_MFT) { + struct folio *folio =3D NULL; + int error; + + while ((folio =3D writeback_iter(mapping, wbc, folio, &error))) + error =3D ntfs_mft_writepage(folio, wbc); + return error; + } + + /* If file is encrypted, deny access, just like NT4. */ + if (NInoEncrypted(ni)) { + ntfs_debug("Denying write access to encrypted file."); + return -EACCES; + } + + return iomap_writepages(&wpc); +} + +static int ntfs_swap_activate(struct swap_info_struct *sis, + struct file *swap_file, sector_t *span) +{ + return iomap_swapfile_activate(sis, swap_file, span, + &ntfs_read_iomap_ops); +} + +const struct address_space_operations ntfs_aops =3D { + .read_folio =3D ntfs_read_folio, + .readahead =3D ntfs_readahead, + .writepages =3D ntfs_writepages, + .direct_IO =3D noop_direct_IO, + .dirty_folio =3D iomap_dirty_folio, + .bmap =3D ntfs_bmap, + .migrate_folio =3D filemap_migrate_folio, + .is_partially_uptodate =3D iomap_is_partially_uptodate, + .error_remove_folio =3D generic_error_remove_folio, + .release_folio =3D iomap_release_folio, + .invalidate_folio =3D iomap_invalidate_folio, + .swap_activate =3D ntfs_swap_activate, +}; + +void mark_ntfs_record_dirty(struct folio *folio) +{ + iomap_dirty_folio(folio->mapping, folio); +} + +int ntfs_dev_read(struct super_block *sb, void *buf, loff_t start, loff_t = size) +{ + pgoff_t idx, idx_end; + loff_t offset, end =3D start + size; + u32 from, to, buf_off =3D 0; + struct folio *folio; + + idx =3D start >> PAGE_SHIFT; + idx_end =3D end >> PAGE_SHIFT; + from =3D start & ~PAGE_MASK; + + if (idx =3D=3D idx_end) + idx_end++; + + for (; idx < idx_end; idx++, from =3D 0) { + folio =3D read_mapping_folio(sb->s_bdev->bd_mapping, idx, NULL); + if (IS_ERR(folio)) { + ntfs_error(sb, "Unable to read %ld page", idx); + return PTR_ERR(folio); + } + + offset =3D (loff_t)idx << PAGE_SHIFT; + to =3D min_t(u32, end - offset, PAGE_SIZE); + + memcpy_from_folio(buf + buf_off, folio, from, to); + buf_off +=3D to; + folio_put(folio); } + + return 0; } =20 -#endif /* NTFS_RW */ +int ntfs_dev_write(struct super_block *sb, void *buf, loff_t start, + loff_t size, bool wait) +{ + pgoff_t idx, idx_end; + loff_t offset, end =3D start + size; + u32 from, to, buf_off =3D 0; + struct folio *folio; + + idx =3D start >> PAGE_SHIFT; + idx_end =3D end >> PAGE_SHIFT; + from =3D start & ~PAGE_MASK; + + if (idx =3D=3D idx_end) + idx_end++; + + for (; idx < idx_end; idx++, from =3D 0) { + folio =3D read_mapping_folio(sb->s_bdev->bd_mapping, idx, NULL); + if (IS_ERR(folio)) { + ntfs_error(sb, "Unable to read %ld page", idx); + return PTR_ERR(folio); + } + + offset =3D (loff_t)idx << PAGE_SHIFT; + to =3D min_t(u32, end - offset, PAGE_SIZE); + + memcpy_to_folio(folio, from, buf + buf_off, to); + buf_off +=3D to; + folio_mark_uptodate(folio); + folio_mark_dirty(folio); + if (wait) + folio_wait_stable(folio); + folio_put(folio); + } + + return 0; +} diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c new file mode 100644 index 000000000000..3c3492e632e7 --- /dev/null +++ b/fs/ntfs/iomap.c @@ -0,0 +1,754 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * iomap callack functions + * + * Copyright (c) 2025 LG Electronics Co., Ltd. + */ + +#include +#include +#include + +#include "aops.h" +#include "attrib.h" +#include "mft.h" +#include "ntfs.h" +#include "malloc.h" +#include "iomap.h" + +/* + * ioomap_zero_range is called for an area beyond the initialized size, + * garbage values can be read, so zeroing out is needed. + */ +static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, + unsigned int len, struct folio *folio) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + unsigned long sector_size =3D 1UL << inode->i_blkbits; + loff_t start_down, end_up, init; + + if (!NInoNonResident(ni)) + goto out; + + start_down =3D round_down(pos, sector_size); + end_up =3D (pos + len - 1) | (sector_size - 1); + init =3D ni->initialized_size; + + if (init >=3D start_down && init <=3D end_up) { + if (init < pos) { + loff_t offset =3D offset_in_folio(folio, pos + len); + + if (offset =3D=3D 0) + offset =3D folio_size(folio); + folio_zero_segments(folio, + offset_in_folio(folio, init), + offset_in_folio(folio, pos), + offset, + folio_size(folio)); + + } else { + loff_t offset =3D max_t(loff_t, pos + len, init); + + offset =3D offset_in_folio(folio, offset); + if (offset =3D=3D 0) + offset =3D folio_size(folio); + folio_zero_segment(folio, + offset, + folio_size(folio)); + } + } else if (init <=3D pos) { + loff_t offset =3D 0, offset2 =3D offset_in_folio(folio, pos + len); + + if ((init >> folio_shift(folio)) =3D=3D (pos >> folio_shift(folio))) + offset =3D offset_in_folio(folio, init); + if (offset2 =3D=3D 0) + offset2 =3D folio_size(folio); + folio_zero_segments(folio, + offset, + offset_in_folio(folio, pos), + offset2, + folio_size(folio)); + } + +out: + folio_unlock(folio); + folio_put(folio); +} + +const struct iomap_write_ops ntfs_iomap_folio_ops =3D { + .put_folio =3D ntfs_iomap_put_folio, +}; + +static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offs= et, + unsigned int flags, struct iomap *iomap) +{ + struct ntfs_inode *base_ni, *ni =3D NTFS_I(inode); + struct ntfs_attr_search_ctx *ctx; + loff_t i_size; + u32 attr_len; + int err =3D 0; + char *kattr; + + if (NInoAttr(ni)) + base_ni =3D ni->ext.base_ntfs_ino; + else + base_ni =3D ni; + + ctx =3D ntfs_attr_get_search_ctx(base_ni, NULL); + if (!ctx) { + err =3D -ENOMEM; + goto out; + } + + err =3D ntfs_attr_lookup(ni->type, ni->name, ni->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx); + if (unlikely(err)) + goto out; + + attr_len =3D le32_to_cpu(ctx->attr->data.resident.value_length); + if (unlikely(attr_len > ni->initialized_size)) + attr_len =3D ni->initialized_size; + i_size =3D i_size_read(inode); + + if (unlikely(attr_len > i_size)) { + /* Race with shrinking truncate. */ + attr_len =3D i_size; + } + + if (offset >=3D attr_len) { + if (flags & IOMAP_REPORT) + err =3D -ENOENT; + else + err =3D -EFAULT; + goto out; + } + + kattr =3D (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_of= fset); + + iomap->inline_data =3D kmemdup(kattr, attr_len, GFP_KERNEL); + if (!iomap->inline_data) { + err =3D -ENOMEM; + goto out; + } + + iomap->type =3D IOMAP_INLINE; + iomap->offset =3D 0; + iomap->length =3D min_t(loff_t, attr_len, PAGE_SIZE); + +out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + + return err; +} + +static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t = offset, + loff_t length, unsigned int flags, struct iomap *iomap) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + s64 vcn; + s64 lcn; + struct runlist_element *rl; + struct ntfs_volume *vol =3D ni->vol; + loff_t vcn_ofs; + loff_t rl_length; + + vcn =3D NTFS_B_TO_CLU(vol, offset); + vcn_ofs =3D NTFS_B_TO_CLU_OFS(vol, offset); + + down_write(&ni->runlist.lock); + rl =3D ntfs_attr_vcn_to_rl(ni, vcn, &lcn); + if (IS_ERR(rl)) { + up_write(&ni->runlist.lock); + return PTR_ERR(rl); + } + + if (flags & IOMAP_REPORT) { + if (lcn < LCN_HOLE) { + up_write(&ni->runlist.lock); + return -ENOENT; + } + } else if (lcn < LCN_ENOENT) { + up_write(&ni->runlist.lock); + return -EINVAL; + } + + iomap->bdev =3D inode->i_sb->s_bdev; + iomap->offset =3D offset; + + if (lcn <=3D LCN_DELALLOC) { + if (lcn =3D=3D LCN_DELALLOC) + iomap->type =3D IOMAP_DELALLOC; + else + iomap->type =3D IOMAP_HOLE; + iomap->addr =3D IOMAP_NULL_ADDR; + } else { + if (!(flags & IOMAP_ZERO) && offset >=3D ni->initialized_size) + iomap->type =3D IOMAP_UNWRITTEN; + else + iomap->type =3D IOMAP_MAPPED; + iomap->addr =3D NTFS_CLU_TO_B(vol, lcn) + vcn_ofs; + } + + rl_length =3D NTFS_CLU_TO_B(vol, rl->length - (vcn - rl->vcn)); + + if (rl_length =3D=3D 0 && rl->lcn > LCN_DELALLOC) { + ntfs_error(inode->i_sb, + "runlist(vcn : %lld, length : %lld, lcn : %lld) is corrupted\n", + rl->vcn, rl->length, rl->lcn); + up_write(&ni->runlist.lock); + return -EIO; + } + + if (rl_length && length > rl_length - vcn_ofs) + iomap->length =3D rl_length - vcn_ofs; + else + iomap->length =3D length; + up_write(&ni->runlist.lock); + + if (!(flags & IOMAP_ZERO) && + iomap->type =3D=3D IOMAP_MAPPED && + iomap->offset < ni->initialized_size && + iomap->offset + iomap->length > ni->initialized_size) { + iomap->length =3D round_up(ni->initialized_size, 1 << inode->i_blkbits) - + iomap->offset; + } + iomap->flags |=3D IOMAP_F_MERGED; + + return 0; +} + +static int ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_= t length, + unsigned int flags, struct iomap *iomap, struct iomap *srcmap) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + int ret; + + if (NInoNonResident(ni)) + ret =3D ntfs_read_iomap_begin_non_resident(inode, offset, length, + flags, iomap); + else + ret =3D ntfs_read_iomap_begin_resident(inode, offset, flags, iomap); + + return ret; +} + +static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t len= gth, + ssize_t written, unsigned int flags, struct iomap *iomap) +{ + if (iomap->type =3D=3D IOMAP_INLINE) + kfree(iomap->inline_data); + + return written; +} + +const struct iomap_ops ntfs_read_iomap_ops =3D { + .iomap_begin =3D ntfs_read_iomap_begin, + .iomap_end =3D ntfs_read_iomap_end, +}; + +/* + * Check that the cached iomap still matches the NTFS runlist before + * iomap_zero_range() is called. if the runlist changes while iomap is + * iterating a cached iomap, iomap_zero_range() may overwrite folios + * that have been already written with valid data. + */ +static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *ioma= p) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + struct runlist_element *rl; + s64 vcn, lcn; + + if (!NInoNonResident(ni)) + return false; + + vcn =3D iomap->offset >> ni->vol->cluster_size_bits; + + down_read(&ni->runlist.lock); + rl =3D __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); + if (IS_ERR(rl)) { + up_read(&ni->runlist.lock); + return false; + } + lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); + up_read(&ni->runlist.lock); + return lcn =3D=3D LCN_DELALLOC; +} + +const struct iomap_write_ops ntfs_zero_iomap_folio_ops =3D { + .put_folio =3D ntfs_iomap_put_folio, + .iomap_valid =3D ntfs_iomap_valid, +}; + +static int ntfs_zero_read_iomap_end(struct inode *inode, loff_t pos, loff_= t length, + ssize_t written, unsigned int flags, struct iomap *iomap) +{ + if ((flags & IOMAP_ZERO) && (iomap->flags & IOMAP_F_STALE)) + return -EPERM; + return written; +} + +const struct iomap_ops ntfs_zero_read_iomap_ops =3D { + .iomap_begin =3D ntfs_read_iomap_begin, + .iomap_end =3D ntfs_zero_read_iomap_end, +}; + +int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length, boo= l bdirect) +{ + if (bdirect) { + if ((offset | length) & (SECTOR_SIZE - 1)) + return -EINVAL; + + return blkdev_issue_zeroout(inode->i_sb->s_bdev, + offset >> SECTOR_SHIFT, + length >> SECTOR_SHIFT, + GFP_NOFS, + BLKDEV_ZERO_NOUNMAP); + } + + return iomap_zero_range(inode, + offset, length, + NULL, + &ntfs_zero_read_iomap_ops, + &ntfs_zero_iomap_folio_ops, + NULL); +} + +static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t= offset, + loff_t length, struct iomap *iomap) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + struct ntfs_volume *vol =3D ni->vol; + loff_t vcn_ofs, rl_length; + struct runlist_element *rl, *rlc; + bool is_retry =3D false; + int err; + s64 vcn, lcn; + s64 max_clu_count =3D + NTFS_B_TO_CLU(vol, round_up(length, vol->cluster_size)); + + vcn =3D NTFS_B_TO_CLU(vol, offset); + vcn_ofs =3D NTFS_B_TO_CLU_OFS(vol, offset); + + down_read(&ni->runlist.lock); + rl =3D ni->runlist.rl; + if (!rl) { + up_read(&ni->runlist.lock); + err =3D ntfs_map_runlist(ni, vcn); + if (err) { + mutex_unlock(&ni->mrec_lock); + return -ENOENT; + } + down_read(&ni->runlist.lock); + rl =3D ni->runlist.rl; + } + up_read(&ni->runlist.lock); + + down_write(&ni->runlist.lock); +remap_rl: + /* Seek to element containing target vcn. */ + rl =3D __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); + if (IS_ERR(rl)) { + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + return -EIO; + } + lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); + + if (lcn <=3D LCN_RL_NOT_MAPPED && is_retry =3D=3D false) { + is_retry =3D true; + if (!ntfs_map_runlist_nolock(ni, vcn, NULL)) { + rl =3D ni->runlist.rl; + goto remap_rl; + } + } + + max_clu_count =3D min(max_clu_count, rl->length - (vcn - rl->vcn)); + if (max_clu_count =3D=3D 0) { + ntfs_error(inode->i_sb, + "runlist(vcn : %lld, length : %lld) is corrupted\n", + rl->vcn, rl->length); + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + return -EIO; + } + + iomap->bdev =3D inode->i_sb->s_bdev; + iomap->offset =3D offset; + + if (lcn <=3D LCN_DELALLOC) { + if (lcn < LCN_DELALLOC) { + max_clu_count =3D + ntfs_available_clusters_count(vol, max_clu_count); + if (max_clu_count < 0) { + err =3D max_clu_count; + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + return err; + } + } + + iomap->type =3D IOMAP_DELALLOC; + iomap->addr =3D IOMAP_NULL_ADDR; + + if (lcn <=3D LCN_HOLE) { + size_t new_rl_count; + + rlc =3D ntfs_malloc_nofs(sizeof(struct runlist_element) * 2); + if (!rlc) { + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + return -ENOMEM; + } + + rlc->vcn =3D vcn; + rlc->lcn =3D LCN_DELALLOC; + rlc->length =3D max_clu_count; + + rlc[1].vcn =3D vcn + max_clu_count; + rlc[1].lcn =3D LCN_RL_NOT_MAPPED; + rlc[1].length =3D 0; + + rl =3D ntfs_runlists_merge(&ni->runlist, rlc, 0, + &new_rl_count); + if (IS_ERR(rl)) { + ntfs_error(vol->sb, "Failed to merge runlists"); + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + ntfs_free(rlc); + return PTR_ERR(rl); + } + + ni->runlist.rl =3D rl; + ni->runlist.count =3D new_rl_count; + ni->i_dealloc_clusters +=3D max_clu_count; + } + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + + if (lcn < LCN_DELALLOC) + ntfs_hold_dirty_clusters(vol, max_clu_count); + + rl_length =3D NTFS_CLU_TO_B(vol, max_clu_count); + if (length > rl_length - vcn_ofs) + iomap->length =3D rl_length - vcn_ofs; + else + iomap->length =3D length; + + iomap->flags =3D IOMAP_F_NEW; + if (lcn <=3D LCN_HOLE) { + loff_t end =3D offset + length; + + if (vcn_ofs || ((vol->cluster_size > iomap->length) && + end < ni->initialized_size)) { + loff_t z_start, z_end; + + z_start =3D vcn << vol->cluster_size_bits; + z_end =3D min_t(loff_t, z_start + vol->cluster_size, + i_size_read(inode)); + if (z_end > z_start) + err =3D ntfs_zero_range(inode, + z_start, + z_end - z_start, + false); + } + if ((!err || err =3D=3D -EPERM) && + max_clu_count > 1 && + (iomap->length & vol->cluster_size_mask && + end < ni->initialized_size)) { + loff_t z_start, z_end; + + z_start =3D (vcn + max_clu_count - 1) << + vol->cluster_size_bits; + z_end =3D min_t(loff_t, z_start + vol->cluster_size, + i_size_read(inode)); + if (z_end > z_start) + err =3D ntfs_zero_range(inode, + z_start, + z_end - z_start, + false); + } + + if (err =3D=3D -EPERM) + err =3D 0; + if (err) { + ntfs_release_dirty_clusters(vol, max_clu_count); + return err; + } + } + } else { + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + + iomap->type =3D IOMAP_MAPPED; + iomap->addr =3D NTFS_CLU_TO_B(vol, lcn) + vcn_ofs; + + rl_length =3D NTFS_CLU_TO_B(vol, max_clu_count); + if (length > rl_length - vcn_ofs) + iomap->length =3D rl_length - vcn_ofs; + else + iomap->length =3D length; + } + + return 0; +} + +static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, lof= f_t offset, + loff_t length, unsigned int flags, struct iomap *iomap, bool mapped) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + struct ntfs_volume *vol =3D ni->vol; + loff_t vcn_ofs, rl_length; + s64 vcn, start_lcn, lcn_count; + bool balloc =3D false, update_mp; + int err; + s64 max_clu_count =3D + NTFS_B_TO_CLU(vol, round_up(length, vol->cluster_size)); + + vcn =3D NTFS_B_TO_CLU(vol, offset); + vcn_ofs =3D NTFS_B_TO_CLU_OFS(vol, offset); + + update_mp =3D (flags & IOMAP_DIRECT) || mapped || + NInoAttr(ni) || ni->mft_no < FILE_first_user; + down_write(&ni->runlist.lock); + err =3D ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count, + max_clu_count, &balloc, update_mp, + !(flags & IOMAP_DIRECT) && !mapped); + up_write(&ni->runlist.lock); + mutex_unlock(&ni->mrec_lock); + if (err) { + ni->i_dealloc_clusters =3D 0; + return err; + } + + iomap->bdev =3D inode->i_sb->s_bdev; + iomap->offset =3D offset; + + rl_length =3D NTFS_CLU_TO_B(vol, lcn_count); + if (length > rl_length - vcn_ofs) + iomap->length =3D rl_length - vcn_ofs; + else + iomap->length =3D length; + + if (start_lcn =3D=3D LCN_HOLE) + iomap->type =3D IOMAP_HOLE; + else + iomap->type =3D IOMAP_MAPPED; + if (balloc =3D=3D true) + iomap->flags =3D IOMAP_F_NEW; + + iomap->addr =3D NTFS_CLU_TO_B(vol, start_lcn) + vcn_ofs; + + if (balloc =3D=3D true) { + if (flags & IOMAP_DIRECT || mapped =3D=3D true) { + loff_t end =3D offset + length; + + if (vcn_ofs || ((vol->cluster_size > iomap->length) && + end < ni->initialized_size)) + err =3D ntfs_zero_range(inode, + start_lcn << + vol->cluster_size_bits, + vol->cluster_size, + true); + if (!err && lcn_count > 1 && + (iomap->length & vol->cluster_size_mask && + end < ni->initialized_size)) + err =3D ntfs_zero_range(inode, + (start_lcn + lcn_count - 1) << + vol->cluster_size_bits, + vol->cluster_size, + true); + } else { + if (lcn_count > ni->i_dealloc_clusters) + ni->i_dealloc_clusters =3D 0; + else + ni->i_dealloc_clusters -=3D lcn_count; + } + if (err < 0) + return err; + } + + if (mapped && iomap->offset + iomap->length > + ni->initialized_size) { + err =3D ntfs_attr_set_initialized_size(ni, iomap->offset + + iomap->length); + } + + return err; +} + +static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t off= set, + struct iomap *iomap) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + struct attr_record *a; + struct ntfs_attr_search_ctx *ctx; + u32 attr_len; + int err =3D 0; + char *kattr; + + ctx =3D ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + err =3D -ENOMEM; + goto out; + } + + err =3D ntfs_attr_lookup(ni->type, ni->name, ni->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx); + if (err) { + if (err =3D=3D -ENOENT) + err =3D -EIO; + goto out; + } + + a =3D ctx->attr; + /* The total length of the attribute value. */ + attr_len =3D le32_to_cpu(a->data.resident.value_length); + kattr =3D (u8 *)a + le16_to_cpu(a->data.resident.value_offset); + + iomap->inline_data =3D kmemdup(kattr, attr_len, GFP_KERNEL); + if (!iomap->inline_data) { + err =3D -ENOMEM; + goto out; + } + + iomap->type =3D IOMAP_INLINE; + iomap->offset =3D 0; + /* iomap requires there is only one INLINE_DATA extent */ + iomap->length =3D attr_len; + +out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + mutex_unlock(&ni->mrec_lock); + return err; +} + +static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset, + loff_t length, unsigned int flags, + struct iomap *iomap, bool da, bool mapped) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + int ret; + + if (NVolShutdown(ni->vol)) + return -EIO; + + mutex_lock(&ni->mrec_lock); + if (NInoNonResident(ni)) { + if (da) + ret =3D ntfs_write_da_iomap_begin_non_resident(inode, + offset, length, flags, iomap, mapped); + else + ret =3D ntfs_write_iomap_begin_non_resident(inode, offset, + length, iomap); + } else + ret =3D ntfs_write_iomap_begin_resident(inode, offset, iomap); + return ret; +} + +static int ntfs_write_iomap_begin(struct inode *inode, loff_t offset, + loff_t length, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap) +{ + return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, + false, false); +} + +static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t le= ngth, + ssize_t written, unsigned int flags, struct iomap *iomap) +{ + if (iomap->type =3D=3D IOMAP_INLINE) { + struct ntfs_inode *ni =3D NTFS_I(inode); + struct ntfs_attr_search_ctx *ctx; + u32 attr_len; + int err; + char *kattr; + + mutex_lock(&ni->mrec_lock); + ctx =3D ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + written =3D -ENOMEM; + mutex_unlock(&ni->mrec_lock); + goto out; + } + + err =3D ntfs_attr_lookup(ni->type, ni->name, ni->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx); + if (err) { + if (err =3D=3D -ENOENT) + err =3D -EIO; + written =3D err; + goto err_out; + } + + /* The total length of the attribute value. */ + attr_len =3D le32_to_cpu(ctx->attr->data.resident.value_length); + if (pos >=3D attr_len || pos + written > attr_len) + goto err_out; + + kattr =3D (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_o= ffset); + memcpy(kattr + pos, iomap_inline_data(iomap, pos), written); + mark_mft_record_dirty(ctx->ntfs_ino); +err_out: + ntfs_attr_put_search_ctx(ctx); + kfree(iomap->inline_data); + mutex_unlock(&ni->mrec_lock); + } + +out: + return written; +} + +const struct iomap_ops ntfs_write_iomap_ops =3D { + .iomap_begin =3D ntfs_write_iomap_begin, + .iomap_end =3D ntfs_write_iomap_end, +}; + +static int ntfs_page_mkwrite_iomap_begin(struct inode *inode, loff_t offse= t, + loff_t length, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap) +{ + return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, + true, true); +} + +const struct iomap_ops ntfs_page_mkwrite_iomap_ops =3D { + .iomap_begin =3D ntfs_page_mkwrite_iomap_begin, + .iomap_end =3D ntfs_write_iomap_end, +}; + +static int ntfs_dio_iomap_begin(struct inode *inode, loff_t offset, + loff_t length, unsigned int flags, + struct iomap *iomap, struct iomap *srcmap) +{ + return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, + true, false); +} + +const struct iomap_ops ntfs_dio_iomap_ops =3D { + .iomap_begin =3D ntfs_dio_iomap_begin, + .iomap_end =3D ntfs_write_iomap_end, +}; + +static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, + struct folio *folio, u64 offset, unsigned int len, u64 end_pos) +{ + if (offset < wpc->iomap.offset || + offset >=3D wpc->iomap.offset + wpc->iomap.length) { + int error; + + error =3D __ntfs_write_iomap_begin(wpc->inode, offset, + NTFS_I(wpc->inode)->allocated_size - offset, + IOMAP_WRITE, &wpc->iomap, true, false); + if (error) + return error; + } + + return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); +} + +const struct iomap_writeback_ops ntfs_writeback_ops =3D { + .writeback_range =3D ntfs_writeback_range, + .writeback_submit =3D iomap_ioend_writeback_submit, +}; --=20 2.25.1