1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2020 Hyunchul Lee <hyc.lee@gmail.com>
4 */
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <fcntl.h>
9
10 #include "exfat_ondisk.h"
11 #include "libexfat.h"
12 #include "fsck.h"
13
write_block(struct exfat_de_iter * iter,unsigned int block)14 static ssize_t write_block(struct exfat_de_iter *iter, unsigned int block)
15 {
16 off_t device_offset;
17 struct exfat *exfat = iter->exfat;
18 struct buffer_desc *desc;
19 unsigned int i;
20
21 desc = &iter->buffer_desc[block & 0x01];
22 device_offset = exfat_c2o(exfat, desc->p_clus) + desc->offset;
23
24 for (i = 0; i < iter->read_size / iter->write_size; i++) {
25 if (desc->dirty[i]) {
26 if (exfat_write(exfat->blk_dev->dev_fd,
27 desc->buffer + i * iter->write_size,
28 iter->write_size,
29 device_offset + i * iter->write_size)
30 != (ssize_t)iter->write_size)
31 return -EIO;
32 desc->dirty[i] = 0;
33 }
34 }
35 return 0;
36 }
37
read_ahead_first_blocks(struct exfat_de_iter * iter)38 static int read_ahead_first_blocks(struct exfat_de_iter *iter)
39 {
40 #ifdef POSIX_FADV_WILLNEED
41 struct exfat *exfat = iter->exfat;
42 clus_t clus_count;
43 unsigned int size;
44
45 clus_count = iter->parent->size / exfat->clus_size;
46
47 if (clus_count > 1) {
48 iter->ra_begin_offset = 0;
49 iter->ra_next_clus = 1;
50 size = exfat->clus_size;
51 } else {
52 iter->ra_begin_offset = 0;
53 iter->ra_next_clus = 0;
54 size = iter->ra_partial_size;
55 }
56 return posix_fadvise(exfat->blk_dev->dev_fd,
57 exfat_c2o(exfat, iter->parent->first_clus), size,
58 POSIX_FADV_WILLNEED);
59 #else
60 return -ENOTSUP;
61 #endif
62 }
63
64 /**
65 * read the next fragment in advance, and assume the fragment
66 * which covers @clus is already read.
67 */
read_ahead_next_blocks(struct exfat_de_iter * iter,clus_t clus,unsigned int offset,clus_t p_clus)68 static int read_ahead_next_blocks(struct exfat_de_iter *iter,
69 clus_t clus, unsigned int offset, clus_t p_clus)
70 {
71 #ifdef POSIX_FADV_WILLNEED
72 struct exfat *exfat = iter->exfat;
73 off_t device_offset;
74 clus_t clus_count, ra_clus, ra_p_clus;
75 unsigned int size;
76 int ret = 0;
77
78 clus_count = iter->parent->size / exfat->clus_size;
79 if (clus + 1 < clus_count) {
80 ra_clus = clus + 1;
81 if (ra_clus == iter->ra_next_clus &&
82 offset >= iter->ra_begin_offset) {
83 ret = get_next_clus(exfat, iter->parent,
84 p_clus, &ra_p_clus);
85 if (ra_p_clus == EXFAT_EOF_CLUSTER)
86 return -EIO;
87
88 device_offset = exfat_c2o(exfat, ra_p_clus);
89 size = ra_clus + 1 < clus_count ?
90 exfat->clus_size : iter->ra_partial_size;
91 ret = posix_fadvise(exfat->blk_dev->dev_fd,
92 device_offset, size,
93 POSIX_FADV_WILLNEED);
94 iter->ra_next_clus = ra_clus + 1;
95 iter->ra_begin_offset = 0;
96 }
97 } else {
98 if (offset >= iter->ra_begin_offset &&
99 offset + iter->ra_partial_size <=
100 exfat->clus_size) {
101 device_offset = exfat_c2o(exfat, p_clus) +
102 offset + iter->ra_partial_size;
103 ret = posix_fadvise(exfat->blk_dev->dev_fd,
104 device_offset, iter->ra_partial_size,
105 POSIX_FADV_WILLNEED);
106 iter->ra_begin_offset =
107 offset + iter->ra_partial_size;
108 }
109 }
110
111 return ret;
112 #else
113 return -ENOTSUP;
114 #endif
115 }
116
read_ahead_next_dir_blocks(struct exfat_de_iter * iter)117 static int read_ahead_next_dir_blocks(struct exfat_de_iter *iter)
118 {
119 #ifdef POSIX_FADV_WILLNEED
120 struct exfat *exfat = iter->exfat;
121 struct list_head *current;
122 struct exfat_inode *next_inode;
123 off_t offset;
124
125 if (list_empty(&exfat->dir_list))
126 return -EINVAL;
127
128 current = exfat->dir_list.next;
129 if (iter->parent == list_entry(current, struct exfat_inode, list) &&
130 current->next != &exfat->dir_list) {
131 next_inode = list_entry(current->next, struct exfat_inode,
132 list);
133 offset = exfat_c2o(exfat, next_inode->first_clus);
134 return posix_fadvise(exfat->blk_dev->dev_fd, offset,
135 iter->ra_partial_size,
136 POSIX_FADV_WILLNEED);
137 }
138
139 return 0;
140 #else
141 return -ENOTSUP;
142 #endif
143 }
144
read_block(struct exfat_de_iter * iter,unsigned int block)145 static ssize_t read_block(struct exfat_de_iter *iter, unsigned int block)
146 {
147 struct exfat *exfat = iter->exfat;
148 struct buffer_desc *desc, *prev_desc;
149 off_t device_offset;
150 ssize_t ret;
151
152 desc = &iter->buffer_desc[block & 0x01];
153 if (block == 0) {
154 desc->p_clus = iter->parent->first_clus;
155 desc->offset = 0;
156 }
157
158 /* if the buffer already contains dirty dentries, write it */
159 if (write_block(iter, block))
160 return -EIO;
161
162 if (block > 0) {
163 if (block > iter->parent->size / iter->read_size)
164 return EOF;
165
166 prev_desc = &iter->buffer_desc[(block-1) & 0x01];
167 if (prev_desc->offset + 2 * iter->read_size <=
168 exfat->clus_size) {
169 desc->p_clus = prev_desc->p_clus;
170 desc->offset = prev_desc->offset + iter->read_size;
171 } else {
172 ret = get_next_clus(exfat, iter->parent,
173 prev_desc->p_clus, &desc->p_clus);
174 desc->offset = 0;
175 if (!ret && desc->p_clus == EXFAT_EOF_CLUSTER)
176 return EOF;
177 else if (ret)
178 return ret;
179 }
180 }
181
182 device_offset = exfat_c2o(exfat, desc->p_clus) + desc->offset;
183 ret = exfat_read(exfat->blk_dev->dev_fd, desc->buffer,
184 iter->read_size, device_offset);
185 if (ret <= 0)
186 return ret;
187
188 /*
189 * if a buffer is filled with dentries, read blocks ahead of time,
190 * otherwise read blocks of the next directory in advance.
191 */
192 if (desc->buffer[iter->read_size - 32] != EXFAT_LAST)
193 read_ahead_next_blocks(iter,
194 (block * iter->read_size) / exfat->clus_size,
195 (block * iter->read_size) % exfat->clus_size,
196 desc->p_clus);
197 else
198 read_ahead_next_dir_blocks(iter);
199 return ret;
200 }
201
exfat_de_iter_init(struct exfat_de_iter * iter,struct exfat * exfat,struct exfat_inode * dir)202 int exfat_de_iter_init(struct exfat_de_iter *iter, struct exfat *exfat,
203 struct exfat_inode *dir)
204 {
205 iter->exfat = exfat;
206 iter->parent = dir;
207 iter->write_size = exfat->sect_size;
208 iter->read_size = exfat->clus_size <= 4*KB ? exfat->clus_size : 4*KB;
209 if (exfat->clus_size <= 32 * KB)
210 iter->ra_partial_size = MAX(4 * KB, exfat->clus_size / 2);
211 else
212 iter->ra_partial_size = exfat->clus_size / 4;
213 iter->ra_partial_size = MIN(iter->ra_partial_size, 8 * KB);
214
215 if (!iter->buffer_desc)
216 iter->buffer_desc = exfat->buffer_desc;
217
218 if (iter->parent->size == 0)
219 return EOF;
220
221 read_ahead_first_blocks(iter);
222 if (read_block(iter, 0) != (ssize_t)iter->read_size) {
223 exfat_err("failed to read directory entries.\n");
224 return -EIO;
225 }
226
227 iter->de_file_offset = 0;
228 iter->next_read_offset = iter->read_size;
229 iter->max_skip_dentries = 0;
230 return 0;
231 }
232
exfat_de_iter_get(struct exfat_de_iter * iter,int ith,struct exfat_dentry ** dentry)233 int exfat_de_iter_get(struct exfat_de_iter *iter,
234 int ith, struct exfat_dentry **dentry)
235 {
236 off_t next_de_file_offset;
237 ssize_t ret;
238 unsigned int block;
239
240 next_de_file_offset = iter->de_file_offset +
241 ith * sizeof(struct exfat_dentry);
242 block = (unsigned int)(next_de_file_offset / iter->read_size);
243
244 if (next_de_file_offset + sizeof(struct exfat_dentry) >
245 iter->parent->size)
246 return EOF;
247 /* the dentry must be in current, or next block which will be read */
248 if (block > iter->de_file_offset / iter->read_size + 1)
249 return -ERANGE;
250
251 /* read next cluster if needed */
252 if (next_de_file_offset >= iter->next_read_offset) {
253 ret = read_block(iter, block);
254 if (ret != (ssize_t)iter->read_size)
255 return ret;
256 iter->next_read_offset += iter->read_size;
257 }
258
259 if (ith + 1 > iter->max_skip_dentries)
260 iter->max_skip_dentries = ith + 1;
261
262 *dentry = (struct exfat_dentry *)
263 (iter->buffer_desc[block & 0x01].buffer +
264 next_de_file_offset % iter->read_size);
265 return 0;
266 }
267
exfat_de_iter_get_dirty(struct exfat_de_iter * iter,int ith,struct exfat_dentry ** dentry)268 int exfat_de_iter_get_dirty(struct exfat_de_iter *iter,
269 int ith, struct exfat_dentry **dentry)
270 {
271 off_t next_file_offset;
272 unsigned int block;
273 int ret, sect_idx;
274
275 ret = exfat_de_iter_get(iter, ith, dentry);
276 if (!ret) {
277 next_file_offset = iter->de_file_offset +
278 ith * sizeof(struct exfat_dentry);
279 block = (unsigned int)(next_file_offset / iter->read_size);
280 sect_idx = (int)((next_file_offset % iter->read_size) /
281 iter->write_size);
282 iter->buffer_desc[block & 0x01].dirty[sect_idx] = 1;
283 }
284
285 return ret;
286 }
287
exfat_de_iter_flush(struct exfat_de_iter * iter)288 int exfat_de_iter_flush(struct exfat_de_iter *iter)
289 {
290 if (write_block(iter, 0) || write_block(iter, 1))
291 return -EIO;
292 return 0;
293 }
294
295 /*
296 * @skip_dentries must be the largest @ith + 1 of exfat_de_iter_get
297 * since the last call of exfat_de_iter_advance
298 */
exfat_de_iter_advance(struct exfat_de_iter * iter,int skip_dentries)299 int exfat_de_iter_advance(struct exfat_de_iter *iter, int skip_dentries)
300 {
301 if (skip_dentries != iter->max_skip_dentries)
302 return -EINVAL;
303
304 iter->max_skip_dentries = 0;
305 iter->de_file_offset = iter->de_file_offset +
306 skip_dentries * sizeof(struct exfat_dentry);
307 return 0;
308 }
309
exfat_de_iter_file_offset(struct exfat_de_iter * iter)310 off_t exfat_de_iter_file_offset(struct exfat_de_iter *iter)
311 {
312 return iter->de_file_offset;
313 }
314