xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/unix_io.c (revision ed6250c95736c0b55da79d6e9dd01369532260c0)
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 /**
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 (stat(dev->d_name, &sbuf)) {
91 		ntfs_log_perror("Failed to access '%s'", dev->d_name);
92 		return -1;
93 	}
94 	if (S_ISBLK(sbuf.st_mode))
95 		NDevSetBlock(dev);
96 
97 	dev->d_private = ntfs_malloc(sizeof(int));
98 	if (!dev->d_private)
99 		return -1;
100 	/*
101 	 * Open file for exclusive access if mounting r/w.
102 	 * Fuseblk takes care about block devices.
103 	 */
104 	if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR)
105 		flags |= O_EXCL;
106 	*(int*)dev->d_private = open(dev->d_name, flags);
107 	if (*(int*)dev->d_private == -1) {
108 		err = errno;
109 		goto err_out;
110 	}
111 
112 	if ((flags & O_RDWR) != O_RDWR)
113 		NDevSetReadOnly(dev);
114 
115 /* locking not implemented in BeOS */
116 #if !defined(__BEOS__) && !defined(__HAIKU__)
117 	memset(&flk, 0, sizeof(flk));
118 	if (NDevReadOnly(dev))
119 		flk.l_type = F_RDLCK;
120 	else
121 		flk.l_type = F_WRLCK;
122 	flk.l_whence = SEEK_SET;
123 	flk.l_start = flk.l_len = 0LL;
124 	if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
125 		err = errno;
126 		ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ?
127 				"read" : "write", dev->d_name);
128 		if (close(DEV_FD(dev)))
129 			ntfs_log_perror("Failed to close '%s'", dev->d_name);
130 		goto err_out;
131 	}
132 #endif
133 	NDevSetOpen(dev);
134 	return 0;
135 err_out:
136 	free(dev->d_private);
137 	dev->d_private = NULL;
138 	errno = err;
139 	return -1;
140 }
141 
142 /**
143  * ntfs_device_unix_io_close - Close the device, releasing the lock
144  * @dev:
145  *
146  * Description...
147  *
148  * Returns:
149  */
150 static int ntfs_device_unix_io_close(struct ntfs_device *dev)
151 {
152 	struct flock flk;
153 
154 	if (!NDevOpen(dev)) {
155 		errno = EBADF;
156 		ntfs_log_perror("Device %s is not open", dev->d_name);
157 		return -1;
158 	}
159 	if (NDevDirty(dev))
160 		if (fsync(DEV_FD(dev))) {
161 			ntfs_log_perror("Failed to fsync device %s", dev->d_name);
162 			return -1;
163 		}
164 /* locking not implemented in BeOS */
165 #if !defined(__BEOS__) && !defined(__HAIKU__)
166 	memset(&flk, 0, sizeof(flk));
167 	flk.l_type = F_UNLCK;
168 	flk.l_whence = SEEK_SET;
169 	flk.l_start = flk.l_len = 0LL;
170 	if (fcntl(DEV_FD(dev), F_SETLK, &flk))
171 		ntfs_log_perror("Could not unlock %s", dev->d_name);
172 #endif
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  */
193 static 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  */
209 static 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  */
225 static 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  */
247 static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
248 		s64 count, s64 offset)
249 {
250 #if defined(__BEOS__) || defined(__HAIKU__)
251 	return read_pos(DEV_FD(dev), offset, buf,count);
252 #else
253 	return pread(DEV_FD(dev), buf, count, offset);
254 #endif
255 }
256 
257 /**
258  * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
259  * @dev:
260  * @buf:
261  * @count:
262  * @offset:
263  *
264  * Description...
265  *
266  * Returns:
267  */
268 static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
269 		s64 count, s64 offset)
270 {
271 	if (NDevReadOnly(dev)) {
272 		errno = EROFS;
273 		return -1;
274 	}
275 	NDevSetDirty(dev);
276 #if defined(__BEOS__) || defined(__HAIKU__)
277 	return write_pos(DEV_FD(dev), offset, buf,count);
278 #else
279 	return pwrite(DEV_FD(dev), buf, count, offset);
280 #endif
281 }
282 
283 /**
284  * ntfs_device_unix_io_sync - Flush any buffered changes to the device
285  * @dev:
286  *
287  * Description...
288  *
289  * Returns:
290  */
291 static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
292 {
293 	int res = 0;
294 
295 	if (!NDevReadOnly(dev)) {
296 		res = fsync(DEV_FD(dev));
297 		if (res)
298 			ntfs_log_perror("Failed to sync device %s", dev->d_name);
299 		else
300 			NDevClearDirty(dev);
301 	}
302 	return res;
303 }
304 
305 /**
306  * ntfs_device_unix_io_stat - Get information about the device
307  * @dev:
308  * @buf:
309  *
310  * Description...
311  *
312  * Returns:
313  */
314 static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
315 {
316 	return fstat(DEV_FD(dev), buf);
317 }
318 
319 /**
320  * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
321  * @dev:
322  * @request:
323  * @argp:
324  *
325  * Description...
326  *
327  * Returns:
328  */
329 static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request,
330 		void *argp)
331 {
332 	return ioctl(DEV_FD(dev), request, argp);
333 }
334 
335 /**
336  * Device operations for working with unix style devices and files.
337  */
338 struct ntfs_device_operations ntfs_device_unix_io_ops = {
339 	.open		= ntfs_device_unix_io_open,
340 	.close		= ntfs_device_unix_io_close,
341 	.seek		= ntfs_device_unix_io_seek,
342 	.read		= ntfs_device_unix_io_read,
343 	.write		= ntfs_device_unix_io_write,
344 	.pread		= ntfs_device_unix_io_pread,
345 	.pwrite		= ntfs_device_unix_io_pwrite,
346 	.sync		= ntfs_device_unix_io_sync,
347 	.stat		= ntfs_device_unix_io_stat,
348 	.ioctl		= ntfs_device_unix_io_ioctl,
349 };
350