/*
 *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

/************************************************************************/
/*                                                                      */
/*  PROJECT : exFAT & FAT12/16/32 File System                           */
/*  FILE    : blkdev.c                                                  */
/*  PURPOSE : sdFAT Block Device Driver Glue Layer                      */
/*                                                                      */
/*----------------------------------------------------------------------*/
/*  NOTES                                                               */
/*                                                                      */
/************************************************************************/

#include <linux/blkdev.h>
#include <linux/log2.h>
#include <linux/backing-dev.h>

#include "sdfat.h"

/*----------------------------------------------------------------------*/
/*  Constant & Macro Definitions                                        */
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/*  Global Variable Definitions                                         */
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/*  Local Variable Definitions                                          */
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/*  FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY                       */
/************************************************************************/

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
	/* EMPTY */
#else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) */
static struct backing_dev_info *inode_to_bdi(struct inode *bd_inode)
{
	return bd_inode->i_mapping->backing_dev_info;
}
#endif

/*======================================================================*/
/*  Function Definitions                                                */
/*======================================================================*/
s32 bdev_open_dev(struct super_block *sb)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	if (fsi->bd_opened)
		return 0;

	fsi->bd_opened = true;
	return 0;
}

s32 bdev_close_dev(struct super_block *sb)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	fsi->bd_opened = false;
	return 0;
}

static inline s32 block_device_ejected(struct super_block *sb)
{
	struct inode *bd_inode = sb->s_bdev->bd_inode;
	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);

	return (bdi->dev == NULL);
}

s32 bdev_check_bdi_valid(struct super_block *sb)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	if (block_device_ejected(sb)) {
		if (!(fsi->prev_eio & SDFAT_EIO_BDI)) {
			fsi->prev_eio |= SDFAT_EIO_BDI;
			sdfat_log_msg(sb, KERN_ERR, "%s: block device is "
				"eliminated.(bdi:%p)", __func__, sb->s_bdi);
		}
		return -ENXIO;
	}

	return 0;
}

#if IS_BUILTIN(CONFIG_SDFAT_FS)
static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
	u32 sects_per_page = (PAGE_SIZE >> sb->s_blocksize_bits);
	struct blk_plug plug;
	u64 i;

	blk_start_plug(&plug);
	for (i = 0; i < num_secs; i++) {
		if (i && !(i & (sects_per_page - 1)))
			blk_flush_plug(current);
		sb_breadahead(sb, (sector_t)(secno + i));
	}
	blk_finish_plug(&plug);
}
#else
static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
	u64 i;

	for (i = 0; i < num_secs; i++)
		sb_breadahead(sb, (sector_t)(secno + i));
}
#endif

/* Make a readahead request */
s32 bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	if (!fsi->bd_opened)
		return -EIO;

	__bdev_readahead(sb, secno, num_secs);

	return 0;
}

s32 bdev_mread(struct super_block *sb, u64 secno, struct buffer_head **bh, u64 num_secs, s32 read)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
	u8 blksize_bits = sb->s_blocksize_bits;
#ifdef CONFIG_SDFAT_DBG_IOCTL
	struct sdfat_sb_info *sbi = SDFAT_SB(sb);
	long flags = sbi->debug_flags;

	if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
		return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */

	if (!fsi->bd_opened)
		return -EIO;

	brelse(*bh);

	if (read)
		*bh = __bread(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);
	else
		*bh = __getblk(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);

	/* read successfully */
	if (*bh)
		return 0;

	/*
	 * patch 1.2.4 : reset ONCE warning message per volume.
	 */
	if (!(fsi->prev_eio & SDFAT_EIO_READ)) {
		fsi->prev_eio |= SDFAT_EIO_READ;
		sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
		sdfat_debug_warn_on(1);
	}

	return -EIO;
}

s32 bdev_mwrite(struct super_block *sb, u64 secno, struct buffer_head *bh, u64 num_secs, s32 sync)
{
	u64 count;
	struct buffer_head *bh2;
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
#ifdef CONFIG_SDFAT_DBG_IOCTL
	struct sdfat_sb_info *sbi = SDFAT_SB(sb);
	long flags = sbi->debug_flags;

	if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
		return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */

	if (!fsi->bd_opened)
		return -EIO;

	if (secno == bh->b_blocknr) {
		set_buffer_uptodate(bh);
		mark_buffer_dirty(bh);
		if (sync && (sync_dirty_buffer(bh) != 0))
			return -EIO;
	} else {
		count = num_secs << sb->s_blocksize_bits;

		bh2 = __getblk(sb->s_bdev, (sector_t)secno, count);

		if (!bh2)
			goto no_bh;

		lock_buffer(bh2);
		memcpy(bh2->b_data, bh->b_data, count);
		set_buffer_uptodate(bh2);
		mark_buffer_dirty(bh2);
		unlock_buffer(bh2);
		if (sync && (sync_dirty_buffer(bh2) != 0)) {
			__brelse(bh2);
			goto no_bh;
		}
		__brelse(bh2);
	}
	return 0;
no_bh:
	/*
	 * patch 1.2.4 : reset ONCE warning message per volume.
	 */
	if (!(fsi->prev_eio & SDFAT_EIO_WRITE)) {
		fsi->prev_eio |= SDFAT_EIO_WRITE;
		sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
		sdfat_debug_warn_on(1);
	}

	return -EIO;
}

