Beispiel #1
0
/**
 * ntfs_device_block_size_set - set block size of a device
 * @dev:	open device
 * @block_size: block size to set @dev to
 *
 * On success, return 0.
 * On error return -1 with errno set to the error code.
 *
 * The following error codes are defined:
 *	EINVAL		Input parameter error
 *	EOPNOTSUPP	System does not support BLKBSZSET ioctl
 *	ENOTTY		@dev is a file or a device not supporting BLKBSZSET
 */
int ntfs_device_block_size_set(struct ntfs_device *dev,
		int block_size __attribute__((unused)))
{
	if (!dev) {
		errno = EINVAL;
		return -1;
	}
#ifdef BLKBSZSET
	{
		size_t s_block_size = block_size;
		if (!dev->d_ops->ioctl(dev, BLKBSZSET, &s_block_size)) {
			ntfs_log_debug("Used BLKBSZSET to set block size to "
					"%d bytes.\n", block_size);
			return 0;
		}
		/* If not a block device, pretend it was successful. */
		if (!NDevBlock(dev))
			return 0;
	}
#else
	/* If not a block device, pretend it was successful. */
	if (!NDevBlock(dev))
		return 0;
	errno = EOPNOTSUPP;
#endif
	return -1;
}
Beispiel #2
0
/**
 * ntfs_device_unix_io_open - Open a device and lock it exclusively
 * @dev:
 * @flags:
 *
 * Description...
 *
 * Returns:
 */
static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags)
{
	struct flock flk;
	struct stat sbuf;
	int err;

	if (NDevOpen(dev)) {
		errno = EBUSY;
		return -1;
	}
	if (stat(dev->d_name, &sbuf)) {
		ntfs_log_perror("Failed to access '%s'", dev->d_name);
		return -1;
	}
	if (S_ISBLK(sbuf.st_mode))
		NDevSetBlock(dev);
	
	dev->d_private = ntfs_malloc(sizeof(int));
	if (!dev->d_private)
		return -1;
	/*
	 * Open file for exclusive access if mounting r/w.
	 * Fuseblk takes care about block devices.
	 */ 
	if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR)
		flags |= O_EXCL;
	*(int*)dev->d_private = open(dev->d_name, flags);
	if (*(int*)dev->d_private == -1) {
		err = errno;
		goto err_out;
	}
	
	if ((flags & O_RDWR) != O_RDWR)
		NDevSetReadOnly(dev);
	
	memset(&flk, 0, sizeof(flk));
	if (NDevReadOnly(dev))
		flk.l_type = F_RDLCK;
	else
		flk.l_type = F_WRLCK;
	flk.l_whence = SEEK_SET;
	flk.l_start = flk.l_len = 0LL;
	if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
		err = errno;
		ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? 
				"read" : "write", dev->d_name);
		if (close(DEV_FD(dev)))
			ntfs_log_perror("Failed to close '%s'", dev->d_name);
		goto err_out;
	}
	
	NDevSetOpen(dev);
	return 0;
err_out:
	free(dev->d_private);
	dev->d_private = NULL;
	errno = err;
	return -1;
}