2 * unix_io.c - Unix style disk io functions. Originated from the Linux-NTFS project.
4 * Copyright (c) 2000-2006 Anton Altaparmakov
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.
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.
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
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
44 #ifdef HAVE_SYS_STAT_H
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_FD_H
64 #define DEV_FD(dev) (*(int *)dev->d_private)
66 /* Define to nothing if not present on this system. */
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
77 * http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf
79 static int ntfs_fsync(int fildes
)
82 #if defined(__APPLE__) || defined(__DARWIN__)
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)."
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.
91 ret
= fcntl(fildes
, F_FULLFSYNC
, NULL
);
94 * If we are not on a file system that supports this,
95 * then fall back to a plain fsync.
106 * ntfs_device_unix_io_open - Open a device and lock it exclusively
114 static int ntfs_device_unix_io_open(struct ntfs_device
*dev
, int flags
)
124 if (stat(dev
->d_name
, &sbuf
)) {
125 ntfs_log_perror("Failed to access '%s'", dev
->d_name
);
128 if (S_ISBLK(sbuf
.st_mode
))
131 dev
->d_private
= ntfs_malloc(sizeof(int));
135 * Open file for exclusive access if mounting r/w.
136 * Fuseblk takes care about block devices.
138 if (!NDevBlock(dev
) && (flags
& O_RDWR
) == O_RDWR
)
140 *(int*)dev
->d_private
= open(dev
->d_name
, flags
);
141 if (*(int*)dev
->d_private
== -1) {
146 if ((flags
& O_RDWR
) != O_RDWR
)
147 NDevSetReadOnly(dev
);
149 memset(&flk
, 0, sizeof(flk
));
150 if (NDevReadOnly(dev
))
151 flk
.l_type
= F_RDLCK
;
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
)) {
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
);
168 free(dev
->d_private
);
169 dev
->d_private
= NULL
;
175 * ntfs_device_unix_io_close - Close the device, releasing the lock
182 static int ntfs_device_unix_io_close(struct ntfs_device
*dev
)
186 if (!NDevOpen(dev
)) {
188 ntfs_log_perror("Device %s is not open", dev
->d_name
);
192 if (ntfs_fsync(DEV_FD(dev
))) {
193 ntfs_log_perror("Failed to fsync device %s", dev
->d_name
);
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
);
208 free(dev
->d_private
);
209 dev
->d_private
= NULL
;
214 * ntfs_device_unix_io_seek - Seek to a place on the device
223 static s64
ntfs_device_unix_io_seek(struct ntfs_device
*dev
, s64 offset
,
226 return lseek(DEV_FD(dev
), offset
, whence
);
230 * ntfs_device_unix_io_read - Read from the device, from the current location
239 static s64
ntfs_device_unix_io_read(struct ntfs_device
*dev
, void *buf
,
242 return read(DEV_FD(dev
), buf
, count
);
246 * ntfs_device_unix_io_write - Write to the device, at the current location
255 static s64
ntfs_device_unix_io_write(struct ntfs_device
*dev
, const void *buf
,
258 if (NDevReadOnly(dev
)) {
263 return write(DEV_FD(dev
), buf
, count
);
267 * ntfs_device_unix_io_pread - Perform a positioned read from the device
277 static s64
ntfs_device_unix_io_pread(struct ntfs_device
*dev
, void *buf
,
278 s64 count
, s64 offset
)
280 return pread(DEV_FD(dev
), buf
, count
, offset
);
284 * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
294 static s64
ntfs_device_unix_io_pwrite(struct ntfs_device
*dev
, const void *buf
,
295 s64 count
, s64 offset
)
297 if (NDevReadOnly(dev
)) {
302 return pwrite(DEV_FD(dev
), buf
, count
, offset
);
306 * ntfs_device_unix_io_sync - Flush any buffered changes to the device
313 static int ntfs_device_unix_io_sync(struct ntfs_device
*dev
)
317 if (!NDevReadOnly(dev
)) {
318 res
= ntfs_fsync(DEV_FD(dev
));
320 ntfs_log_perror("Failed to sync device %s", dev
->d_name
);
328 * ntfs_device_unix_io_stat - Get information about the device
336 static int ntfs_device_unix_io_stat(struct ntfs_device
*dev
, struct stat
*buf
)
338 return fstat(DEV_FD(dev
), buf
);
342 * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
351 static int ntfs_device_unix_io_ioctl(struct ntfs_device
*dev
, int request
,
354 return ioctl(DEV_FD(dev
), request
, argp
);
358 * Device operations for working with unix style devices and files.
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
,