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
56 #ifdef HAVE_LINUX_FS_H
67 #define DEV_FD(dev) (*(int *)dev->d_private)
69 /* Define to nothing if not present on this system. */
75 * fsync replacement which makes every effort to try to get the data down to
76 * disk, using different means for different operating systems. Specifically,
77 * it issues the proper fcntl for Mac OS X or does fsync where it is available
78 * or as a last resort calls the fsync function. Information on this problem
80 * http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf
82 static int ntfs_fsync(int fildes
)
85 #if defined(__APPLE__) || defined(__DARWIN__)
87 # 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)."
90 * Apple has disabled fsync() for internal disk drives in OS X.
91 * To force a synchronization of disk contents, we use a Mac OS X
92 * specific fcntl, F_FULLFSYNC.
94 ret
= fcntl(fildes
, F_FULLFSYNC
, NULL
);
97 * If we are not on a file system that supports this,
98 * then fall back to a plain fsync.
109 * ntfs_device_unix_io_open - Open a device and lock it exclusively
117 static int ntfs_device_unix_io_open(struct ntfs_device
*dev
, int flags
)
127 if (stat(dev
->d_name
, &sbuf
)) {
128 ntfs_log_perror("Failed to access '%s'", dev
->d_name
);
131 if (S_ISBLK(sbuf
.st_mode
))
134 dev
->d_private
= ntfs_malloc(sizeof(int));
138 * Open file for exclusive access if mounting r/w.
139 * Fuseblk takes care about block devices.
141 if (!NDevBlock(dev
) && (flags
& O_RDWR
) == O_RDWR
)
143 *(int*)dev
->d_private
= open(dev
->d_name
, flags
);
144 if (*(int*)dev
->d_private
== -1) {
146 /* if permission error and rw, retry read-only */
147 if ((err
== EACCES
) && ((flags
& O_RDWR
) == O_RDWR
))
151 #ifdef HAVE_LINUX_FS_H
152 /* Check whether the device was forced read-only */
153 if (NDevBlock(dev
) && ((flags
& O_RDWR
) == O_RDWR
)) {
157 r
= ioctl(DEV_FD(dev
), BLKROGET
, &state
);
160 if (close(DEV_FD(dev
)))
167 if ((flags
& O_RDWR
) != O_RDWR
)
168 NDevSetReadOnly(dev
);
170 memset(&flk
, 0, sizeof(flk
));
171 if (NDevReadOnly(dev
))
172 flk
.l_type
= F_RDLCK
;
174 flk
.l_type
= F_WRLCK
;
175 flk
.l_whence
= SEEK_SET
;
176 flk
.l_start
= flk
.l_len
= 0LL;
177 if (fcntl(DEV_FD(dev
), F_SETLK
, &flk
)) {
179 ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev
) ?
180 "read" : "write", dev
->d_name
);
181 if (close(DEV_FD(dev
)))
182 ntfs_log_perror("Failed to close '%s'", dev
->d_name
);
189 free(dev
->d_private
);
190 dev
->d_private
= NULL
;
196 * ntfs_device_unix_io_close - Close the device, releasing the lock
203 static int ntfs_device_unix_io_close(struct ntfs_device
*dev
)
207 if (!NDevOpen(dev
)) {
209 ntfs_log_perror("Device %s is not open", dev
->d_name
);
213 if (ntfs_fsync(DEV_FD(dev
))) {
214 ntfs_log_perror("Failed to fsync device %s", dev
->d_name
);
218 memset(&flk
, 0, sizeof(flk
));
219 flk
.l_type
= F_UNLCK
;
220 flk
.l_whence
= SEEK_SET
;
221 flk
.l_start
= flk
.l_len
= 0LL;
222 if (fcntl(DEV_FD(dev
), F_SETLK
, &flk
))
223 ntfs_log_perror("Could not unlock %s", dev
->d_name
);
224 if (close(DEV_FD(dev
))) {
225 ntfs_log_perror("Failed to close device %s", dev
->d_name
);
229 free(dev
->d_private
);
230 dev
->d_private
= NULL
;
235 * ntfs_device_unix_io_seek - Seek to a place on the device
244 static s64
ntfs_device_unix_io_seek(struct ntfs_device
*dev
, s64 offset
,
247 return lseek(DEV_FD(dev
), offset
, whence
);
251 * ntfs_device_unix_io_read - Read from the device, from the current location
260 static s64
ntfs_device_unix_io_read(struct ntfs_device
*dev
, void *buf
,
263 return read(DEV_FD(dev
), buf
, count
);
267 * ntfs_device_unix_io_write - Write to the device, at the current location
276 static s64
ntfs_device_unix_io_write(struct ntfs_device
*dev
, const void *buf
,
279 if (NDevReadOnly(dev
)) {
284 return write(DEV_FD(dev
), buf
, count
);
288 * ntfs_device_unix_io_pread - Perform a positioned read from the device
298 static s64
ntfs_device_unix_io_pread(struct ntfs_device
*dev
, void *buf
,
299 s64 count
, s64 offset
)
301 return pread(DEV_FD(dev
), buf
, count
, offset
);
305 * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
315 static s64
ntfs_device_unix_io_pwrite(struct ntfs_device
*dev
, const void *buf
,
316 s64 count
, s64 offset
)
318 if (NDevReadOnly(dev
)) {
323 return pwrite(DEV_FD(dev
), buf
, count
, offset
);
327 * ntfs_device_unix_io_sync - Flush any buffered changes to the device
334 static int ntfs_device_unix_io_sync(struct ntfs_device
*dev
)
338 if (!NDevReadOnly(dev
)) {
339 res
= ntfs_fsync(DEV_FD(dev
));
341 ntfs_log_perror("Failed to sync device %s", dev
->d_name
);
349 * ntfs_device_unix_io_stat - Get information about the device
357 static int ntfs_device_unix_io_stat(struct ntfs_device
*dev
, struct stat
*buf
)
359 return fstat(DEV_FD(dev
), buf
);
363 * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
372 static int ntfs_device_unix_io_ioctl(struct ntfs_device
*dev
,
373 unsigned long request
, void *argp
)
375 return ioctl(DEV_FD(dev
), request
, argp
);
379 * Device operations for working with unix style devices and files.
381 struct ntfs_device_operations ntfs_device_unix_io_ops
= {
382 .open
= ntfs_device_unix_io_open
,
383 .close
= ntfs_device_unix_io_close
,
384 .seek
= ntfs_device_unix_io_seek
,
385 .read
= ntfs_device_unix_io_read
,
386 .write
= ntfs_device_unix_io_write
,
387 .pread
= ntfs_device_unix_io_pread
,
388 .pwrite
= ntfs_device_unix_io_pwrite
,
389 .sync
= ntfs_device_unix_io_sync
,
390 .stat
= ntfs_device_unix_io_stat
,
391 .ioctl
= ntfs_device_unix_io_ioctl
,