2 * device.c - Low level device io functions. Originated from the Linux-NTFS project.
4 * Copyright (c) 2004-2006 Anton Altaparmakov
5 * Copyright (c) 2004-2006 Szabolcs Szakacsits
6 * Copyright (c) 2010 Jean-Pierre Andre
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the NTFS-3G
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 #ifdef HAVE_SYS_TYPES_H
44 #include <sys/types.h>
46 #ifdef HAVE_SYS_STAT_H
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
55 #ifdef HAVE_SYS_PARAM_H
56 #include <sys/param.h>
58 #ifdef HAVE_SYS_MOUNT_H
59 #include <sys/mount.h>
61 #ifdef HAVE_SYS_DISK_H
64 #ifdef HAVE_LINUX_FD_H
67 #ifdef HAVE_LINUX_HDREG_H
68 #include <linux/hdreg.h>
78 #if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
79 #define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */
81 #if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64)
82 #define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */
84 #if defined(linux) && !defined(HDIO_GETGEO)
85 #define HDIO_GETGEO 0x0301 /* Get device geometry. */
87 #if defined(linux) && defined(_IO) && !defined(BLKSSZGET)
88 # define BLKSSZGET _IO(0x12,104) /* Get device sector size in bytes. */
90 #if defined(linux) && defined(_IO) && !defined(BLKBSZSET)
91 # define BLKBSZSET _IOW(0x12,113,size_t) /* Set device block size in bytes. */
95 * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it
96 * @name: name of the device (must be present)
97 * @state: initial device state (usually zero)
98 * @dops: ntfs device operations to use with the device (must be present)
99 * @priv_data: pointer to private data (optional)
101 * Allocate an ntfs device structure and pre-initialize it with the user-
102 * specified device operations @dops, device state @state, device name @name,
103 * and optional private data @priv_data.
105 * Note, @name is copied and can hence be freed after this functions returns.
107 * On success return a pointer to the allocated ntfs device structure and on
108 * error return NULL with errno set to the error code returned by ntfs_malloc().
110 struct ntfs_device
*ntfs_device_alloc(const char *name
, const long state
,
111 struct ntfs_device_operations
*dops
, void *priv_data
)
113 struct ntfs_device
*dev
;
120 dev
= ntfs_malloc(sizeof(struct ntfs_device
));
122 if (!(dev
->d_name
= strdup(name
))) {
129 dev
->d_state
= state
;
130 dev
->d_private
= priv_data
;
136 * ntfs_device_free - free an ntfs device structure
137 * @dev: ntfs device structure to free
139 * Free the ntfs device structure @dev.
141 * Return 0 on success or -1 on error with errno set to the error code. The
142 * following error codes are defined:
143 * EINVAL Invalid pointer @dev.
144 * EBUSY Device is still open. Close it before freeing it!
146 int ntfs_device_free(struct ntfs_device
*dev
)
164 * returns zero if successful.
167 int ntfs_device_sync(struct ntfs_device
*dev
)
170 struct ntfs_device_operations
*dops
;
172 if (NDevDirty(dev
)) {
174 ret
= dops
->sync(dev
);
181 * ntfs_pread - positioned read from disk
182 * @dev: device to read from
183 * @pos: position in device to read from
184 * @count: number of bytes to read
185 * @b: output data buffer
187 * This function will read @count bytes from device @dev at position @pos into
188 * the data buffer @b.
190 * On success, return the number of successfully read bytes. If this number is
191 * lower than @count this means that we have either reached end of file or
192 * encountered an error during the read so that the read is partial. 0 means
193 * end of file or nothing to read (@count is 0).
195 * On error and nothing has been read, return -1 with errno set appropriately
196 * to the return code of either seek, read, or set to EINVAL in case of
199 s64
ntfs_pread(struct ntfs_device
*dev
, const s64 pos
, s64 count
, void *b
)
202 struct ntfs_device_operations
*dops
;
204 ntfs_log_trace("pos %lld, count %lld\n",(long long)pos
,(long long)count
);
206 if (!b
|| count
< 0 || pos
< 0) {
215 for (total
= 0; count
; count
-= br
, total
+= br
) {
216 br
= dops
->pread(dev
, (char*)b
+ total
, count
, pos
+ total
);
217 /* If everything ok, continue. */
220 /* If EOF or error return number of bytes read. */
223 /* Nothing read and error, return error status. */
226 /* Finally, return the number of bytes read. */
231 * ntfs_pwrite - positioned write to disk
232 * @dev: device to write to
233 * @pos: position in file descriptor to write to
234 * @count: number of bytes to write
235 * @b: data buffer to write to disk
237 * This function will write @count bytes from data buffer @b to the device @dev
240 * On success, return the number of successfully written bytes. If this number
241 * is lower than @count this means that the write has been interrupted in
242 * flight or that an error was encountered during the write so that the write
243 * is partial. 0 means nothing was written (also return 0 when @count is 0).
245 * On error and nothing has been written, return -1 with errno set
246 * appropriately to the return code of either seek, write, or set
247 * to EINVAL in case of invalid arguments.
249 s64
ntfs_pwrite(struct ntfs_device
*dev
, const s64 pos
, s64 count
,
252 s64 written
, total
, ret
= -1;
253 struct ntfs_device_operations
*dops
;
255 ntfs_log_trace("pos %lld, count %lld\n",(long long)pos
,(long long)count
);
257 if (!b
|| count
< 0 || pos
< 0) {
263 if (NDevReadOnly(dev
)) {
271 for (total
= 0; count
; count
-= written
, total
+= written
) {
272 written
= dops
->pwrite(dev
, (const char*)b
+ total
, count
,
274 /* If everything ok, continue. */
278 * If nothing written or error return number of bytes written.
280 if (!written
|| total
)
282 /* Nothing written and error, return error status. */
286 if (NDevSync(dev
) && total
&& dops
->sync(dev
)) {
287 total
--; /* on sync error, return partially written */
295 * ntfs_mst_pread - multi sector transfer (mst) positioned read
296 * @dev: device to read from
297 * @pos: position in file descriptor to read from
298 * @count: number of blocks to read
299 * @bksize: size of each block that needs mst deprotecting
300 * @b: output data buffer
302 * Multi sector transfer (mst) positioned read. This function will read @count
303 * blocks of size @bksize bytes each from device @dev at position @pos into the
304 * the data buffer @b.
306 * On success, return the number of successfully read blocks. If this number is
307 * lower than @count this means that we have reached end of file, that the read
308 * was interrupted, or that an error was encountered during the read so that
309 * the read is partial. 0 means end of file or nothing was read (also return 0
310 * when @count or @bksize are 0).
312 * On error and nothing was read, return -1 with errno set appropriately to the
313 * return code of either seek, read, or set to EINVAL in case of invalid
316 * NOTE: If an incomplete multi sector transfer has been detected the magic
317 * will have been changed to magic_BAAD but no error will be returned. Thus it
318 * is possible that we return count blocks as being read but that any number
319 * (between zero and count!) of these blocks is actually subject to a multi
320 * sector transfer error. This should be detected by the caller by checking for
321 * the magic being "BAAD".
323 s64
ntfs_mst_pread(struct ntfs_device
*dev
, const s64 pos
, s64 count
,
324 const u32 bksize
, void *b
)
328 if (bksize
& (bksize
- 1) || bksize
% NTFS_BLOCK_SIZE
) {
333 br
= ntfs_pread(dev
, pos
, count
* bksize
, b
);
337 * Apply fixups to successfully read data, disregarding any errors
338 * returned from the MST fixup function. This is because we want to
339 * fixup everything possible and we rely on the fact that the "BAAD"
340 * magic will be detected later on.
343 for (i
= 0; i
< count
; ++i
)
344 ntfs_mst_post_read_fixup((NTFS_RECORD
*)
345 ((u8
*)b
+ i
* bksize
), bksize
);
346 /* Finally, return the number of complete blocks read. */
351 * ntfs_mst_pwrite - multi sector transfer (mst) positioned write
352 * @dev: device to write to
353 * @pos: position in file descriptor to write to
354 * @count: number of blocks to write
355 * @bksize: size of each block that needs mst protecting
356 * @b: data buffer to write to disk
358 * Multi sector transfer (mst) positioned write. This function will write
359 * @count blocks of size @bksize bytes each from data buffer @b to the device
360 * @dev at position @pos.
362 * On success, return the number of successfully written blocks. If this number
363 * is lower than @count this means that the write has been interrupted or that
364 * an error was encountered during the write so that the write is partial. 0
365 * means nothing was written (also return 0 when @count or @bksize are 0).
367 * On error and nothing has been written, return -1 with errno set
368 * appropriately to the return code of either seek, write, or set
369 * to EINVAL in case of invalid arguments.
371 * NOTE: We mst protect the data, write it, then mst deprotect it using a quick
372 * deprotect algorithm (no checking). This saves us from making a copy before
373 * the write and at the same time causes the usn to be incremented in the
374 * buffer. This conceptually fits in better with the idea that cached data is
375 * always deprotected and protection is performed when the data is actually
376 * going to hit the disk and the cache is immediately deprotected again
377 * simulating an mst read on the written data. This way cache coherency is
380 s64
ntfs_mst_pwrite(struct ntfs_device
*dev
, const s64 pos
, s64 count
,
381 const u32 bksize
, void *b
)
385 if (count
< 0 || bksize
% NTFS_BLOCK_SIZE
) {
391 /* Prepare data for writing. */
392 for (i
= 0; i
< count
; ++i
) {
395 err
= ntfs_mst_pre_write_fixup((NTFS_RECORD
*)
396 ((u8
*)b
+ i
* bksize
), bksize
);
398 /* Abort write at this position. */
405 /* Write the prepared data. */
406 written
= ntfs_pwrite(dev
, pos
, count
* bksize
, b
);
407 /* Quickly deprotect the data again. */
408 for (i
= 0; i
< count
; ++i
)
409 ntfs_mst_post_write_fixup((NTFS_RECORD
*)((u8
*)b
+ i
* bksize
));
412 /* Finally, return the number of complete blocks written. */
413 return written
/ bksize
;
417 * ntfs_cluster_read - read ntfs clusters
418 * @vol: volume to read from
419 * @lcn: starting logical cluster number
420 * @count: number of clusters to read
421 * @b: output data buffer
423 * Read @count ntfs clusters starting at logical cluster number @lcn from
424 * volume @vol into buffer @b. Return number of clusters read or -1 on error,
425 * with errno set to the error code.
427 s64
ntfs_cluster_read(const ntfs_volume
*vol
, const s64 lcn
, const s64 count
,
432 if (!vol
|| lcn
< 0 || count
< 0) {
436 if (vol
->nr_clusters
< lcn
+ count
) {
438 ntfs_log_perror("Trying to read outside of volume "
439 "(%lld < %lld)", (long long)vol
->nr_clusters
,
440 (long long)lcn
+ count
);
443 br
= ntfs_pread(vol
->dev
, lcn
<< vol
->cluster_size_bits
,
444 count
<< vol
->cluster_size_bits
, b
);
446 ntfs_log_perror("Error reading cluster(s)");
449 return br
>> vol
->cluster_size_bits
;
453 * ntfs_cluster_write - write ntfs clusters
454 * @vol: volume to write to
455 * @lcn: starting logical cluster number
456 * @count: number of clusters to write
457 * @b: data buffer to write to disk
459 * Write @count ntfs clusters starting at logical cluster number @lcn from
460 * buffer @b to volume @vol. Return the number of clusters written or -1 on
461 * error, with errno set to the error code.
463 s64
ntfs_cluster_write(const ntfs_volume
*vol
, const s64 lcn
,
464 const s64 count
, const void *b
)
468 if (!vol
|| lcn
< 0 || count
< 0) {
472 if (vol
->nr_clusters
< lcn
+ count
) {
474 ntfs_log_perror("Trying to write outside of volume "
475 "(%lld < %lld)", (long long)vol
->nr_clusters
,
476 (long long)lcn
+ count
);
479 if (!NVolReadOnly(vol
))
480 bw
= ntfs_pwrite(vol
->dev
, lcn
<< vol
->cluster_size_bits
,
481 count
<< vol
->cluster_size_bits
, b
);
483 bw
= count
<< vol
->cluster_size_bits
;
485 ntfs_log_perror("Error writing cluster(s)");
488 return bw
>> vol
->cluster_size_bits
;
492 * ntfs_device_offset_valid - test if a device offset is valid
494 * @ofs: offset to test for validity
496 * Test if the offset @ofs is an existing location on the device described
497 * by the open device structure @dev.
499 * Return 0 if it is valid and -1 if it is not valid.
501 static int ntfs_device_offset_valid(struct ntfs_device
*dev
, s64 ofs
)
505 if (dev
->d_ops
->seek(dev
, ofs
, SEEK_SET
) >= 0 &&
506 dev
->d_ops
->read(dev
, &ch
, 1) == 1)
512 * ntfs_device_size_get - return the size of a device in blocks
514 * @block_size: block size in bytes in which to return the result
516 * Return the number of @block_size sized blocks in the device described by the
519 * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o.
521 * On error return -1 with errno set to the error code.
523 s64
ntfs_device_size_get(struct ntfs_device
*dev
, int block_size
)
527 if (!dev
|| block_size
<= 0 || (block_size
- 1) & block_size
) {
534 if (dev
->d_ops
->ioctl(dev
, BLKGETSIZE64
, &size
) >= 0) {
535 ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n",
536 (unsigned long long)size
,
537 (unsigned long long)size
);
538 return (s64
)size
/ block_size
;
543 { unsigned long size
;
545 if (dev
->d_ops
->ioctl(dev
, BLKGETSIZE
, &size
) >= 0) {
546 ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu (0x%lx)\n",
548 return (s64
)size
* 512 / block_size
;
553 { struct floppy_struct this_floppy
;
555 if (dev
->d_ops
->ioctl(dev
, FDGETPRM
, &this_floppy
) >= 0) {
556 ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n",
557 (unsigned long)this_floppy
.size
,
558 (unsigned long)this_floppy
.size
);
559 return (s64
)this_floppy
.size
* 512 / block_size
;
563 #ifdef DIOCGMEDIASIZE
568 if (dev
->d_ops
->ioctl(dev
, DIOCGMEDIASIZE
, &size
) >= 0) {
569 ntfs_log_debug("DIOCGMEDIASIZE nr bytes = %llu (0x%llx)\n",
570 (unsigned long long)size
,
571 (unsigned long long)size
);
572 return (s64
)size
/ block_size
;
576 #ifdef DKIOCGETBLOCKCOUNT
582 sector_size
= ntfs_device_sector_size_get(dev
);
583 if (sector_size
>= 0 && dev
->d_ops
->ioctl(dev
,
584 DKIOCGETBLOCKCOUNT
, &blocks
) >= 0)
586 ntfs_log_debug("DKIOCGETBLOCKCOUNT nr blocks = %llu (0x%llx)\n",
587 (unsigned long long) blocks
,
588 (unsigned long long) blocks
);
589 return blocks
* sector_size
/ block_size
;
594 * We couldn't figure it out by using a specialized ioctl,
595 * so do binary search to find the size of the device.
598 for (high
= 1024LL; !ntfs_device_offset_valid(dev
, high
); high
<<= 1)
600 while (low
< high
- 1LL) {
601 const s64 mid
= (low
+ high
) / 2;
603 if (!ntfs_device_offset_valid(dev
, mid
))
608 dev
->d_ops
->seek(dev
, 0LL, SEEK_SET
);
609 return (low
+ 1LL) / block_size
;
613 * ntfs_device_partition_start_sector_get - get starting sector of a partition
616 * On success, return the starting sector of the partition @dev in the parent
617 * block device of @dev. On error return -1 with errno set to the error code.
619 * The following error codes are defined:
620 * EINVAL Input parameter error
621 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
622 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
624 s64
ntfs_device_partition_start_sector_get(struct ntfs_device
*dev
)
631 { struct hd_geometry geo
;
633 if (!dev
->d_ops
->ioctl(dev
, HDIO_GETGEO
, &geo
)) {
634 ntfs_log_debug("HDIO_GETGEO start_sect = %lu (0x%lx)\n",
635 geo
.start
, geo
.start
);
646 * ntfs_device_heads_get - get number of heads of device
649 * On success, return the number of heads on the device @dev. On error return
650 * -1 with errno set to the error code.
652 * The following error codes are defined:
653 * EINVAL Input parameter error
654 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
655 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
657 int ntfs_device_heads_get(struct ntfs_device
*dev
)
664 { struct hd_geometry geo
;
666 if (!dev
->d_ops
->ioctl(dev
, HDIO_GETGEO
, &geo
)) {
667 ntfs_log_debug("HDIO_GETGEO heads = %u (0x%x)\n",
669 (unsigned)geo
.heads
);
680 * ntfs_device_sectors_per_track_get - get number of sectors per track of device
683 * On success, return the number of sectors per track on the device @dev. On
684 * error return -1 with errno set to the error code.
686 * The following error codes are defined:
687 * EINVAL Input parameter error
688 * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
689 * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
691 int ntfs_device_sectors_per_track_get(struct ntfs_device
*dev
)
698 { struct hd_geometry geo
;
700 if (!dev
->d_ops
->ioctl(dev
, HDIO_GETGEO
, &geo
)) {
701 ntfs_log_debug("HDIO_GETGEO sectors_per_track = %u (0x%x)\n",
702 (unsigned)geo
.sectors
,
703 (unsigned)geo
.sectors
);
714 * ntfs_device_sector_size_get - get sector size of a device
717 * On success, return the sector size in bytes of the device @dev.
718 * On error return -1 with errno set to the error code.
720 * The following error codes are defined:
721 * EINVAL Input parameter error
722 * EOPNOTSUPP System does not support BLKSSZGET ioctl
723 * ENOTTY @dev is a file or a device not supporting BLKSSZGET
725 int ntfs_device_sector_size_get(struct ntfs_device
*dev
)
735 if (!dev
->d_ops
->ioctl(dev
, BLKSSZGET
, §_size
)) {
736 ntfs_log_debug("BLKSSZGET sector size = %d bytes\n",
741 #elif defined(DIOCGSECTORSIZE)
744 size_t sect_size
= 0;
746 if (!dev
->d_ops
->ioctl(dev
, DIOCGSECTORSIZE
, §_size
)) {
747 ntfs_log_debug("DIOCGSECTORSIZE sector size = %d bytes\n",
752 #elif defined(DKIOCGETBLOCKSIZE)
755 uint32_t sect_size
= 0;
757 if (!dev
->d_ops
->ioctl(dev
, DKIOCGETBLOCKSIZE
, §_size
)) {
758 ntfs_log_debug("DKIOCGETBLOCKSIZE sector size = %d bytes\n",
770 * ntfs_device_block_size_set - set block size of a device
772 * @block_size: block size to set @dev to
774 * On success, return 0.
775 * On error return -1 with errno set to the error code.
777 * The following error codes are defined:
778 * EINVAL Input parameter error
779 * EOPNOTSUPP System does not support BLKBSZSET ioctl
780 * ENOTTY @dev is a file or a device not supporting BLKBSZSET
782 int ntfs_device_block_size_set(struct ntfs_device
*dev
,
783 int block_size
__attribute__((unused
)))
791 size_t s_block_size
= block_size
;
792 if (!dev
->d_ops
->ioctl(dev
, BLKBSZSET
, &s_block_size
)) {
793 ntfs_log_debug("Used BLKBSZSET to set block size to "
794 "%d bytes.\n", block_size
);
797 /* If not a block device, pretend it was successful. */
802 /* If not a block device, pretend it was successful. */