amap_smart.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  1. /*
  2. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  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. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /************************************************************************/
  18. /* */
  19. /* PROJECT : exFAT & FAT12/16/32 File System */
  20. /* FILE : amap_smart.c */
  21. /* PURPOSE : FAT32 Smart allocation code for sdFAT */
  22. /* */
  23. /*----------------------------------------------------------------------*/
  24. /* NOTES */
  25. /* */
  26. /* */
  27. /************************************************************************/
  28. #include <linux/slab.h>
  29. #include <linux/vmalloc.h>
  30. #include "sdfat.h"
  31. #include "core.h"
  32. #include "amap_smart.h"
  33. /* AU list related functions */
  34. static inline void amap_list_del(struct list_head *entry)
  35. {
  36. __list_del(entry->prev, entry->next);
  37. /* Will be used to check if the entry is a single entry(selected) */
  38. entry->prev = NULL;
  39. entry->next = NULL;
  40. }
  41. static inline int amap_insert_to_list(AU_INFO_T *au, struct slist_head *shead)
  42. {
  43. struct slist_head *entry = &au->shead;
  44. ASSERT(!entry->head);
  45. entry->next = shead->next;
  46. entry->head = shead;
  47. shead->next = entry;
  48. return 0;
  49. }
  50. static inline int amap_remove_from_list(AU_INFO_T *au, struct slist_head *shead)
  51. {
  52. struct slist_head *entry = &au->shead;
  53. struct slist_head *iter;
  54. BUG_ON(entry->head != shead);
  55. iter = shead;
  56. while (iter->next) {
  57. if (iter->next == entry) {
  58. // iter->next = iter->next->next
  59. iter->next = entry->next;
  60. entry->next = NULL;
  61. entry->head = NULL;
  62. return 0;
  63. }
  64. iter = iter->next;
  65. }
  66. BUG_ON("Not reachable");
  67. }
  68. /* Full-linear serach => Find AU with max. number of fclu */
  69. static inline AU_INFO_T *amap_find_hot_au_largest(struct slist_head *shead)
  70. {
  71. struct slist_head *iter;
  72. uint16_t max_fclu = 0;
  73. AU_INFO_T *entry, *ret = NULL;
  74. ASSERT(shead->head == shead); /* Singly-list condition */
  75. ASSERT(shead->next != shead);
  76. iter = shead->next;
  77. while (iter) {
  78. entry = list_entry(iter, AU_INFO_T, shead);
  79. if (entry->free_clusters > max_fclu) {
  80. max_fclu = entry->free_clusters;
  81. ret = entry;
  82. }
  83. iter = iter->next;
  84. }
  85. return ret;
  86. }
  87. /* Find partially used AU with max. number of fclu.
  88. * If there is no partial AU available, pick a clean one
  89. */
  90. static inline AU_INFO_T *amap_find_hot_au_partial(AMAP_T *amap)
  91. {
  92. struct slist_head *iter;
  93. uint16_t max_fclu = 0;
  94. AU_INFO_T *entry, *ret = NULL;
  95. iter = &amap->slist_hot;
  96. ASSERT(iter->head == iter); /* Singly-list condition */
  97. ASSERT(iter->next != iter);
  98. iter = iter->next;
  99. while (iter) {
  100. entry = list_entry(iter, AU_INFO_T, shead);
  101. if (entry->free_clusters > max_fclu) {
  102. if (entry->free_clusters < amap->clusters_per_au) {
  103. max_fclu = entry->free_clusters;
  104. ret = entry;
  105. } else {
  106. if (!ret)
  107. ret = entry;
  108. }
  109. }
  110. iter = iter->next;
  111. }
  112. return ret;
  113. }
  114. /*
  115. * Size-base AU management functions
  116. */
  117. /*
  118. * Add au into cold AU MAP
  119. * au: an isolated (not in a list) AU data structure
  120. */
  121. int amap_add_cold_au(AMAP_T *amap, AU_INFO_T *au)
  122. {
  123. FCLU_NODE_T *fclu_node = NULL;
  124. /* Check if a single entry */
  125. BUG_ON(au->head.prev);
  126. /* Ignore if the au is full */
  127. if (!au->free_clusters)
  128. return 0;
  129. /* Find entry */
  130. fclu_node = NODE(au->free_clusters, amap);
  131. /* Insert to the list */
  132. list_add_tail(&(au->head), &(fclu_node->head));
  133. /* Update fclu_hint (Increase) */
  134. if (au->free_clusters > amap->fclu_hint)
  135. amap->fclu_hint = au->free_clusters;
  136. return 0;
  137. }
  138. /*
  139. * Remove an AU from AU MAP
  140. */
  141. int amap_remove_cold_au(AMAP_T *amap, AU_INFO_T *au)
  142. {
  143. struct list_head *prev = au->head.prev;
  144. /* Single entries are not managed in lists */
  145. if (!prev) {
  146. BUG_ON(au->free_clusters > 0);
  147. return 0;
  148. }
  149. /* remove from list */
  150. amap_list_del(&(au->head));
  151. return 0;
  152. }
  153. /* "Find" best fit AU
  154. * returns NULL if there is no AU w/ enough free space.
  155. *
  156. * This function doesn't change AU status.
  157. * The caller should call amap_remove_cold_au() if needed.
  158. */
  159. AU_INFO_T *amap_find_cold_au_bestfit(AMAP_T *amap, uint16_t free_clusters)
  160. {
  161. AU_INFO_T *au = NULL;
  162. FCLU_NODE_T *fclu_iter;
  163. if (free_clusters <= 0 || free_clusters > amap->clusters_per_au) {
  164. EMSG("AMAP: amap_find_cold_au_bestfit / unexpected arg. (%d)\n",
  165. free_clusters);
  166. return NULL;
  167. }
  168. fclu_iter = NODE(free_clusters, amap);
  169. if (amap->fclu_hint < free_clusters) {
  170. /* There is no AUs with enough free_clusters */
  171. return NULL;
  172. }
  173. /* Naive Hash management (++) */
  174. do {
  175. if (!list_empty(&fclu_iter->head)) {
  176. struct list_head *first = fclu_iter->head.next;
  177. au = list_entry(first, AU_INFO_T, head);
  178. break;
  179. }
  180. fclu_iter++;
  181. } while (fclu_iter < (amap->fclu_nodes + amap->clusters_per_au));
  182. // BUG_ON(au->free_clusters < 0);
  183. BUG_ON(au && (au->free_clusters > amap->clusters_per_au));
  184. return au;
  185. }
  186. /* "Pop" best fit AU
  187. *
  188. * returns NULL if there is no AU w/ enough free space.
  189. * The returned AU will not be in the list anymore.
  190. */
  191. AU_INFO_T *amap_pop_cold_au_bestfit(AMAP_T *amap, uint16_t free_clusters)
  192. {
  193. /* Naive implementation */
  194. AU_INFO_T *au;
  195. au = amap_find_cold_au_bestfit(amap, free_clusters);
  196. if (au)
  197. amap_remove_cold_au(amap, au);
  198. return au;
  199. }
  200. /* Pop the AU with the largest free space
  201. *
  202. * search from 'start_fclu' to 0
  203. * (target freecluster : -1 for each step)
  204. * start_fclu = 0 means to search from the max. value
  205. */
  206. AU_INFO_T *amap_pop_cold_au_largest(AMAP_T *amap, uint16_t start_fclu)
  207. {
  208. AU_INFO_T *au = NULL;
  209. FCLU_NODE_T *fclu_iter;
  210. if (!start_fclu)
  211. start_fclu = amap->clusters_per_au;
  212. if (start_fclu > amap->clusters_per_au)
  213. start_fclu = amap->clusters_per_au;
  214. /* Use hint (search start point) */
  215. if (amap->fclu_hint < start_fclu)
  216. fclu_iter = NODE(amap->fclu_hint, amap);
  217. else
  218. fclu_iter = NODE(start_fclu, amap);
  219. /* Naive Hash management */
  220. do {
  221. if (!list_empty(&fclu_iter->head)) {
  222. struct list_head *first = fclu_iter->head.next;
  223. au = list_entry(first, AU_INFO_T, head);
  224. // BUG_ON((au < amap->entries) || ((amap->entries + amap->n_au) <= au));
  225. amap_list_del(first);
  226. // (Hint) Possible maximum value of free clusters (among cold)
  227. /* if it wasn't the whole search, don't update fclu_hint */
  228. if (start_fclu == amap->clusters_per_au)
  229. amap->fclu_hint = au->free_clusters;
  230. break;
  231. }
  232. fclu_iter--;
  233. } while (amap->fclu_nodes <= fclu_iter);
  234. return au;
  235. }
  236. /*
  237. * ===============================================
  238. * Allocation Map related functions
  239. * ===============================================
  240. */
  241. /* Create AMAP related data structure (mount time) */
  242. int amap_create(struct super_block *sb, u32 pack_ratio, u32 sect_per_au, u32 hidden_sect)
  243. {
  244. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  245. AMAP_T *amap;
  246. int total_used_clusters;
  247. int n_au_table = 0;
  248. int i, i_clu, i_au;
  249. int i_au_root = -1, i_au_hot_from = INT_MAX;
  250. u32 misaligned_sect = hidden_sect;
  251. u64 tmp;
  252. BUG_ON(!fsi->bd_opened);
  253. if (fsi->amap)
  254. return -EEXIST;
  255. /* Check conditions */
  256. if (fsi->vol_type != FAT32) {
  257. sdfat_msg(sb, KERN_ERR, "smart allocation is only available "
  258. "with fat32-fs");
  259. return -ENOTSUPP;
  260. }
  261. if (fsi->num_sectors < AMAP_MIN_SUPPORT_SECTORS) {
  262. sdfat_msg(sb, KERN_ERR, "smart allocation is only available "
  263. "with sectors above %d", AMAP_MIN_SUPPORT_SECTORS);
  264. return -ENOTSUPP;
  265. }
  266. /* AU size must be a multiple of clu_size */
  267. if ((sect_per_au <= 0) || (sect_per_au & (fsi->sect_per_clus - 1))) {
  268. sdfat_msg(sb, KERN_ERR,
  269. "invalid AU size (sect_per_au : %u, "
  270. "sect_per_clus : %u) "
  271. "please re-format for performance.",
  272. sect_per_au, fsi->sect_per_clus);
  273. return -EINVAL;
  274. }
  275. /* the start sector of this partition must be a multiple of clu_size */
  276. if (misaligned_sect & (fsi->sect_per_clus - 1)) {
  277. sdfat_msg(sb, KERN_ERR,
  278. "misaligned part (start sect : %u, "
  279. "sect_per_clus : %u) "
  280. "please re-format for performance.",
  281. misaligned_sect, fsi->sect_per_clus);
  282. return -EINVAL;
  283. }
  284. /* data start sector must be a multiple of clu_size */
  285. if (fsi->data_start_sector & (fsi->sect_per_clus - 1)) {
  286. sdfat_msg(sb, KERN_ERR,
  287. "misaligned data area (start sect : %llu, "
  288. "sect_per_clus : %u) "
  289. "please re-format for performance.",
  290. fsi->data_start_sector, fsi->sect_per_clus);
  291. return -EINVAL;
  292. }
  293. misaligned_sect &= (sect_per_au - 1);
  294. /* Allocate data structrues */
  295. amap = kzalloc(sizeof(AMAP_T), GFP_NOIO);
  296. if (!amap)
  297. return -ENOMEM;
  298. amap->sb = sb;
  299. tmp = fsi->num_sectors + misaligned_sect + sect_per_au - 1;
  300. do_div(tmp, sect_per_au);
  301. amap->n_au = tmp;
  302. amap->n_clean_au = 0;
  303. amap->n_full_au = 0;
  304. /* Reflect block-partition align first,
  305. * then partition-data_start align
  306. */
  307. amap->clu_align_bias = (misaligned_sect / fsi->sect_per_clus);
  308. amap->clu_align_bias += (fsi->data_start_sector >> fsi->sect_per_clus_bits) - CLUS_BASE;
  309. amap->clusters_per_au = sect_per_au / fsi->sect_per_clus;
  310. /* That is,
  311. * the size of cluster is at least 4KB if the size of AU is 4MB
  312. */
  313. if (amap->clusters_per_au > MAX_CLU_PER_AU) {
  314. sdfat_log_msg(sb, KERN_INFO,
  315. "too many clusters per AU (clus/au:%d > %d).",
  316. amap->clusters_per_au,
  317. MAX_CLU_PER_AU);
  318. }
  319. /* is it needed? why here? */
  320. // set_sb_dirty(sb);
  321. spin_lock_init(&amap->amap_lock);
  322. amap->option.packing_ratio = pack_ratio;
  323. amap->option.au_size = sect_per_au;
  324. amap->option.au_align_factor = hidden_sect;
  325. /* Allocate AU info table */
  326. n_au_table = (amap->n_au + N_AU_PER_TABLE - 1) / N_AU_PER_TABLE;
  327. amap->au_table = kmalloc(sizeof(AU_INFO_T *) * n_au_table, GFP_NOIO);
  328. if (!amap->au_table) {
  329. sdfat_msg(sb, KERN_ERR,
  330. "failed to alloc amap->au_table\n");
  331. kfree(amap);
  332. return -ENOMEM;
  333. }
  334. for (i = 0; i < n_au_table; i++)
  335. amap->au_table[i] = (AU_INFO_T *)get_zeroed_page(GFP_NOIO);
  336. /* Allocate buckets indexed by # of free clusters */
  337. amap->fclu_order = get_order(sizeof(FCLU_NODE_T) * amap->clusters_per_au);
  338. // XXX: amap->clusters_per_au limitation is 512 (w/ 8 byte list_head)
  339. sdfat_log_msg(sb, KERN_INFO, "page orders for AU nodes : %d "
  340. "(clus_per_au : %d, node_size : %lu)",
  341. amap->fclu_order,
  342. amap->clusters_per_au,
  343. (unsigned long)sizeof(FCLU_NODE_T));
  344. if (!amap->fclu_order)
  345. amap->fclu_nodes = (FCLU_NODE_T *)get_zeroed_page(GFP_NOIO);
  346. else
  347. amap->fclu_nodes = vzalloc(PAGE_SIZE << amap->fclu_order);
  348. amap->fclu_hint = amap->clusters_per_au;
  349. /* Hot AU list, ignored AU list */
  350. amap->slist_hot.next = NULL;
  351. amap->slist_hot.head = &amap->slist_hot;
  352. amap->total_fclu_hot = 0;
  353. amap->slist_ignored.next = NULL;
  354. amap->slist_ignored.head = &amap->slist_ignored;
  355. /* Strategy related vars. */
  356. amap->cur_cold.au = NULL;
  357. amap->cur_hot.au = NULL;
  358. amap->n_need_packing = 0;
  359. /* Build AMAP info */
  360. total_used_clusters = 0; // Count # of used clusters
  361. i_au_root = i_AU_of_CLU(amap, fsi->root_dir);
  362. i_au_hot_from = amap->n_au - (SMART_ALLOC_N_HOT_AU - 1);
  363. for (i = 0; i < amap->clusters_per_au; i++)
  364. INIT_LIST_HEAD(&amap->fclu_nodes[i].head);
  365. /*
  366. * Thanks to kzalloc()
  367. * amap->entries[i_au].free_clusters = 0;
  368. * amap->entries[i_au].head.prev = NULL;
  369. * amap->entries[i_au].head.next = NULL;
  370. */
  371. /* Parse FAT table */
  372. for (i_clu = CLUS_BASE; i_clu < fsi->num_clusters; i_clu++) {
  373. u32 clu_data;
  374. AU_INFO_T *au;
  375. if (fat_ent_get(sb, i_clu, &clu_data)) {
  376. sdfat_msg(sb, KERN_ERR,
  377. "failed to read fat entry(%u)\n", i_clu);
  378. goto free_and_eio;
  379. }
  380. if (IS_CLUS_FREE(clu_data)) {
  381. au = GET_AU(amap, i_AU_of_CLU(amap, i_clu));
  382. au->free_clusters++;
  383. } else
  384. total_used_clusters++;
  385. }
  386. /* Build AU list */
  387. for (i_au = 0; i_au < amap->n_au; i_au++) {
  388. AU_INFO_T *au = GET_AU(amap, i_au);
  389. au->idx = i_au;
  390. BUG_ON(au->free_clusters > amap->clusters_per_au);
  391. if (au->free_clusters == amap->clusters_per_au)
  392. amap->n_clean_au++;
  393. else if (au->free_clusters == 0)
  394. amap->n_full_au++;
  395. /* If hot, insert to the hot list */
  396. if (i_au >= i_au_hot_from) {
  397. amap_add_hot_au(amap, au);
  398. amap->total_fclu_hot += au->free_clusters;
  399. } else if (i_au != i_au_root || SMART_ALLOC_N_HOT_AU == 0) {
  400. /* Otherwise, insert to the free cluster hash */
  401. amap_add_cold_au(amap, au);
  402. }
  403. }
  404. /* Hot list -> (root) -> (last) -> (last - 1) -> ... */
  405. if (i_au_root >= 0 && SMART_ALLOC_N_HOT_AU > 0) {
  406. amap_add_hot_au(amap, GET_AU(amap, i_au_root));
  407. amap->total_fclu_hot += GET_AU(amap, i_au_root)->free_clusters;
  408. }
  409. fsi->amap = amap;
  410. fsi->used_clusters = total_used_clusters;
  411. sdfat_msg(sb, KERN_INFO,
  412. "AMAP: Smart allocation enabled (opt : %u / %u / %u)",
  413. amap->option.au_size, amap->option.au_align_factor,
  414. amap->option.packing_ratio);
  415. /* Debug purpose - check */
  416. //{
  417. //u32 used_clusters;
  418. //fat_count_used_clusters(sb, &used_clusters)
  419. //ASSERT(used_clusters == total_used_clusters);
  420. //}
  421. return 0;
  422. free_and_eio:
  423. if (amap) {
  424. if (amap->au_table) {
  425. for (i = 0; i < n_au_table; i++)
  426. free_page((unsigned long)amap->au_table[i]);
  427. kfree(amap->au_table);
  428. }
  429. if (amap->fclu_nodes) {
  430. if (!amap->fclu_order)
  431. free_page((unsigned long)amap->fclu_nodes);
  432. else
  433. vfree(amap->fclu_nodes);
  434. }
  435. kfree(amap);
  436. }
  437. return -EIO;
  438. }
  439. /* Free AMAP related structure */
  440. void amap_destroy(struct super_block *sb)
  441. {
  442. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  443. int n_au_table;
  444. if (!amap)
  445. return;
  446. DMSG("%s\n", __func__);
  447. n_au_table = (amap->n_au + N_AU_PER_TABLE - 1) / N_AU_PER_TABLE;
  448. if (amap->au_table) {
  449. int i;
  450. for (i = 0; i < n_au_table; i++)
  451. free_page((unsigned long)amap->au_table[i]);
  452. kfree(amap->au_table);
  453. }
  454. if (!amap->fclu_order)
  455. free_page((unsigned long)amap->fclu_nodes);
  456. else
  457. vfree(amap->fclu_nodes);
  458. kfree(amap);
  459. SDFAT_SB(sb)->fsi.amap = NULL;
  460. }
  461. /*
  462. * Check status of FS
  463. * and change destination if needed to disable AU-aligned alloc.
  464. * (from ALLOC_COLD_ALIGNED to ALLOC_COLD_SEQ)
  465. */
  466. static inline int amap_update_dest(AMAP_T *amap, int ori_dest)
  467. {
  468. FS_INFO_T *fsi = &(SDFAT_SB(amap->sb)->fsi);
  469. int n_partial_au, n_partial_freeclus;
  470. if (ori_dest != ALLOC_COLD_ALIGNED)
  471. return ori_dest;
  472. /* # of partial AUs and # of clusters in those AUs */
  473. n_partial_au = amap->n_au - amap->n_clean_au - amap->n_full_au;
  474. n_partial_freeclus = fsi->num_clusters - fsi->used_clusters -
  475. amap->clusters_per_au * amap->n_clean_au;
  476. /* Status of AUs : Full / Partial / Clean
  477. * If there are many partial (and badly fragmented) AUs,
  478. * the throughput will decrease extremly.
  479. *
  480. * The follow code will treat those worst cases.
  481. */
  482. /* XXX: AMAP heuristics */
  483. if ((amap->n_clean_au * 50 <= amap->n_au) &&
  484. (n_partial_freeclus*2) < (n_partial_au*amap->clusters_per_au)) {
  485. /* If clean AUs are fewer than 2% of n_au (80 AUs per 16GB)
  486. * and fragment ratio is more than 2 (AVG free_clusters=half AU)
  487. *
  488. * disable clean-first allocation
  489. * enable VFAT-like sequential allocation
  490. */
  491. return ALLOC_COLD_SEQ;
  492. }
  493. return ori_dest;
  494. }
  495. #define PACKING_SOFTLIMIT (amap->option.packing_ratio)
  496. #define PACKING_HARDLIMIT (amap->option.packing_ratio * 4)
  497. /*
  498. * Pick a packing AU if needed.
  499. * Otherwise just return NULL
  500. *
  501. * This function includes some heuristics.
  502. */
  503. static inline AU_INFO_T *amap_get_packing_au(AMAP_T *amap, int dest, int num_to_wb, int *clu_to_skip)
  504. {
  505. AU_INFO_T *au = NULL;
  506. if (dest == ALLOC_COLD_PACKING) {
  507. /* ALLOC_COLD_PACKING:
  508. * Packing-first mode for defrag.
  509. * Optimized to save clean AU
  510. *
  511. * 1) best-fit AU
  512. * 2) Smallest AU (w/ minimum free clusters)
  513. */
  514. if (num_to_wb >= amap->clusters_per_au)
  515. num_to_wb = num_to_wb % amap->clusters_per_au;
  516. /* 이거 주석처리하면, AU size 딱 맞을때는 clean, 나머지는 작은거부터 */
  517. if (num_to_wb == 0)
  518. num_to_wb = 1; // Don't use clean AUs
  519. au = amap_find_cold_au_bestfit(amap, num_to_wb);
  520. if (au && au->free_clusters == amap->clusters_per_au && num_to_wb > 1) {
  521. /* if au is clean then get a new partial one */
  522. au = amap_find_cold_au_bestfit(amap, 1);
  523. }
  524. if (au) {
  525. amap->n_need_packing = 0;
  526. amap_remove_cold_au(amap, au);
  527. return au;
  528. }
  529. }
  530. /* Heuristic packing:
  531. * This will improve QoS greatly.
  532. *
  533. * Count # of AU_ALIGNED allocation.
  534. * If the number exceeds the specific threshold,
  535. * allocate on a partial AU or generate random I/O.
  536. */
  537. if ((PACKING_SOFTLIMIT > 0) &&
  538. (amap->n_need_packing >= PACKING_SOFTLIMIT) &&
  539. (num_to_wb < (int)amap->clusters_per_au)) {
  540. /* Best-fit packing:
  541. * If num_to_wb (expected number to be allocated) is smaller
  542. * than AU_SIZE, find a best-fit AU.
  543. */
  544. /* Back margin (heuristics) */
  545. if (num_to_wb < amap->clusters_per_au / 4)
  546. num_to_wb = amap->clusters_per_au / 4;
  547. au = amap_find_cold_au_bestfit(amap, num_to_wb);
  548. if (au != NULL) {
  549. amap_remove_cold_au(amap, au);
  550. MMSG("AMAP: packing (cnt: %d) / softlimit, "
  551. "best-fit (num_to_wb: %d))\n",
  552. amap->n_need_packing, num_to_wb);
  553. if (au->free_clusters > num_to_wb) { // Best-fit search: if 문 무조건 hit
  554. *clu_to_skip = au->free_clusters - num_to_wb;
  555. /* otherwise don't skip */
  556. }
  557. amap->n_need_packing = 0;
  558. return au;
  559. }
  560. }
  561. if ((PACKING_HARDLIMIT != 0) &&
  562. amap->n_need_packing >= PACKING_HARDLIMIT) {
  563. /* Compulsory SLC flushing:
  564. * If there was no chance to do best-fit packing
  565. * and the # of AU-aligned allocation exceeds HARD threshold,
  566. * then pick a clean AU and generate a compulsory random I/O.
  567. */
  568. au = amap_pop_cold_au_largest(amap, amap->clusters_per_au);
  569. if (au) {
  570. MMSG("AMAP: packing (cnt: %d) / hard-limit, largest)\n",
  571. amap->n_need_packing);
  572. if (au->free_clusters >= 96) {
  573. *clu_to_skip = au->free_clusters / 2;
  574. MMSG("AMAP: cluster idx re-position\n");
  575. }
  576. amap->n_need_packing = 0;
  577. return au;
  578. }
  579. }
  580. /* Update # of clean AU allocation */
  581. amap->n_need_packing++;
  582. return NULL;
  583. }
  584. /* Pick a target AU:
  585. * This function should be called
  586. * only if there are one or more free clusters in the bdev.
  587. */
  588. TARGET_AU_T *amap_get_target_au(AMAP_T *amap, int dest, int num_to_wb)
  589. {
  590. int loop_count = 0;
  591. retry:
  592. if (++loop_count >= 3) {
  593. /* No space available (or AMAP consistency error)
  594. * This could happen because of the ignored AUs but not likely
  595. * (because the defrag daemon will not work if there is no enough space)
  596. */
  597. BUG_ON(amap->slist_ignored.next == NULL);
  598. return NULL;
  599. }
  600. /* Hot clusters (DIR) */
  601. if (dest == ALLOC_HOT) {
  602. /* Working hot AU exist? */
  603. if (amap->cur_hot.au == NULL || amap->cur_hot.au->free_clusters == 0) {
  604. AU_INFO_T *au;
  605. if (amap->total_fclu_hot == 0) {
  606. /* No more hot AU avaialbe */
  607. dest = ALLOC_COLD;
  608. goto retry;
  609. }
  610. au = amap_find_hot_au_partial(amap);
  611. BUG_ON(au == NULL);
  612. BUG_ON(au->free_clusters <= 0);
  613. amap->cur_hot.au = au;
  614. amap->cur_hot.idx = 0;
  615. amap->cur_hot.clu_to_skip = 0;
  616. }
  617. /* Now allocate on a hot AU */
  618. return &amap->cur_hot;
  619. }
  620. /* Cold allocation:
  621. * If amap->cur_cold.au has one or more free cluster(s),
  622. * then just return amap->cur_cold
  623. */
  624. if ((!amap->cur_cold.au)
  625. || (amap->cur_cold.idx == amap->clusters_per_au)
  626. || (amap->cur_cold.au->free_clusters == 0)) {
  627. AU_INFO_T *au = NULL;
  628. const AU_INFO_T *old_au = amap->cur_cold.au;
  629. int n_clu_to_skip = 0;
  630. if (old_au) {
  631. ASSERT(!IS_AU_WORKING(old_au, amap));
  632. /* must be NOT WORKING AU.
  633. * (only for information gathering)
  634. */
  635. }
  636. /* Next target AU is needed:
  637. * There are 3 possible ALLOC options for cold AU
  638. *
  639. * ALLOC_COLD_ALIGNED: Clean AU first, but heuristic packing is ON
  640. * ALLOC_COLD_PACKING: Packing AU first (usually for defrag)
  641. * ALLOC_COLD_SEQ : Sequential AU allocation (VFAT-like)
  642. */
  643. /* Experimental: Modify allocation destination if needed (ALIGNED => SEQ) */
  644. // dest = amap_update_dest(amap, dest);
  645. if ((dest == ALLOC_COLD_SEQ) && old_au) {
  646. int i_au = old_au->idx + 1;
  647. while (i_au != old_au->idx) {
  648. au = GET_AU(amap, i_au);
  649. if ((au->free_clusters > 0) &&
  650. !IS_AU_HOT(au, amap) &&
  651. !IS_AU_IGNORED(au, amap)) {
  652. MMSG("AMAP: new cold AU(%d) with %d "
  653. "clusters (seq)\n",
  654. au->idx, au->free_clusters);
  655. amap_remove_cold_au(amap, au);
  656. goto ret_new_cold;
  657. }
  658. i_au++;
  659. if (i_au >= amap->n_au)
  660. i_au = 0;
  661. }
  662. // no cold AUs are available => Hot allocation
  663. dest = ALLOC_HOT;
  664. goto retry;
  665. }
  666. /*
  667. * Check if packing is needed
  668. * (ALLOC_COLD_PACKING is treated by this function)
  669. */
  670. au = amap_get_packing_au(amap, dest, num_to_wb, &n_clu_to_skip);
  671. if (au) {
  672. MMSG("AMAP: new cold AU(%d) with %d clusters "
  673. "(packing)\n", au->idx, au->free_clusters);
  674. goto ret_new_cold;
  675. }
  676. /* ALLOC_COLD_ALIGNED */
  677. /* Check if the adjacent AU is clean */
  678. if (old_au && ((old_au->idx + 1) < amap->n_au)) {
  679. au = GET_AU(amap, old_au->idx + 1);
  680. if ((au->free_clusters == amap->clusters_per_au) &&
  681. !IS_AU_HOT(au, amap) &&
  682. !IS_AU_IGNORED(au, amap)) {
  683. MMSG("AMAP: new cold AU(%d) with %d clusters "
  684. "(adjacent)\n", au->idx, au->free_clusters);
  685. amap_remove_cold_au(amap, au);
  686. goto ret_new_cold;
  687. }
  688. }
  689. /* Clean or largest AU */
  690. au = amap_pop_cold_au_largest(amap, 0);
  691. if (!au) {
  692. //ASSERT(amap->total_fclu_hot == (fsi->num_clusters - fsi->used_clusters - 2));
  693. dest = ALLOC_HOT;
  694. goto retry;
  695. }
  696. MMSG("AMAP: New cold AU (%d) with %d clusters\n",
  697. au->idx, au->free_clusters);
  698. ret_new_cold:
  699. SET_AU_WORKING(au);
  700. amap->cur_cold.au = au;
  701. amap->cur_cold.idx = 0;
  702. amap->cur_cold.clu_to_skip = n_clu_to_skip;
  703. }
  704. return &amap->cur_cold;
  705. }
  706. /* Put and update target AU */
  707. void amap_put_target_au(AMAP_T *amap, TARGET_AU_T *cur, unsigned int num_allocated)
  708. {
  709. /* Update AMAP info vars. */
  710. if (num_allocated > 0 &&
  711. (cur->au->free_clusters + num_allocated) == amap->clusters_per_au) {
  712. /* if the target AU was a clean AU before this allocation ... */
  713. amap->n_clean_au--;
  714. }
  715. if (num_allocated > 0 &&
  716. cur->au->free_clusters == 0)
  717. amap->n_full_au++;
  718. if (IS_AU_HOT(cur->au, amap)) {
  719. /* Hot AU */
  720. MMSG("AMAP: hot allocation at AU %d\n", cur->au->idx);
  721. amap->total_fclu_hot -= num_allocated;
  722. /* Intra-AU round-robin */
  723. if (cur->idx >= amap->clusters_per_au)
  724. cur->idx = 0;
  725. /* No more space available */
  726. if (cur->au->free_clusters == 0)
  727. cur->au = NULL;
  728. } else {
  729. /* non-hot AU */
  730. ASSERT(IS_AU_WORKING(cur->au, amap));
  731. if (cur->idx >= amap->clusters_per_au || cur->au->free_clusters == 0) {
  732. /* It should be inserted back to AU MAP */
  733. cur->au->shead.head = NULL; // SET_AU_NOT_WORKING
  734. amap_add_cold_au(amap, cur->au);
  735. // cur->au = NULL; // This value will be used for the next AU selection
  736. cur->idx = amap->clusters_per_au; // AU closing
  737. }
  738. }
  739. }
  740. /* Reposition target->idx for packing (Heuristics):
  741. * Skip (num_to_skip) free clusters in (cur->au)
  742. */
  743. static inline int amap_skip_cluster(struct super_block *sb, TARGET_AU_T *cur, int num_to_skip)
  744. {
  745. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  746. u32 clu, read_clu;
  747. MMSG_VAR(int num_to_skip_orig = num_to_skip);
  748. if (num_to_skip >= cur->au->free_clusters) {
  749. EMSG("AMAP(%s): skip mis-use. amap_566\n", __func__);
  750. return -EIO;
  751. }
  752. clu = CLU_of_i_AU(amap, cur->au->idx, cur->idx);
  753. while (num_to_skip > 0) {
  754. if (clu >= CLUS_BASE) {
  755. /* Cf.
  756. * If AMAP's integrity is okay,
  757. * we don't need to check if (clu < fsi->num_clusters)
  758. */
  759. if (fat_ent_get(sb, clu, &read_clu))
  760. return -EIO;
  761. if (IS_CLUS_FREE(read_clu))
  762. num_to_skip--;
  763. }
  764. // Move clu->idx
  765. clu++;
  766. (cur->idx)++;
  767. if (cur->idx >= amap->clusters_per_au) {
  768. /* End of AU (Not supposed) */
  769. EMSG("AMAP: Skip - End of AU?! (amap_596)\n");
  770. cur->idx = 0;
  771. return -EIO;
  772. }
  773. }
  774. MMSG("AMAP: Skip_clusters (%d skipped => %d, among %d free clus)\n",
  775. num_to_skip_orig, cur->idx, cur->au->free_clusters);
  776. return 0;
  777. }
  778. /* AMAP-based allocation function for FAT32 */
  779. s32 amap_fat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
  780. {
  781. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  782. TARGET_AU_T *cur = NULL;
  783. AU_INFO_T *target_au = NULL; /* Allocation target AU */
  784. s32 ret = -ENOSPC;
  785. u32 last_clu = CLUS_EOF, read_clu;
  786. u32 new_clu, total_cnt;
  787. u32 num_allocated = 0, num_allocated_each = 0;
  788. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  789. BUG_ON(!amap);
  790. BUG_ON(IS_CLUS_EOF(fsi->used_clusters));
  791. total_cnt = fsi->num_clusters - CLUS_BASE;
  792. if (unlikely(total_cnt < fsi->used_clusters)) {
  793. sdfat_fs_error_ratelimit(sb,
  794. "AMAP(%s): invalid used clusters(t:%u,u:%u)\n",
  795. __func__, total_cnt, fsi->used_clusters);
  796. return -EIO;
  797. }
  798. if (num_alloc > total_cnt - fsi->used_clusters)
  799. return -ENOSPC;
  800. p_chain->dir = CLUS_EOF;
  801. set_sb_dirty(sb);
  802. // spin_lock(&amap->amap_lock);
  803. retry_alloc:
  804. /* Allocation strategy implemented */
  805. cur = amap_get_target_au(amap, dest, fsi->reserved_clusters);
  806. if (unlikely(!cur)) {
  807. // There is no available AU (only ignored-AU are left)
  808. sdfat_msg(sb, KERN_ERR, "AMAP Allocator: no avaialble AU.");
  809. goto error;
  810. }
  811. /* If there are clusters to skip */
  812. if (cur->clu_to_skip > 0) {
  813. if (amap_skip_cluster(sb, &amap->cur_cold, cur->clu_to_skip)) {
  814. ret = -EIO;
  815. goto error;
  816. }
  817. cur->clu_to_skip = 0;
  818. }
  819. target_au = cur->au;
  820. /*
  821. * cur->au : target AU info pointer
  822. * cur->idx : the intra-cluster idx in the AU to start from
  823. */
  824. BUG_ON(!cur->au);
  825. BUG_ON(!cur->au->free_clusters);
  826. BUG_ON(cur->idx >= amap->clusters_per_au);
  827. num_allocated_each = 0;
  828. new_clu = CLU_of_i_AU(amap, target_au->idx, cur->idx);
  829. do {
  830. /* Allocate at the target AU */
  831. if ((new_clu >= CLUS_BASE) && (new_clu < fsi->num_clusters)) {
  832. if (fat_ent_get(sb, new_clu, &read_clu)) {
  833. // spin_unlock(&amap->amap_lock);
  834. ret = -EIO;
  835. goto error;
  836. }
  837. if (IS_CLUS_FREE(read_clu)) {
  838. BUG_ON(GET_AU(amap, i_AU_of_CLU(amap, new_clu)) != target_au);
  839. /* Free cluster found */
  840. if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
  841. ret = -EIO;
  842. goto error;
  843. }
  844. num_allocated_each++;
  845. if (IS_CLUS_EOF(p_chain->dir)) {
  846. p_chain->dir = new_clu;
  847. } else {
  848. if (fat_ent_set(sb, last_clu, new_clu)) {
  849. ret = -EIO;
  850. goto error;
  851. }
  852. }
  853. last_clu = new_clu;
  854. /* Update au info */
  855. target_au->free_clusters--;
  856. }
  857. }
  858. new_clu++;
  859. (cur->idx)++;
  860. /* End of the AU */
  861. if ((cur->idx >= amap->clusters_per_au) || !(target_au->free_clusters))
  862. break;
  863. } while (num_allocated_each < num_alloc);
  864. /* Update strategy info */
  865. amap_put_target_au(amap, cur, num_allocated_each);
  866. num_allocated += num_allocated_each;
  867. fsi->used_clusters += num_allocated_each;
  868. num_alloc -= num_allocated_each;
  869. if (num_alloc > 0)
  870. goto retry_alloc;
  871. // spin_unlock(&amap->amap_lock);
  872. return 0;
  873. error:
  874. if (num_allocated)
  875. fsi->fs_func->free_cluster(sb, p_chain, 0);
  876. return ret;
  877. }
  878. /* Free cluster for FAT32 (not implemented yet) */
  879. s32 amap_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
  880. {
  881. return -ENOTSUPP;
  882. }
  883. /*
  884. * This is called by fat_free_cluster()
  885. * to update AMAP info.
  886. */
  887. s32 amap_release_cluster(struct super_block *sb, u32 clu)
  888. {
  889. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  890. AU_INFO_T *au;
  891. int i_au;
  892. // spin_lock(&amap->amap_lock);
  893. /* Update AU info */
  894. i_au = i_AU_of_CLU(amap, clu);
  895. BUG_ON(i_au >= amap->n_au);
  896. au = GET_AU(amap, i_au);
  897. if (au->free_clusters >= amap->clusters_per_au) {
  898. sdfat_fs_error(sb, "%s, au->free_clusters(%hd) is "
  899. "greater than or equal to amap->clusters_per_au(%hd)",
  900. __func__, au->free_clusters, amap->clusters_per_au);
  901. return -EIO;
  902. }
  903. if (IS_AU_HOT(au, amap)) {
  904. MMSG("AMAP: Hot cluster freed\n");
  905. au->free_clusters++;
  906. amap->total_fclu_hot++;
  907. } else if (!IS_AU_WORKING(au, amap) && !IS_AU_IGNORED(au, amap)) {
  908. /* Ordinary AU - update AU tree */
  909. // Can be optimized by implementing amap_update_au
  910. amap_remove_cold_au(amap, au);
  911. au->free_clusters++;
  912. amap_add_cold_au(amap, au);
  913. } else
  914. au->free_clusters++;
  915. /* Update AMAP info */
  916. if (au->free_clusters == amap->clusters_per_au)
  917. amap->n_clean_au++;
  918. if (au->free_clusters == 1)
  919. amap->n_full_au--;
  920. // spin_unlock(&amap->amap_lock);
  921. return 0;
  922. }
  923. /*
  924. * Check if the cluster is in a working AU
  925. * The caller should hold sb lock.
  926. * This func. should be used only if smart allocation is on
  927. */
  928. s32 amap_check_working(struct super_block *sb, u32 clu)
  929. {
  930. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  931. AU_INFO_T *au;
  932. BUG_ON(!amap);
  933. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  934. return IS_AU_WORKING(au, amap);
  935. }
  936. /*
  937. * Return the # of free clusters in that AU
  938. */
  939. s32 amap_get_freeclus(struct super_block *sb, u32 clu)
  940. {
  941. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  942. AU_INFO_T *au;
  943. BUG_ON(!amap);
  944. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  945. return (s32)au->free_clusters;
  946. }
  947. /*
  948. * Add the AU containing 'clu' to the ignored AU list.
  949. * The AU will not be used by the allocator.
  950. *
  951. * XXX: Ignored counter needed
  952. */
  953. s32 amap_mark_ignore(struct super_block *sb, u32 clu)
  954. {
  955. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  956. AU_INFO_T *au;
  957. BUG_ON(!amap);
  958. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  959. if (IS_AU_HOT(au, amap)) {
  960. /* Doesn't work with hot AUs */
  961. return -EPERM;
  962. } else if (IS_AU_WORKING(au, amap)) {
  963. return -EBUSY;
  964. }
  965. //BUG_ON(IS_AU_IGNORED(au, amap) && (GET_IGN_CNT(au) == 0));
  966. if (IS_AU_IGNORED(au, amap))
  967. return 0;
  968. amap_remove_cold_au(amap, au);
  969. amap_insert_to_list(au, &amap->slist_ignored);
  970. BUG_ON(!IS_AU_IGNORED(au, amap));
  971. //INC_IGN_CNT(au);
  972. MMSG("AMAP: Mark ignored AU (%d)\n", au->idx);
  973. return 0;
  974. }
  975. /*
  976. * This function could be used only on IGNORED AUs.
  977. * The caller should care whether it's ignored or not before using this func.
  978. */
  979. s32 amap_unmark_ignore(struct super_block *sb, u32 clu)
  980. {
  981. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  982. AU_INFO_T *au;
  983. BUG_ON(!amap);
  984. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  985. BUG_ON(!IS_AU_IGNORED(au, amap));
  986. // BUG_ON(GET_IGN_CNT(au) == 0);
  987. amap_remove_from_list(au, &amap->slist_ignored);
  988. amap_add_cold_au(amap, au);
  989. BUG_ON(IS_AU_IGNORED(au, amap));
  990. //DEC_IGN_CNT(au);
  991. MMSG("AMAP: Unmark ignored AU (%d)\n", au->idx);
  992. return 0;
  993. }
  994. /*
  995. * Unmark all ignored AU
  996. * This will return # of unmarked AUs
  997. */
  998. s32 amap_unmark_ignore_all(struct super_block *sb)
  999. {
  1000. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  1001. struct slist_head *entry;
  1002. AU_INFO_T *au;
  1003. int n = 0;
  1004. BUG_ON(!amap);
  1005. entry = amap->slist_ignored.next;
  1006. while (entry) {
  1007. au = list_entry(entry, AU_INFO_T, shead);
  1008. BUG_ON(au != GET_AU(amap, au->idx));
  1009. BUG_ON(!IS_AU_IGNORED(au, amap));
  1010. //CLEAR_IGN_CNT(au);
  1011. amap_remove_from_list(au, &amap->slist_ignored);
  1012. amap_add_cold_au(amap, au);
  1013. MMSG("AMAP: Unmark ignored AU (%d)\n", au->idx);
  1014. n++;
  1015. entry = amap->slist_ignored.next;
  1016. }
  1017. BUG_ON(amap->slist_ignored.next != NULL);
  1018. MMSG("AMAP: unmark_ignore_all, total %d AUs\n", n);
  1019. return n;
  1020. }
  1021. /**
  1022. * @fn amap_get_au_stat
  1023. * @brief report AUs status depending on mode
  1024. * @return positive on success, 0 otherwise
  1025. * @param sbi super block info
  1026. * @param mode TOTAL, CLEAN and FULL
  1027. */
  1028. u32 amap_get_au_stat(struct super_block *sb, s32 mode)
  1029. {
  1030. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  1031. if (!amap)
  1032. return 0;
  1033. if (mode == VOL_AU_STAT_TOTAL)
  1034. return amap->n_au;
  1035. else if (mode == VOL_AU_STAT_CLEAN)
  1036. return amap->n_clean_au;
  1037. else if (mode == VOL_AU_STAT_FULL)
  1038. return amap->n_full_au;
  1039. return 0;
  1040. }