blob: 7af5316da70b4b968474b9679271b359026c5a6a [file] [log] [blame]
Marek Vasut9ad82a72025-03-17 04:12:45 +01001/*
2 io.c (02.09.09)
3 exFAT file system implementation library.
4
5 Free exFAT implementation.
6 Copyright (C) 2010-2023 Andrew Nayenko
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21*/
22
23#include "exfat.h"
24#include <inttypes.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <string.h>
30#include <errno.h>
31#if defined(__APPLE__)
32#include <sys/disk.h>
33#elif defined(__OpenBSD__)
34#include <sys/param.h>
35#include <sys/disklabel.h>
36#include <sys/dkio.h>
37#include <sys/ioctl.h>
38#elif defined(__NetBSD__)
39#include <sys/ioctl.h>
40#elif __linux__
41#include <sys/mount.h>
42#endif
43#ifdef USE_UBLIO
44#include <sys/uio.h>
45#include <ublio.h>
46#endif
47
48struct exfat_dev
49{
50 int fd;
51 enum exfat_mode mode;
52 off_t size; /* in bytes */
53#ifdef USE_UBLIO
54 off_t pos;
55 ublio_filehandle_t ufh;
56#endif
57};
58
59static bool is_open(int fd)
60{
61 return fcntl(fd, F_GETFD) != -1;
62}
63
64static int open_ro(const char* spec)
65{
66 return open(spec, O_RDONLY);
67}
68
69static int open_rw(const char* spec)
70{
71 int fd = open(spec, O_RDWR);
72#ifdef __linux__
73 int ro = 0;
74
75 /*
76 This ioctl is needed because after "blockdev --setro" kernel still
77 allows to open the device in read-write mode but fails writes.
78 */
79 if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
80 {
81 close(fd);
82 errno = EROFS;
83 return -1;
84 }
85#endif
86 return fd;
87}
88
89struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
90{
91 struct exfat_dev* dev;
92 struct stat stbuf;
93#ifdef USE_UBLIO
94 struct ublio_param up;
95#endif
96
97 /* The system allocates file descriptors sequentially. If we have been
98 started with stdin (0), stdout (1) or stderr (2) closed, the system
99 will give us descriptor 0, 1 or 2 later when we open block device,
100 FUSE communication pipe, etc. As a result, functions using stdin,
101 stdout or stderr will actually work with a different thing and can
102 corrupt it. Protect descriptors 0, 1 and 2 from such misuse. */
103 while (!is_open(STDIN_FILENO)
104 || !is_open(STDOUT_FILENO)
105 || !is_open(STDERR_FILENO))
106 {
107 /* we don't need those descriptors, let them leak */
108 if (open("/dev/null", O_RDWR) == -1)
109 {
110 exfat_error("failed to open /dev/null");
111 return NULL;
112 }
113 }
114
115 dev = malloc(sizeof(struct exfat_dev));
116 if (dev == NULL)
117 {
118 exfat_error("failed to allocate memory for device structure");
119 return NULL;
120 }
121
122 switch (mode)
123 {
124 case EXFAT_MODE_RO:
125 dev->fd = open_ro(spec);
126 if (dev->fd == -1)
127 {
128 free(dev);
129 exfat_error("failed to open '%s' in read-only mode: %s", spec,
130 strerror(errno));
131 return NULL;
132 }
133 dev->mode = EXFAT_MODE_RO;
134 break;
135 case EXFAT_MODE_RW:
136 dev->fd = open_rw(spec);
137 if (dev->fd == -1)
138 {
139 free(dev);
140 exfat_error("failed to open '%s' in read-write mode: %s", spec,
141 strerror(errno));
142 return NULL;
143 }
144 dev->mode = EXFAT_MODE_RW;
145 break;
146 case EXFAT_MODE_ANY:
147 dev->fd = open_rw(spec);
148 if (dev->fd != -1)
149 {
150 dev->mode = EXFAT_MODE_RW;
151 break;
152 }
153 dev->fd = open_ro(spec);
154 if (dev->fd != -1)
155 {
156 dev->mode = EXFAT_MODE_RO;
157 exfat_warn("'%s' is write-protected, mounting read-only", spec);
158 break;
159 }
160 free(dev);
161 exfat_error("failed to open '%s': %s", spec, strerror(errno));
162 return NULL;
163 }
164
165 if (fstat(dev->fd, &stbuf) != 0)
166 {
167 close(dev->fd);
168 free(dev);
169 exfat_error("failed to fstat '%s'", spec);
170 return NULL;
171 }
172 if (!S_ISBLK(stbuf.st_mode) &&
173 !S_ISCHR(stbuf.st_mode) &&
174 !S_ISREG(stbuf.st_mode))
175 {
176 close(dev->fd);
177 free(dev);
178 exfat_error("'%s' is neither a device, nor a regular file", spec);
179 return NULL;
180 }
181
182#if defined(__APPLE__)
183 if (!S_ISREG(stbuf.st_mode))
184 {
185 uint32_t block_size = 0;
186 uint64_t blocks = 0;
187
188 if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
189 {
190 close(dev->fd);
191 free(dev);
192 exfat_error("failed to get block size");
193 return NULL;
194 }
195 if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
196 {
197 close(dev->fd);
198 free(dev);
199 exfat_error("failed to get blocks count");
200 return NULL;
201 }
202 dev->size = blocks * block_size;
203 }
204 else
205#elif defined(__OpenBSD__)
206 if (!S_ISREG(stbuf.st_mode))
207 {
208 struct disklabel lab;
209 struct partition* pp;
210 char* partition;
211
212 if (ioctl(dev->fd, DIOCGDINFO, &lab) == -1)
213 {
214 close(dev->fd);
215 free(dev);
216 exfat_error("failed to get disklabel");
217 return NULL;
218 }
219
220 /* Don't need to check that partition letter is valid as we won't get
221 this far otherwise. */
222 partition = strchr(spec, '\0') - 1;
223 pp = &(lab.d_partitions[*partition - 'a']);
224 dev->size = DL_GETPSIZE(pp) * lab.d_secsize;
225
226 if (pp->p_fstype != FS_NTFS)
227 exfat_warn("partition type is not 0x07 (NTFS/exFAT); "
228 "you can fix this with fdisk(8)");
229 }
230 else
231#elif defined(__NetBSD__)
232 if (!S_ISREG(stbuf.st_mode))
233 {
234 off_t size;
235
236 if (ioctl(dev->fd, DIOCGMEDIASIZE, &size) == -1)
237 {
238 close(dev->fd);
239 free(dev);
240 exfat_error("failed to get media size");
241 return NULL;
242 }
243 dev->size = size;
244 }
245 else
246#endif
247 {
248 /* works for Linux, FreeBSD, Solaris */
249 dev->size = exfat_seek(dev, 0, SEEK_END);
250 if (dev->size <= 0)
251 {
252 close(dev->fd);
253 free(dev);
254 exfat_error("failed to get size of '%s'", spec);
255 return NULL;
256 }
257 if (exfat_seek(dev, 0, SEEK_SET) == -1)
258 {
259 close(dev->fd);
260 free(dev);
261 exfat_error("failed to seek to the beginning of '%s'", spec);
262 return NULL;
263 }
264 }
265
266#ifdef USE_UBLIO
267 memset(&up, 0, sizeof(struct ublio_param));
268 up.up_blocksize = 256 * 1024;
269 up.up_items = 64;
270 up.up_grace = 32;
271 up.up_priv = &dev->fd;
272
273 dev->pos = 0;
274 dev->ufh = ublio_open(&up);
275 if (dev->ufh == NULL)
276 {
277 close(dev->fd);
278 free(dev);
279 exfat_error("failed to initialize ublio");
280 return NULL;
281 }
282#endif
283
284 return dev;
285}
286
287int exfat_close(struct exfat_dev* dev)
288{
289 int rc = 0;
290
291#ifdef USE_UBLIO
292 if (ublio_close(dev->ufh) != 0)
293 {
294 exfat_error("failed to close ublio");
295 rc = -EIO;
296 }
297#endif
298 if (close(dev->fd) != 0)
299 {
300 exfat_error("failed to close device: %s", strerror(errno));
301 rc = -EIO;
302 }
303 free(dev);
304 return rc;
305}
306
307int exfat_fsync(struct exfat_dev* dev)
308{
309 int rc = 0;
310
311#ifdef USE_UBLIO
312 if (ublio_fsync(dev->ufh) != 0)
313 {
314 exfat_error("ublio fsync failed");
315 rc = -EIO;
316 }
317#endif
318 if (fsync(dev->fd) != 0)
319 {
320 exfat_error("fsync failed: %s", strerror(errno));
321 rc = -EIO;
322 }
323 return rc;
324}
325
326enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
327{
328 return dev->mode;
329}
330
331off_t exfat_get_size(const struct exfat_dev* dev)
332{
333 return dev->size;
334}
335
336off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
337{
338#ifdef USE_UBLIO
339 /* XXX SEEK_CUR will be handled incorrectly */
340 return dev->pos = lseek(dev->fd, offset, whence);
341#else
342 return lseek(dev->fd, offset, whence);
343#endif
344}
345
346ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size)
347{
348#ifdef USE_UBLIO
349 ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos);
350 if (result >= 0)
351 dev->pos += size;
352 return result;
353#else
354 return read(dev->fd, buffer, size);
355#endif
356}
357
358ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size)
359{
360#ifdef USE_UBLIO
361 ssize_t result = ublio_pwrite(dev->ufh, (void*) buffer, size, dev->pos);
362 if (result >= 0)
363 dev->pos += size;
364 return result;
365#else
366 return write(dev->fd, buffer, size);
367#endif
368}
369
370ssize_t exfat_pread(struct exfat_dev* dev, void* buffer, size_t size,
371 off_t offset)
372{
373#ifdef USE_UBLIO
374 return ublio_pread(dev->ufh, buffer, size, offset);
375#else
376 return pread(dev->fd, buffer, size, offset);
377#endif
378}
379
380ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
381 off_t offset)
382{
383#ifdef USE_UBLIO
384 return ublio_pwrite(dev->ufh, (void*) buffer, size, offset);
385#else
386 return pwrite(dev->fd, buffer, size, offset);
387#endif
388}
389
390ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
391 void* buffer, size_t size, off_t offset)
392{
393 uint64_t uoffset = offset;
394 cluster_t cluster;
395 char* bufp = buffer;
396 off_t lsize, loffset, remainder;
397
398 if (offset < 0)
399 return -EINVAL;
400 if (uoffset >= node->size)
401 return 0;
402 if (size == 0)
403 return 0;
404
405 if (uoffset + size > node->valid_size)
406 {
407 ssize_t bytes = 0;
408
409 if (uoffset < node->valid_size)
410 {
411 bytes = exfat_generic_pread(ef, node, buffer,
412 node->valid_size - uoffset, offset);
413 if (bytes < 0 || (size_t) bytes < node->valid_size - uoffset)
414 return bytes;
415 }
416 memset(buffer + bytes, 0,
417 MIN(size - bytes, node->size - node->valid_size));
418 return MIN(size, node->size - uoffset);
419 }
420
421 cluster = exfat_advance_cluster(ef, node, uoffset / CLUSTER_SIZE(*ef->sb));
422 if (CLUSTER_INVALID(*ef->sb, cluster))
423 {
424 exfat_error("invalid cluster 0x%x while reading", cluster);
425 return -EIO;
426 }
427
428 loffset = uoffset % CLUSTER_SIZE(*ef->sb);
429 remainder = MIN(size, node->size - uoffset);
430 while (remainder > 0)
431 {
432 if (CLUSTER_INVALID(*ef->sb, cluster))
433 {
434 exfat_error("invalid cluster 0x%x while reading", cluster);
435 return -EIO;
436 }
437 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
438 if (exfat_pread(ef->dev, bufp, lsize,
439 exfat_c2o(ef, cluster) + loffset) < 0)
440 {
441 exfat_error("failed to read cluster %#x", cluster);
442 return -EIO;
443 }
444 bufp += lsize;
445 loffset = 0;
446 remainder -= lsize;
447 cluster = exfat_next_cluster(ef, node, cluster);
448 }
449 if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
450 exfat_update_atime(node);
451 return MIN(size, node->size - uoffset) - remainder;
452}
453
454ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
455 const void* buffer, size_t size, off_t offset)
456{
457 uint64_t uoffset = offset;
458 int rc;
459 cluster_t cluster;
460 const char* bufp = buffer;
461 off_t lsize, loffset, remainder;
462
463 if (offset < 0)
464 return -EINVAL;
465 if (uoffset > node->size)
466 {
467 rc = exfat_truncate(ef, node, uoffset, true);
468 if (rc != 0)
469 return rc;
470 }
471 if (uoffset + size > node->size)
472 {
473 rc = exfat_truncate(ef, node, uoffset + size, false);
474 if (rc != 0)
475 return rc;
476 }
477 if (size == 0)
478 return 0;
479
480 cluster = exfat_advance_cluster(ef, node, uoffset / CLUSTER_SIZE(*ef->sb));
481 if (CLUSTER_INVALID(*ef->sb, cluster))
482 {
483 exfat_error("invalid cluster 0x%x while writing", cluster);
484 return -EIO;
485 }
486
487 loffset = uoffset % CLUSTER_SIZE(*ef->sb);
488 remainder = size;
489 while (remainder > 0)
490 {
491 if (CLUSTER_INVALID(*ef->sb, cluster))
492 {
493 exfat_error("invalid cluster 0x%x while writing", cluster);
494 return -EIO;
495 }
496 lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
497 if (exfat_pwrite(ef->dev, bufp, lsize,
498 exfat_c2o(ef, cluster) + loffset) < 0)
499 {
500 exfat_error("failed to write cluster %#x", cluster);
501 return -EIO;
502 }
503 bufp += lsize;
504 loffset = 0;
505 remainder -= lsize;
506 node->valid_size = MAX(node->valid_size, uoffset + size - remainder);
507 cluster = exfat_next_cluster(ef, node, cluster);
508 }
509 if (!(node->attrib & EXFAT_ATTRIB_DIR))
510 /* directory's mtime should be updated by the caller only when it
511 creates or removes something in this directory */
512 exfat_update_mtime(node);
513 return size - remainder;
514}