-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbdev-io.c
More file actions
236 lines (200 loc) · 5.83 KB
/
bdev-io.c
File metadata and controls
236 lines (200 loc) · 5.83 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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* NTFS block device I/O.
*
* Copyright (c) 2026 LG Electronics Co., Ltd.
*/
#include <linux/blkdev.h>
#include "ntfs.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 16, 0)
/*
* ntfs_bdev_read - Read data directly from block device using bio
* @bdev: block device to read from
* @data: destination buffer
* @start: starting byte offset on the block device
* @size: number of bytes to read
*
* Reads @size bytes starting from byte offset @start directly from the block
* device using one or more BIOs. This function bypasses the page cache
* completely and performs synchronous I/O with REQ_META | REQ_SYNC flags set.
*
* The @start offset must be sector-aligned (512 bytes). If it is not aligned,
* the function will return -EINVAL.
*
* If the destination buffer @data is not a vmalloc address, it falls back
* to the more efficient bdev_rw_virt() helper.
*
* Return: 0 on success, negative error code on failure.
*/
int ntfs_bdev_read(struct block_device *bdev, char *data, loff_t start, size_t size)
{
unsigned int done = 0, added;
int error;
struct bio *bio;
enum req_op op;
sector_t sector = start >> SECTOR_SHIFT;
if (start & (SECTOR_SIZE - 1))
return -EINVAL;
op = REQ_OP_READ | REQ_META | REQ_SYNC;
if (!is_vmalloc_addr(data))
return bdev_rw_virt(bdev, sector, data, size, op);
bio = bio_alloc(bdev,
bio_max_segs(DIV_ROUND_UP(size, PAGE_SIZE)),
op, GFP_KERNEL);
bio->bi_iter.bi_sector = sector;
do {
added = bio_add_vmalloc_chunk(bio, data + done, size - done);
if (!added) {
struct bio *prev = bio;
bio = bio_alloc(prev->bi_bdev,
bio_max_segs(DIV_ROUND_UP(size - done, PAGE_SIZE)),
prev->bi_opf, GFP_KERNEL);
bio->bi_iter.bi_sector = bio_end_sector(prev);
bio_chain(prev, bio);
submit_bio(prev);
}
done += added;
} while (done < size);
error = submit_bio_wait(bio);
bio_put(bio);
if (op == REQ_OP_READ)
invalidate_kernel_vmap_range(data, size);
return error;
}
#else
int ntfs_dev_read(struct super_block *sb, void *buf, loff_t start, size_t size)
{
pgoff_t idx, idx_end;
loff_t offset, end = start + size;
u32 from, to, buf_off = 0;
struct folio *folio;
idx = start >> PAGE_SHIFT;
idx_end = end >> PAGE_SHIFT;
from = start & ~PAGE_MASK;
if (idx == idx_end)
idx_end++;
for (; idx < idx_end; idx++, from = 0) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
folio = read_mapping_folio(sb->s_bdev->bd_mapping, idx, NULL);
#else
folio = read_mapping_folio(sb->s_bdev->bd_inode->i_mapping, idx, NULL);
#endif
if (IS_ERR(folio)) {
ntfs_error(sb, "Unable to read %ld page", idx);
return PTR_ERR(folio);
}
offset = (loff_t)idx << PAGE_SHIFT;
to = min_t(u32, end - offset, PAGE_SIZE);
memcpy_from_folio(buf + buf_off, folio, from, to);
buf_off += to;
folio_put(folio);
}
return 0;
}
#endif
#else
int ntfs_dev_read(struct super_block *sb, void *buf, loff_t start, size_t size)
{
pgoff_t idx, idx_end;
loff_t offset, end = start + size;
u32 from, to, buf_off = 0;
struct page *page;
char *kaddr;
idx = start >> PAGE_SHIFT;
idx_end = end >> PAGE_SHIFT;
from = start & ~PAGE_MASK;
if (idx == idx_end)
idx_end++;
for (; idx < idx_end; idx++, from = 0) {
page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, idx, NULL);
if (IS_ERR(page)) {
ntfs_error(sb, "Unable to read %ld page", idx);
return PTR_ERR(page);
}
kaddr = kmap_atomic(page);
offset = (loff_t)idx << PAGE_SHIFT;
to = min_t(u32, end - offset, PAGE_SIZE);
memcpy(buf + buf_off, kaddr + from, to);
buf_off += to;
kunmap_atomic(kaddr);
put_page(page);
}
return 0;
}
#endif
/*
* ntfs_bdev_write - Update block device contents via page cache
* @sb: super block of the mounted NTFS filesystem
* @buf: source buffer containing data to write
* @start: starting byte offset on the block device
* @size: number of bytes to write
*
* Writes @size bytes from @buf to the block device (sb->s_bdev) starting
* at byte offset @start. The write is performed entirely through the page
* cache of the block device's address space.
*/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size)
{
pgoff_t idx, idx_end;
loff_t offset, end = start + size;
u32 from, to, buf_off = 0;
struct folio *folio;
idx = start >> PAGE_SHIFT;
idx_end = end >> PAGE_SHIFT;
from = start & ~PAGE_MASK;
if (idx == idx_end)
idx_end++;
for (; idx < idx_end; idx++, from = 0) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
folio = read_mapping_folio(sb->s_bdev->bd_mapping, idx, NULL);
#else
folio = read_mapping_folio(sb->s_bdev->bd_inode->i_mapping, idx, NULL);
#endif
if (IS_ERR(folio)) {
ntfs_error(sb, "Unable to read %ld page", idx);
return PTR_ERR(folio);
}
offset = (loff_t)idx << PAGE_SHIFT;
to = min_t(u32, end - offset, PAGE_SIZE);
memcpy_to_folio(folio, from, buf + buf_off, to);
buf_off += to;
folio_mark_uptodate(folio);
folio_mark_dirty(folio);
folio_put(folio);
}
return 0;
}
#else
int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size)
{
pgoff_t idx, idx_end;
loff_t offset, end = start + size;
u32 from, to, buf_off = 0;
struct page *page;
char *kaddr;
idx = start >> PAGE_SHIFT;
idx_end = end >> PAGE_SHIFT;
from = start & ~PAGE_MASK;
if (idx == idx_end)
idx_end++;
for (; idx < idx_end; idx++, from = 0) {
page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, idx, NULL);
if (IS_ERR(page)) {
ntfs_error(sb, "Unable to read %ld page", idx);
return PTR_ERR(page);
}
kaddr = kmap_atomic(page);
offset = (loff_t)idx << PAGE_SHIFT;
to = min_t(u32, end - offset, PAGE_SIZE);
memcpy(kaddr + from, buf + buf_off, to);
buf_off += to;
kunmap_atomic(kaddr);
SetPageUptodate(page);
set_page_dirty(page);
put_page(page);
}
return 0;
}
#endif