balloc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <linux/backing-dev.h>
  10. #include "exfat_raw.h"
  11. #include "exfat_fs.h"
  12. #if BITS_PER_LONG == 32
  13. #define __le_long __le32
  14. #define lel_to_cpu(A) le32_to_cpu(A)
  15. #define cpu_to_lel(A) cpu_to_le32(A)
  16. #elif BITS_PER_LONG == 64
  17. #define __le_long __le64
  18. #define lel_to_cpu(A) le64_to_cpu(A)
  19. #define cpu_to_lel(A) cpu_to_le64(A)
  20. #else
  21. #error "BITS_PER_LONG not 32 or 64"
  22. #endif
  23. /*
  24. * Allocation Bitmap Management Functions
  25. */
  26. static bool exfat_test_bitmap_range(struct super_block *sb, unsigned int clu,
  27. unsigned int count)
  28. {
  29. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  30. unsigned int start = clu;
  31. unsigned int end = clu + count;
  32. unsigned int ent_idx, i, b;
  33. unsigned int bit_offset, bits_to_check;
  34. __le_long *bitmap_le;
  35. unsigned long mask, word;
  36. if (!is_valid_cluster(sbi, start) || !is_valid_cluster(sbi, end - 1))
  37. return false;
  38. while (start < end) {
  39. ent_idx = CLUSTER_TO_BITMAP_ENT(start);
  40. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  41. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  42. bitmap_le = (__le_long *)sbi->vol_amap[i]->b_data;
  43. /* Calculate how many bits we can check in the current word */
  44. bit_offset = b % BITS_PER_LONG;
  45. bits_to_check = min(end - start,
  46. (unsigned int)(BITS_PER_LONG - bit_offset));
  47. /* Create a bitmask for the range of bits to check */
  48. if (bits_to_check >= BITS_PER_LONG)
  49. mask = ~0UL;
  50. else
  51. mask = ((1UL << bits_to_check) - 1) << bit_offset;
  52. word = lel_to_cpu(bitmap_le[b / BITS_PER_LONG]);
  53. /* Check if all bits in the mask are set */
  54. if ((word & mask) != mask)
  55. return false;
  56. start += bits_to_check;
  57. }
  58. return true;
  59. }
  60. static int exfat_allocate_bitmap(struct super_block *sb,
  61. struct exfat_dentry *ep)
  62. {
  63. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  64. struct blk_plug plug;
  65. long long map_size;
  66. unsigned int i, j, need_map_size;
  67. sector_t sector;
  68. unsigned int max_ra_count;
  69. sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
  70. map_size = le64_to_cpu(ep->dentry.bitmap.size);
  71. need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
  72. + 1;
  73. if (need_map_size != map_size) {
  74. exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
  75. need_map_size, map_size);
  76. /*
  77. * Only allowed when bogus allocation
  78. * bitmap size is large
  79. */
  80. if (need_map_size > map_size)
  81. return -EIO;
  82. }
  83. sbi->map_sectors = ((need_map_size - 1) >>
  84. (sb->s_blocksize_bits)) + 1;
  85. sbi->vol_amap = kvmalloc_objs(struct buffer_head *, sbi->map_sectors);
  86. if (!sbi->vol_amap)
  87. return -ENOMEM;
  88. sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
  89. max_ra_count = min(sb->s_bdi->ra_pages, sb->s_bdi->io_pages) <<
  90. (PAGE_SHIFT - sb->s_blocksize_bits);
  91. for (i = 0; i < sbi->map_sectors; i++) {
  92. /* Trigger the next readahead in advance. */
  93. if (max_ra_count && 0 == (i % max_ra_count)) {
  94. blk_start_plug(&plug);
  95. for (j = i; j < min(max_ra_count, sbi->map_sectors - i) + i; j++)
  96. sb_breadahead(sb, sector + j);
  97. blk_finish_plug(&plug);
  98. }
  99. sbi->vol_amap[i] = sb_bread(sb, sector + i);
  100. if (!sbi->vol_amap[i])
  101. goto err_out;
  102. }
  103. if (exfat_test_bitmap_range(sb, sbi->map_clu,
  104. EXFAT_B_TO_CLU_ROUND_UP(map_size, sbi)) == false)
  105. goto err_out;
  106. return 0;
  107. err_out:
  108. j = 0;
  109. /* release all buffers and free vol_amap */
  110. while (j < i)
  111. brelse(sbi->vol_amap[j++]);
  112. kvfree(sbi->vol_amap);
  113. sbi->vol_amap = NULL;
  114. return -EIO;
  115. }
  116. int exfat_load_bitmap(struct super_block *sb)
  117. {
  118. unsigned int i, type;
  119. struct exfat_chain clu;
  120. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  121. exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
  122. while (clu.dir != EXFAT_EOF_CLUSTER) {
  123. for (i = 0; i < sbi->dentries_per_clu; i++) {
  124. struct exfat_dentry *ep;
  125. struct buffer_head *bh;
  126. ep = exfat_get_dentry(sb, &clu, i, &bh);
  127. if (!ep)
  128. return -EIO;
  129. type = exfat_get_entry_type(ep);
  130. if (type == TYPE_BITMAP &&
  131. ep->dentry.bitmap.flags == 0x0) {
  132. int err;
  133. err = exfat_allocate_bitmap(sb, ep);
  134. brelse(bh);
  135. return err;
  136. }
  137. brelse(bh);
  138. if (type == TYPE_UNUSED)
  139. return -EINVAL;
  140. }
  141. if (exfat_get_next_cluster(sb, &clu.dir))
  142. return -EIO;
  143. }
  144. return -EINVAL;
  145. }
  146. void exfat_free_bitmap(struct exfat_sb_info *sbi)
  147. {
  148. int i;
  149. for (i = 0; i < sbi->map_sectors; i++)
  150. __brelse(sbi->vol_amap[i]);
  151. kvfree(sbi->vol_amap);
  152. }
  153. int exfat_set_bitmap(struct super_block *sb, unsigned int clu, bool sync)
  154. {
  155. int i, b;
  156. unsigned int ent_idx;
  157. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  158. if (!is_valid_cluster(sbi, clu))
  159. return -EINVAL;
  160. ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
  161. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  162. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  163. set_bit_le(b, sbi->vol_amap[i]->b_data);
  164. exfat_update_bh(sbi->vol_amap[i], sync);
  165. return 0;
  166. }
  167. int exfat_clear_bitmap(struct super_block *sb, unsigned int clu, bool sync)
  168. {
  169. int i, b;
  170. unsigned int ent_idx;
  171. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  172. if (!is_valid_cluster(sbi, clu))
  173. return -EIO;
  174. ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
  175. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  176. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  177. if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
  178. return -EIO;
  179. clear_bit_le(b, sbi->vol_amap[i]->b_data);
  180. exfat_update_bh(sbi->vol_amap[i], sync);
  181. return 0;
  182. }
  183. bool exfat_test_bitmap(struct super_block *sb, unsigned int clu)
  184. {
  185. int i, b;
  186. unsigned int ent_idx;
  187. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  188. if (!sbi->vol_amap)
  189. return true;
  190. if (!is_valid_cluster(sbi, clu))
  191. return false;
  192. ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
  193. i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  194. b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
  195. if (!test_bit_le(b, sbi->vol_amap[i]->b_data))
  196. return false;
  197. return true;
  198. }
  199. /*
  200. * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
  201. * the cluster heap.
  202. */
  203. unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
  204. {
  205. unsigned int i, map_i, map_b, ent_idx;
  206. unsigned int clu_base, clu_free;
  207. unsigned long clu_bits, clu_mask;
  208. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  209. __le_long bitval;
  210. WARN_ON(clu < EXFAT_FIRST_CLUSTER);
  211. ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP_ENT(clu), BITS_PER_LONG);
  212. clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx);
  213. clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
  214. map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
  215. map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
  216. for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
  217. i += BITS_PER_LONG) {
  218. bitval = *(__le_long *)(sbi->vol_amap[map_i]->b_data + map_b);
  219. if (clu_mask > 0) {
  220. bitval |= cpu_to_lel(clu_mask);
  221. clu_mask = 0;
  222. }
  223. if (lel_to_cpu(bitval) != ULONG_MAX) {
  224. clu_bits = lel_to_cpu(bitval);
  225. clu_free = clu_base + ffz(clu_bits);
  226. if (clu_free < sbi->num_clusters)
  227. return clu_free;
  228. }
  229. clu_base += BITS_PER_LONG;
  230. map_b += sizeof(long);
  231. if (map_b >= sb->s_blocksize ||
  232. clu_base >= sbi->num_clusters) {
  233. if (++map_i >= sbi->map_sectors) {
  234. clu_base = EXFAT_FIRST_CLUSTER;
  235. map_i = 0;
  236. }
  237. map_b = 0;
  238. }
  239. }
  240. return EXFAT_EOF_CLUSTER;
  241. }
  242. int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
  243. {
  244. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  245. unsigned int count = 0;
  246. unsigned int i, map_i = 0, map_b = 0;
  247. unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
  248. unsigned int last_mask = total_clus & (BITS_PER_LONG - 1);
  249. unsigned long *bitmap, clu_bits;
  250. total_clus &= ~last_mask;
  251. for (i = 0; i < total_clus; i += BITS_PER_LONG) {
  252. bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
  253. count += hweight_long(*bitmap);
  254. map_b += sizeof(long);
  255. if (map_b >= (unsigned int)sb->s_blocksize) {
  256. map_i++;
  257. map_b = 0;
  258. }
  259. }
  260. if (last_mask) {
  261. bitmap = (void *)(sbi->vol_amap[map_i]->b_data + map_b);
  262. clu_bits = lel_to_cpu(*(__le_long *)bitmap);
  263. count += hweight_long(clu_bits & BITMAP_LAST_WORD_MASK(last_mask));
  264. }
  265. *ret_count = count;
  266. return 0;
  267. }
  268. int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
  269. {
  270. unsigned int trim_begin, trim_end, count, next_free_clu;
  271. u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
  272. struct super_block *sb = inode->i_sb;
  273. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  274. int err = 0;
  275. clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
  276. EXFAT_FIRST_CLUSTER);
  277. clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
  278. trim_minlen = range->minlen >> sbi->cluster_size_bits;
  279. if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
  280. return -EINVAL;
  281. if (clu_end >= sbi->num_clusters)
  282. clu_end = sbi->num_clusters - 1;
  283. mutex_lock(&sbi->bitmap_lock);
  284. trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
  285. if (trim_begin == EXFAT_EOF_CLUSTER)
  286. goto unlock;
  287. next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
  288. if (next_free_clu == EXFAT_EOF_CLUSTER)
  289. goto unlock;
  290. do {
  291. if (next_free_clu == trim_end + 1) {
  292. /* extend trim range for continuous free cluster */
  293. trim_end++;
  294. } else {
  295. /* trim current range if it's larger than trim_minlen */
  296. count = trim_end - trim_begin + 1;
  297. if (count >= trim_minlen) {
  298. err = sb_issue_discard(sb,
  299. exfat_cluster_to_sector(sbi, trim_begin),
  300. count * sbi->sect_per_clus, GFP_NOFS, 0);
  301. if (err)
  302. goto unlock;
  303. trimmed_total += count;
  304. }
  305. /* set next start point of the free hole */
  306. trim_begin = trim_end = next_free_clu;
  307. }
  308. if (next_free_clu >= clu_end)
  309. break;
  310. if (fatal_signal_pending(current)) {
  311. err = -ERESTARTSYS;
  312. goto unlock;
  313. }
  314. next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
  315. } while (next_free_clu != EXFAT_EOF_CLUSTER &&
  316. next_free_clu > trim_end);
  317. /* try to trim remainder */
  318. count = trim_end - trim_begin + 1;
  319. if (count >= trim_minlen) {
  320. err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
  321. count * sbi->sect_per_clus, GFP_NOFS, 0);
  322. if (err)
  323. goto unlock;
  324. trimmed_total += count;
  325. }
  326. unlock:
  327. mutex_unlock(&sbi->bitmap_lock);
  328. range->len = trimmed_total << sbi->cluster_size_bits;
  329. return err;
  330. }