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 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 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 */ 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 /* Compressed data streams are handled in compress.c. */ 94 if (NInoNonResident(ni) && NInoCompressed(ni)) 95 return ntfs_read_compressed_block(folio); 96 } 97 98 iomap_read_folio(&ntfs_read_iomap_ops, &ctx, NULL); 99 return 0; 100 } 101 102 /* 103 * ntfs_bmap - map logical file block to physical device block 104 * @mapping: address space mapping to which the block to be mapped belongs 105 * @block: logical block to map to its physical device block 106 * 107 * For regular, non-resident files (i.e. not compressed and not encrypted), map 108 * the logical @block belonging to the file described by the address space 109 * mapping @mapping to its physical device block. 110 * 111 * The size of the block is equal to the @s_blocksize field of the super block 112 * of the mounted file system which is guaranteed to be smaller than or equal 113 * to the cluster size thus the block is guaranteed to fit entirely inside the 114 * cluster which means we do not need to care how many contiguous bytes are 115 * available after the beginning of the block. 116 * 117 * Return the physical device block if the mapping succeeded or 0 if the block 118 * is sparse or there was an error. 119 * 120 * Note: This is a problem if someone tries to run bmap() on $Boot system file 121 * as that really is in block zero but there is nothing we can do. bmap() is 122 * just broken in that respect (just like it cannot distinguish sparse from 123 * not available or error). 124 */ 125 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block) 126 { 127 s64 ofs, size; 128 loff_t i_size; 129 s64 lcn; 130 unsigned long blocksize, flags; 131 struct ntfs_inode *ni = NTFS_I(mapping->host); 132 struct ntfs_volume *vol = ni->vol; 133 unsigned int delta; 134 unsigned char blocksize_bits; 135 136 ntfs_debug("Entering for mft_no 0x%llx, logical block 0x%llx.", 137 ni->mft_no, (unsigned long long)block); 138 if (ni->type != AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni) || 139 NInoMstProtected(ni)) { 140 ntfs_error(vol->sb, "BMAP does not make sense for %s attributes, returning 0.", 141 (ni->type != AT_DATA) ? "non-data" : 142 (!NInoNonResident(ni) ? "resident" : 143 "encrypted")); 144 return 0; 145 } 146 /* None of these can happen. */ 147 blocksize = vol->sb->s_blocksize; 148 blocksize_bits = vol->sb->s_blocksize_bits; 149 ofs = (s64)block << blocksize_bits; 150 read_lock_irqsave(&ni->size_lock, flags); 151 size = ni->initialized_size; 152 i_size = i_size_read(VFS_I(ni)); 153 read_unlock_irqrestore(&ni->size_lock, flags); 154 /* 155 * If the offset is outside the initialized size or the block straddles 156 * the initialized size then pretend it is a hole unless the 157 * initialized size equals the file size. 158 */ 159 if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size))) 160 goto hole; 161 down_read(&ni->runlist.lock); 162 lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ntfs_bytes_to_cluster(vol, ofs), 163 false); 164 up_read(&ni->runlist.lock); 165 if (unlikely(lcn < LCN_HOLE)) { 166 /* 167 * Step down to an integer to avoid gcc doing a long long 168 * comparision in the switch when we know @lcn is between 169 * LCN_HOLE and LCN_EIO (i.e. -1 to -5). 170 * 171 * Otherwise older gcc (at least on some architectures) will 172 * try to use __cmpdi2() which is of course not available in 173 * the kernel. 174 */ 175 switch ((int)lcn) { 176 case LCN_ENOENT: 177 /* 178 * If the offset is out of bounds then pretend it is a 179 * hole. 180 */ 181 goto hole; 182 case LCN_ENOMEM: 183 ntfs_error(vol->sb, 184 "Not enough memory to complete mapping for inode 0x%llx. Returning 0.", 185 ni->mft_no); 186 break; 187 default: 188 ntfs_error(vol->sb, 189 "Failed to complete mapping for inode 0x%llx. Run chkdsk. Returning 0.", 190 ni->mft_no); 191 break; 192 } 193 return 0; 194 } 195 if (lcn < 0) { 196 /* It is a hole. */ 197 hole: 198 ntfs_debug("Done (returning hole)."); 199 return 0; 200 } 201 /* 202 * The block is really allocated and fullfils all our criteria. 203 * Convert the cluster to units of block size and return the result. 204 */ 205 delta = ofs & vol->cluster_size_mask; 206 if (unlikely(sizeof(block) < sizeof(lcn))) { 207 block = lcn = (ntfs_cluster_to_bytes(vol, lcn) + delta) >> 208 blocksize_bits; 209 /* If the block number was truncated return 0. */ 210 if (unlikely(block != lcn)) { 211 ntfs_error(vol->sb, 212 "Physical block 0x%llx is too large to be returned, returning 0.", 213 (long long)lcn); 214 return 0; 215 } 216 } else 217 block = (ntfs_cluster_to_bytes(vol, lcn) + delta) >> 218 blocksize_bits; 219 ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn); 220 return block; 221 } 222 223 static void ntfs_readahead(struct readahead_control *rac) 224 { 225 struct address_space *mapping = rac->mapping; 226 struct inode *inode = mapping->host; 227 struct ntfs_inode *ni = NTFS_I(inode); 228 struct iomap_read_folio_ctx ctx = { 229 .ops = &ntfs_iomap_bio_read_ops, 230 .rac = rac, 231 }; 232 233 /* 234 * Resident files are not cached in the page cache, 235 * and readahead is not implemented for compressed files. 236 */ 237 if (!NInoNonResident(ni) || NInoCompressed(ni)) 238 return; 239 iomap_readahead(&ntfs_read_iomap_ops, &ctx, NULL); 240 } 241 242 static int ntfs_writepages(struct address_space *mapping, 243 struct writeback_control *wbc) 244 { 245 struct inode *inode = mapping->host; 246 struct ntfs_inode *ni = NTFS_I(inode); 247 struct iomap_writepage_ctx wpc = { 248 .inode = mapping->host, 249 .wbc = wbc, 250 .ops = &ntfs_writeback_ops, 251 }; 252 bool need_iput = false; 253 int ret; 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 /* 271 * Prevent eviction in writeback to avoid deadlock in 272 * ntfs_drop_big_inode(). 273 */ 274 if ((ni->type == AT_DATA || ni->type == AT_INDEX_ALLOCATION) && 275 igrab(inode)) 276 need_iput = true; 277 278 ret = iomap_writepages(&wpc); 279 280 if (need_iput) 281 iput(inode); 282 283 return ret; 284 } 285 286 static int ntfs_swap_activate(struct swap_info_struct *sis, 287 struct file *swap_file, sector_t *span) 288 { 289 return iomap_swapfile_activate(sis, swap_file, span, 290 &ntfs_read_iomap_ops); 291 } 292 293 const struct address_space_operations ntfs_aops = { 294 .read_folio = ntfs_read_folio, 295 .readahead = ntfs_readahead, 296 .writepages = ntfs_writepages, 297 .dirty_folio = iomap_dirty_folio, 298 .bmap = ntfs_bmap, 299 .migrate_folio = filemap_migrate_folio, 300 .is_partially_uptodate = iomap_is_partially_uptodate, 301 .error_remove_folio = generic_error_remove_folio, 302 .release_folio = iomap_release_folio, 303 .invalidate_folio = iomap_invalidate_folio, 304 .swap_activate = ntfs_swap_activate, 305 }; 306 307 const struct address_space_operations ntfs_mft_aops = { 308 .read_folio = ntfs_read_folio, 309 .readahead = ntfs_readahead, 310 .writepages = ntfs_mft_writepages, 311 .dirty_folio = iomap_dirty_folio, 312 .bmap = ntfs_bmap, 313 .migrate_folio = filemap_migrate_folio, 314 .is_partially_uptodate = iomap_is_partially_uptodate, 315 .error_remove_folio = generic_error_remove_folio, 316 .release_folio = iomap_release_folio, 317 .invalidate_folio = iomap_invalidate_folio, 318 }; 319