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/* Foxconn modified start pling 11/24/2009 */ 72extern __off64_t lseek64 (int __fd, __off64_t __offset, int __whence); 73/* Foxconn modified end pling 11/24/2009 */ 74 75/** 76 * ntfs_device_unix_io_open - Open a device and lock it exclusively 77 * @dev: 78 * @flags: 79 * 80 * Description... 81 * 82 * Returns: 83 */ 84static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags) 85{ 86 struct flock flk; 87 struct stat sbuf; 88 int err; 89 90 if (NDevOpen(dev)) { 91 errno = EBUSY; 92 return -1; 93 } 94 if (stat(dev->d_name, &sbuf)) { 95 ntfs_log_perror("Failed to access '%s'", dev->d_name); 96 return -1; 97 } 98 if (S_ISBLK(sbuf.st_mode)) 99 NDevSetBlock(dev); 100 101 dev->d_private = ntfs_malloc(sizeof(int)); 102 if (!dev->d_private) 103 return -1; 104 /* 105 * Open file for exclusive access if mounting r/w. 106 * Fuseblk takes care about block devices. 107 */ 108 if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR) 109 flags |= O_EXCL; 110 *(int*)dev->d_private = open(dev->d_name, flags); 111 if (*(int*)dev->d_private == -1) { 112 err = errno; 113 goto err_out; 114 } 115 116 if ((flags & O_RDWR) != O_RDWR) 117 NDevSetReadOnly(dev); 118 119 memset(&flk, 0, sizeof(flk)); 120 if (NDevReadOnly(dev)) 121 flk.l_type = F_RDLCK; 122 else 123 flk.l_type = F_WRLCK; 124 flk.l_whence = SEEK_SET; 125 flk.l_start = flk.l_len = 0LL; 126 if (fcntl(DEV_FD(dev), F_SETLK, &flk)) { 127 err = errno; 128 ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? 129 "read" : "write", dev->d_name); 130 if (close(DEV_FD(dev))) 131 ntfs_log_perror("Failed to close '%s'", dev->d_name); 132 goto err_out; 133 } 134 135 NDevSetOpen(dev); 136 return 0; 137err_out: 138 free(dev->d_private); 139 dev->d_private = NULL; 140 errno = err; 141 return -1; 142} 143 144/** 145 * ntfs_device_unix_io_close - Close the device, releasing the lock 146 * @dev: 147 * 148 * Description... 149 * 150 * Returns: 151 */ 152static int ntfs_device_unix_io_close(struct ntfs_device *dev) 153{ 154 struct flock flk; 155 156 if (!NDevOpen(dev)) { 157 errno = EBADF; 158 ntfs_log_perror("Device %s is not open", dev->d_name); 159 return -1; 160 } 161 if (NDevDirty(dev)) 162 if (fsync(DEV_FD(dev))) { 163 ntfs_log_perror("Failed to fsync device %s", dev->d_name); 164 return -1; 165 } 166 167 memset(&flk, 0, sizeof(flk)); 168 flk.l_type = F_UNLCK; 169 flk.l_whence = SEEK_SET; 170 flk.l_start = flk.l_len = 0LL; 171 if (fcntl(DEV_FD(dev), F_SETLK, &flk)) 172 ntfs_log_perror("Could not unlock %s", dev->d_name); 173 if (close(DEV_FD(dev))) { 174 ntfs_log_perror("Failed to close device %s", dev->d_name); 175 return -1; 176 } 177 NDevClearOpen(dev); 178 free(dev->d_private); 179 dev->d_private = NULL; 180 return 0; 181} 182 183/** 184 * ntfs_device_unix_io_seek - Seek to a place on the device 185 * @dev: 186 * @offset: 187 * @whence: 188 * 189 * Description... 190 * 191 * Returns: 192 */ 193static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset, 194 int whence) 195{ 196 return lseek(DEV_FD(dev), offset, whence); 197} 198 199/** 200 * ntfs_device_unix_io_read - Read from the device, from the current location 201 * @dev: 202 * @buf: 203 * @count: 204 * 205 * Description... 206 * 207 * Returns: 208 */ 209static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf, 210 s64 count) 211{ 212 return read(DEV_FD(dev), buf, count); 213} 214 215/** 216 * ntfs_device_unix_io_write - Write to the device, at the current location 217 * @dev: 218 * @buf: 219 * @count: 220 * 221 * Description... 222 * 223 * Returns: 224 */ 225static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf, 226 s64 count) 227{ 228 if (NDevReadOnly(dev)) { 229 errno = EROFS; 230 return -1; 231 } 232 NDevSetDirty(dev); 233 return write(DEV_FD(dev), buf, count); 234} 235 236/** 237 * ntfs_device_unix_io_pread - Perform a positioned read from the device 238 * @dev: 239 * @buf: 240 * @count: 241 * @offset: 242 * 243 * Description... 244 * 245 * Returns: 246 */ 247static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf, 248 s64 count, s64 offset) 249{ 250 /* Foxconn modified start pling 04/22/2009 */ 251 //return pread(DEV_FD(dev), buf, count, (unsigned long)offset); 252 if (lseek64(DEV_FD(dev), offset, SEEK_SET) < 0) 253 perror("lseek"); 254 255 return read(DEV_FD(dev), buf, count); 256 /* Foxconn modified end pling 04/22/2009 */ 257} 258 259/** 260 * ntfs_device_unix_io_pwrite - Perform a positioned write to the device 261 * @dev: 262 * @buf: 263 * @count: 264 * @offset: 265 * 266 * Description... 267 * 268 * Returns: 269 */ 270static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf, 271 s64 count, s64 offset) 272{ 273 if (NDevReadOnly(dev)) { 274 errno = EROFS; 275 return -1; 276 } 277 NDevSetDirty(dev); 278 279 /* Foxconn modified start pling 04/22/2009 */ 280 //return pwrite(DEV_FD(dev), buf, count, offset); 281 if (lseek64(DEV_FD(dev), offset, SEEK_SET) < 0) 282 perror("lseek"); 283 return write(DEV_FD(dev), buf, count); 284 /* Foxconn modified end pling 04/22/2009 */ 285} 286 287/** 288 * ntfs_device_unix_io_sync - Flush any buffered changes to the device 289 * @dev: 290 * 291 * Description... 292 * 293 * Returns: 294 */ 295static int ntfs_device_unix_io_sync(struct ntfs_device *dev) 296{ 297 int res = 0; 298 299 if (!NDevReadOnly(dev)) { 300 res = fsync(DEV_FD(dev)); 301 if (res) 302 ntfs_log_perror("Failed to sync device %s", dev->d_name); 303 else 304 NDevClearDirty(dev); 305 } 306 return res; 307} 308 309/** 310 * ntfs_device_unix_io_stat - Get information about the device 311 * @dev: 312 * @buf: 313 * 314 * Description... 315 * 316 * Returns: 317 */ 318static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf) 319{ 320 return fstat(DEV_FD(dev), buf); 321} 322 323/** 324 * ntfs_device_unix_io_ioctl - Perform an ioctl on the device 325 * @dev: 326 * @request: 327 * @argp: 328 * 329 * Description... 330 * 331 * Returns: 332 */ 333static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request, 334 void *argp) 335{ 336 return ioctl(DEV_FD(dev), request, argp); 337} 338 339/** 340 * Device operations for working with unix style devices and files. 341 */ 342struct ntfs_device_operations ntfs_device_unix_io_ops = { 343 .open = ntfs_device_unix_io_open, 344 .close = ntfs_device_unix_io_close, 345 .seek = ntfs_device_unix_io_seek, 346 .read = ntfs_device_unix_io_read, 347 .write = ntfs_device_unix_io_write, 348 .pread = ntfs_device_unix_io_pread, 349 .pwrite = ntfs_device_unix_io_pwrite, 350 .sync = ntfs_device_unix_io_sync, 351 .stat = ntfs_device_unix_io_stat, 352 .ioctl = ntfs_device_unix_io_ioctl, 353}; 354