xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/unix_io.c (revision c90684742e7361651849be4116d0e5de3a817194)
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 	memset(&flk, 0, sizeof(flk));
116 	if (NDevReadOnly(dev))
117 		flk.l_type = F_RDLCK;
118 	else
119 		flk.l_type = F_WRLCK;
120 	flk.l_whence = SEEK_SET;
121 	flk.l_start = flk.l_len = 0LL;
122 	if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
123 		err = errno;
124 		ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ?
125 				"read" : "write", dev->d_name);
126 		if (close(DEV_FD(dev)))
127 			ntfs_log_perror("Failed to close '%s'", dev->d_name);
128 		goto err_out;
129 	}
130 
131 	NDevSetOpen(dev);
132 	return 0;
133 err_out:
134 	free(dev->d_private);
135 	dev->d_private = NULL;
136 	errno = err;
137 	return -1;
138 }
139 
140 /**
141  * ntfs_device_unix_io_close - Close the device, releasing the lock
142  * @dev:
143  *
144  * Description...
145  *
146  * Returns:
147  */
148 static int ntfs_device_unix_io_close(struct ntfs_device *dev)
149 {
150 	struct flock flk;
151 
152 	if (!NDevOpen(dev)) {
153 		errno = EBADF;
154 		ntfs_log_perror("Device %s is not open", dev->d_name);
155 		return -1;
156 	}
157 	if (NDevDirty(dev))
158 		if (fsync(DEV_FD(dev))) {
159 			ntfs_log_perror("Failed to fsync device %s", dev->d_name);
160 			return -1;
161 		}
162 
163 	memset(&flk, 0, sizeof(flk));
164 	flk.l_type = F_UNLCK;
165 	flk.l_whence = SEEK_SET;
166 	flk.l_start = flk.l_len = 0LL;
167 	if (fcntl(DEV_FD(dev), F_SETLK, &flk))
168 		ntfs_log_perror("Could not unlock %s", dev->d_name);
169 	if (close(DEV_FD(dev))) {
170 		ntfs_log_perror("Failed to close device %s", dev->d_name);
171 		return -1;
172 	}
173 	NDevClearOpen(dev);
174 	free(dev->d_private);
175 	dev->d_private = NULL;
176 	return 0;
177 }
178 
179 /**
180  * ntfs_device_unix_io_seek - Seek to a place on the device
181  * @dev:
182  * @offset:
183  * @whence:
184  *
185  * Description...
186  *
187  * Returns:
188  */
189 static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
190 		int whence)
191 {
192 	return lseek(DEV_FD(dev), offset, whence);
193 }
194 
195 /**
196  * ntfs_device_unix_io_read - Read from the device, from the current location
197  * @dev:
198  * @buf:
199  * @count:
200  *
201  * Description...
202  *
203  * Returns:
204  */
205 static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
206 		s64 count)
207 {
208 	return read(DEV_FD(dev), buf, count);
209 }
210 
211 /**
212  * ntfs_device_unix_io_write - Write to the device, at the current location
213  * @dev:
214  * @buf:
215  * @count:
216  *
217  * Description...
218  *
219  * Returns:
220  */
221 static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
222 		s64 count)
223 {
224 	if (NDevReadOnly(dev)) {
225 		errno = EROFS;
226 		return -1;
227 	}
228 	NDevSetDirty(dev);
229 	return write(DEV_FD(dev), buf, count);
230 }
231 
232 /**
233  * ntfs_device_unix_io_pread - Perform a positioned read from the device
234  * @dev:
235  * @buf:
236  * @count:
237  * @offset:
238  *
239  * Description...
240  *
241  * Returns:
242  */
243 static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
244 		s64 count, s64 offset)
245 {
246 	return pread(DEV_FD(dev), buf, count, offset);
247 }
248 
249 /**
250  * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
251  * @dev:
252  * @buf:
253  * @count:
254  * @offset:
255  *
256  * Description...
257  *
258  * Returns:
259  */
260 static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
261 		s64 count, s64 offset)
262 {
263 	if (NDevReadOnly(dev)) {
264 		errno = EROFS;
265 		return -1;
266 	}
267 	NDevSetDirty(dev);
268 	return pwrite(DEV_FD(dev), buf, count, offset);
269 }
270 
271 /**
272  * ntfs_device_unix_io_sync - Flush any buffered changes to the device
273  * @dev:
274  *
275  * Description...
276  *
277  * Returns:
278  */
279 static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
280 {
281 	int res = 0;
282 
283 	if (!NDevReadOnly(dev)) {
284 		res = fsync(DEV_FD(dev));
285 		if (res)
286 			ntfs_log_perror("Failed to sync device %s", dev->d_name);
287 		else
288 			NDevClearDirty(dev);
289 	}
290 	return res;
291 }
292 
293 /**
294  * ntfs_device_unix_io_stat - Get information about the device
295  * @dev:
296  * @buf:
297  *
298  * Description...
299  *
300  * Returns:
301  */
302 static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
303 {
304 	return fstat(DEV_FD(dev), buf);
305 }
306 
307 /**
308  * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
309  * @dev:
310  * @request:
311  * @argp:
312  *
313  * Description...
314  *
315  * Returns:
316  */
317 static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request,
318 		void *argp)
319 {
320 	return ioctl(DEV_FD(dev), request, argp);
321 }
322 
323 /**
324  * Device operations for working with unix style devices and files.
325  */
326 struct ntfs_device_operations ntfs_device_unix_io_ops = {
327 	.open		= ntfs_device_unix_io_open,
328 	.close		= ntfs_device_unix_io_close,
329 	.seek		= ntfs_device_unix_io_seek,
330 	.read		= ntfs_device_unix_io_read,
331 	.write		= ntfs_device_unix_io_write,
332 	.pread		= ntfs_device_unix_io_pread,
333 	.pwrite		= ntfs_device_unix_io_pwrite,
334 	.sync		= ntfs_device_unix_io_sync,
335 	.stat		= ntfs_device_unix_io_stat,
336 	.ioctl		= ntfs_device_unix_io_ioctl,
337 };
338