fatent.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 <asm/unaligned.h>
  7. #include <linux/buffer_head.h>
  8. #include "exfat_raw.h"
  9. #include "exfat_fs.h"
  10. static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
  11. struct buffer_head *bh)
  12. {
  13. struct buffer_head *c_bh;
  14. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  15. sector_t sec2;
  16. int err = 0;
  17. if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
  18. sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
  19. c_bh = sb_getblk(sb, sec2);
  20. if (!c_bh)
  21. return -ENOMEM;
  22. memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
  23. set_buffer_uptodate(c_bh);
  24. mark_buffer_dirty(c_bh);
  25. if (sb->s_flags & SB_SYNCHRONOUS)
  26. err = sync_dirty_buffer(c_bh);
  27. brelse(c_bh);
  28. }
  29. return err;
  30. }
  31. static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
  32. unsigned int *content)
  33. {
  34. unsigned int off;
  35. sector_t sec;
  36. struct buffer_head *bh;
  37. sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
  38. off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
  39. bh = sb_bread(sb, sec);
  40. if (!bh)
  41. return -EIO;
  42. *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
  43. /* remap reserved clusters to simplify code */
  44. if (*content > EXFAT_BAD_CLUSTER)
  45. *content = EXFAT_EOF_CLUSTER;
  46. brelse(bh);
  47. return 0;
  48. }
  49. int exfat_ent_set(struct super_block *sb, unsigned int loc,
  50. unsigned int content)
  51. {
  52. unsigned int off;
  53. sector_t sec;
  54. __le32 *fat_entry;
  55. struct buffer_head *bh;
  56. sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
  57. off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
  58. bh = sb_bread(sb, sec);
  59. if (!bh)
  60. return -EIO;
  61. fat_entry = (__le32 *)&(bh->b_data[off]);
  62. *fat_entry = cpu_to_le32(content);
  63. exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
  64. exfat_mirror_bh(sb, sec, bh);
  65. brelse(bh);
  66. return 0;
  67. }
  68. static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
  69. unsigned int clus)
  70. {
  71. if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus)
  72. return false;
  73. return true;
  74. }
  75. int exfat_ent_get(struct super_block *sb, unsigned int loc,
  76. unsigned int *content)
  77. {
  78. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  79. int err;
  80. if (!is_valid_cluster(sbi, loc)) {
  81. exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
  82. loc);
  83. return -EIO;
  84. }
  85. err = __exfat_ent_get(sb, loc, content);
  86. if (err) {
  87. exfat_fs_error(sb,
  88. "failed to access to FAT (entry 0x%08x, err:%d)",
  89. loc, err);
  90. return err;
  91. }
  92. if (*content == EXFAT_FREE_CLUSTER) {
  93. exfat_fs_error(sb,
  94. "invalid access to FAT free cluster (entry 0x%08x)",
  95. loc);
  96. return -EIO;
  97. }
  98. if (*content == EXFAT_BAD_CLUSTER) {
  99. exfat_fs_error(sb,
  100. "invalid access to FAT bad cluster (entry 0x%08x)",
  101. loc);
  102. return -EIO;
  103. }
  104. if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
  105. exfat_fs_error(sb,
  106. "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
  107. loc, *content);
  108. return -EIO;
  109. }
  110. return 0;
  111. }
  112. int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
  113. unsigned int len)
  114. {
  115. if (!len)
  116. return 0;
  117. while (len > 1) {
  118. if (exfat_ent_set(sb, chain, chain + 1))
  119. return -EIO;
  120. chain++;
  121. len--;
  122. }
  123. if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
  124. return -EIO;
  125. return 0;
  126. }
  127. int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
  128. {
  129. unsigned int num_clusters = 0;
  130. unsigned int clu;
  131. struct super_block *sb = inode->i_sb;
  132. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  133. /* invalid cluster number */
  134. if (p_chain->dir == EXFAT_FREE_CLUSTER ||
  135. p_chain->dir == EXFAT_EOF_CLUSTER ||
  136. p_chain->dir < EXFAT_FIRST_CLUSTER)
  137. return 0;
  138. /* no cluster to truncate */
  139. if (p_chain->size == 0)
  140. return 0;
  141. /* check cluster validation */
  142. if (!is_valid_cluster(sbi, p_chain->dir)) {
  143. exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
  144. return -EIO;
  145. }
  146. clu = p_chain->dir;
  147. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  148. do {
  149. exfat_clear_bitmap(inode, clu);
  150. clu++;
  151. num_clusters++;
  152. } while (num_clusters < p_chain->size);
  153. } else {
  154. do {
  155. exfat_clear_bitmap(inode, clu);
  156. if (exfat_get_next_cluster(sb, &clu))
  157. goto dec_used_clus;
  158. num_clusters++;
  159. } while (clu != EXFAT_EOF_CLUSTER);
  160. }
  161. dec_used_clus:
  162. sbi->used_clusters -= num_clusters;
  163. return 0;
  164. }
  165. int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
  166. unsigned int *ret_clu)
  167. {
  168. unsigned int clu, next;
  169. unsigned int count = 0;
  170. next = p_chain->dir;
  171. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  172. *ret_clu = next + p_chain->size - 1;
  173. return 0;
  174. }
  175. do {
  176. count++;
  177. clu = next;
  178. if (exfat_ent_get(sb, clu, &next))
  179. return -EIO;
  180. } while (next != EXFAT_EOF_CLUSTER);
  181. if (p_chain->size != count) {
  182. exfat_fs_error(sb,
  183. "bogus directory size (clus : ondisk(%d) != counted(%d))",
  184. p_chain->size, count);
  185. return -EIO;
  186. }
  187. *ret_clu = clu;
  188. return 0;
  189. }
  190. int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
  191. {
  192. struct super_block *sb = dir->i_sb;
  193. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  194. struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  195. int nr_bhs = MAX_BUF_PER_PAGE;
  196. sector_t blknr, last_blknr;
  197. int err, i, n;
  198. blknr = exfat_cluster_to_sector(sbi, clu);
  199. last_blknr = blknr + sbi->sect_per_clus;
  200. if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
  201. exfat_fs_error_ratelimit(sb,
  202. "%s: out of range(sect:%llu len:%u)",
  203. __func__, (unsigned long long)blknr,
  204. sbi->sect_per_clus);
  205. return -EIO;
  206. }
  207. /* Zeroing the unused blocks on this cluster */
  208. while (blknr < last_blknr) {
  209. for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
  210. bhs[n] = sb_getblk(sb, blknr);
  211. if (!bhs[n]) {
  212. err = -ENOMEM;
  213. goto release_bhs;
  214. }
  215. memset(bhs[n]->b_data, 0, sb->s_blocksize);
  216. }
  217. err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
  218. if (err)
  219. goto release_bhs;
  220. for (i = 0; i < n; i++)
  221. brelse(bhs[i]);
  222. }
  223. return 0;
  224. release_bhs:
  225. exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
  226. for (i = 0; i < n; i++)
  227. bforget(bhs[i]);
  228. return err;
  229. }
  230. int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
  231. struct exfat_chain *p_chain)
  232. {
  233. int ret = -ENOSPC;
  234. unsigned int num_clusters = 0, total_cnt;
  235. unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
  236. struct super_block *sb = inode->i_sb;
  237. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  238. total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
  239. if (unlikely(total_cnt < sbi->used_clusters)) {
  240. exfat_fs_error_ratelimit(sb,
  241. "%s: invalid used clusters(t:%u,u:%u)\n",
  242. __func__, total_cnt, sbi->used_clusters);
  243. return -EIO;
  244. }
  245. if (num_alloc > total_cnt - sbi->used_clusters)
  246. return -ENOSPC;
  247. hint_clu = p_chain->dir;
  248. /* find new cluster */
  249. if (hint_clu == EXFAT_EOF_CLUSTER) {
  250. if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
  251. exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
  252. sbi->clu_srch_ptr);
  253. sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
  254. }
  255. hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
  256. if (hint_clu == EXFAT_EOF_CLUSTER)
  257. return -ENOSPC;
  258. }
  259. /* check cluster validation */
  260. if (!is_valid_cluster(sbi, hint_clu)) {
  261. exfat_err(sb, "hint_cluster is invalid (%u)",
  262. hint_clu);
  263. hint_clu = EXFAT_FIRST_CLUSTER;
  264. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  265. if (exfat_chain_cont_cluster(sb, p_chain->dir,
  266. num_clusters))
  267. return -EIO;
  268. p_chain->flags = ALLOC_FAT_CHAIN;
  269. }
  270. }
  271. p_chain->dir = EXFAT_EOF_CLUSTER;
  272. while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
  273. EXFAT_EOF_CLUSTER) {
  274. if (new_clu != hint_clu &&
  275. p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  276. if (exfat_chain_cont_cluster(sb, p_chain->dir,
  277. num_clusters)) {
  278. ret = -EIO;
  279. goto free_cluster;
  280. }
  281. p_chain->flags = ALLOC_FAT_CHAIN;
  282. }
  283. /* update allocation bitmap */
  284. if (exfat_set_bitmap(inode, new_clu)) {
  285. ret = -EIO;
  286. goto free_cluster;
  287. }
  288. num_clusters++;
  289. /* update FAT table */
  290. if (p_chain->flags == ALLOC_FAT_CHAIN) {
  291. if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
  292. ret = -EIO;
  293. goto free_cluster;
  294. }
  295. }
  296. if (p_chain->dir == EXFAT_EOF_CLUSTER) {
  297. p_chain->dir = new_clu;
  298. } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
  299. if (exfat_ent_set(sb, last_clu, new_clu)) {
  300. ret = -EIO;
  301. goto free_cluster;
  302. }
  303. }
  304. last_clu = new_clu;
  305. if (--num_alloc == 0) {
  306. sbi->clu_srch_ptr = hint_clu;
  307. sbi->used_clusters += num_clusters;
  308. p_chain->size += num_clusters;
  309. return 0;
  310. }
  311. hint_clu = new_clu + 1;
  312. if (hint_clu >= sbi->num_clusters) {
  313. hint_clu = EXFAT_FIRST_CLUSTER;
  314. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  315. if (exfat_chain_cont_cluster(sb, p_chain->dir,
  316. num_clusters)) {
  317. ret = -EIO;
  318. goto free_cluster;
  319. }
  320. p_chain->flags = ALLOC_FAT_CHAIN;
  321. }
  322. }
  323. }
  324. free_cluster:
  325. if (num_clusters)
  326. exfat_free_cluster(inode, p_chain);
  327. return ret;
  328. }
  329. int exfat_count_num_clusters(struct super_block *sb,
  330. struct exfat_chain *p_chain, unsigned int *ret_count)
  331. {
  332. unsigned int i, count;
  333. unsigned int clu;
  334. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  335. if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
  336. *ret_count = 0;
  337. return 0;
  338. }
  339. if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
  340. *ret_count = p_chain->size;
  341. return 0;
  342. }
  343. clu = p_chain->dir;
  344. count = 0;
  345. for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
  346. count++;
  347. if (exfat_ent_get(sb, clu, &clu))
  348. return -EIO;
  349. if (clu == EXFAT_EOF_CLUSTER)
  350. break;
  351. }
  352. *ret_count = count;
  353. return 0;
  354. }