xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/unix_io.c (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
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 		return -1;
157 	}
158 	if (NDevDirty(dev))
159 		fsync(DEV_FD(dev));
160 
161 /* locking not implemented in BeOS */
162 #if !defined(__BEOS__) && !defined(__HAIKU__)
163 	/* Release exclusive (mandatory) lock on the whole device. */
164 	memset(&flk, 0, sizeof(flk));
165 	flk.l_type = F_UNLCK;
166 	flk.l_whence = SEEK_SET;
167 	flk.l_start = flk.l_len = 0LL;
168 	if (fcntl(DEV_FD(dev), F_SETLK, &flk))
169 		ntfs_log_perror("ntfs_device_unix_io_close: Warning: Could not "
170 				"unlock %s", dev->d_name);
171 #endif
172 	/* Close the file descriptor and clear our open flag. */
173 	if (close(DEV_FD(dev)))
174 		return -1;
175 	NDevClearOpen(dev);
176 	free(dev->d_private);
177 	dev->d_private = NULL;
178 	return 0;
179 }
180 
181 /**
182  * ntfs_device_unix_io_seek - Seek to a place on the device
183  * @dev:
184  * @offset:
185  * @whence:
186  *
187  * Description...
188  *
189  * Returns:
190  */
191 static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
192 		int whence)
193 {
194 	return lseek(DEV_FD(dev), offset, whence);
195 }
196 
197 /**
198  * ntfs_device_unix_io_read - Read from the device, from the current location
199  * @dev:
200  * @buf:
201  * @count:
202  *
203  * Description...
204  *
205  * Returns:
206  */
207 static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
208 		s64 count)
209 {
210 	return read(DEV_FD(dev), buf, count);
211 }
212 
213 /**
214  * ntfs_device_unix_io_write - Write to the device, at the current location
215  * @dev:
216  * @buf:
217  * @count:
218  *
219  * Description...
220  *
221  * Returns:
222  */
223 static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
224 		s64 count)
225 {
226 	if (NDevReadOnly(dev)) {
227 		errno = EROFS;
228 		return -1;
229 	}
230 	NDevSetDirty(dev);
231 	return write(DEV_FD(dev), buf, count);
232 }
233 
234 /**
235  * ntfs_device_unix_io_pread - Perform a positioned read from the device
236  * @dev:
237  * @buf:
238  * @count:
239  * @offset:
240  *
241  * Description...
242  *
243  * Returns:
244  */
245 static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
246 		s64 count, s64 offset)
247 {
248 #if defined(__BEOS__) || defined(__HAIKU__)
249 	return read_pos(DEV_FD(dev), offset, buf,count);
250 #else
251 	return pread(DEV_FD(dev), buf, count, offset);
252 #endif
253 }
254 
255 /**
256  * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
257  * @dev:
258  * @buf:
259  * @count:
260  * @offset:
261  *
262  * Description...
263  *
264  * Returns:
265  */
266 static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
267 		s64 count, s64 offset)
268 {
269 	if (NDevReadOnly(dev)) {
270 		errno = EROFS;
271 		return -1;
272 	}
273 	NDevSetDirty(dev);
274 #if defined(__BEOS__) || defined(__HAIKU__)
275 	return write_pos(DEV_FD(dev), offset, buf,count);
276 #else
277 	return pwrite(DEV_FD(dev), buf, count, offset);
278 #endif
279 }
280 
281 /**
282  * ntfs_device_unix_io_sync - Flush any buffered changes to the device
283  * @dev:
284  *
285  * Description...
286  *
287  * Returns:
288  */
289 static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
290 {
291 	if (!NDevReadOnly(dev)) {
292 		int res = fsync(DEV_FD(dev));
293 		if (!res)
294 			NDevClearDirty(dev);
295 		return res;
296 	}
297 	return 0;
298 }
299 
300 /**
301  * ntfs_device_unix_io_stat - Get information about the device
302  * @dev:
303  * @buf:
304  *
305  * Description...
306  *
307  * Returns:
308  */
309 static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
310 {
311 	return fstat(DEV_FD(dev), buf);
312 }
313 
314 /**
315  * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
316  * @dev:
317  * @request:
318  * @argp:
319  *
320  * Description...
321  *
322  * Returns:
323  */
324 static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request,
325 		void *argp)
326 {
327 	return ioctl(DEV_FD(dev), request, argp);
328 }
329 
330 /**
331  * Device operations for working with unix style devices and files.
332  */
333 struct ntfs_device_operations ntfs_device_unix_io_ops = {
334 	.open		= ntfs_device_unix_io_open,
335 	.close		= ntfs_device_unix_io_close,
336 	.seek		= ntfs_device_unix_io_seek,
337 	.read		= ntfs_device_unix_io_read,
338 	.write		= ntfs_device_unix_io_write,
339 	.pread		= ntfs_device_unix_io_pread,
340 	.pwrite		= ntfs_device_unix_io_pwrite,
341 	.sync		= ntfs_device_unix_io_sync,
342 	.stat		= ntfs_device_unix_io_stat,
343 	.ioctl		= ntfs_device_unix_io_ioctl,
344 };
345