1N/A/**
1N/A * device.c - Low level device io functions. Part of the Linux-NTFS project.
1N/A *
1N/A * Copyright (c) 2004-2006 Anton Altaparmakov
1N/A *
1N/A * This program/include file is free software; you can redistribute it and/or
1N/A * modify it under the terms of the GNU General Public License as published
1N/A * by the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program/include file is distributed in the hope that it will be
1N/A * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1N/A * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program (in the main directory of the Linux-NTFS
1N/A * distribution in the file COPYING); if not, write to the Free Software
1N/A * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1N/A */
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A#include "config.h"
1N/A#endif
1N/A
1N/A#ifdef HAVE_UNISTD_H
1N/A#include <unistd.h>
1N/A#endif
1N/A#ifdef HAVE_STDLIB_H
1N/A#include <stdlib.h>
1N/A#endif
1N/A#ifdef HAVE_STRING_H
1N/A#include <string.h>
1N/A#endif
1N/A#ifdef HAVE_ERRNO_H
1N/A#include <errno.h>
1N/A#endif
1N/A#ifdef HAVE_STDIO_H
1N/A#include <stdio.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_TYPES_H
1N/A#include <sys/types.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_STAT_H
1N/A#include <sys/stat.h>
1N/A#endif
1N/A#ifdef HAVE_FCNTL_H
1N/A#include <fcntl.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_IOCTL_H
1N/A#include <sys/ioctl.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_PARAM_H
1N/A#include <sys/param.h>
1N/A#endif
1N/A#ifdef HAVE_SYS_MOUNT_H
1N/A#include <sys/mount.h>
1N/A#endif
1N/A#ifdef HAVE_LINUX_FD_H
1N/A#include <linux/fd.h>
1N/A#endif
1N/A#ifdef HAVE_LINUX_HDREG_H
1N/A#include <linux/hdreg.h>
1N/A#endif
1N/A
1N/A#include "compat.h"
1N/A#include "types.h"
1N/A#include "mst.h"
1N/A#include "debug.h"
1N/A#include "device.h"
1N/A#include "logging.h"
1N/A
1N/A#if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
1N/A#define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */
1N/A#endif
1N/A#if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64)
1N/A#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */
1N/A#endif
1N/A#if defined(linux) && !defined(HDIO_GETGEO)
1N/A#define HDIO_GETGEO 0x0301 /* Get device geometry. */
1N/A#endif
1N/A#if defined(linux) && defined(_IO) && !defined(BLKSSZGET)
1N/A# define BLKSSZGET _IO(0x12,104) /* Get device sector size in bytes. */
1N/A#endif
1N/A#if defined(linux) && defined(_IO) && !defined(BLKBSZSET)
1N/A# define BLKBSZSET _IOW(0x12,113,size_t) /* Set device block size in bytes. */
1N/A#endif
1N/A
1N/A/**
1N/A * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it
1N/A * @name: name of the device (must be present)
1N/A * @state: initial device state (usually zero)
1N/A * @dops: ntfs device operations to use with the device (must be present)
1N/A * @priv_data: pointer to private data (optional)
1N/A *
1N/A * Allocate an ntfs device structure and pre-initialize it with the user-
1N/A * specified device operations @dops, device state @state, device name @name,
1N/A * and optional private data @priv_data.
1N/A *
1N/A * Note, @name is copied and can hence be freed after this functions returns.
1N/A *
1N/A * On success return a pointer to the allocated ntfs device structure and on
1N/A * error return NULL with errno set to the error code returned by malloc().
1N/A */
1N/Astruct ntfs_device *ntfs_device_alloc(const char *name, const long state,
1N/A struct ntfs_device_operations *dops, void *priv_data)
1N/A{
1N/A struct ntfs_device *dev;
1N/A
1N/A if (!name) {
1N/A errno = EINVAL;
1N/A return NULL;
1N/A }
1N/A
1N/A dev = (struct ntfs_device *)ntfs_malloc(sizeof(struct ntfs_device));
1N/A if (dev) {
1N/A if (!(dev->d_name = strdup(name))) {
1N/A int eo = errno;
1N/A free(dev);
1N/A errno = eo;
1N/A return NULL;
1N/A }
1N/A dev->d_ops = dops;
1N/A dev->d_state = state;
1N/A dev->d_private = priv_data;
1N/A }
1N/A return dev;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_free - free an ntfs device structure
1N/A * @dev: ntfs device structure to free
1N/A *
1N/A * Free the ntfs device structure @dev.
1N/A *
1N/A * Return 0 on success or -1 on error with errno set to the error code. The
1N/A * following error codes are defined:
1N/A * EINVAL Invalid pointer @dev.
1N/A * EBUSY Device is still open. Close it before freeing it!
1N/A */
1N/Aint ntfs_device_free(struct ntfs_device *dev)
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (NDevOpen(dev)) {
1N/A errno = EBUSY;
1N/A return -1;
1N/A }
1N/A free(dev->d_name);
1N/A free(dev);
1N/A return 0;
1N/A}
1N/A
1N/A/**
1N/A * fake_pread - read operation disguised as pread
1N/A * @dev: device to read from
1N/A * @b: output data buffer
1N/A * @count: number of bytes to read
1N/A * @pos: position in device to read from
1N/A *
1N/A * Auxiliary function, used when we emulate pread by seek() + a sequence of
1N/A * read()s.
1N/A */
1N/Astatic s64 fake_pread(struct ntfs_device *dev, void *b, s64 count,
1N/A s64 pos __attribute__((unused)))
1N/A{
1N/A return dev->d_ops->read(dev, b, count);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_pread - positioned read from disk
1N/A * @dev: device to read from
1N/A * @pos: position in device to read from
1N/A * @count: number of bytes to read
1N/A * @b: output data buffer
1N/A *
1N/A * This function will read @count bytes from device @dev at position @pos into
1N/A * the data buffer @b.
1N/A *
1N/A * On success, return the number of successfully read bytes. If this number is
1N/A * lower than @count this means that we have either reached end of file or
1N/A * encountered an error during the read so that the read is partial. 0 means
1N/A * end of file or nothing to read (@count is 0).
1N/A *
1N/A * On error and nothing has been read, return -1 with errno set appropriately
1N/A * to the return code of either seek, read, or set to EINVAL in case of
1N/A * invalid arguments.
1N/A */
1N/As64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b)
1N/A{
1N/A s64 br, total;
1N/A struct ntfs_device_operations *dops;
1N/A s64 (*_pread)(struct ntfs_device *, void *, s64, s64);
1N/A
1N/A ntfs_log_trace("Entering for pos 0x%llx, count 0x%llx.\n", pos, count);
1N/A if (!b || count < 0 || pos < 0) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (!count)
1N/A return 0;
1N/A dops = dev->d_ops;
1N/A _pread = dops->pread;
1N/A if (!_pread)
1N/A _pread = fake_pread;
1N/Aseek:
1N/A /* Locate to position if pread is to be emulated by seek() + read(). */
1N/A if (_pread == fake_pread &&
1N/A dops->seek(dev, pos, SEEK_SET) == (off_t)-1) {
1N/A ntfs_log_perror("ntfs_pread: device seek to 0x%llx returned "
1N/A "error", pos);
1N/A return -1;
1N/A }
1N/A /* Read the data. */
1N/A for (total = 0; count; count -= br, total += br) {
1N/A br = _pread(dev, (char*)b + total, count, pos + total);
1N/A /* If everything ok, continue. */
1N/A if (br > 0)
1N/A continue;
1N/A /* If EOF or error return number of bytes read. */
1N/A if (!br || total)
1N/A return total;
1N/A /*
1N/A * If pread is not supported by the OS, fall back to emulating
1N/A * it by seek() + read() and set the device pread() pointer to
1N/A * NULL so we automatically use seek() + read() from now on.
1N/A */
1N/A if (errno == ENOSYS && _pread != fake_pread) {
1N/A _pread = fake_pread;
1N/A dops->pread = NULL;
1N/A goto seek;
1N/A }
1N/A /* Nothing read and error, return error status. */
1N/A return br;
1N/A }
1N/A /* Finally, return the number of bytes read. */
1N/A return total;
1N/A}
1N/A
1N/A/**
1N/A * fake_pwrite - write operation disguised as pwrite
1N/A * @dev: device to write to
1N/A * @b: input data buffer
1N/A * @count: number of bytes to write
1N/A * @pos: position in device to write to
1N/A *
1N/A * Auxiliary function, used when we emulate pwrite by seek() + a sequence of
1N/A * write()s.
1N/A */
1N/Astatic s64 fake_pwrite(struct ntfs_device *dev, const void *b, s64 count,
1N/A s64 pos __attribute__((unused)))
1N/A{
1N/A return dev->d_ops->write(dev, b, count);
1N/A}
1N/A
1N/A/**
1N/A * ntfs_pwrite - positioned write to disk
1N/A * @dev: device to write to
1N/A * @pos: position in file descriptor to write to
1N/A * @count: number of bytes to write
1N/A * @b: data buffer to write to disk
1N/A *
1N/A * This function will write @count bytes from data buffer @b to the device @dev
1N/A * at position @pos.
1N/A *
1N/A * On success, return the number of successfully written bytes. If this number
1N/A * is lower than @count this means that the write has been interrupted in
1N/A * flight or that an error was encountered during the write so that the write
1N/A * is partial. 0 means nothing was written (also return 0 when @count is 0).
1N/A *
1N/A * On error and nothing has been written, return -1 with errno set
1N/A * appropriately to the return code of either seek, write, or set
1N/A * to EINVAL in case of invalid arguments.
1N/A */
1N/As64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
1N/A const void *b)
1N/A{
1N/A s64 written, total;
1N/A struct ntfs_device_operations *dops;
1N/A s64 (*_pwrite)(struct ntfs_device *, const void *, s64, s64);
1N/A
1N/A ntfs_log_trace("Entering for pos 0x%llx, count 0x%llx.\n", pos, count);
1N/A if (!b || count < 0 || pos < 0) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (!count)
1N/A return 0;
1N/A if (NDevReadOnly(dev)) {
1N/A errno = EROFS;
1N/A return -1;
1N/A }
1N/A dops = dev->d_ops;
1N/A _pwrite = dops->pwrite;
1N/A if (!_pwrite)
1N/A _pwrite = fake_pwrite;
1N/Aseek:
1N/A /*
1N/A * Locate to position if pwrite is to be emulated by seek() + write().
1N/A */
1N/A if (_pwrite == fake_pwrite &&
1N/A dops->seek(dev, pos, SEEK_SET) == (off_t)-1) {
1N/A ntfs_log_perror("ntfs_pwrite: seek to 0x%llx returned error",
1N/A pos);
1N/A return -1;
1N/A }
1N/A NDevSetDirty(dev);
1N/A /* Write the data. */
1N/A for (total = 0; count; count -= written, total += written) {
1N/A written = _pwrite(dev, (const char*)b + total, count,
1N/A pos + total);
1N/A /* If everything ok, continue. */
1N/A if (written > 0)
1N/A continue;
1N/A /*
1N/A * If nothing written or error return number of bytes written.
1N/A */
1N/A if (!written || total)
1N/A break;
1N/A /*
1N/A * If pwrite is not supported by the OS, fall back to emulating
1N/A * it by seek() + write() and set the device pwrite() pointer
1N/A * to NULL so we automatically use seek() + write() from now
1N/A * on.
1N/A */
1N/A if (errno == ENOSYS && _pwrite != fake_pwrite) {
1N/A _pwrite = fake_pwrite;
1N/A dops->pwrite = NULL;
1N/A goto seek;
1N/A }
1N/A /* Nothing written and error, return error status. */
1N/A return written;
1N/A }
1N/A /* Finally, return the number of bytes written. */
1N/A return total;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_mst_pread - multi sector transfer (mst) positioned read
1N/A * @dev: device to read from
1N/A * @pos: position in file descriptor to read from
1N/A * @count: number of blocks to read
1N/A * @bksize: size of each block that needs mst deprotecting
1N/A * @b: output data buffer
1N/A *
1N/A * Multi sector transfer (mst) positioned read. This function will read @count
1N/A * blocks of size @bksize bytes each from device @dev at position @pos into the
1N/A * the data buffer @b.
1N/A *
1N/A * On success, return the number of successfully read blocks. If this number is
1N/A * lower than @count this means that we have reached end of file, that the read
1N/A * was interrupted, or that an error was encountered during the read so that
1N/A * the read is partial. 0 means end of file or nothing was read (also return 0
1N/A * when @count or @bksize are 0).
1N/A *
1N/A * On error and nothing was read, return -1 with errno set appropriately to the
1N/A * return code of either seek, read, or set to EINVAL in case of invalid
1N/A * arguments.
1N/A *
1N/A * NOTE: If an incomplete multi sector transfer has been detected the magic
1N/A * will have been changed to magic_BAAD but no error will be returned. Thus it
1N/A * is possible that we return count blocks as being read but that any number
1N/A * (between zero and count!) of these blocks is actually subject to a multi
1N/A * sector transfer error. This should be detected by the caller by checking for
1N/A * the magic being "BAAD".
1N/A */
1N/As64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count,
1N/A const u32 bksize, void *b)
1N/A{
1N/A s64 br, i;
1N/A
1N/A if (bksize & (bksize - 1) || bksize % NTFS_BLOCK_SIZE) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A /* Do the read. */
1N/A br = ntfs_pread(dev, pos, count * bksize, b);
1N/A if (br < 0)
1N/A return br;
1N/A /*
1N/A * Apply fixups to successfully read data, disregarding any errors
1N/A * returned from the MST fixup function. This is because we want to
1N/A * fixup everything possible and we rely on the fact that the "BAAD"
1N/A * magic will be detected later on.
1N/A */
1N/A count = br / bksize;
1N/A for (i = 0; i < count; ++i)
1N/A ntfs_mst_post_read_fixup((NTFS_RECORD*)
1N/A ((u8*)b + i * bksize), bksize);
1N/A /* Finally, return the number of complete blocks read. */
1N/A return count;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_mst_pwrite - multi sector transfer (mst) positioned write
1N/A * @dev: device to write to
1N/A * @pos: position in file descriptor to write to
1N/A * @count: number of blocks to write
1N/A * @bksize: size of each block that needs mst protecting
1N/A * @b: data buffer to write to disk
1N/A *
1N/A * Multi sector transfer (mst) positioned write. This function will write
1N/A * @count blocks of size @bksize bytes each from data buffer @b to the device
1N/A * @dev at position @pos.
1N/A *
1N/A * On success, return the number of successfully written blocks. If this number
1N/A * is lower than @count this means that the write has been interrupted or that
1N/A * an error was encountered during the write so that the write is partial. 0
1N/A * means nothing was written (also return 0 when @count or @bksize are 0).
1N/A *
1N/A * On error and nothing has been written, return -1 with errno set
1N/A * appropriately to the return code of either seek, write, or set
1N/A * to EINVAL in case of invalid arguments.
1N/A *
1N/A * NOTE: We mst protect the data, write it, then mst deprotect it using a quick
1N/A * deprotect algorithm (no checking). This saves us from making a copy before
1N/A * the write and at the same time causes the usn to be incremented in the
1N/A * buffer. This conceptually fits in better with the idea that cached data is
1N/A * always deprotected and protection is performed when the data is actually
1N/A * going to hit the disk and the cache is immediately deprotected again
1N/A * simulating an mst read on the written data. This way cache coherency is
1N/A * achieved.
1N/A */
1N/As64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count,
1N/A const u32 bksize, void *b)
1N/A{
1N/A s64 written, i;
1N/A
1N/A if (count < 0 || bksize % NTFS_BLOCK_SIZE) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (!count)
1N/A return 0;
1N/A /* Prepare data for writing. */
1N/A for (i = 0; i < count; ++i) {
1N/A int err;
1N/A
1N/A err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)
1N/A ((u8*)b + i * bksize), bksize);
1N/A if (err < 0) {
1N/A /* Abort write at this position. */
1N/A if (!i)
1N/A return err;
1N/A count = i;
1N/A break;
1N/A }
1N/A }
1N/A /* Write the prepared data. */
1N/A written = ntfs_pwrite(dev, pos, count * bksize, b);
1N/A /* Quickly deprotect the data again. */
1N/A for (i = 0; i < count; ++i)
1N/A ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)b + i * bksize));
1N/A if (written <= 0)
1N/A return written;
1N/A /* Finally, return the number of complete blocks written. */
1N/A return written / bksize;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_cluster_read - read ntfs clusters
1N/A * @vol: volume to read from
1N/A * @lcn: starting logical cluster number
1N/A * @count: number of clusters to read
1N/A * @b: output data buffer
1N/A *
1N/A * Read @count ntfs clusters starting at logical cluster number @lcn from
1N/A * volume @vol into buffer @b. Return number of clusters read or -1 on error,
1N/A * with errno set to the error code.
1N/A */
1N/As64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, const s64 count,
1N/A void *b)
1N/A{
1N/A s64 br;
1N/A
1N/A if (!vol || lcn < 0 || count < 0) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (vol->nr_clusters < lcn + count) {
1N/A errno = ESPIPE;
1N/A return -1;
1N/A }
1N/A br = ntfs_pread(vol->u.dev, lcn << vol->cluster_size_bits,
1N/A count << vol->cluster_size_bits, b);
1N/A if (br < 0) {
1N/A ntfs_log_perror("Error reading cluster(s)");
1N/A return br;
1N/A }
1N/A return br >> vol->cluster_size_bits;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_cluster_write - write ntfs clusters
1N/A * @vol: volume to write to
1N/A * @lcn: starting logical cluster number
1N/A * @count: number of clusters to write
1N/A * @b: data buffer to write to disk
1N/A *
1N/A * Write @count ntfs clusters starting at logical cluster number @lcn from
1N/A * buffer @b to volume @vol. Return the number of clusters written or -1 on
1N/A * error, with errno set to the error code.
1N/A */
1N/As64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn,
1N/A const s64 count, const void *b)
1N/A{
1N/A s64 bw;
1N/A
1N/A if (!vol || lcn < 0 || count < 0) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A if (vol->nr_clusters < lcn + count) {
1N/A errno = ESPIPE;
1N/A return -1;
1N/A }
1N/A if (!NVolReadOnly(vol))
1N/A bw = ntfs_pwrite(vol->u.dev, lcn << vol->cluster_size_bits,
1N/A count << vol->cluster_size_bits, b);
1N/A else
1N/A bw = count << vol->cluster_size_bits;
1N/A if (bw < 0) {
1N/A ntfs_log_perror("Error writing cluster(s)");
1N/A return bw;
1N/A }
1N/A return bw >> vol->cluster_size_bits;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_offset_valid - test if a device offset is valid
1N/A * @dev: open device
1N/A * @ofs: offset to test for validity
1N/A *
1N/A * Test if the offset @ofs is an existing location on the device described
1N/A * by the open device structure @dev.
1N/A *
1N/A * Return 0 if it is valid and -1 if it is not valid.
1N/A */
1N/Astatic int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs)
1N/A{
1N/A char ch;
1N/A
1N/A if (dev->d_ops->seek(dev, ofs, SEEK_SET) >= 0 &&
1N/A dev->d_ops->read(dev, &ch, 1) == 1)
1N/A return 0;
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_size_get - return the size of a device in blocks
1N/A * @dev: open device
1N/A * @block_size: block size in bytes in which to return the result
1N/A *
1N/A * Return the number of @block_size sized blocks in the device described by the
1N/A * open device @dev.
1N/A *
1N/A * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o.
1N/A *
1N/A * On error return -1 with errno set to the error code.
1N/A */
1N/As64 ntfs_device_size_get(struct ntfs_device *dev, int block_size)
1N/A{
1N/A s64 high, low;
1N/A
1N/A if (!dev || block_size <= 0 || (block_size - 1) & block_size) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef BLKGETSIZE64
1N/A { u64 size;
1N/A
1N/A if (dev->d_ops->ioctl(dev, BLKGETSIZE64, &size) >= 0) {
1N/A ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n",
1N/A (unsigned long long)size,
1N/A (unsigned long long)size);
1N/A return (s64)size / block_size;
1N/A }
1N/A }
1N/A#endif
1N/A#ifdef BLKGETSIZE
1N/A { unsigned long size;
1N/A
1N/A if (dev->d_ops->ioctl(dev, BLKGETSIZE, &size) >= 0) {
1N/A ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu (0x%lx)\n",
1N/A size, size);
1N/A return (s64)size * 512 / block_size;
1N/A }
1N/A }
1N/A#endif
1N/A#ifdef FDGETPRM
1N/A { struct floppy_struct this_floppy;
1N/A
1N/A if (dev->d_ops->ioctl(dev, FDGETPRM, &this_floppy) >= 0) {
1N/A ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n",
1N/A (unsigned long)this_floppy.size,
1N/A (unsigned long)this_floppy.size);
1N/A return (s64)this_floppy.size * 512 / block_size;
1N/A }
1N/A }
1N/A#endif
1N/A /*
1N/A * We couldn't figure it out by using a specialized ioctl,
1N/A * so do binary search to find the size of the device.
1N/A */
1N/A low = 0LL;
1N/A for (high = 1024LL; !ntfs_device_offset_valid(dev, high); high <<= 1)
1N/A low = high;
1N/A while (low < high - 1LL) {
1N/A const s64 mid = (low + high) / 2;
1N/A
1N/A if (!ntfs_device_offset_valid(dev, mid))
1N/A low = mid;
1N/A else
1N/A high = mid;
1N/A }
1N/A dev->d_ops->seek(dev, 0LL, SEEK_SET);
1N/A return (low + 1LL) / block_size;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_partition_start_sector_get - get starting sector of a partition
1N/A * @dev: open device
1N/A *
1N/A * On success, return the starting sector of the partition @dev in the parent
1N/A * block device of @dev. On error return -1 with errno set to the error code.
1N/A *
1N/A * The following error codes are defined:
1N/A * EINVAL Input parameter error
1N/A * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
1N/A * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
1N/A */
1N/As64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev)
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef HDIO_GETGEO
1N/A { struct hd_geometry geo;
1N/A
1N/A if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
1N/A ntfs_log_debug("HDIO_GETGEO start_sect = %lu (0x%lx)\n",
1N/A geo.start, geo.start);
1N/A return geo.start;
1N/A }
1N/A }
1N/A#else
1N/A errno = EOPNOTSUPP;
1N/A#endif
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_heads_get - get number of heads of device
1N/A * @dev: open device
1N/A *
1N/A * On success, return the number of heads on the device @dev. On error return
1N/A * -1 with errno set to the error code.
1N/A *
1N/A * The following error codes are defined:
1N/A * EINVAL Input parameter error
1N/A * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
1N/A * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
1N/A */
1N/Aint ntfs_device_heads_get(struct ntfs_device *dev)
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef HDIO_GETGEO
1N/A { struct hd_geometry geo;
1N/A
1N/A if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
1N/A ntfs_log_debug("HDIO_GETGEO heads = %u (0x%x)\n",
1N/A (unsigned)geo.heads,
1N/A (unsigned)geo.heads);
1N/A return geo.heads;
1N/A }
1N/A }
1N/A#else
1N/A errno = EOPNOTSUPP;
1N/A#endif
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_sectors_per_track_get - get number of sectors per track of device
1N/A * @dev: open device
1N/A *
1N/A * On success, return the number of sectors per track on the device @dev. On
1N/A * error return -1 with errno set to the error code.
1N/A *
1N/A * The following error codes are defined:
1N/A * EINVAL Input parameter error
1N/A * EOPNOTSUPP System does not support HDIO_GETGEO ioctl
1N/A * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO
1N/A */
1N/Aint ntfs_device_sectors_per_track_get(struct ntfs_device *dev)
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef HDIO_GETGEO
1N/A { struct hd_geometry geo;
1N/A
1N/A if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) {
1N/A ntfs_log_debug("HDIO_GETGEO sectors_per_track = %u (0x%x)\n",
1N/A (unsigned)geo.sectors,
1N/A (unsigned)geo.sectors);
1N/A return geo.sectors;
1N/A }
1N/A }
1N/A#else
1N/A errno = EOPNOTSUPP;
1N/A#endif
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_sector_size_get - get sector size of a device
1N/A * @dev: open device
1N/A *
1N/A * On success, return the sector size in bytes of the device @dev.
1N/A * On error return -1 with errno set to the error code.
1N/A *
1N/A * The following error codes are defined:
1N/A * EINVAL Input parameter error
1N/A * EOPNOTSUPP System does not support BLKSSZGET ioctl
1N/A * ENOTTY @dev is a file or a device not supporting BLKSSZGET
1N/A */
1N/Aint ntfs_device_sector_size_get(struct ntfs_device *dev)
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef BLKSSZGET
1N/A {
1N/A int sect_size = 0;
1N/A
1N/A if (!dev->d_ops->ioctl(dev, BLKSSZGET, &sect_size)) {
1N/A ntfs_log_debug("BLKSSZGET sector size = %d bytes\n",
1N/A sect_size);
1N/A return sect_size;
1N/A }
1N/A }
1N/A#else
1N/A errno = EOPNOTSUPP;
1N/A#endif
1N/A return -1;
1N/A}
1N/A
1N/A/**
1N/A * ntfs_device_block_size_set - set block size of a device
1N/A * @dev: open device
1N/A * @block_size: block size to set @dev to
1N/A *
1N/A * On success, return 0.
1N/A * On error return -1 with errno set to the error code.
1N/A *
1N/A * The following error codes are defined:
1N/A * EINVAL Input parameter error
1N/A * EOPNOTSUPP System does not support BLKBSZSET ioctl
1N/A * ENOTTY @dev is a file or a device not supporting BLKBSZSET
1N/A */
1N/Aint ntfs_device_block_size_set(struct ntfs_device *dev,
1N/A int block_size __attribute__((unused)))
1N/A{
1N/A if (!dev) {
1N/A errno = EINVAL;
1N/A return -1;
1N/A }
1N/A#ifdef BLKBSZSET
1N/A {
1N/A size_t s_block_size = block_size;
1N/A if (!dev->d_ops->ioctl(dev, BLKBSZSET, &s_block_size)) {
1N/A ntfs_log_debug("Used BLKBSZSET to set block size to "
1N/A "%d bytes.\n", block_size);
1N/A return 0;
1N/A }
1N/A /* If not a block device, pretend it was successful. */
1N/A if (!NDevBlock(dev))
1N/A return 0;
1N/A }
1N/A#else
1N/A /* If not a block device, pretend it was successful. */
1N/A if (!NDevBlock(dev))
1N/A return 0;
1N/A errno = EOPNOTSUPP;
1N/A#endif
1N/A return -1;
1N/A}