fatent.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/unaligned.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/blkdev.h>
  9. #include "exfat_raw.h"
  10. #include "exfat_fs.h"
  11. static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
  12. struct buffer_head *bh)
  13. {
  14. struct buffer_head *c_bh;
  15. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  16. sector_t sec2;
  17. int err = 0;
  18. if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
  19. sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
  20. c_bh = sb_getblk(sb, sec2);
  21. if (!c_bh)
  22. return -ENOMEM;
  23. memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
  24. set_buffer_uptodate(c_bh);
  25. mark_buffer_dirty(c_bh);
  26. if (sb->s_flags & SB_SYNCHRONOUS)
  27. err = sync_dirty_buffer(c_bh);
  28. brelse(c_bh);
  29. }
  30. return err;
  31. }
  32. static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
  33. unsigned int *content, struct buffer_head **last)
  34. {
  35. unsigned int off;
  36. sector_t sec;
  37. struct buffer_head *bh = last ? *last : NULL;
  38. sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
  39. off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
  40. if (!bh || bh->b_blocknr != sec || !buffer_uptodate(bh)) {
  41. brelse(bh);
  42. bh = sb_bread(sb, sec);
  43. if (last)
  44. *last = bh;
  45. if (unlikely(!bh))
  46. return -EIO;
  47. }
  48. *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
  49. /* remap reserved clusters to simplify code */
  50. if (*content > EXFAT_BAD_CLUSTER)
  51. *content = EXFAT_EOF_CLUSTER;
  52. if (!last)
  53. brelse(bh);
  54. return 0;
  55. }
  56. int exfat_ent_set(struct super_block *sb, unsigned int loc,
  57. unsigned int content)
  58. {
  59. unsigned int off;
  60. sector_t sec;
  61. __le32 *fat_entry;
  62. struct buffer_head *bh;
  63. sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
  64. off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
  65. bh = sb_bread(sb, sec);
  66. if (!bh)
  67. return -EIO;
  68. fat_entry = (__le32 *)&(bh->b_data[off]);
  69. *fat_entry = cpu_to_le32(content);
  70. exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
  71. exfat_mirror_bh(sb, sec, bh);
  72. brelse(bh);
  73. return 0;
  74. }
  75. /*
  76. * Caller must release the buffer_head if no error return.
  77. */
  78. int exfat_ent_get(struct super_block *sb, unsigned int loc,
  79. unsigned int *content, struct buffer_head **last)
  80. {
  81. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  82. if (!is_valid_cluster(sbi, loc)) {
  83. exfat_fs_error_ratelimit(sb,
  84. "invalid access to FAT (entry 0x%08x)",
  85. loc);
  86. goto err;
  87. }
  88. if (unlikely(__exfat_ent_get(sb, loc, content, last))) {
  89. exfat_fs_error_ratelimit(sb,
  90. "failed to access to FAT (entry 0x%08x)",
  91. loc);
  92. goto err;
  93. }
  94. if (unlikely(*content == EXFAT_FREE_CLUSTER)) {
  95. exfat_fs_error_ratelimit(sb,
  96. "invalid access to FAT free cluster (entry 0x%08x)",
  97. loc);
  98. goto err;
  99. }
  100. if (unlikely(*content == EXFAT_BAD_CLUSTER)) {
  101. exfat_fs_error_ratelimit(sb,
  102. "invalid access to FAT bad cluster (entry 0x%08x)",
  103. loc);
  104. goto err;
  105. }
  106. if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
  107. exfat_fs_error_ratelimit(sb,
  108. "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
  109. loc, *content);
  110. goto err;
  111. }
  112. return 0;
  113. err:
  114. if (last) {
  115. brelse(*last);
  116. /* Avoid double release */
  117. *last = NULL;
  118. }
  119. return -EIO;
  120. }
  121. int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
  122. unsigned int len)
  123. {
  124. if (!len)
  125. return 0;
  126. while (len > 1) {
  127. if (exfat_ent_set(sb, chain, chain + 1))
  128. return -EIO;
  129. chain++;
  130. len--;
  131. }
  132. if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
  133. return -EIO;
  134. return 0;
  135. }
  136. static inline void exfat_discard_cluster(struct super_block *sb,
  137. unsigned int clu, unsigned int num_clusters)
  138. {
  139. int ret;
  140. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  141. ret = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, clu),
  142. sbi->sect_per_clus * num_clusters, GFP_NOFS, 0);
  143. if (ret == -EOPNOTSUPP) {
  144. exfat_err(sb, "discard not supported by device, disabling");
  145. sbi->options.discard = 0;
  146. }
  147. }
  148. /* This function must be called with bitmap_lock held */
  149. static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
  150. {
  151. struct super_block *sb = inode->i_sb;
  152. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  153. int cur_cmap_i, next_cmap_i;
  154. unsigned int num_clusters = 0;
  155. unsigned int clu;
  156. /* invalid cluster number */
  157. if (p_chain->dir == EXFAT_FREE_CLUSTER ||
  158. p_chain->dir == EXFAT_EOF_CLUSTER ||
  159. p_chain->dir < EXFAT_FIRST_CLUSTER)
  160. return 0;
  161. /* no cluster to truncate */
  162. if (p_chain->size == 0)
  163. return 0;
  164. /* check cluster validation */
  165. if (!is_valid_cluster(sbi, p_chain->dir)) {
  166. exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
  167. return -EIO;
  168. }
  169. clu = p_chain->dir;
  170. cur_cmap_i = next_cmap_i =
  171. BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu));
  172. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  173. int err;
  174. unsigned int last_cluster = p_chain->dir + p_chain->size - 1;
  175. do {
  176. bool sync = false;
  177. if (clu < last_cluster)
  178. next_cmap_i =
  179. BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1));
  180. /* flush bitmap only if index would be changed or for last cluster */
  181. if (clu == last_cluster || cur_cmap_i != next_cmap_i) {
  182. sync = true;
  183. cur_cmap_i = next_cmap_i;
  184. }
  185. err = exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode)));
  186. if (err)
  187. break;
  188. clu++;
  189. num_clusters++;
  190. } while (num_clusters < p_chain->size);
  191. if (sbi->options.discard)
  192. exfat_discard_cluster(sb, p_chain->dir, p_chain->size);
  193. } else {
  194. unsigned int nr_clu = 1;
  195. do {
  196. bool sync = false;
  197. unsigned int n_clu = clu;
  198. int err = exfat_get_next_cluster(sb, &n_clu);
  199. if (err || n_clu == EXFAT_EOF_CLUSTER)
  200. sync = true;
  201. else
  202. next_cmap_i =
  203. BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu));
  204. if (cur_cmap_i != next_cmap_i) {
  205. sync = true;
  206. cur_cmap_i = next_cmap_i;
  207. }
  208. if (exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode))))
  209. break;
  210. if (sbi->options.discard) {
  211. if (n_clu == clu + 1)
  212. nr_clu++;
  213. else {
  214. exfat_discard_cluster(sb, clu - nr_clu + 1, nr_clu);
  215. nr_clu = 1;
  216. }
  217. }
  218. clu = n_clu;
  219. num_clusters++;
  220. if (err)
  221. break;
  222. if (num_clusters >= sbi->num_clusters - EXFAT_FIRST_CLUSTER) {
  223. /*
  224. * The cluster chain includes a loop, scan the
  225. * bitmap to get the number of used clusters.
  226. */
  227. exfat_count_used_clusters(sb, &sbi->used_clusters);
  228. return 0;
  229. }
  230. } while (clu != EXFAT_EOF_CLUSTER);
  231. }
  232. sbi->used_clusters -= num_clusters;
  233. return 0;
  234. }
  235. int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
  236. {
  237. int ret = 0;
  238. mutex_lock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
  239. ret = __exfat_free_cluster(inode, p_chain);
  240. mutex_unlock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
  241. return ret;
  242. }
  243. int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
  244. unsigned int *ret_clu)
  245. {
  246. struct buffer_head *bh = NULL;
  247. unsigned int clu, next;
  248. unsigned int count = 0;
  249. next = p_chain->dir;
  250. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  251. *ret_clu = next + p_chain->size - 1;
  252. return 0;
  253. }
  254. do {
  255. count++;
  256. clu = next;
  257. if (exfat_ent_get(sb, clu, &next, &bh))
  258. return -EIO;
  259. } while (next != EXFAT_EOF_CLUSTER && count <= p_chain->size);
  260. brelse(bh);
  261. if (p_chain->size != count) {
  262. exfat_fs_error(sb,
  263. "bogus directory size (clus : ondisk(%d) != counted(%d))",
  264. p_chain->size, count);
  265. return -EIO;
  266. }
  267. *ret_clu = clu;
  268. return 0;
  269. }
  270. int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
  271. {
  272. struct super_block *sb = dir->i_sb;
  273. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  274. struct buffer_head *bh;
  275. sector_t blknr, last_blknr, i;
  276. blknr = exfat_cluster_to_sector(sbi, clu);
  277. last_blknr = blknr + sbi->sect_per_clus;
  278. if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
  279. exfat_fs_error_ratelimit(sb,
  280. "%s: out of range(sect:%llu len:%u)",
  281. __func__, (unsigned long long)blknr,
  282. sbi->sect_per_clus);
  283. return -EIO;
  284. }
  285. /* Zeroing the unused blocks on this cluster */
  286. for (i = blknr; i < last_blknr; i++) {
  287. bh = sb_getblk(sb, i);
  288. if (!bh)
  289. return -ENOMEM;
  290. memset(bh->b_data, 0, sb->s_blocksize);
  291. set_buffer_uptodate(bh);
  292. mark_buffer_dirty(bh);
  293. brelse(bh);
  294. }
  295. if (IS_DIRSYNC(dir))
  296. return sync_blockdev_range(sb->s_bdev,
  297. EXFAT_BLK_TO_B(blknr, sb),
  298. EXFAT_BLK_TO_B(last_blknr, sb) - 1);
  299. return 0;
  300. }
  301. int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
  302. struct exfat_chain *p_chain, bool sync_bmap)
  303. {
  304. int ret = -ENOSPC;
  305. unsigned int total_cnt;
  306. unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
  307. struct super_block *sb = inode->i_sb;
  308. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  309. total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
  310. if (unlikely(total_cnt < sbi->used_clusters)) {
  311. exfat_fs_error_ratelimit(sb,
  312. "%s: invalid used clusters(t:%u,u:%u)\n",
  313. __func__, total_cnt, sbi->used_clusters);
  314. return -EIO;
  315. }
  316. if (num_alloc > total_cnt - sbi->used_clusters)
  317. return -ENOSPC;
  318. mutex_lock(&sbi->bitmap_lock);
  319. hint_clu = p_chain->dir;
  320. /* find new cluster */
  321. if (hint_clu == EXFAT_EOF_CLUSTER) {
  322. if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
  323. exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)",
  324. sbi->clu_srch_ptr);
  325. sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
  326. }
  327. hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
  328. if (hint_clu == EXFAT_EOF_CLUSTER) {
  329. ret = -ENOSPC;
  330. goto unlock;
  331. }
  332. }
  333. /* check cluster validation */
  334. if (!is_valid_cluster(sbi, hint_clu)) {
  335. if (hint_clu != sbi->num_clusters)
  336. exfat_err(sb, "hint_cluster is invalid (%u), rewind to the first cluster",
  337. hint_clu);
  338. hint_clu = EXFAT_FIRST_CLUSTER;
  339. p_chain->flags = ALLOC_FAT_CHAIN;
  340. }
  341. p_chain->dir = EXFAT_EOF_CLUSTER;
  342. while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
  343. EXFAT_EOF_CLUSTER) {
  344. if (new_clu != hint_clu &&
  345. p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  346. if (exfat_chain_cont_cluster(sb, p_chain->dir,
  347. p_chain->size)) {
  348. ret = -EIO;
  349. goto free_cluster;
  350. }
  351. p_chain->flags = ALLOC_FAT_CHAIN;
  352. }
  353. /* update allocation bitmap */
  354. if (exfat_set_bitmap(sb, new_clu, sync_bmap)) {
  355. ret = -EIO;
  356. goto free_cluster;
  357. }
  358. /* update FAT table */
  359. if (p_chain->flags == ALLOC_FAT_CHAIN) {
  360. if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
  361. ret = -EIO;
  362. goto free_cluster;
  363. }
  364. }
  365. if (p_chain->dir == EXFAT_EOF_CLUSTER) {
  366. p_chain->dir = new_clu;
  367. } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
  368. if (exfat_ent_set(sb, last_clu, new_clu)) {
  369. ret = -EIO;
  370. goto free_cluster;
  371. }
  372. }
  373. p_chain->size++;
  374. last_clu = new_clu;
  375. if (p_chain->size == num_alloc) {
  376. sbi->clu_srch_ptr = hint_clu;
  377. sbi->used_clusters += num_alloc;
  378. mutex_unlock(&sbi->bitmap_lock);
  379. return 0;
  380. }
  381. hint_clu = new_clu + 1;
  382. if (hint_clu >= sbi->num_clusters) {
  383. hint_clu = EXFAT_FIRST_CLUSTER;
  384. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  385. if (exfat_chain_cont_cluster(sb, p_chain->dir,
  386. p_chain->size)) {
  387. ret = -EIO;
  388. goto free_cluster;
  389. }
  390. p_chain->flags = ALLOC_FAT_CHAIN;
  391. }
  392. }
  393. }
  394. free_cluster:
  395. __exfat_free_cluster(inode, p_chain);
  396. unlock:
  397. mutex_unlock(&sbi->bitmap_lock);
  398. return ret;
  399. }
  400. int exfat_count_num_clusters(struct super_block *sb,
  401. struct exfat_chain *p_chain, unsigned int *ret_count)
  402. {
  403. unsigned int i, count;
  404. unsigned int clu;
  405. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  406. struct buffer_head *bh = NULL;
  407. if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
  408. *ret_count = 0;
  409. return 0;
  410. }
  411. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  412. *ret_count = p_chain->size;
  413. return 0;
  414. }
  415. clu = p_chain->dir;
  416. count = 0;
  417. for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
  418. count++;
  419. if (exfat_ent_get(sb, clu, &clu, &bh))
  420. return -EIO;
  421. if (clu == EXFAT_EOF_CLUSTER)
  422. break;
  423. }
  424. brelse(bh);
  425. *ret_count = count;
  426. /*
  427. * since exfat_count_used_clusters() is not called, sbi->used_clusters
  428. * cannot be used here.
  429. */
  430. if (unlikely(i == sbi->num_clusters && clu != EXFAT_EOF_CLUSTER)) {
  431. exfat_fs_error(sb, "The cluster chain has a loop");
  432. return -EIO;
  433. }
  434. return 0;
  435. }