cluster.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. cluster.c (03.09.09)
  3. exFAT file system implementation library.
  4. Copyright (C) 2010-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "exfat.h"
  17. #include <errno.h>
  18. #include <string.h>
  19. /*
  20. * Sector to absolute offset.
  21. */
  22. static off_t s2o(const struct exfat* ef, off_t sector)
  23. {
  24. return sector << ef->sb->sector_bits;
  25. }
  26. /*
  27. * Cluster to sector.
  28. */
  29. static off_t c2s(const struct exfat* ef, cluster_t cluster)
  30. {
  31. if (cluster < EXFAT_FIRST_DATA_CLUSTER)
  32. exfat_bug("invalid cluster number %u", cluster);
  33. return le32_to_cpu(ef->sb->cluster_sector_start) +
  34. ((off_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits);
  35. }
  36. /*
  37. * Cluster to absolute offset.
  38. */
  39. off_t exfat_c2o(const struct exfat* ef, cluster_t cluster)
  40. {
  41. return s2o(ef, c2s(ef, cluster));
  42. }
  43. /*
  44. * Sector to cluster.
  45. */
  46. static cluster_t s2c(const struct exfat* ef, off_t sector)
  47. {
  48. return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >>
  49. ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER;
  50. }
  51. /*
  52. * Size in bytes to size in clusters (rounded upwards).
  53. */
  54. static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes)
  55. {
  56. uint64_t cluster_size = CLUSTER_SIZE(*ef->sb);
  57. return (bytes + cluster_size - 1) / cluster_size;
  58. }
  59. cluster_t exfat_next_cluster(const struct exfat* ef,
  60. const struct exfat_node* node, cluster_t cluster)
  61. {
  62. le32_t next;
  63. off_t fat_offset;
  64. if (cluster < EXFAT_FIRST_DATA_CLUSTER)
  65. exfat_bug("bad cluster 0x%x", cluster);
  66. if (IS_CONTIGUOUS(*node))
  67. return cluster + 1;
  68. fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
  69. + cluster * sizeof(cluster_t);
  70. exfat_pread(ef->dev, &next, sizeof(next), fat_offset);
  71. return le32_to_cpu(next);
  72. }
  73. cluster_t exfat_advance_cluster(const struct exfat* ef,
  74. struct exfat_node* node, uint32_t count)
  75. {
  76. uint32_t i;
  77. if (node->fptr_index > count)
  78. {
  79. node->fptr_index = 0;
  80. node->fptr_cluster = node->start_cluster;
  81. }
  82. for (i = node->fptr_index; i < count; i++)
  83. {
  84. node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
  85. if (CLUSTER_INVALID(node->fptr_cluster))
  86. break; /* the caller should handle this and print appropriate
  87. error message */
  88. }
  89. node->fptr_index = count;
  90. return node->fptr_cluster;
  91. }
  92. static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
  93. {
  94. const size_t start_index = start / 8;
  95. const size_t end_index = DIV_ROUND_UP(end, 8);
  96. size_t i;
  97. size_t c;
  98. for (i = start_index; i < end_index; i++)
  99. {
  100. if (bitmap[i] == 0xff)
  101. continue;
  102. for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
  103. if (BMAP_GET(bitmap, c) == 0)
  104. {
  105. BMAP_SET(bitmap, c);
  106. return c + EXFAT_FIRST_DATA_CLUSTER;
  107. }
  108. }
  109. return EXFAT_CLUSTER_END;
  110. }
  111. void exfat_flush_cmap(struct exfat* ef)
  112. {
  113. exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
  114. exfat_c2o(ef, ef->cmap.start_cluster));
  115. ef->cmap.dirty = false;
  116. }
  117. static void set_next_cluster(const struct exfat* ef, int contiguous,
  118. cluster_t current, cluster_t next)
  119. {
  120. off_t fat_offset;
  121. le32_t next_le32;
  122. if (contiguous)
  123. return;
  124. fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
  125. + current * sizeof(cluster_t);
  126. next_le32 = cpu_to_le32(next);
  127. exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset);
  128. }
  129. static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint)
  130. {
  131. cluster_t cluster;
  132. hint -= EXFAT_FIRST_DATA_CLUSTER;
  133. if (hint >= ef->cmap.chunk_size)
  134. hint = 0;
  135. cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size);
  136. if (cluster == EXFAT_CLUSTER_END)
  137. cluster = find_bit_and_set(ef->cmap.chunk, 0, hint);
  138. if (cluster == EXFAT_CLUSTER_END)
  139. {
  140. exfat_error("no free space left");
  141. return EXFAT_CLUSTER_END;
  142. }
  143. ef->cmap.dirty = true;
  144. return cluster;
  145. }
  146. static void free_cluster(struct exfat* ef, cluster_t cluster)
  147. {
  148. if (CLUSTER_INVALID(cluster))
  149. exfat_bug("freeing invalid cluster 0x%x", cluster);
  150. if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
  151. exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
  152. ef->cmap.size);
  153. BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
  154. ef->cmap.dirty = true;
  155. }
  156. static void make_noncontiguous(const struct exfat* ef, cluster_t first,
  157. cluster_t last)
  158. {
  159. cluster_t c;
  160. for (c = first; c < last; c++)
  161. set_next_cluster(ef, 0, c, c + 1);
  162. }
  163. static int shrink_file(struct exfat* ef, struct exfat_node* node,
  164. uint32_t current, uint32_t difference);
  165. static int grow_file(struct exfat* ef, struct exfat_node* node,
  166. uint32_t current, uint32_t difference)
  167. {
  168. cluster_t previous;
  169. cluster_t next;
  170. uint32_t allocated = 0;
  171. if (difference == 0)
  172. exfat_bug("zero clusters count passed");
  173. if (node->start_cluster != EXFAT_CLUSTER_FREE)
  174. {
  175. /* get the last cluster of the file */
  176. previous = exfat_advance_cluster(ef, node, current - 1);
  177. if (CLUSTER_INVALID(previous))
  178. {
  179. exfat_error("invalid cluster 0x%x while growing", previous);
  180. return -EIO;
  181. }
  182. }
  183. else
  184. {
  185. if (node->fptr_index != 0)
  186. exfat_bug("non-zero pointer index (%u)", node->fptr_index);
  187. /* file does not have clusters (i.e. is empty), allocate
  188. the first one for it */
  189. previous = allocate_cluster(ef, 0);
  190. if (CLUSTER_INVALID(previous))
  191. return -ENOSPC;
  192. node->fptr_cluster = node->start_cluster = previous;
  193. allocated = 1;
  194. /* file consists of only one cluster, so it's contiguous */
  195. node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
  196. }
  197. while (allocated < difference)
  198. {
  199. next = allocate_cluster(ef, previous + 1);
  200. if (CLUSTER_INVALID(next))
  201. {
  202. if (allocated != 0)
  203. shrink_file(ef, node, current + allocated, allocated);
  204. return -ENOSPC;
  205. }
  206. if (next != previous - 1 && IS_CONTIGUOUS(*node))
  207. {
  208. /* it's a pity, but we are not able to keep the file contiguous
  209. anymore */
  210. make_noncontiguous(ef, node->start_cluster, previous);
  211. node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
  212. node->flags |= EXFAT_ATTRIB_DIRTY;
  213. }
  214. set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next);
  215. previous = next;
  216. allocated++;
  217. }
  218. set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END);
  219. return 0;
  220. }
  221. static int shrink_file(struct exfat* ef, struct exfat_node* node,
  222. uint32_t current, uint32_t difference)
  223. {
  224. cluster_t previous;
  225. cluster_t next;
  226. if (difference == 0)
  227. exfat_bug("zero difference passed");
  228. if (node->start_cluster == EXFAT_CLUSTER_FREE)
  229. exfat_bug("unable to shrink empty file (%u clusters)", current);
  230. if (current < difference)
  231. exfat_bug("file underflow (%u < %u)", current, difference);
  232. /* crop the file */
  233. if (current > difference)
  234. {
  235. cluster_t last = exfat_advance_cluster(ef, node,
  236. current - difference - 1);
  237. if (CLUSTER_INVALID(last))
  238. {
  239. exfat_error("invalid cluster 0x%x while shrinking", last);
  240. return -EIO;
  241. }
  242. previous = exfat_next_cluster(ef, node, last);
  243. set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END);
  244. }
  245. else
  246. {
  247. previous = node->start_cluster;
  248. node->start_cluster = EXFAT_CLUSTER_FREE;
  249. }
  250. node->fptr_index = 0;
  251. node->fptr_cluster = node->start_cluster;
  252. /* free remaining clusters */
  253. while (difference--)
  254. {
  255. if (CLUSTER_INVALID(previous))
  256. {
  257. exfat_error("invalid cluster 0x%x while freeing after shrink",
  258. previous);
  259. return -EIO;
  260. }
  261. next = exfat_next_cluster(ef, node, previous);
  262. set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
  263. EXFAT_CLUSTER_FREE);
  264. free_cluster(ef, previous);
  265. previous = next;
  266. }
  267. return 0;
  268. }
  269. static void erase_raw(struct exfat* ef, size_t size, off_t offset)
  270. {
  271. exfat_pwrite(ef->dev, ef->zero_cluster, size, offset);
  272. }
  273. static int erase_range(struct exfat* ef, struct exfat_node* node,
  274. uint64_t begin, uint64_t end)
  275. {
  276. uint64_t cluster_boundary;
  277. cluster_t cluster;
  278. if (begin >= end)
  279. return 0;
  280. cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1;
  281. cluster = exfat_advance_cluster(ef, node,
  282. begin / CLUSTER_SIZE(*ef->sb));
  283. if (CLUSTER_INVALID(cluster))
  284. {
  285. exfat_error("invalid cluster 0x%x while erasing", cluster);
  286. return -EIO;
  287. }
  288. /* erase from the beginning to the closest cluster boundary */
  289. erase_raw(ef, MIN(cluster_boundary, end) - begin,
  290. exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb));
  291. /* erase whole clusters */
  292. while (cluster_boundary < end)
  293. {
  294. cluster = exfat_next_cluster(ef, node, cluster);
  295. /* the cluster cannot be invalid because we have just allocated it */
  296. if (CLUSTER_INVALID(cluster))
  297. exfat_bug("invalid cluster 0x%x after allocation", cluster);
  298. erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
  299. cluster_boundary += CLUSTER_SIZE(*ef->sb);
  300. }
  301. return 0;
  302. }
  303. int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size)
  304. {
  305. uint32_t c1 = bytes2clusters(ef, node->size);
  306. uint32_t c2 = bytes2clusters(ef, size);
  307. int rc = 0;
  308. if (node->references == 0 && node->parent)
  309. exfat_bug("no references, node changes can be lost");
  310. if (node->size == size)
  311. return 0;
  312. if (c1 < c2)
  313. rc = grow_file(ef, node, c1, c2 - c1);
  314. else if (c1 > c2)
  315. rc = shrink_file(ef, node, c1, c1 - c2);
  316. if (rc != 0)
  317. return rc;
  318. rc = erase_range(ef, node, node->size, size);
  319. if (rc != 0)
  320. return rc;
  321. exfat_update_mtime(node);
  322. node->size = size;
  323. node->flags |= EXFAT_ATTRIB_DIRTY;
  324. return 0;
  325. }
  326. uint32_t exfat_count_free_clusters(const struct exfat* ef)
  327. {
  328. uint32_t free_clusters = 0;
  329. uint32_t i;
  330. for (i = 0; i < ef->cmap.size; i++)
  331. if (BMAP_GET(ef->cmap.chunk, i) == 0)
  332. free_clusters++;
  333. return free_clusters;
  334. }
  335. static int find_used_clusters(const struct exfat* ef,
  336. cluster_t* a, cluster_t* b)
  337. {
  338. const cluster_t end = le32_to_cpu(ef->sb->cluster_count);
  339. /* find first used cluster */
  340. for (*a = *b + 1; *a < end; (*a)++)
  341. if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER))
  342. break;
  343. if (*a >= end)
  344. return 1;
  345. /* find last contiguous used cluster */
  346. for (*b = *a; *b < end; (*b)++)
  347. if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0)
  348. {
  349. (*b)--;
  350. break;
  351. }
  352. return 0;
  353. }
  354. int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b)
  355. {
  356. cluster_t ca, cb;
  357. if (*a == 0 && *b == 0)
  358. ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1;
  359. else
  360. {
  361. ca = s2c(ef, *a);
  362. cb = s2c(ef, *b);
  363. }
  364. if (find_used_clusters(ef, &ca, &cb) != 0)
  365. return 1;
  366. if (*a != 0 || *b != 0)
  367. *a = c2s(ef, ca);
  368. *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb);
  369. return 0;
  370. }