s32 bdev_sync_all(struct super_block *sb)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
#ifdef CONFIG_SDFAT_DBG_IOCTL
	struct sdfat_sb_info *sbi = SDFAT_SB(sb);
	long flags = sbi->debug_flags;

	if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
		return -EIO;
#endif /* CONFIG_SDFAT_DBG_IOCTL */

	if (!fsi->bd_opened)
		return -EIO;

	return sync_blockdev(sb->s_bdev);
}

/*
 *  Sector Read/Write Functions
 */
s32 read_sect(struct super_block *sb, u64 sec, struct buffer_head **bh, s32 read)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	BUG_ON(!bh);
	if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
		sdfat_fs_error_ratelimit(sb,
				"%s: out of range (sect:%llu)", __func__, sec);
		return -EIO;
	}

	if (bdev_mread(sb, sec, bh, 1, read)) {
		sdfat_fs_error_ratelimit(sb,
				"%s: I/O error (sect:%llu)", __func__, sec);
		return -EIO;
	}

	return 0;
}

s32 write_sect(struct super_block *sb, u64 sec, struct buffer_head *bh, s32 sync)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	BUG_ON(!bh);
	if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
		sdfat_fs_error_ratelimit(sb,
				"%s: out of range (sect:%llu)", __func__, sec);
		return -EIO;
	}

	if (bdev_mwrite(sb, sec, bh, 1, sync)) {
		sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu)",
						__func__, sec);
		return -EIO;
	}

	return 0;
}

s32 read_msect(struct super_block *sb, u64 sec, struct buffer_head **bh, u64 num_secs, s32 read)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	BUG_ON(!bh);
	if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
		sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
						__func__, sec, num_secs);
		return -EIO;
	}

	if (bdev_mread(sb, sec, bh, num_secs, read)) {
		sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
						__func__, sec, num_secs);
		return -EIO;
	}

	return 0;
}

s32 write_msect(struct super_block *sb, u64 sec, struct buffer_head *bh, u64 num_secs, s32 sync)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	BUG_ON(!bh);
	if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
		sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
						__func__, sec, num_secs);
		return -EIO;
	}


	if (bdev_mwrite(sb, sec, bh, num_secs, sync)) {
		sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
						__func__, sec, num_secs);
		return -EIO;
	}

	return 0;
}

static inline void __blkdev_write_bhs(struct buffer_head **bhs, s32 nr_bhs)
{
	s32 i;

	for (i = 0; i < nr_bhs; i++)
		write_dirty_buffer(bhs[i], WRITE);
}

static inline s32 __blkdev_sync_bhs(struct buffer_head **bhs, s32 nr_bhs)
{
	s32 i, err = 0;

	for (i = 0; i < nr_bhs; i++) {
		wait_on_buffer(bhs[i]);
		if (!err && !buffer_uptodate(bhs[i]))
			err = -EIO;
	}
	return err;
}

static inline s32 __buffer_zeroed(struct super_block *sb, u64 blknr, u64 num_secs)
{
	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
	s32 nr_bhs = MAX_BUF_PER_PAGE;
	u64 last_blknr = blknr + num_secs;
	s32 err, i, n;
	struct blk_plug plug;

	/* Zeroing the unused blocks on this cluster */
	n = 0;
	blk_start_plug(&plug);
	while (blknr < last_blknr) {
		bhs[n] = sb_getblk(sb, (sector_t)blknr);
		if (!bhs[n]) {
			err = -ENOMEM;
			blk_finish_plug(&plug);
			goto error;
		}
		memset(bhs[n]->b_data, 0, sb->s_blocksize);
		set_buffer_uptodate(bhs[n]);
		mark_buffer_dirty(bhs[n]);

		n++;
		blknr++;

		if (blknr == last_blknr)
			break;

		if (n == nr_bhs) {
			__blkdev_write_bhs(bhs, n);

			for (i = 0; i < n; i++)
				brelse(bhs[i]);
			n = 0;
		}
	}
	__blkdev_write_bhs(bhs, n);
	blk_finish_plug(&plug);

	err = __blkdev_sync_bhs(bhs, n);
	if (err)
		goto error;

	for (i = 0; i < n; i++)
		brelse(bhs[i]);

	return 0;

error:
	EMSG("%s: failed zeroed sect %llu\n", __func__, blknr);
	for (i = 0; i < n; i++)
		bforget(bhs[i]);

	return err;
}

s32 write_msect_zero(struct super_block *sb, u64 sec, u64 num_secs)
{
	FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);

	if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
		sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
						__func__, sec, num_secs);
		return -EIO;
	}

	/* Just return -EAGAIN if it is failed */
	if (__buffer_zeroed(sb, sec, num_secs))
		return -EAGAIN;

	return 0;
} /* end of write_msect_zero */

/* end of blkdev.c */