1 /**
   2  * unix_io.c - Unix style disk io functions. Part of 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 Linux-NTFS
  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 "compat.h"
  58 #include "types.h"
  59 #include "mst.h"
  60 #include "debug.h"
  61 #include "device.h"
  62 #include "logging.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  * ntfs_device_unix_io_open - Open a device and lock it exclusively
  73  * @dev:
  74  * @flags:
  75  *
  76  * Description...
  77  *
  78  * Returns:
  79  */
  80 static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags)
  81 {
  82         struct flock flk;
  83         struct stat sbuf;
  84         int err;
  85 
  86         if (NDevOpen(dev)) {
  87                 errno = EBUSY;
  88                 return -1;
  89         }
  90         if (!(dev->d_private = ntfs_malloc(sizeof(int))))
  91                 return -1;
  92         *(int*)dev->d_private = open(dev->d_name, flags);
  93         if (*(int*)dev->d_private == -1) {
  94                 err = errno;
  95                 goto err_out;
  96         }
  97         /* Setup our read-only flag. */
  98         if ((flags & O_RDWR) != O_RDWR)
  99                 NDevSetReadOnly(dev);
 100         /* Acquire exclusive (mandatory) lock on the whole device. */
 101         memset(&flk, 0, sizeof(flk));
 102         if (NDevReadOnly(dev))
 103                 flk.l_type = F_RDLCK;
 104         else
 105                 flk.l_type = F_WRLCK;
 106         flk.l_whence = SEEK_SET;
 107         flk.l_start = flk.l_len = 0LL;
 108         if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
 109                 err = errno;
 110                 ntfs_log_debug("ntfs_device_unix_io_open: Could not lock %s "
 111                                 "for %s\n", dev->d_name, NDevReadOnly(dev) ?
 112                                 "reading" : "writing");
 113                 if (close(DEV_FD(dev)))
 114                         ntfs_log_perror("ntfs_device_unix_io_open: Warning: "
 115                                         "Could not close %s", dev->d_name);
 116                 goto err_out;
 117         }
 118         /* Determine if device is a block device or not, ignoring errors. */
 119         if (!fstat(DEV_FD(dev), &sbuf) && S_ISBLK(sbuf.st_mode))
 120                 NDevSetBlock(dev);
 121         /* Set our open flag. */
 122         NDevSetOpen(dev);
 123         return 0;
 124 err_out:
 125         free(dev->d_private);
 126         dev->d_private = NULL;
 127         errno = err;
 128         return -1;
 129 }
 130 
 131 /**
 132  * ntfs_device_unix_io_close - Close the device, releasing the lock
 133  * @dev:
 134  *
 135  * Description...
 136  *
 137  * Returns:
 138  */
 139 static int ntfs_device_unix_io_close(struct ntfs_device *dev)
 140 {
 141         struct flock flk;
 142 
 143         if (!NDevOpen(dev)) {
 144                 errno = EBADF;
 145                 return -1;
 146         }
 147         if (NDevDirty(dev))
 148                 fsync(DEV_FD(dev));
 149         /* Release exclusive (mandatory) lock on the whole device. */
 150         memset(&flk, 0, sizeof(flk));
 151         flk.l_type = F_UNLCK;
 152         flk.l_whence = SEEK_SET;
 153         flk.l_start = flk.l_len = 0LL;
 154         if (fcntl(DEV_FD(dev), F_SETLK, &flk))
 155                 ntfs_log_perror("ntfs_device_unix_io_close: Warning: Could not "
 156                                 "unlock %s", dev->d_name);
 157         /* Close the file descriptor and clear our open flag. */
 158         if (close(DEV_FD(dev)))
 159                 return -1;
 160         NDevClearOpen(dev);
 161         free(dev->d_private);
 162         dev->d_private = NULL;
 163         return 0;
 164 }
 165 
 166 /**
 167  * ntfs_device_unix_io_seek - Seek to a place on the device
 168  * @dev:
 169  * @offset:
 170  * @whence:
 171  *
 172  * Description...
 173  *
 174  * Returns:
 175  */
 176 static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
 177                 int whence)
 178 {
 179         return lseek(DEV_FD(dev), offset, whence);
 180 }
 181 
 182 /**
 183  * ntfs_device_unix_io_read - Read from the device, from the current location
 184  * @dev:
 185  * @buf:
 186  * @count:
 187  *
 188  * Description...
 189  *
 190  * Returns:
 191  */
 192 static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
 193                 s64 count)
 194 {
 195         return read(DEV_FD(dev), buf, count);
 196 }
 197 
 198 /**
 199  * ntfs_device_unix_io_write - Write to the device, at the current location
 200  * @dev:
 201  * @buf:
 202  * @count:
 203  *
 204  * Description...
 205  *
 206  * Returns:
 207  */
 208 static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
 209                 s64 count)
 210 {
 211         if (NDevReadOnly(dev)) {
 212                 errno = EROFS;
 213                 return -1;
 214         }
 215         NDevSetDirty(dev);
 216         return write(DEV_FD(dev), buf, count);
 217 }
 218 
 219 /**
 220  * ntfs_device_unix_io_pread - Perform a positioned read from the device
 221  * @dev:
 222  * @buf:
 223  * @count:
 224  * @offset:
 225  *
 226  * Description...
 227  *
 228  * Returns:
 229  */
 230 static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
 231                 s64 count, s64 offset)
 232 {
 233         return pread(DEV_FD(dev), buf, count, offset);
 234 }
 235 
 236 /**
 237  * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
 238  * @dev:
 239  * @buf:
 240  * @count:
 241  * @offset:
 242  *
 243  * Description...
 244  *
 245  * Returns:
 246  */
 247 static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
 248                 s64 count, s64 offset)
 249 {
 250         if (NDevReadOnly(dev)) {
 251                 errno = EROFS;
 252                 return -1;
 253         }
 254         NDevSetDirty(dev);
 255         return pwrite(DEV_FD(dev), buf, count, offset);
 256 }
 257 
 258 /**
 259  * ntfs_device_unix_io_sync - Flush any buffered changes to the device
 260  * @dev:
 261  *
 262  * Description...
 263  *
 264  * Returns:
 265  */
 266 static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
 267 {
 268         if (!NDevReadOnly(dev) && NDevDirty(dev)) {
 269                 int res = fsync(DEV_FD(dev));
 270                 if (!res)
 271                         NDevClearDirty(dev);
 272                 return res;
 273         }
 274         return 0;
 275 }
 276 
 277 /**
 278  * ntfs_device_unix_io_stat - Get information about the device
 279  * @dev:
 280  * @buf:
 281  *
 282  * Description...
 283  *
 284  * Returns:
 285  */
 286 static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
 287 {
 288         return fstat(DEV_FD(dev), buf);
 289 }
 290 
 291 /**
 292  * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
 293  * @dev:
 294  * @request:
 295  * @argp:
 296  *
 297  * Description...
 298  *
 299  * Returns:
 300  */
 301 static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request,
 302                 void *argp)
 303 {
 304         return ioctl(DEV_FD(dev), request, argp);
 305 }
 306 
 307 /**
 308  * Device operations for working with unix style devices and files.
 309  */
 310 struct ntfs_device_operations ntfs_device_unix_io_ops = {
 311         .open           = ntfs_device_unix_io_open,
 312         .close          = ntfs_device_unix_io_close,
 313         .seek           = ntfs_device_unix_io_seek,
 314         .read           = ntfs_device_unix_io_read,
 315         .write          = ntfs_device_unix_io_write,
 316         .pread          = ntfs_device_unix_io_pread,
 317         .pwrite         = ntfs_device_unix_io_pwrite,
 318         .sync           = ntfs_device_unix_io_sync,
 319         .stat           = ntfs_device_unix_io_stat,
 320         .ioctl          = ntfs_device_unix_io_ioctl,
 321 };