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