1N/A * device.c - Low level device io functions. Part of the Linux-NTFS project. 1N/A * Copyright (c) 2004-2006 Anton Altaparmakov 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 * 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 * 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 * 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 * 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 * Note, @name is copied and can hence be freed after this functions returns. 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 * ntfs_device_free - free an ntfs device structure 1N/A * @dev: ntfs device structure to free 1N/A * Free the ntfs device structure @dev. 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 * 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 * Auxiliary function, used when we emulate pread by seek() + a sequence of 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 * This function will read @count bytes from device @dev at position @pos into 1N/A * the data buffer @b. 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 * 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 /* Locate to position if pread is to be emulated by seek() + read(). */ 1N/A /* Read the data. */ 1N/A /* If everything ok, continue. */ 1N/A /* If EOF or error return number of bytes read. */ 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 /* Nothing read and error, return error status. */ 1N/A /* Finally, return the number of bytes read. */ 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 * Auxiliary function, used when we emulate pwrite by seek() + a sequence of 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 * This function will write @count bytes from data buffer @b to the device @dev 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 * 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 * Locate to position if pwrite is to be emulated by seek() + write(). 1N/A /* Write the data. */ 1N/A /* If everything ok, continue. */ 1N/A * If nothing written or error return number of bytes written. 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 /* Nothing written and error, return error status. */ 1N/A /* Finally, return the number of bytes written. */ 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 * 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 * 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 * 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 * 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 * 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 /* Finally, return the number of complete blocks read. */ 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 * 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 * 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 * 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 * 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 /* Prepare data for writing. */ 1N/A /* Abort write at this position. */ 1N/A /* Write the prepared data. */ 1N/A /* Quickly deprotect the data again. */ 1N/A /* Finally, return the number of complete blocks written. */ 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 * 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 * 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 * 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 * ntfs_device_offset_valid - test if a device offset is valid 1N/A * @ofs: offset to test for validity 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 * Return 0 if it is valid and -1 if it is not valid. 1N/A * ntfs_device_size_get - return the size of a device in blocks 1N/A * @block_size: block size in bytes in which to return the result 1N/A * Return the number of @block_size sized blocks in the device described by the 1N/A * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o. 1N/A * On error return -1 with errno set to the error code. 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 * ntfs_device_partition_start_sector_get - get starting sector of a partition 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 * 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 * ntfs_device_heads_get - get number of heads of device 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 * 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 * ntfs_device_sectors_per_track_get - get number of sectors per track of device 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 * 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 * ntfs_device_sector_size_get - get sector size of a device 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 * 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 * ntfs_device_block_size_set - set block size of a device 1N/A * @block_size: block size to set @dev to 1N/A * On success, return 0. 1N/A * On error return -1 with errno set to the error code. 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 /* If not a block device, pretend it was successful. */ 1N/A /* If not a block device, pretend it was successful. */