blkdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /************************************************************************/
  18. /* */
  19. /* PROJECT : exFAT & FAT12/16/32 File System */
  20. /* FILE : blkdev.c */
  21. /* PURPOSE : sdFAT Block Device Driver Glue Layer */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /************************************************************************/
  27. #include <linux/blkdev.h>
  28. #include <linux/log2.h>
  29. #include <linux/backing-dev.h>
  30. #include "sdfat.h"
  31. /*----------------------------------------------------------------------*/
  32. /* Constant & Macro Definitions */
  33. /*----------------------------------------------------------------------*/
  34. /*----------------------------------------------------------------------*/
  35. /* Global Variable Definitions */
  36. /*----------------------------------------------------------------------*/
  37. /*----------------------------------------------------------------------*/
  38. /* Local Variable Definitions */
  39. /*----------------------------------------------------------------------*/
  40. /*----------------------------------------------------------------------*/
  41. /* FUNCTIONS WHICH HAS KERNEL VERSION DEPENDENCY */
  42. /************************************************************************/
  43. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
  44. /* EMPTY */
  45. #else /* LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) */
  46. static struct backing_dev_info *inode_to_bdi(struct inode *bd_inode)
  47. {
  48. return bd_inode->i_mapping->backing_dev_info;
  49. }
  50. #endif
  51. /*======================================================================*/
  52. /* Function Definitions */
  53. /*======================================================================*/
  54. s32 bdev_open_dev(struct super_block *sb)
  55. {
  56. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  57. if (fsi->bd_opened)
  58. return 0;
  59. fsi->bd_opened = true;
  60. return 0;
  61. }
  62. s32 bdev_close_dev(struct super_block *sb)
  63. {
  64. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  65. fsi->bd_opened = false;
  66. return 0;
  67. }
  68. static inline s32 block_device_ejected(struct super_block *sb)
  69. {
  70. struct inode *bd_inode = sb->s_bdev->bd_inode;
  71. struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
  72. return (bdi->dev == NULL);
  73. }
  74. s32 bdev_check_bdi_valid(struct super_block *sb)
  75. {
  76. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  77. if (block_device_ejected(sb)) {
  78. if (!(fsi->prev_eio & SDFAT_EIO_BDI)) {
  79. fsi->prev_eio |= SDFAT_EIO_BDI;
  80. sdfat_log_msg(sb, KERN_ERR, "%s: block device is "
  81. "eliminated.(bdi:%p)", __func__, sb->s_bdi);
  82. }
  83. return -ENXIO;
  84. }
  85. return 0;
  86. }
  87. #if IS_BUILTIN(CONFIG_SDFAT_FS)
  88. static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
  89. {
  90. u32 sects_per_page = (PAGE_SIZE >> sb->s_blocksize_bits);
  91. struct blk_plug plug;
  92. u64 i;
  93. blk_start_plug(&plug);
  94. for (i = 0; i < num_secs; i++) {
  95. if (i && !(i & (sects_per_page - 1)))
  96. blk_flush_plug(current);
  97. sb_breadahead(sb, (sector_t)(secno + i));
  98. }
  99. blk_finish_plug(&plug);
  100. }
  101. #else
  102. static void __bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
  103. {
  104. u64 i;
  105. for (i = 0; i < num_secs; i++)
  106. sb_breadahead(sb, (sector_t)(secno + i));
  107. }
  108. #endif
  109. /* Make a readahead request */
  110. s32 bdev_readahead(struct super_block *sb, u64 secno, u64 num_secs)
  111. {
  112. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  113. if (!fsi->bd_opened)
  114. return -EIO;
  115. __bdev_readahead(sb, secno, num_secs);
  116. return 0;
  117. }
  118. s32 bdev_mread(struct super_block *sb, u64 secno, struct buffer_head **bh, u64 num_secs, s32 read)
  119. {
  120. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  121. u8 blksize_bits = sb->s_blocksize_bits;
  122. #ifdef CONFIG_SDFAT_DBG_IOCTL
  123. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  124. long flags = sbi->debug_flags;
  125. if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
  126. return -EIO;
  127. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  128. if (!fsi->bd_opened)
  129. return -EIO;
  130. brelse(*bh);
  131. if (read)
  132. *bh = __bread(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);
  133. else
  134. *bh = __getblk(sb->s_bdev, (sector_t)secno, num_secs << blksize_bits);
  135. /* read successfully */
  136. if (*bh)
  137. return 0;
  138. /*
  139. * patch 1.2.4 : reset ONCE warning message per volume.
  140. */
  141. if (!(fsi->prev_eio & SDFAT_EIO_READ)) {
  142. fsi->prev_eio |= SDFAT_EIO_READ;
  143. sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
  144. sdfat_debug_warn_on(1);
  145. }
  146. return -EIO;
  147. }
  148. s32 bdev_mwrite(struct super_block *sb, u64 secno, struct buffer_head *bh, u64 num_secs, s32 sync)
  149. {
  150. u64 count;
  151. struct buffer_head *bh2;
  152. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  153. #ifdef CONFIG_SDFAT_DBG_IOCTL
  154. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  155. long flags = sbi->debug_flags;
  156. if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
  157. return -EIO;
  158. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  159. if (!fsi->bd_opened)
  160. return -EIO;
  161. if (secno == bh->b_blocknr) {
  162. set_buffer_uptodate(bh);
  163. mark_buffer_dirty(bh);
  164. if (sync && (sync_dirty_buffer(bh) != 0))
  165. return -EIO;
  166. } else {
  167. count = num_secs << sb->s_blocksize_bits;
  168. bh2 = __getblk(sb->s_bdev, (sector_t)secno, count);
  169. if (!bh2)
  170. goto no_bh;
  171. lock_buffer(bh2);
  172. memcpy(bh2->b_data, bh->b_data, count);
  173. set_buffer_uptodate(bh2);
  174. mark_buffer_dirty(bh2);
  175. unlock_buffer(bh2);
  176. if (sync && (sync_dirty_buffer(bh2) != 0)) {
  177. __brelse(bh2);
  178. goto no_bh;
  179. }
  180. __brelse(bh2);
  181. }
  182. return 0;
  183. no_bh:
  184. /*
  185. * patch 1.2.4 : reset ONCE warning message per volume.
  186. */
  187. if (!(fsi->prev_eio & SDFAT_EIO_WRITE)) {
  188. fsi->prev_eio |= SDFAT_EIO_WRITE;
  189. sdfat_log_msg(sb, KERN_ERR, "%s: No bh. I/O error.", __func__);
  190. sdfat_debug_warn_on(1);
  191. }
  192. return -EIO;
  193. }
  194. s32 bdev_sync_all(struct super_block *sb)
  195. {
  196. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  197. #ifdef CONFIG_SDFAT_DBG_IOCTL
  198. struct sdfat_sb_info *sbi = SDFAT_SB(sb);
  199. long flags = sbi->debug_flags;
  200. if (flags & SDFAT_DEBUGFLAGS_ERROR_RW)
  201. return -EIO;
  202. #endif /* CONFIG_SDFAT_DBG_IOCTL */
  203. if (!fsi->bd_opened)
  204. return -EIO;
  205. return sync_blockdev(sb->s_bdev);
  206. }
  207. /*
  208. * Sector Read/Write Functions
  209. */
  210. s32 read_sect(struct super_block *sb, u64 sec, struct buffer_head **bh, s32 read)
  211. {
  212. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  213. BUG_ON(!bh);
  214. if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
  215. sdfat_fs_error_ratelimit(sb,
  216. "%s: out of range (sect:%llu)", __func__, sec);
  217. return -EIO;
  218. }
  219. if (bdev_mread(sb, sec, bh, 1, read)) {
  220. sdfat_fs_error_ratelimit(sb,
  221. "%s: I/O error (sect:%llu)", __func__, sec);
  222. return -EIO;
  223. }
  224. return 0;
  225. }
  226. s32 write_sect(struct super_block *sb, u64 sec, struct buffer_head *bh, s32 sync)
  227. {
  228. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  229. BUG_ON(!bh);
  230. if ((sec >= fsi->num_sectors) && (fsi->num_sectors > 0)) {
  231. sdfat_fs_error_ratelimit(sb,
  232. "%s: out of range (sect:%llu)", __func__, sec);
  233. return -EIO;
  234. }
  235. if (bdev_mwrite(sb, sec, bh, 1, sync)) {
  236. sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu)",
  237. __func__, sec);
  238. return -EIO;
  239. }
  240. return 0;
  241. }
  242. s32 read_msect(struct super_block *sb, u64 sec, struct buffer_head **bh, u64 num_secs, s32 read)
  243. {
  244. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  245. BUG_ON(!bh);
  246. if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
  247. sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
  248. __func__, sec, num_secs);
  249. return -EIO;
  250. }
  251. if (bdev_mread(sb, sec, bh, num_secs, read)) {
  252. sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
  253. __func__, sec, num_secs);
  254. return -EIO;
  255. }
  256. return 0;
  257. }
  258. s32 write_msect(struct super_block *sb, u64 sec, struct buffer_head *bh, u64 num_secs, s32 sync)
  259. {
  260. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  261. BUG_ON(!bh);
  262. if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
  263. sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
  264. __func__, sec, num_secs);
  265. return -EIO;
  266. }
  267. if (bdev_mwrite(sb, sec, bh, num_secs, sync)) {
  268. sdfat_fs_error_ratelimit(sb, "%s: I/O error (sect:%llu len:%llu)",
  269. __func__, sec, num_secs);
  270. return -EIO;
  271. }
  272. return 0;
  273. }
  274. static inline void __blkdev_write_bhs(struct buffer_head **bhs, s32 nr_bhs)
  275. {
  276. s32 i;
  277. for (i = 0; i < nr_bhs; i++)
  278. write_dirty_buffer(bhs[i], WRITE);
  279. }
  280. static inline s32 __blkdev_sync_bhs(struct buffer_head **bhs, s32 nr_bhs)
  281. {
  282. s32 i, err = 0;
  283. for (i = 0; i < nr_bhs; i++) {
  284. wait_on_buffer(bhs[i]);
  285. if (!err && !buffer_uptodate(bhs[i]))
  286. err = -EIO;
  287. }
  288. return err;
  289. }
  290. static inline s32 __buffer_zeroed(struct super_block *sb, u64 blknr, u64 num_secs)
  291. {
  292. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  293. s32 nr_bhs = MAX_BUF_PER_PAGE;
  294. u64 last_blknr = blknr + num_secs;
  295. s32 err, i, n;
  296. struct blk_plug plug;
  297. /* Zeroing the unused blocks on this cluster */
  298. n = 0;
  299. blk_start_plug(&plug);
  300. while (blknr < last_blknr) {
  301. bhs[n] = sb_getblk(sb, (sector_t)blknr);
  302. if (!bhs[n]) {
  303. err = -ENOMEM;
  304. blk_finish_plug(&plug);
  305. goto error;
  306. }
  307. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  308. set_buffer_uptodate(bhs[n]);
  309. mark_buffer_dirty(bhs[n]);
  310. n++;
  311. blknr++;
  312. if (blknr == last_blknr)
  313. break;
  314. if (n == nr_bhs) {
  315. __blkdev_write_bhs(bhs, n);
  316. for (i = 0; i < n; i++)
  317. brelse(bhs[i]);
  318. n = 0;
  319. }
  320. }
  321. __blkdev_write_bhs(bhs, n);
  322. blk_finish_plug(&plug);
  323. err = __blkdev_sync_bhs(bhs, n);
  324. if (err)
  325. goto error;
  326. for (i = 0; i < n; i++)
  327. brelse(bhs[i]);
  328. return 0;
  329. error:
  330. EMSG("%s: failed zeroed sect %llu\n", __func__, blknr);
  331. for (i = 0; i < n; i++)
  332. bforget(bhs[i]);
  333. return err;
  334. }
  335. s32 write_msect_zero(struct super_block *sb, u64 sec, u64 num_secs)
  336. {
  337. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  338. if (((sec+num_secs) > fsi->num_sectors) && (fsi->num_sectors > 0)) {
  339. sdfat_fs_error_ratelimit(sb, "%s: out of range(sect:%llu len:%llu)",
  340. __func__, sec, num_secs);
  341. return -EIO;
  342. }
  343. /* Just return -EAGAIN if it is failed */
  344. if (__buffer_zeroed(sb, sec, num_secs))
  345. return -EAGAIN;
  346. return 0;
  347. } /* end of write_msect_zero */
  348. /* end of blkdev.c */