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#ifdef HAVE_LINUX_FS_H
57#include <linux/fs.h>
58#endif
59
60#include "types.h"
61#include "mst.h"
62#include "debug.h"
63#include "device.h"
64#include "logging.h"
65#include "misc.h"
66
67#define DEV_FD(dev)	(*(int *)dev->d_private)
68
69/* Define to nothing if not present on this system. */
70#ifndef O_EXCL
71#	define O_EXCL 0
72#endif
73
74/**
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
79 * was retrieved from:
80 *   http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf
81 */
82static int ntfs_fsync(int fildes)
83{
84	int ret = -1;
85#if defined(__APPLE__) || defined(__DARWIN__)
86# ifndef F_FULLFSYNC
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)."
88# endif
89	/*
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.
93	 */
94	ret = fcntl(fildes, F_FULLFSYNC, NULL);
95	if (ret) {
96		/*
97		 * If we are not on a file system that supports this,
98		 * then fall back to a plain fsync.
99		 */
100		ret = fsync(fildes);
101	}
102#else
103	ret = fsync(fildes);
104#endif
105	return ret;
106}
107
108/**
109 * ntfs_device_unix_io_open - Open a device and lock it exclusively
110 * @dev:
111 * @flags:
112 *
113 * Description...
114 *
115 * Returns:
116 */
117static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags)
118{
119	struct flock flk;
120	struct stat sbuf;
121	int err;
122
123	if (NDevOpen(dev)) {
124		errno = EBUSY;
125		return -1;
126	}
127	if (stat(dev->d_name, &sbuf)) {
128		ntfs_log_perror("Failed to access '%s'", dev->d_name);
129		return -1;
130	}
131	if (S_ISBLK(sbuf.st_mode))
132		NDevSetBlock(dev);
133
134	dev->d_private = ntfs_malloc(sizeof(int));
135	if (!dev->d_private)
136		return -1;
137	/*
138	 * Open file for exclusive access if mounting r/w.
139	 * Fuseblk takes care about block devices.
140	 */
141	if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR)
142		flags |= O_EXCL;
143	*(int*)dev->d_private = open(dev->d_name, flags);
144	if (*(int*)dev->d_private == -1) {
145		err = errno;
146			/* if permission error and rw, retry read-only */
147		if ((err == EACCES) && ((flags & O_RDWR) == O_RDWR))
148			err = EROFS;
149		goto err_out;
150	}
151#ifdef HAVE_LINUX_FS_H
152		/* Check whether the device was forced read-only */
153	if (NDevBlock(dev) && ((flags & O_RDWR) == O_RDWR)) {
154		int r;
155		int state;
156
157		r = ioctl(DEV_FD(dev), BLKROGET, &state);
158		if (!r && state) {
159			err = EROFS;
160			if (close(DEV_FD(dev)))
161				err = errno;
162			goto err_out;
163   		}
164	}
165#endif
166
167	if ((flags & O_RDWR) != O_RDWR)
168		NDevSetReadOnly(dev);
169
170	memset(&flk, 0, sizeof(flk));
171	if (NDevReadOnly(dev))
172		flk.l_type = F_RDLCK;
173	else
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)) {
178		err = errno;
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);
183		goto err_out;
184	}
185
186	NDevSetOpen(dev);
187	return 0;
188err_out:
189	free(dev->d_private);
190	dev->d_private = NULL;
191	errno = err;
192	return -1;
193}
194
195/**
196 * ntfs_device_unix_io_close - Close the device, releasing the lock
197 * @dev:
198 *
199 * Description...
200 *
201 * Returns:
202 */
203static int ntfs_device_unix_io_close(struct ntfs_device *dev)
204{
205	struct flock flk;
206
207	if (!NDevOpen(dev)) {
208		errno = EBADF;
209		ntfs_log_perror("Device %s is not open", dev->d_name);
210		return -1;
211	}
212	if (NDevDirty(dev))
213		if (ntfs_fsync(DEV_FD(dev))) {
214			ntfs_log_perror("Failed to fsync device %s", dev->d_name);
215			return -1;
216		}
217
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);
226		return -1;
227	}
228	NDevClearOpen(dev);
229	free(dev->d_private);
230	dev->d_private = NULL;
231	return 0;
232}
233
234/**
235 * ntfs_device_unix_io_seek - Seek to a place on the device
236 * @dev:
237 * @offset:
238 * @whence:
239 *
240 * Description...
241 *
242 * Returns:
243 */
244static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
245		int whence)
246{
247	return lseek(DEV_FD(dev), offset, whence);
248}
249
250/**
251 * ntfs_device_unix_io_read - Read from the device, from the current location
252 * @dev:
253 * @buf:
254 * @count:
255 *
256 * Description...
257 *
258 * Returns:
259 */
260static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
261		s64 count)
262{
263	return read(DEV_FD(dev), buf, count);
264}
265
266/**
267 * ntfs_device_unix_io_write - Write to the device, at the current location
268 * @dev:
269 * @buf:
270 * @count:
271 *
272 * Description...
273 *
274 * Returns:
275 */
276static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
277		s64 count)
278{
279	if (NDevReadOnly(dev)) {
280		errno = EROFS;
281		return -1;
282	}
283	NDevSetDirty(dev);
284	return write(DEV_FD(dev), buf, count);
285}
286
287/**
288 * ntfs_device_unix_io_pread - Perform a positioned read from the device
289 * @dev:
290 * @buf:
291 * @count:
292 * @offset:
293 *
294 * Description...
295 *
296 * Returns:
297 */
298static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
299		s64 count, s64 offset)
300{
301	return pread(DEV_FD(dev), buf, count, offset);
302}
303
304/**
305 * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
306 * @dev:
307 * @buf:
308 * @count:
309 * @offset:
310 *
311 * Description...
312 *
313 * Returns:
314 */
315static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
316		s64 count, s64 offset)
317{
318	if (NDevReadOnly(dev)) {
319		errno = EROFS;
320		return -1;
321	}
322	NDevSetDirty(dev);
323	return pwrite(DEV_FD(dev), buf, count, offset);
324}
325
326/**
327 * ntfs_device_unix_io_sync - Flush any buffered changes to the device
328 * @dev:
329 *
330 * Description...
331 *
332 * Returns:
333 */
334static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
335{
336	int res = 0;
337
338	if (!NDevReadOnly(dev)) {
339		res = ntfs_fsync(DEV_FD(dev));
340		if (res)
341			ntfs_log_perror("Failed to sync device %s", dev->d_name);
342		else
343			NDevClearDirty(dev);
344	}
345	return res;
346}
347
348/**
349 * ntfs_device_unix_io_stat - Get information about the device
350 * @dev:
351 * @buf:
352 *
353 * Description...
354 *
355 * Returns:
356 */
357static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
358{
359	return fstat(DEV_FD(dev), buf);
360}
361
362/**
363 * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
364 * @dev:
365 * @request:
366 * @argp:
367 *
368 * Description...
369 *
370 * Returns:
371 */
372static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev,
373		unsigned long request, void *argp)
374{
375	return ioctl(DEV_FD(dev), request, argp);
376}
377
378/**
379 * Device operations for working with unix style devices and files.
380 */
381struct 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,
392};
393