1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NTFS kernel address space operations and page cache handling.
4 *
5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
6 * Copyright (c) 2002 Richard Russon
7 * Copyright (c) 2025 LG Electronics Co., Ltd.
8 */
9
10 #include <linux/writeback.h>
11
12 #include "attrib.h"
13 #include "mft.h"
14 #include "ntfs.h"
15 #include "debug.h"
16 #include "iomap.h"
17
ntfs_iomap_read_end_io(struct bio * bio)18 static void ntfs_iomap_read_end_io(struct bio *bio)
19 {
20 int error = blk_status_to_errno(bio->bi_status);
21 struct folio_iter iter;
22
23 bio_for_each_folio_all(iter, bio) {
24 struct folio *folio = iter.folio;
25 struct ntfs_inode *ni = NTFS_I(folio->mapping->host);
26 s64 init_size;
27 loff_t pos = folio_pos(folio);
28
29 init_size = ni->initialized_size;
30 if (pos + iter.offset < init_size &&
31 pos + iter.offset + iter.length > init_size)
32 folio_zero_segment(folio, offset_in_folio(folio, init_size),
33 iter.offset + iter.length);
34
35 iomap_finish_folio_read(folio, iter.offset, iter.length, error);
36 }
37 bio_put(bio);
38 }
39
ntfs_iomap_bio_submit_read(const struct iomap_iter * iter,struct iomap_read_folio_ctx * ctx)40 static void ntfs_iomap_bio_submit_read(const struct iomap_iter *iter,
41 struct iomap_read_folio_ctx *ctx)
42 {
43 iomap_bio_submit_read_endio(iter, ctx, ntfs_iomap_read_end_io);
44 }
45
46 static const struct iomap_read_ops ntfs_iomap_bio_read_ops = {
47 .read_folio_range = iomap_bio_read_folio_range,
48 .submit_read = ntfs_iomap_bio_submit_read,
49 };
50
51 /*
52 * ntfs_read_folio - Read data for a folio from the device
53 * @file: open file to which the folio @folio belongs or NULL
54 * @folio: page cache folio to fill with data
55 *
56 * This function handles reading data into the page cache. It first checks
57 * for specific ntfs attribute type like encryption and compression.
58 *
59 * - If the attribute is encrypted, access is denied (-EACCES) because
60 * decryption is not supported in this path.
61 * - If the attribute is non-resident and compressed, the read operation is
62 * delegated to ntfs_read_compressed_block().
63 * - For normal resident or non-resident attribute, it utilizes the generic
64 * iomap infrastructure via iomap_bio_read_folio() to perform the I/O.
65 *
66 * Return: 0 on success, or -errno on error.
67 */
ntfs_read_folio(struct file * file,struct folio * folio)68 static int ntfs_read_folio(struct file *file, struct folio *folio)
69 {
70 struct ntfs_inode *ni = NTFS_I(folio->mapping->host);
71 struct iomap_read_folio_ctx ctx = {
72 .cur_folio = folio,
73 .ops = &ntfs_iomap_bio_read_ops,
74 };
75
76 /*
77 * Only $DATA attributes can be encrypted and only unnamed $DATA
78 * attributes can be compressed. Index root can have the flags set but
79 * this means to create compressed/encrypted files, not that the
80 * attribute is compressed/encrypted. Note we need to check for
81 * AT_INDEX_ALLOCATION since this is the type of both directory and
82 * index inodes.
83 */
84 if (ni->type != AT_INDEX_ALLOCATION) {
85 /*
86 * EFS-encrypted files are not supported.
87 * (decryption/encryption is not implemented yet)
88 */
89 if (NInoEncrypted(ni)) {
90 folio_unlock(folio);
91 return -EOPNOTSUPP;
92 }
93 if (NInoWofCompressed(ni)) {
94 #ifdef CONFIG_NTFS_FS_WOF_COMPRESSION
95 return ntfs_read_wof_compressed_block(folio);
96 #else
97 folio_unlock(folio);
98 return -EOPNOTSUPP;
99 #endif
100 }
101 /* Compressed data streams are handled in compress.c. */
102 if (NInoNonResident(ni) && NInoCompressed(ni))
103 return ntfs_read_compressed_block(folio);
104 }
105
106 iomap_read_folio(&ntfs_read_iomap_ops, &ctx, NULL);
107 return 0;
108 }
109
110 /*
111 * ntfs_bmap - map logical file block to physical device block
112 * @mapping: address space mapping to which the block to be mapped belongs
113 * @block: logical block to map to its physical device block
114 *
115 * For regular, non-resident files (i.e. not compressed and not encrypted), map
116 * the logical @block belonging to the file described by the address space
117 * mapping @mapping to its physical device block.
118 *
119 * The size of the block is equal to the @s_blocksize field of the super block
120 * of the mounted file system which is guaranteed to be smaller than or equal
121 * to the cluster size thus the block is guaranteed to fit entirely inside the
122 * cluster which means we do not need to care how many contiguous bytes are
123 * available after the beginning of the block.
124 *
125 * Return the physical device block if the mapping succeeded or 0 if the block
126 * is sparse or there was an error.
127 *
128 * Note: This is a problem if someone tries to run bmap() on $Boot system file
129 * as that really is in block zero but there is nothing we can do. bmap() is
130 * just broken in that respect (just like it cannot distinguish sparse from
131 * not available or error).
132 */
ntfs_bmap(struct address_space * mapping,sector_t block)133 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
134 {
135 s64 ofs, size;
136 loff_t i_size;
137 s64 lcn;
138 unsigned long blocksize, flags;
139 struct ntfs_inode *ni = NTFS_I(mapping->host);
140 struct ntfs_volume *vol = ni->vol;
141 unsigned int delta;
142 unsigned char blocksize_bits;
143
144 ntfs_debug("Entering for mft_no 0x%llx, logical block 0x%llx.",
145 ni->mft_no, (unsigned long long)block);
146 if (ni->type != AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni) ||
147 NInoWofCompressed(ni) || NInoMstProtected(ni)) {
148 ntfs_error(vol->sb, "BMAP does not make sense for %s attributes, returning 0.",
149 (ni->type != AT_DATA) ? "non-data" :
150 (!NInoNonResident(ni) ? "resident" :
151 (NInoWofCompressed(ni) ? "WOF-compressed" :
152 "encrypted")));
153 return 0;
154 }
155 /* None of these can happen. */
156 blocksize = vol->sb->s_blocksize;
157 blocksize_bits = vol->sb->s_blocksize_bits;
158 ofs = (s64)block << blocksize_bits;
159 read_lock_irqsave(&ni->size_lock, flags);
160 size = ni->initialized_size;
161 i_size = i_size_read(VFS_I(ni));
162 read_unlock_irqrestore(&ni->size_lock, flags);
163 /*
164 * If the offset is outside the initialized size or the block straddles
165 * the initialized size then pretend it is a hole unless the
166 * initialized size equals the file size.
167 */
168 if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size)))
169 goto hole;
170 down_read(&ni->runlist.lock);
171 lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ntfs_bytes_to_cluster(vol, ofs),
172 false);
173 up_read(&ni->runlist.lock);
174 if (unlikely(lcn < LCN_HOLE)) {
175 /*
176 * Step down to an integer to avoid gcc doing a long long
177 * comparision in the switch when we know @lcn is between
178 * LCN_HOLE and LCN_EIO (i.e. -1 to -5).
179 *
180 * Otherwise older gcc (at least on some architectures) will
181 * try to use __cmpdi2() which is of course not available in
182 * the kernel.
183 */
184 switch ((int)lcn) {
185 case LCN_ENOENT:
186 /*
187 * If the offset is out of bounds then pretend it is a
188 * hole.
189 */
190 goto hole;
191 case LCN_ENOMEM:
192 ntfs_error(vol->sb,
193 "Not enough memory to complete mapping for inode 0x%llx. Returning 0.",
194 ni->mft_no);
195 break;
196 default:
197 ntfs_error(vol->sb,
198 "Failed to complete mapping for inode 0x%llx. Run chkdsk. Returning 0.",
199 ni->mft_no);
200 break;
201 }
202 return 0;
203 }
204 if (lcn < 0) {
205 /* It is a hole. */
206 hole:
207 ntfs_debug("Done (returning hole).");
208 return 0;
209 }
210 /*
211 * The block is really allocated and fullfils all our criteria.
212 * Convert the cluster to units of block size and return the result.
213 */
214 delta = ofs & vol->cluster_size_mask;
215 if (unlikely(sizeof(block) < sizeof(lcn))) {
216 block = lcn = (ntfs_cluster_to_bytes(vol, lcn) + delta) >>
217 blocksize_bits;
218 /* If the block number was truncated return 0. */
219 if (unlikely(block != lcn)) {
220 ntfs_error(vol->sb,
221 "Physical block 0x%llx is too large to be returned, returning 0.",
222 (long long)lcn);
223 return 0;
224 }
225 } else
226 block = (ntfs_cluster_to_bytes(vol, lcn) + delta) >>
227 blocksize_bits;
228 ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn);
229 return block;
230 }
231
ntfs_readahead(struct readahead_control * rac)232 static void ntfs_readahead(struct readahead_control *rac)
233 {
234 struct address_space *mapping = rac->mapping;
235 struct inode *inode = mapping->host;
236 struct ntfs_inode *ni = NTFS_I(inode);
237 struct iomap_read_folio_ctx ctx = {
238 .ops = &ntfs_iomap_bio_read_ops,
239 .rac = rac,
240 };
241
242 /*
243 * Resident files are not cached in the page cache,
244 * and readahead is not implemented for compressed files.
245 */
246 if (!NInoNonResident(ni) || NInoCompressed(ni) ||
247 NInoWofCompressed(ni))
248 return;
249 iomap_readahead(&ntfs_read_iomap_ops, &ctx, NULL);
250 }
251
ntfs_writepages(struct address_space * mapping,struct writeback_control * wbc)252 static int ntfs_writepages(struct address_space *mapping,
253 struct writeback_control *wbc)
254 {
255 struct inode *inode = mapping->host;
256 struct ntfs_inode *ni = NTFS_I(inode);
257 struct iomap_writepage_ctx wpc = {
258 .inode = mapping->host,
259 .wbc = wbc,
260 .ops = &ntfs_writeback_ops,
261 };
262 bool need_iput = false;
263 int ret;
264
265 if (NVolShutdown(ni->vol))
266 return -EIO;
267
268 if (!NInoNonResident(ni))
269 return 0;
270
271 /*
272 * EFS-encrypted files are not supported.
273 * (decryption/encryption is not implemented yet)
274 */
275 if (NInoEncrypted(ni)) {
276 ntfs_debug("Encrypted I/O not supported");
277 return -EOPNOTSUPP;
278 }
279
280 /*
281 * Prevent eviction in writeback to avoid deadlock in
282 * ntfs_drop_big_inode().
283 */
284 if ((ni->type == AT_DATA || ni->type == AT_INDEX_ALLOCATION) &&
285 igrab(inode))
286 need_iput = true;
287
288 ret = iomap_writepages(&wpc);
289
290 if (need_iput)
291 iput(inode);
292
293 return ret;
294 }
295
ntfs_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)296 static int ntfs_swap_activate(struct swap_info_struct *sis,
297 struct file *swap_file, sector_t *span)
298 {
299 if (NInoWofCompressed(NTFS_I(file_inode(swap_file))))
300 return -EOPNOTSUPP;
301
302 return iomap_swapfile_activate(sis, swap_file, span,
303 &ntfs_read_iomap_ops);
304 }
305
306 const struct address_space_operations ntfs_aops = {
307 .read_folio = ntfs_read_folio,
308 .readahead = ntfs_readahead,
309 .writepages = ntfs_writepages,
310 .dirty_folio = iomap_dirty_folio,
311 .bmap = ntfs_bmap,
312 .migrate_folio = filemap_migrate_folio,
313 .is_partially_uptodate = iomap_is_partially_uptodate,
314 .error_remove_folio = generic_error_remove_folio,
315 .release_folio = iomap_release_folio,
316 .invalidate_folio = iomap_invalidate_folio,
317 .swap_activate = ntfs_swap_activate,
318 };
319
320 const struct address_space_operations ntfs_mft_aops = {
321 .read_folio = ntfs_read_folio,
322 .readahead = ntfs_readahead,
323 .writepages = ntfs_mft_writepages,
324 .dirty_folio = iomap_dirty_folio,
325 .bmap = ntfs_bmap,
326 .migrate_folio = filemap_migrate_folio,
327 .is_partially_uptodate = iomap_is_partially_uptodate,
328 .error_remove_folio = generic_error_remove_folio,
329 .release_folio = iomap_release_folio,
330 .invalidate_folio = iomap_invalidate_folio,
331 };
332