77c6112f71153536b413879d5ae2f4264d2dd2ac
[lwext4.git] / blockdev / linux / file_dev.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #define _LARGEFILE64_SOURCE
30 #define _FILE_OFFSET_BITS 64
31
32 #include <ext4_config.h>
33 #include <ext4_blockdev.h>
34 #include <ext4_errno.h>
35 #include <ext4_debug.h>
36 #include <stdio.h>
37 #include <stdbool.h>
38 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #ifdef __APPLE__
44 #include <fcntl.h>
45 #include <sys/disk.h>
46 #include <stdlib.h>
47 #include <sys/socket.h>
48 #endif
49
50 /**@brief   Default filename.*/
51 static const char *fname = "ext2";
52
53 /**@brief   Image block size.*/
54 #define EXT4_FILEDEV_BSIZE 512
55
56 /**@brief   Image file descriptor.*/
57 static int dev_file;
58
59 #define DROP_LINUXCACHE_BUFFERS 0
60
61 /**********************BLOCKDEV INTERFACE**************************************/
62 static int file_dev_open(struct ext4_blockdev *bdev);
63 static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
64                          uint32_t blk_cnt);
65 static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
66                           uint64_t blk_id, uint32_t blk_cnt);
67 static int file_dev_close(struct ext4_blockdev *bdev);
68
69 /******************************************************************************/
70 EXT4_BLOCKDEV_STATIC_INSTANCE(file_dev, EXT4_FILEDEV_BSIZE, 0, file_dev_open,
71                 file_dev_bread, file_dev_bwrite, file_dev_close, 0, 0);
72
73 /******************************************************************************/
74 static int file_dev_open(struct ext4_blockdev *bdev)
75 {
76         dev_file = open(fname, O_RDWR);
77
78         if (dev_file < 0) {
79                 printf("open of %s failed %d\n", fname, errno);
80                 ext4_blockdev_errno = errno;
81                 return EIO;
82         }
83
84 #ifdef __APPLE__
85         /* The lseek approach to finding the device's size does not seem
86          * to work on macOS so do it this way instead.
87          */
88         uint64_t sectors = 0;
89         if (ioctl(dev_file, DKIOCGETBLOCKCOUNT, &sectors) < 0) {
90                 printf("ioctl DKIOCGETBLOCKCOUNT failed %d\n", errno);
91                 close(dev_file);
92                 return EFAULT;
93         }
94         uint32_t sector_size = 0;
95         if (ioctl(dev_file, DKIOCGETBLOCKSIZE, &sector_size) < 0) {
96                 printf("ioctl DKIOCGETBLOCKSIZE failed %d\n", errno);
97                 close(dev_file);
98                 return EFAULT;
99         }
100
101         off_t size = sectors * sector_size;
102 #else
103         off_t size = lseek(dev_file, 0, SEEK_END);
104         if (size == ((off_t) -1)) {
105                 return EFAULT;
106         }
107
108 #endif
109
110         file_dev.part_offset = 0;
111         file_dev.part_size = size;
112         file_dev.bdif->ph_bcnt = file_dev.part_size / file_dev.bdif->ph_bsize;
113
114         return EOK;
115 }
116
117 /******************************************************************************/
118
119 static int file_dev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
120                          uint32_t blk_cnt)
121 {
122         if (lseek(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET) < 0) {
123                 ext4_blockdev_errno = errno;
124                 printf("lseek failed %d\n", errno);
125                 return EIO;
126         }
127         if (!blk_cnt)
128                 return EOK;
129         if (read(dev_file, buf, bdev->bdif->ph_bsize * blk_cnt) < 0) {
130                 ext4_blockdev_errno = errno;
131                 printf("read failed %d\n", errno);
132                 return EIO;
133         }
134
135         return EOK;
136 }
137
138 static void drop_cache(void)
139 {
140 #if defined(__linux__) && DROP_LINUXCACHE_BUFFERS
141         int fd;
142         char *data = "3";
143
144         sync();
145         fd = open("/proc/sys/vm/drop_caches", O_WRONLY);
146         write(fd, data, sizeof(char));
147         close(fd);
148 #endif
149 }
150
151 /******************************************************************************/
152 static int file_dev_bwrite(struct ext4_blockdev *bdev, const void *buf,
153                           uint64_t blk_id, uint32_t blk_cnt)
154 {
155         if (lseek(dev_file, blk_id * bdev->bdif->ph_bsize, SEEK_SET) < 0) {
156                 ext4_blockdev_errno = errno;
157                 printf("fseeko failed %d\n", errno);
158                 return EIO;
159         }
160         if (!blk_cnt)
161                 return EOK;
162         if (write(dev_file, buf, bdev->bdif->ph_bsize * blk_cnt) < 0) {
163                 ext4_blockdev_errno = errno;
164                 printf("write failed %d\n", errno);
165                 return EIO;
166         }
167
168         drop_cache();
169         return EOK;
170 }
171 /******************************************************************************/
172 static int file_dev_close(struct ext4_blockdev *bdev)
173 {
174         close(dev_file);
175         return EOK;
176 }
177
178 /******************************************************************************/
179 struct ext4_blockdev *file_dev_get(void)
180 {
181         return &file_dev;
182 }
183 /******************************************************************************/
184 void file_dev_name_set(const char *n)
185 {
186         fname = n;
187 }
188 /******************************************************************************/