Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 1 | /** |
| 2 | * unix_io.c - Unix style disk io functions. Originated from the Linux-NTFS project. |
| 3 | * |
| 4 | * Copyright (c) 2000-2006 Anton Altaparmakov |
| 5 | * |
| 6 | * This program/include file is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as published |
| 8 | * by the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program/include file is distributed in the hope that it will be |
| 12 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program (in the main directory of the NTFS-3G |
| 18 | * distribution in the file COPYING); if not, write to the Free Software |
| 19 | * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #ifdef HAVE_UNISTD_H |
| 27 | #include <unistd.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_STRING_H |
| 33 | #include <string.h> |
| 34 | #endif |
| 35 | #ifdef HAVE_ERRNO_H |
| 36 | #include <errno.h> |
| 37 | #endif |
| 38 | #ifdef HAVE_STDIO_H |
| 39 | #include <stdio.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_SYS_TYPES_H |
| 42 | #include <sys/types.h> |
| 43 | #endif |
| 44 | #ifdef HAVE_SYS_STAT_H |
| 45 | #include <sys/stat.h> |
| 46 | #endif |
| 47 | #ifdef HAVE_FCNTL_H |
| 48 | #include <fcntl.h> |
| 49 | #endif |
| 50 | #ifdef HAVE_SYS_IOCTL_H |
| 51 | #include <sys/ioctl.h> |
| 52 | #endif |
| 53 | #ifdef HAVE_LINUX_FD_H |
| 54 | #include <linux/fd.h> |
| 55 | #endif |
| 56 | |
| 57 | #include "types.h" |
| 58 | #include "mst.h" |
| 59 | #include "debug.h" |
| 60 | #include "device.h" |
| 61 | #include "logging.h" |
| 62 | #include "misc.h" |
| 63 | |
| 64 | #define DEV_FD(dev) (*(int *)dev->d_private) |
| 65 | |
| 66 | /* Define to nothing if not present on this system. */ |
| 67 | #ifndef O_EXCL |
| 68 | # define O_EXCL 0 |
| 69 | #endif |
| 70 | |
| 71 | /** |
| 72 | * fsync replacement which makes every effort to try to get the data down to |
| 73 | * disk, using different means for different operating systems. Specifically, |
| 74 | * it issues the proper fcntl for Mac OS X or does fsync where it is available |
| 75 | * or as a last resort calls the fsync function. Information on this problem |
| 76 | * was retrieved from: |
| 77 | * http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf |
| 78 | */ |
| 79 | static int ntfs_fsync(int fildes) |
| 80 | { |
| 81 | int ret = -1; |
| 82 | #if defined(__APPLE__) || defined(__DARWIN__) |
| 83 | # ifndef F_FULLFSYNC |
| 84 | # error "Mac OS X: F_FULLFSYNC is not defined. Either you didn't include fcntl.h or you're using an older, unsupported version of Mac OS X (pre-10.3)." |
| 85 | # endif |
| 86 | /* |
| 87 | * Apple has disabled fsync() for internal disk drives in OS X. |
| 88 | * To force a synchronization of disk contents, we use a Mac OS X |
| 89 | * specific fcntl, F_FULLFSYNC. |
| 90 | */ |
| 91 | ret = fcntl(fildes, F_FULLFSYNC, NULL); |
| 92 | if (ret) { |
| 93 | /* |
| 94 | * If we are not on a file system that supports this, |
| 95 | * then fall back to a plain fsync. |
| 96 | */ |
| 97 | ret = fsync(fildes); |
| 98 | } |
| 99 | #else |
| 100 | ret = fsync(fildes); |
| 101 | #endif |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * ntfs_device_unix_io_open - Open a device and lock it exclusively |
| 107 | * @dev: |
| 108 | * @flags: |
| 109 | * |
| 110 | * Description... |
| 111 | * |
| 112 | * Returns: |
| 113 | */ |
| 114 | static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags) |
| 115 | { |
| 116 | struct flock flk; |
| 117 | struct stat sbuf; |
| 118 | int err; |
| 119 | |
| 120 | if (NDevOpen(dev)) { |
| 121 | errno = EBUSY; |
| 122 | return -1; |
| 123 | } |
| 124 | if (stat(dev->d_name, &sbuf)) { |
| 125 | ntfs_log_perror("Failed to access '%s'", dev->d_name); |
| 126 | return -1; |
| 127 | } |
| 128 | if (S_ISBLK(sbuf.st_mode)) |
| 129 | NDevSetBlock(dev); |
| 130 | |
| 131 | dev->d_private = ntfs_malloc(sizeof(int)); |
| 132 | if (!dev->d_private) |
| 133 | return -1; |
| 134 | /* |
| 135 | * Open file for exclusive access if mounting r/w. |
| 136 | * Fuseblk takes care about block devices. |
| 137 | */ |
| 138 | if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR) |
| 139 | flags |= O_EXCL; |
| 140 | *(int*)dev->d_private = open(dev->d_name, flags); |
| 141 | if (*(int*)dev->d_private == -1) { |
| 142 | err = errno; |
| 143 | goto err_out; |
| 144 | } |
| 145 | |
| 146 | if ((flags & O_RDWR) != O_RDWR) |
| 147 | NDevSetReadOnly(dev); |
| 148 | |
| 149 | memset(&flk, 0, sizeof(flk)); |
| 150 | if (NDevReadOnly(dev)) |
| 151 | flk.l_type = F_RDLCK; |
| 152 | else |
| 153 | flk.l_type = F_WRLCK; |
| 154 | flk.l_whence = SEEK_SET; |
| 155 | flk.l_start = flk.l_len = 0LL; |
| 156 | if (fcntl(DEV_FD(dev), F_SETLK, &flk)) { |
| 157 | err = errno; |
| 158 | ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? |
| 159 | "read" : "write", dev->d_name); |
| 160 | if (close(DEV_FD(dev))) |
| 161 | ntfs_log_perror("Failed to close '%s'", dev->d_name); |
| 162 | goto err_out; |
| 163 | } |
| 164 | |
| 165 | NDevSetOpen(dev); |
| 166 | return 0; |
| 167 | err_out: |
| 168 | free(dev->d_private); |
| 169 | dev->d_private = NULL; |
| 170 | errno = err; |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * ntfs_device_unix_io_close - Close the device, releasing the lock |
| 176 | * @dev: |
| 177 | * |
| 178 | * Description... |
| 179 | * |
| 180 | * Returns: |
| 181 | */ |
| 182 | static int ntfs_device_unix_io_close(struct ntfs_device *dev) |
| 183 | { |
| 184 | struct flock flk; |
| 185 | |
| 186 | if (!NDevOpen(dev)) { |
| 187 | errno = EBADF; |
| 188 | ntfs_log_perror("Device %s is not open", dev->d_name); |
| 189 | return -1; |
| 190 | } |
| 191 | if (NDevDirty(dev)) |
| 192 | if (ntfs_fsync(DEV_FD(dev))) { |
| 193 | ntfs_log_perror("Failed to fsync device %s", dev->d_name); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | memset(&flk, 0, sizeof(flk)); |
| 198 | flk.l_type = F_UNLCK; |
| 199 | flk.l_whence = SEEK_SET; |
| 200 | flk.l_start = flk.l_len = 0LL; |
| 201 | if (fcntl(DEV_FD(dev), F_SETLK, &flk)) |
| 202 | ntfs_log_perror("Could not unlock %s", dev->d_name); |
| 203 | if (close(DEV_FD(dev))) { |
| 204 | ntfs_log_perror("Failed to close device %s", dev->d_name); |
| 205 | return -1; |
| 206 | } |
| 207 | NDevClearOpen(dev); |
| 208 | free(dev->d_private); |
| 209 | dev->d_private = NULL; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * ntfs_device_unix_io_seek - Seek to a place on the device |
| 215 | * @dev: |
| 216 | * @offset: |
| 217 | * @whence: |
| 218 | * |
| 219 | * Description... |
| 220 | * |
| 221 | * Returns: |
| 222 | */ |
| 223 | static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset, |
| 224 | int whence) |
| 225 | { |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 226 | return lseek(DEV_FD(dev), offset, whence); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * ntfs_device_unix_io_read - Read from the device, from the current location |
| 231 | * @dev: |
| 232 | * @buf: |
| 233 | * @count: |
| 234 | * |
| 235 | * Description... |
| 236 | * |
| 237 | * Returns: |
| 238 | */ |
| 239 | static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf, |
| 240 | s64 count) |
| 241 | { |
| 242 | return read(DEV_FD(dev), buf, count); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * ntfs_device_unix_io_write - Write to the device, at the current location |
| 247 | * @dev: |
| 248 | * @buf: |
| 249 | * @count: |
| 250 | * |
| 251 | * Description... |
| 252 | * |
| 253 | * Returns: |
| 254 | */ |
| 255 | static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf, |
| 256 | s64 count) |
| 257 | { |
| 258 | if (NDevReadOnly(dev)) { |
| 259 | errno = EROFS; |
| 260 | return -1; |
| 261 | } |
| 262 | NDevSetDirty(dev); |
| 263 | return write(DEV_FD(dev), buf, count); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * ntfs_device_unix_io_pread - Perform a positioned read from the device |
| 268 | * @dev: |
| 269 | * @buf: |
| 270 | * @count: |
| 271 | * @offset: |
| 272 | * |
| 273 | * Description... |
| 274 | * |
| 275 | * Returns: |
| 276 | */ |
| 277 | static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf, |
| 278 | s64 count, s64 offset) |
| 279 | { |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 280 | return pread(DEV_FD(dev), buf, count, offset); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * ntfs_device_unix_io_pwrite - Perform a positioned write to the device |
| 285 | * @dev: |
| 286 | * @buf: |
| 287 | * @count: |
| 288 | * @offset: |
| 289 | * |
| 290 | * Description... |
| 291 | * |
| 292 | * Returns: |
| 293 | */ |
| 294 | static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf, |
| 295 | s64 count, s64 offset) |
| 296 | { |
| 297 | if (NDevReadOnly(dev)) { |
| 298 | errno = EROFS; |
| 299 | return -1; |
| 300 | } |
| 301 | NDevSetDirty(dev); |
Steve Kondik | 79165c3 | 2015-11-09 19:43:00 -0800 | [diff] [blame] | 302 | return pwrite(DEV_FD(dev), buf, count, offset); |
Steve Kondik | 2111ad7 | 2013-07-07 12:07:44 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
| 306 | * ntfs_device_unix_io_sync - Flush any buffered changes to the device |
| 307 | * @dev: |
| 308 | * |
| 309 | * Description... |
| 310 | * |
| 311 | * Returns: |
| 312 | */ |
| 313 | static int ntfs_device_unix_io_sync(struct ntfs_device *dev) |
| 314 | { |
| 315 | int res = 0; |
| 316 | |
| 317 | if (!NDevReadOnly(dev)) { |
| 318 | res = ntfs_fsync(DEV_FD(dev)); |
| 319 | if (res) |
| 320 | ntfs_log_perror("Failed to sync device %s", dev->d_name); |
| 321 | else |
| 322 | NDevClearDirty(dev); |
| 323 | } |
| 324 | return res; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * ntfs_device_unix_io_stat - Get information about the device |
| 329 | * @dev: |
| 330 | * @buf: |
| 331 | * |
| 332 | * Description... |
| 333 | * |
| 334 | * Returns: |
| 335 | */ |
| 336 | static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf) |
| 337 | { |
| 338 | return fstat(DEV_FD(dev), buf); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * ntfs_device_unix_io_ioctl - Perform an ioctl on the device |
| 343 | * @dev: |
| 344 | * @request: |
| 345 | * @argp: |
| 346 | * |
| 347 | * Description... |
| 348 | * |
| 349 | * Returns: |
| 350 | */ |
| 351 | static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request, |
| 352 | void *argp) |
| 353 | { |
| 354 | return ioctl(DEV_FD(dev), request, argp); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Device operations for working with unix style devices and files. |
| 359 | */ |
| 360 | struct ntfs_device_operations ntfs_device_unix_io_ops = { |
| 361 | .open = ntfs_device_unix_io_open, |
| 362 | .close = ntfs_device_unix_io_close, |
| 363 | .seek = ntfs_device_unix_io_seek, |
| 364 | .read = ntfs_device_unix_io_read, |
| 365 | .write = ntfs_device_unix_io_write, |
| 366 | .pread = ntfs_device_unix_io_pread, |
| 367 | .pwrite = ntfs_device_unix_io_pwrite, |
| 368 | .sync = ntfs_device_unix_io_sync, |
| 369 | .stat = ntfs_device_unix_io_stat, |
| 370 | .ioctl = ntfs_device_unix_io_ioctl, |
| 371 | }; |