-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathaops.c
More file actions
491 lines (465 loc) · 15 KB
/
aops.c
File metadata and controls
491 lines (465 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* 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.
*/
#include <linux/writeback.h>
#include "attrib.h"
#include "mft.h"
#include "ntfs.h"
#include "debug.h"
#include "iomap.h"
/*
* 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
*
* This function handles reading data into the page cache. It first checks
* for specific ntfs attribute type like encryption and compression.
*
* - 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, or -errno on error.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
static int ntfs_read_folio(struct file *file, struct folio *folio)
#else
static int ntfs_readpage(struct file *file, struct page *page)
#endif
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct ntfs_inode *ni = 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
* 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 != AT_INDEX_ALLOCATION) {
/*
* EFS-encrypted files are not supported.
* (decryption/encryption is not implemented yet)
*/
if (NInoEncrypted(ni)) {
folio_unlock(folio);
return -EOPNOTSUPP;
}
/* Compressed data streams are handled in compress.c. */
if (NInoNonResident(ni) && NInoCompressed(ni))
return ntfs_read_compressed_block(folio);
}
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
struct page *page = &folio->page;
#endif
loff_t i_size;
struct inode *vi;
struct ntfs_inode *ni;
BUG_ON(!PageLocked(page));
vi = page->mapping->host;
i_size = i_size_read(vi);
/* Is the page fully outside i_size? (truncate in progress) */
if (unlikely(page->index >= (i_size + PAGE_SIZE - 1) >>
PAGE_SHIFT)) {
zero_user(page, 0, PAGE_SIZE);
ntfs_debug("Read outside i_size - truncated?");
SetPageUptodate(page);
unlock_page(page);
return 0;
}
/*
* This can potentially happen because we clear PageUptodate() during
* ntfs_writepage() of MstProtected() attributes.
*/
if (PageUptodate(page)) {
unlock_page(page);
return 0;
}
ni = NTFS_I(vi);
/*
* 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 != AT_INDEX_ALLOCATION) {
/* If attribute is encrypted, deny access, just like NT4. */
if (NInoEncrypted(ni)) {
BUG_ON(ni->type != AT_DATA);
unlock_page(page);
return -EACCES;
}
/* Compressed data streams are handled in compress.c. */
if (NInoNonResident(ni) && NInoCompressed(ni)) {
BUG_ON(ni->type != AT_DATA);
BUG_ON(ni->name_len);
return ntfs_read_compressed_block(page);
}
}
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
iomap_bio_read_folio(folio, &ntfs_read_iomap_ops);
return 0;
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
return iomap_read_folio(folio, &ntfs_read_iomap_ops);
#else
return iomap_readpage(page, &ntfs_read_iomap_ops);
#endif
#endif
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0)
/*
* 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 calling
* the ntfs version of the generic block_write_full_page() 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 copying
* the data to the mft record (which at this stage is most likely in memory).
* 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 via 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_page().
*/
static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
{
loff_t i_size;
struct inode *vi = page->mapping->host;
struct ntfs_inode *ni = NTFS_I(vi);
struct iomap_writepage_ctx wpc = { };
BUG_ON(!PageLocked(page));
if (!NInoNonResident(ni)) {
unlock_page(page);
return 0;
}
i_size = i_size_read(vi);
/* Is the page fully outside i_size? (truncate in progress) */
if (unlikely(page->index >= (i_size + PAGE_SIZE - 1) >>
PAGE_SHIFT)) {
/*
* The page may have dirty, unmapped buffers. Make them
* freeable here, so the page does not leak.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
struct folio *folio = page_folio(page);
iomap_invalidate_folio(folio, 0, PAGE_SIZE);
#else
iomap_invalidatepage(page, 0, PAGE_SIZE);
#endif
unlock_page(page);
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 != AT_INDEX_ALLOCATION) {
/* If file is encrypted, deny access, just like NT4. */
if (NInoEncrypted(ni)) {
unlock_page(page);
BUG_ON(ni->type != 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 != AT_DATA);
BUG_ON(ni->name_len);
unlock_page(page);
ntfs_error(vi->i_sb, "Writing to compressed files is not supported yet. Sorry.");
return -EOPNOTSUPP;
}
}
return iomap_writepage(page, wbc, &wpc, &ntfs_writeback_ops);
}
#endif
/*
* ntfs_bmap - map logical file block to physical device block
* @mapping: address space mapping to which the block to be mapped belongs
* @block: logical block to map to its physical device block
*
* For regular, non-resident files (i.e. not compressed and not encrypted), map
* the logical @block belonging to the file described by the address space
* mapping @mapping to its physical device block.
*
* The size of the block is equal to the @s_blocksize field of the super block
* of the mounted file system which is guaranteed to be smaller than or equal
* to the cluster size thus the block is guaranteed to fit entirely inside the
* cluster which means we do not need to care how many contiguous bytes are
* available after the beginning of the block.
*
* Return the physical device block if the mapping succeeded or 0 if the block
* is sparse or there was an error.
*
* Note: This is a problem if someone tries to run bmap() on $Boot system file
* as that really is in block zero but there is nothing we can do. bmap() is
* just broken in that respect (just like it cannot distinguish sparse from
* not available or error).
*/
static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
{
s64 ofs, size;
loff_t i_size;
s64 lcn;
unsigned long blocksize, flags;
struct ntfs_inode *ni = NTFS_I(mapping->host);
struct ntfs_volume *vol = ni->vol;
unsigned int delta;
unsigned char blocksize_bits;
ntfs_debug("Entering for mft_no 0x%lx, logical block 0x%llx.",
ni->mft_no, (unsigned long long)block);
if (ni->type != AT_DATA || !NInoNonResident(ni) || NInoEncrypted(ni) ||
NInoMstProtected(ni)) {
ntfs_error(vol->sb, "BMAP does not make sense for %s attributes, returning 0.",
(ni->type != AT_DATA) ? "non-data" :
(!NInoNonResident(ni) ? "resident" :
"encrypted"));
return 0;
}
/* None of these can happen. */
blocksize = vol->sb->s_blocksize;
blocksize_bits = vol->sb->s_blocksize_bits;
ofs = (s64)block << blocksize_bits;
read_lock_irqsave(&ni->size_lock, flags);
size = ni->initialized_size;
i_size = i_size_read(VFS_I(ni));
read_unlock_irqrestore(&ni->size_lock, flags);
/*
* If the offset is outside the initialized size or the block straddles
* the initialized size then pretend it is a hole unless the
* initialized size equals the file size.
*/
if (unlikely(ofs >= size || (ofs + blocksize > size && size < i_size)))
goto hole;
down_read(&ni->runlist.lock);
lcn = ntfs_attr_vcn_to_lcn_nolock(ni, ntfs_bytes_to_cluster(vol, ofs),
false);
up_read(&ni->runlist.lock);
if (unlikely(lcn < LCN_HOLE)) {
/*
* Step down to an integer to avoid gcc doing a long long
* comparision in the switch when we know @lcn is between
* LCN_HOLE and LCN_EIO (i.e. -1 to -5).
*
* Otherwise older gcc (at least on some architectures) will
* try to use __cmpdi2() which is of course not available in
* the kernel.
*/
switch ((int)lcn) {
case LCN_ENOENT:
/*
* If the offset is out of bounds then pretend it is a
* hole.
*/
goto hole;
case LCN_ENOMEM:
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);
break;
}
return 0;
}
if (lcn < 0) {
/* It is a hole. */
hole:
ntfs_debug("Done (returning hole).");
return 0;
}
/*
* The block is really allocated and fullfils all our criteria.
* Convert the cluster to units of block size and return the result.
*/
delta = ofs & vol->cluster_size_mask;
if (unlikely(sizeof(block) < sizeof(lcn))) {
block = lcn = (ntfs_cluster_to_bytes(vol, lcn) + delta) >>
blocksize_bits;
/* If the block number was truncated return 0. */
if (unlikely(block != lcn)) {
ntfs_error(vol->sb,
"Physical block 0x%llx is too large to be returned, returning 0.",
(long long)lcn);
return 0;
}
} else
block = (ntfs_cluster_to_bytes(vol, lcn) + delta) >>
blocksize_bits;
ntfs_debug("Done (returning block 0x%llx).", (unsigned long long)lcn);
return block;
}
static void ntfs_readahead(struct readahead_control *rac)
{
struct address_space *mapping = rac->mapping;
struct inode *inode = mapping->host;
struct ntfs_inode *ni = NTFS_I(inode);
/*
* Resident files are not cached in the page cache,
* and readahead is not implemented for compressed files.
*/
if (!NInoNonResident(ni) || NInoCompressed(ni))
return;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
iomap_bio_readahead(rac, &ntfs_read_iomap_ops);
#else
iomap_readahead(rac, &ntfs_read_iomap_ops);
#endif
}
static int ntfs_writepages(struct address_space *mapping,
struct writeback_control *wbc)
{
struct inode *inode = mapping->host;
struct ntfs_inode *ni = NTFS_I(inode);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
struct iomap_writepage_ctx wpc = {
.inode = mapping->host,
.wbc = wbc,
.ops = &ntfs_writeback_ops,
};
#else
struct iomap_writepage_ctx wpc = { };
#endif
if (NVolShutdown(ni->vol))
return -EIO;
if (!NInoNonResident(ni))
return 0;
/*
* EFS-encrypted files are not supported.
* (decryption/encryption is not implemented yet)
*/
if (NInoEncrypted(ni)) {
ntfs_debug("Encrypted I/O not supported");
return -EOPNOTSUPP;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 17, 0)
return iomap_writepages(&wpc);
#else
return iomap_writepages(mapping, wbc, &wpc, &ntfs_writeback_ops);
#endif
}
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 = {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
.read_folio = ntfs_read_folio,
#else
.readpage = ntfs_readpage,
#endif
.readahead = ntfs_readahead,
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0)
.writepage = ntfs_writepage,
#endif
.writepages = ntfs_writepages,
.direct_IO = noop_direct_IO,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
.dirty_folio = iomap_dirty_folio,
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
.dirty_folio = filemap_dirty_folio,
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
.dirty_folio = iomap_dirty_folio,
#else
.set_page_dirty = __set_page_dirty_nobuffers,
#endif
#endif
#endif
.bmap = ntfs_bmap,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
.migrate_folio = filemap_migrate_folio,
#else
.migratepage = iomap_migrate_page,
#endif
.is_partially_uptodate = iomap_is_partially_uptodate,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
.error_remove_folio = generic_error_remove_folio,
#else
.error_remove_page = generic_error_remove_page,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
.release_folio = iomap_release_folio,
.invalidate_folio = iomap_invalidate_folio,
#else
.releasepage = iomap_releasepage,
.invalidatepage = iomap_invalidatepage,
#endif
.swap_activate = ntfs_swap_activate,
};
const struct address_space_operations ntfs_mft_aops = {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0)
.read_folio = ntfs_read_folio,
#else
.readpage = ntfs_readpage,
#endif
.readahead = ntfs_readahead,
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0)
.writepage = ntfs_writepage,
#endif
.writepages = ntfs_mft_writepages,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
.dirty_folio = iomap_dirty_folio,
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
.dirty_folio = filemap_dirty_folio,
#else
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)
.dirty_folio = iomap_dirty_folio,
#else
.set_page_dirty = __set_page_dirty_nobuffers,
#endif
#endif
#endif
.bmap = ntfs_bmap,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
.migrate_folio = filemap_migrate_folio,
#else
.migratepage = iomap_migrate_page,
#endif
.is_partially_uptodate = iomap_is_partially_uptodate,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 8, 0)
.error_remove_folio = generic_error_remove_folio,
#else
.error_remove_page = generic_error_remove_page,
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
.release_folio = iomap_release_folio,
.invalidate_folio = iomap_invalidate_folio,
#else
.releasepage = iomap_releasepage,
.invalidatepage = iomap_invalidatepage,
#endif
};