balloc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/slab.h>
  7. #include <linux/bitmap.h>
  8. #include <linux/buffer_head.h>
  9. #include "exfat_raw.h"
  10. #include "exfat_fs.h"
  11. #if BITS_PER_LONG == 32
  12. #define __le_long __le32
  13. #define lel_to_cpu(A) le32_to_cpu(A)
  14. #define cpu_to_lel(A) cpu_to_le32(A)
  15. #elif BITS_PER_LONG == 64
  16. #define __le_long __le64
  17. #define lel_to_cpu(A) le64_to_cpu(A)
  18. #define cpu_to_lel(A) cpu_to_le64(A)
  19. #else
  20. #error "BITS_PER_LONG not 32 or 64"
  21. #endif
  22. /*
  23. * Allocation Bitmap Management Functions
  24. */
  25. static int exfat_allocate_bitmap(struct super_block *sb,
  26. struct exfat_dentry *ep)
  27. {
  28. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  29. long long map_size;
  30. unsigned int i, need_map_size;
  31. sector_t sector;
  32. sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
  33. map_size = le64_to_cpu(ep->dentry.bitmap.size);
  34. need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
  35. + 1;
  36. if (need_map_size != map_size) {
  37. exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
  38. need_map_size, map_size);
  39. /*
  40. * Only allowed when bogus allocation
  41. * bitmap size is large
  42. */
  43. if (need_map_size > map_size)
  44. return -EIO;
  45. }
  46. sbi->map_sectors = ((need_map_size - 1) >>
  47. (sb->s_blocksize_bits)) + 1;
  48. sbi->vol_amap = kvmalloc_array(sbi->map_sectors,
  49. sizeof(struct buffer_head *), GFP_KERNEL);
  50. if (!sbi->vol_amap)
  51. return -ENOMEM;
  52. sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
  53. for (i = 0; i < sbi->map_sectors; i++) {
  54. sbi->vol_amap[i] = sb_bread(sb, sector + i);
  55. if (!sbi->vol_amap[i]) {
  56. /* release all buffers and free vol_amap */
  57. int j = 0;
  58. while (j < i)
  59. brelse(sbi->vol_amap[j++]);
  60. kvfree(sbi->vol_amap);
  61. sbi->vol_amap = NULL;
  62. return -EIO;
  63. }
  64. }
  65. return 0;
  66. }
  67. int exfat_load_bitmap(struct super_block *sb)
  68. {
  69. unsigned int i, type;
  70. struct exfat_chain clu;
  71. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  72. exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
  73. while (clu.dir != EXFAT_EOF_CLUSTER) {
  74. for (i = 0; i < sbi->dentries_per_clu; i++) {
  75. struct exfat_dentry *ep;
  76. struct buffer_head *bh;
  77. ep = exfat_get_dentry(sb, &clu, i, &bh);
  78. if (!ep)
  79. return -EIO;
  80. type = exfat_get_entry_type(ep);
  81. if (type == TYPE_BITMAP &&
  82. ep->dentry.bitmap.flags == 0x0) {
  83. int err;
  84. err = exfat_allocate_bitmap(sb, ep);
  85. brelse(bh);
  86. return err;
  87. }
  88. brelse(bh);
  89. if (type == TYPE_UNUSED)
  90. return -EINVAL;
  91. }
  92. if (exfat_get_next_cluster(sb, &clu.dir))
  93. return -EIO;
  94. }
  95. return -EINVAL;
  96. }
  97. void exfat_free_bitmap(struct exfat_sb_info *sbi)
  98. {
  99. int i;
  100. for (i = 0; i < sbi->map_sectors; i++)
  101. __brelse(sbi->vol_amap[i]);
  102. kvfree(sbi->vol_amap);
  103. }
  104. int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
  105. {
  106. int i, b;
  107. unsigned int ent_idx;
  108. struct super_block *sb = inode->i_sb;
  109. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  110. if (!is_valid_cluster(sbi, clu))
  111. return -EINVAL;
  112. ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
  113. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  114. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  115. set_bit_le(b, sbi->vol_amap[i]->b_data);
  116. exfat_update_bh(sbi->vol_amap[i], sync);
  117. return 0;
  118. }
  119. int exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
  120. {
  121. int i, b;
  122. unsigned int ent_idx;
  123. struct super_block *sb = inode->i_sb;
  124. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  125. struct exfat_mount_options *opts = &sbi->options;
  126. if (!is_valid_cluster(sbi, clu))
  127. return -EIO;
  128. ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
  129. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  130. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  131. if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
  132. return -EIO;
  133. clear_bit_le(b, sbi->vol_amap[i]->b_data);
  134. exfat_update_bh(sbi->vol_amap[i], sync);
  135. if (opts->discard) {
  136. int ret_discard;
  137. ret_discard = sb_issue_discard(sb,
  138. exfat_cluster_to_sector(sbi, clu),
  139. (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0);
  140. if (ret_discard == -EOPNOTSUPP) {
  141. exfat_err(sb, "discard not supported by device, disabling");
  142. opts->discard = 0;
  143. }
  144. }
  145. return 0;
  146. }
  147. /*
  148. * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
  149. * the cluster heap.
  150. */
  151. unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
  152. {
  153. unsigned int i, map_i, map_b, ent_idx;
  154. unsigned int clu_base, clu_free;
  155. unsigned long clu_bits, clu_mask;
  156. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  157. __le_long bitval;
  158. WARN_ON(clu < EXFAT_FIRST_CLUSTER);
  159. ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP_ENT(clu), BITS_PER_LONG);
  160. clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx);
  161. clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
  162. map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  163. map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
  164. for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
  165. i += BITS_PER_LONG) {
  166. bitval = *(__le_long *)(sbi->vol_amap[map_i]->b_data + map_b);
  167. if (clu_mask > 0) {
  168. bitval |= cpu_to_lel(clu_mask);
  169. clu_mask = 0;
  170. }
  171. if (lel_to_cpu(bitval) != ULONG_MAX) {
  172. clu_bits = lel_to_cpu(bitval);
  173. clu_free = clu_base + ffz(clu_bits);
  174. if (clu_free < sbi->num_clusters)
  175. return clu_free;
  176. }
  177. clu_base += BITS_PER_LONG;
  178. map_b += sizeof(long);
  179. if (map_b >= sb->s_blocksize ||
  180. clu_base >= sbi->num_clusters) {
  181. if (++map_i >= sbi->map_sectors) {
  182. clu_base = EXFAT_FIRST_CLUSTER;
  183. map_i = 0;
  184. }
  185. map_b = 0;
  186. }
  187. }
  188. return EXFAT_EOF_CLUSTER;
  189. }
  190. int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
  191. {
  192. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  193. unsigned int count = 0;
  194. unsigned int i, map_i = 0, map_b = 0;
  195. unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
  196. unsigned int last_mask = total_clus & (BITS_PER_LONG - 1);
  197. unsigned long *bitmap, clu_bits;
  198. total_clus &= ~last_mask;
  199. for (i = 0; i < total_clus; i += BITS_PER_LONG) {
  200. bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
  201. count += hweight_long(*bitmap);
  202. map_b += sizeof(long);
  203. if (map_b >= (unsigned int)sb->s_blocksize) {
  204. map_i++;
  205. map_b = 0;
  206. }
  207. }
  208. if (last_mask) {
  209. bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
  210. clu_bits = lel_to_cpu(*(__le_long *)bitmap);
  211. count += hweight_long(clu_bits & BITMAP_LAST_WORD_MASK(last_mask));
  212. }
  213. *ret_count = count;
  214. return 0;
  215. }
  216. int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
  217. {
  218. unsigned int trim_begin, trim_end, count, next_free_clu;
  219. u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
  220. struct super_block *sb = inode->i_sb;
  221. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  222. int err = 0;
  223. clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
  224. EXFAT_FIRST_CLUSTER);
  225. clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
  226. trim_minlen = range->minlen >> sbi->cluster_size_bits;
  227. if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
  228. return -EINVAL;
  229. if (clu_end >= sbi->num_clusters)
  230. clu_end = sbi->num_clusters - 1;
  231. mutex_lock(&sbi->bitmap_lock);
  232. trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
  233. if (trim_begin == EXFAT_EOF_CLUSTER)
  234. goto unlock;
  235. next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
  236. if (next_free_clu == EXFAT_EOF_CLUSTER)
  237. goto unlock;
  238. do {
  239. if (next_free_clu == trim_end + 1) {
  240. /* extend trim range for continuous free cluster */
  241. trim_end++;
  242. } else {
  243. /* trim current range if it's larger than trim_minlen */
  244. count = trim_end - trim_begin + 1;
  245. if (count >= trim_minlen) {
  246. err = sb_issue_discard(sb,
  247. exfat_cluster_to_sector(sbi, trim_begin),
  248. count * sbi->sect_per_clus, GFP_NOFS, 0);
  249. if (err)
  250. goto unlock;
  251. trimmed_total += count;
  252. }
  253. /* set next start point of the free hole */
  254. trim_begin = trim_end = next_free_clu;
  255. }
  256. if (next_free_clu >= clu_end)
  257. break;
  258. if (fatal_signal_pending(current)) {
  259. err = -ERESTARTSYS;
  260. goto unlock;
  261. }
  262. next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
  263. } while (next_free_clu != EXFAT_EOF_CLUSTER &&
  264. next_free_clu > trim_end);
  265. /* try to trim remainder */
  266. count = trim_end - trim_begin + 1;
  267. if (count >= trim_minlen) {
  268. err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
  269. count * sbi->sect_per_clus, GFP_NOFS, 0);
  270. if (err)
  271. goto unlock;
  272. trimmed_total += count;
  273. }
  274. unlock:
  275. mutex_unlock(&sbi->bitmap_lock);
  276. range->len = trimmed_total << sbi->cluster_size_bits;
  277. return err;
  278. }