amap_smart.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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) && amap->n_need_packing >= PACKING_HARDLIMIT) {
  562. /* Compulsory SLC flushing:
  563. * If there was no chance to do best-fit packing
  564. * and the # of AU-aligned allocation exceeds HARD threshold,
  565. * then pick a clean AU and generate a compulsory random I/O.
  566. */
  567. au = amap_pop_cold_au_largest(amap, amap->clusters_per_au);
  568. if (au) {
  569. MMSG("AMAP: packing (cnt: %d) / hard-limit, largest)\n",
  570. amap->n_need_packing);
  571. if (au->free_clusters >= 96) {
  572. *clu_to_skip = au->free_clusters / 2;
  573. MMSG("AMAP: cluster idx re-position\n");
  574. }
  575. amap->n_need_packing = 0;
  576. return au;
  577. }
  578. }
  579. /* Update # of clean AU allocation */
  580. amap->n_need_packing++;
  581. return NULL;
  582. }
  583. /* Pick a target AU:
  584. * This function should be called
  585. * only if there are one or more free clusters in the bdev.
  586. */
  587. TARGET_AU_T *amap_get_target_au(AMAP_T *amap, int dest, int num_to_wb)
  588. {
  589. int loop_count = 0;
  590. retry:
  591. if (++loop_count >= 3) {
  592. /* No space available (or AMAP consistency error)
  593. * This could happen because of the ignored AUs but not likely
  594. * (because the defrag daemon will not work if there is no enough space)
  595. */
  596. BUG_ON(amap->slist_ignored.next == NULL);
  597. return NULL;
  598. }
  599. /* Hot clusters (DIR) */
  600. if (dest == ALLOC_HOT) {
  601. /* Working hot AU exist? */
  602. if (amap->cur_hot.au == NULL || amap->cur_hot.au->free_clusters == 0) {
  603. AU_INFO_T *au;
  604. if (amap->total_fclu_hot == 0) {
  605. /* No more hot AU avaialbe */
  606. dest = ALLOC_COLD;
  607. goto retry;
  608. }
  609. au = amap_find_hot_au_partial(amap);
  610. BUG_ON(au == NULL);
  611. BUG_ON(au->free_clusters <= 0);
  612. amap->cur_hot.au = au;
  613. amap->cur_hot.idx = 0;
  614. amap->cur_hot.clu_to_skip = 0;
  615. }
  616. /* Now allocate on a hot AU */
  617. return &amap->cur_hot;
  618. }
  619. /* Cold allocation:
  620. * If amap->cur_cold.au has one or more free cluster(s),
  621. * then just return amap->cur_cold
  622. */
  623. if ((!amap->cur_cold.au)
  624. || (amap->cur_cold.idx == amap->clusters_per_au)
  625. || (amap->cur_cold.au->free_clusters == 0)) {
  626. AU_INFO_T *au = NULL;
  627. const AU_INFO_T *old_au = amap->cur_cold.au;
  628. int n_clu_to_skip = 0;
  629. if (old_au) {
  630. ASSERT(!IS_AU_WORKING(old_au, amap));
  631. /* must be NOT WORKING AU.
  632. * (only for information gathering)
  633. */
  634. }
  635. /* Next target AU is needed:
  636. * There are 3 possible ALLOC options for cold AU
  637. *
  638. * ALLOC_COLD_ALIGNED: Clean AU first, but heuristic packing is ON
  639. * ALLOC_COLD_PACKING: Packing AU first (usually for defrag)
  640. * ALLOC_COLD_SEQ : Sequential AU allocation (VFAT-like)
  641. */
  642. /* Experimental: Modify allocation destination if needed (ALIGNED => SEQ) */
  643. // dest = amap_update_dest(amap, dest);
  644. if ((dest == ALLOC_COLD_SEQ) && old_au) {
  645. int i_au = old_au->idx + 1;
  646. while (i_au != old_au->idx) {
  647. au = GET_AU(amap, i_au);
  648. if ((au->free_clusters > 0) &&
  649. !IS_AU_HOT(au, amap) &&
  650. !IS_AU_IGNORED(au, amap)) {
  651. MMSG("AMAP: new cold AU(%d) with %d "
  652. "clusters (seq)\n",
  653. au->idx, au->free_clusters);
  654. amap_remove_cold_au(amap, au);
  655. goto ret_new_cold;
  656. }
  657. i_au++;
  658. if (i_au >= amap->n_au)
  659. i_au = 0;
  660. }
  661. // no cold AUs are available => Hot allocation
  662. dest = ALLOC_HOT;
  663. goto retry;
  664. }
  665. /*
  666. * Check if packing is needed
  667. * (ALLOC_COLD_PACKING is treated by this function)
  668. */
  669. au = amap_get_packing_au(amap, dest, num_to_wb, &n_clu_to_skip);
  670. if (au) {
  671. MMSG("AMAP: new cold AU(%d) with %d clusters "
  672. "(packing)\n", au->idx, au->free_clusters);
  673. goto ret_new_cold;
  674. }
  675. /* ALLOC_COLD_ALIGNED */
  676. /* Check if the adjacent AU is clean */
  677. if (old_au && ((old_au->idx + 1) < amap->n_au)) {
  678. au = GET_AU(amap, old_au->idx + 1);
  679. if ((au->free_clusters == amap->clusters_per_au) &&
  680. !IS_AU_HOT(au, amap) &&
  681. !IS_AU_IGNORED(au, amap)) {
  682. MMSG("AMAP: new cold AU(%d) with %d clusters "
  683. "(adjacent)\n", au->idx, au->free_clusters);
  684. amap_remove_cold_au(amap, au);
  685. goto ret_new_cold;
  686. }
  687. }
  688. /* Clean or largest AU */
  689. au = amap_pop_cold_au_largest(amap, 0);
  690. if (!au) {
  691. //ASSERT(amap->total_fclu_hot == (fsi->num_clusters - fsi->used_clusters - 2));
  692. dest = ALLOC_HOT;
  693. goto retry;
  694. }
  695. MMSG("AMAP: New cold AU (%d) with %d clusters\n",
  696. au->idx, au->free_clusters);
  697. ret_new_cold:
  698. SET_AU_WORKING(au);
  699. amap->cur_cold.au = au;
  700. amap->cur_cold.idx = 0;
  701. amap->cur_cold.clu_to_skip = n_clu_to_skip;
  702. }
  703. return &amap->cur_cold;
  704. }
  705. /* Put and update target AU */
  706. void amap_put_target_au(AMAP_T *amap, TARGET_AU_T *cur, unsigned int num_allocated)
  707. {
  708. /* Update AMAP info vars. */
  709. if (num_allocated > 0 &&
  710. (cur->au->free_clusters + num_allocated) == amap->clusters_per_au) {
  711. /* if the target AU was a clean AU before this allocation ... */
  712. amap->n_clean_au--;
  713. }
  714. if (num_allocated > 0 &&
  715. cur->au->free_clusters == 0)
  716. amap->n_full_au++;
  717. if (IS_AU_HOT(cur->au, amap)) {
  718. /* Hot AU */
  719. MMSG("AMAP: hot allocation at AU %d\n", cur->au->idx);
  720. amap->total_fclu_hot -= num_allocated;
  721. /* Intra-AU round-robin */
  722. if (cur->idx >= amap->clusters_per_au)
  723. cur->idx = 0;
  724. /* No more space available */
  725. if (cur->au->free_clusters == 0)
  726. cur->au = NULL;
  727. } else {
  728. /* non-hot AU */
  729. ASSERT(IS_AU_WORKING(cur->au, amap));
  730. if (cur->idx >= amap->clusters_per_au || cur->au->free_clusters == 0) {
  731. /* It should be inserted back to AU MAP */
  732. cur->au->shead.head = NULL; // SET_AU_NOT_WORKING
  733. amap_add_cold_au(amap, cur->au);
  734. // cur->au = NULL; // This value will be used for the next AU selection
  735. cur->idx = amap->clusters_per_au; // AU closing
  736. }
  737. }
  738. }
  739. /* Reposition target->idx for packing (Heuristics):
  740. * Skip (num_to_skip) free clusters in (cur->au)
  741. */
  742. static inline int amap_skip_cluster(struct super_block *sb, TARGET_AU_T *cur, int num_to_skip)
  743. {
  744. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  745. u32 clu, read_clu;
  746. MMSG_VAR(int num_to_skip_orig = num_to_skip);
  747. if (num_to_skip >= cur->au->free_clusters) {
  748. EMSG("AMAP(%s): skip mis-use. amap_566\n", __func__);
  749. return -EIO;
  750. }
  751. clu = CLU_of_i_AU(amap, cur->au->idx, cur->idx);
  752. while (num_to_skip > 0) {
  753. if (clu >= CLUS_BASE) {
  754. /* Cf.
  755. * If AMAP's integrity is okay,
  756. * we don't need to check if (clu < fsi->num_clusters)
  757. */
  758. if (fat_ent_get(sb, clu, &read_clu))
  759. return -EIO;
  760. if (IS_CLUS_FREE(read_clu))
  761. num_to_skip--;
  762. }
  763. // Move clu->idx
  764. clu++;
  765. (cur->idx)++;
  766. if (cur->idx >= amap->clusters_per_au) {
  767. /* End of AU (Not supposed) */
  768. EMSG("AMAP: Skip - End of AU?! (amap_596)\n");
  769. cur->idx = 0;
  770. return -EIO;
  771. }
  772. }
  773. MMSG("AMAP: Skip_clusters (%d skipped => %d, among %d free clus)\n",
  774. num_to_skip_orig, cur->idx, cur->au->free_clusters);
  775. return 0;
  776. }
  777. /* AMAP-based allocation function for FAT32 */
  778. s32 amap_fat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
  779. {
  780. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  781. TARGET_AU_T *cur = NULL;
  782. AU_INFO_T *target_au = NULL; /* Allocation target AU */
  783. s32 ret = -ENOSPC;
  784. u32 last_clu = CLUS_EOF, read_clu;
  785. u32 new_clu, total_cnt;
  786. u32 num_allocated = 0, num_allocated_each = 0;
  787. FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
  788. BUG_ON(!amap);
  789. BUG_ON(IS_CLUS_EOF(fsi->used_clusters));
  790. total_cnt = fsi->num_clusters - CLUS_BASE;
  791. if (unlikely(total_cnt < fsi->used_clusters)) {
  792. sdfat_fs_error_ratelimit(sb,
  793. "AMAP(%s): invalid used clusters(t:%u,u:%u)\n",
  794. __func__, total_cnt, fsi->used_clusters);
  795. return -EIO;
  796. }
  797. if (num_alloc > total_cnt - fsi->used_clusters)
  798. return -ENOSPC;
  799. p_chain->dir = CLUS_EOF;
  800. set_sb_dirty(sb);
  801. // spin_lock(&amap->amap_lock);
  802. retry_alloc:
  803. /* Allocation strategy implemented */
  804. cur = amap_get_target_au(amap, dest, fsi->reserved_clusters);
  805. if (unlikely(!cur)) {
  806. // There is no available AU (only ignored-AU are left)
  807. sdfat_msg(sb, KERN_ERR, "AMAP Allocator: no avaialble AU.");
  808. goto error;
  809. }
  810. /* If there are clusters to skip */
  811. if (cur->clu_to_skip > 0) {
  812. if (amap_skip_cluster(sb, &amap->cur_cold, cur->clu_to_skip)) {
  813. ret = -EIO;
  814. goto error;
  815. }
  816. cur->clu_to_skip = 0;
  817. }
  818. target_au = cur->au;
  819. /*
  820. * cur->au : target AU info pointer
  821. * cur->idx : the intra-cluster idx in the AU to start from
  822. */
  823. BUG_ON(!cur->au);
  824. BUG_ON(!cur->au->free_clusters);
  825. BUG_ON(cur->idx >= amap->clusters_per_au);
  826. num_allocated_each = 0;
  827. new_clu = CLU_of_i_AU(amap, target_au->idx, cur->idx);
  828. do {
  829. /* Allocate at the target AU */
  830. if ((new_clu >= CLUS_BASE) && (new_clu < fsi->num_clusters)) {
  831. if (fat_ent_get(sb, new_clu, &read_clu)) {
  832. // spin_unlock(&amap->amap_lock);
  833. ret = -EIO;
  834. goto error;
  835. }
  836. if (IS_CLUS_FREE(read_clu)) {
  837. BUG_ON(GET_AU(amap, i_AU_of_CLU(amap, new_clu)) != target_au);
  838. /* Free cluster found */
  839. if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
  840. ret = -EIO;
  841. goto error;
  842. }
  843. num_allocated_each++;
  844. if (IS_CLUS_EOF(p_chain->dir)) {
  845. p_chain->dir = new_clu;
  846. } else {
  847. if (fat_ent_set(sb, last_clu, new_clu)) {
  848. ret = -EIO;
  849. goto error;
  850. }
  851. }
  852. last_clu = new_clu;
  853. /* Update au info */
  854. target_au->free_clusters--;
  855. }
  856. }
  857. new_clu++;
  858. (cur->idx)++;
  859. /* End of the AU */
  860. if ((cur->idx >= amap->clusters_per_au) || !(target_au->free_clusters))
  861. break;
  862. } while (num_allocated_each < num_alloc);
  863. /* Update strategy info */
  864. amap_put_target_au(amap, cur, num_allocated_each);
  865. num_allocated += num_allocated_each;
  866. fsi->used_clusters += num_allocated_each;
  867. num_alloc -= num_allocated_each;
  868. if (num_alloc > 0)
  869. goto retry_alloc;
  870. // spin_unlock(&amap->amap_lock);
  871. return 0;
  872. error:
  873. if (num_allocated)
  874. fsi->fs_func->free_cluster(sb, p_chain, 0);
  875. return ret;
  876. }
  877. /* Free cluster for FAT32 (not implemented yet) */
  878. s32 amap_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
  879. {
  880. return -ENOTSUPP;
  881. }
  882. /*
  883. * This is called by fat_free_cluster()
  884. * to update AMAP info.
  885. */
  886. s32 amap_release_cluster(struct super_block *sb, u32 clu)
  887. {
  888. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  889. AU_INFO_T *au;
  890. int i_au;
  891. // spin_lock(&amap->amap_lock);
  892. /* Update AU info */
  893. i_au = i_AU_of_CLU(amap, clu);
  894. BUG_ON(i_au >= amap->n_au);
  895. au = GET_AU(amap, i_au);
  896. if (au->free_clusters >= amap->clusters_per_au) {
  897. sdfat_fs_error(sb, "%s, au->free_clusters(%hd) is "
  898. "greater than or equal to amap->clusters_per_au(%hd)",
  899. __func__, au->free_clusters, amap->clusters_per_au);
  900. return -EIO;
  901. }
  902. if (IS_AU_HOT(au, amap)) {
  903. MMSG("AMAP: Hot cluster freed\n");
  904. au->free_clusters++;
  905. amap->total_fclu_hot++;
  906. } else if (!IS_AU_WORKING(au, amap) && !IS_AU_IGNORED(au, amap)) {
  907. /* Ordinary AU - update AU tree */
  908. // Can be optimized by implementing amap_update_au
  909. amap_remove_cold_au(amap, au);
  910. au->free_clusters++;
  911. amap_add_cold_au(amap, au);
  912. } else
  913. au->free_clusters++;
  914. /* Update AMAP info */
  915. if (au->free_clusters == amap->clusters_per_au)
  916. amap->n_clean_au++;
  917. if (au->free_clusters == 1)
  918. amap->n_full_au--;
  919. // spin_unlock(&amap->amap_lock);
  920. return 0;
  921. }
  922. /*
  923. * Check if the cluster is in a working AU
  924. * The caller should hold sb lock.
  925. * This func. should be used only if smart allocation is on
  926. */
  927. s32 amap_check_working(struct super_block *sb, u32 clu)
  928. {
  929. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  930. AU_INFO_T *au;
  931. BUG_ON(!amap);
  932. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  933. return IS_AU_WORKING(au, amap);
  934. }
  935. /*
  936. * Return the # of free clusters in that AU
  937. */
  938. s32 amap_get_freeclus(struct super_block *sb, u32 clu)
  939. {
  940. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  941. AU_INFO_T *au;
  942. BUG_ON(!amap);
  943. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  944. return (s32)au->free_clusters;
  945. }
  946. /*
  947. * Add the AU containing 'clu' to the ignored AU list.
  948. * The AU will not be used by the allocator.
  949. *
  950. * XXX: Ignored counter needed
  951. */
  952. s32 amap_mark_ignore(struct super_block *sb, u32 clu)
  953. {
  954. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  955. AU_INFO_T *au;
  956. BUG_ON(!amap);
  957. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  958. if (IS_AU_HOT(au, amap)) {
  959. /* Doesn't work with hot AUs */
  960. return -EPERM;
  961. } else if (IS_AU_WORKING(au, amap)) {
  962. return -EBUSY;
  963. }
  964. //BUG_ON(IS_AU_IGNORED(au, amap) && (GET_IGN_CNT(au) == 0));
  965. if (IS_AU_IGNORED(au, amap))
  966. return 0;
  967. amap_remove_cold_au(amap, au);
  968. amap_insert_to_list(au, &amap->slist_ignored);
  969. BUG_ON(!IS_AU_IGNORED(au, amap));
  970. //INC_IGN_CNT(au);
  971. MMSG("AMAP: Mark ignored AU (%d)\n", au->idx);
  972. return 0;
  973. }
  974. /*
  975. * This function could be used only on IGNORED AUs.
  976. * The caller should care whether it's ignored or not before using this func.
  977. */
  978. s32 amap_unmark_ignore(struct super_block *sb, u32 clu)
  979. {
  980. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  981. AU_INFO_T *au;
  982. BUG_ON(!amap);
  983. au = GET_AU(amap, i_AU_of_CLU(amap, clu));
  984. BUG_ON(!IS_AU_IGNORED(au, amap));
  985. // BUG_ON(GET_IGN_CNT(au) == 0);
  986. amap_remove_from_list(au, &amap->slist_ignored);
  987. amap_add_cold_au(amap, au);
  988. BUG_ON(IS_AU_IGNORED(au, amap));
  989. //DEC_IGN_CNT(au);
  990. MMSG("AMAP: Unmark ignored AU (%d)\n", au->idx);
  991. return 0;
  992. }
  993. /*
  994. * Unmark all ignored AU
  995. * This will return # of unmarked AUs
  996. */
  997. s32 amap_unmark_ignore_all(struct super_block *sb)
  998. {
  999. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  1000. struct slist_head *entry;
  1001. AU_INFO_T *au;
  1002. int n = 0;
  1003. BUG_ON(!amap);
  1004. entry = amap->slist_ignored.next;
  1005. while (entry) {
  1006. au = list_entry(entry, AU_INFO_T, shead);
  1007. BUG_ON(au != GET_AU(amap, au->idx));
  1008. BUG_ON(!IS_AU_IGNORED(au, amap));
  1009. //CLEAR_IGN_CNT(au);
  1010. amap_remove_from_list(au, &amap->slist_ignored);
  1011. amap_add_cold_au(amap, au);
  1012. MMSG("AMAP: Unmark ignored AU (%d)\n", au->idx);
  1013. n++;
  1014. entry = amap->slist_ignored.next;
  1015. }
  1016. BUG_ON(amap->slist_ignored.next != NULL);
  1017. MMSG("AMAP: unmark_ignore_all, total %d AUs\n", n);
  1018. return n;
  1019. }
  1020. /**
  1021. * @fn amap_get_au_stat
  1022. * @brief report AUs status depending on mode
  1023. * @return positive on success, 0 otherwise
  1024. * @param sbi super block info
  1025. * @param mode TOTAL, CLEAN and FULL
  1026. */
  1027. u32 amap_get_au_stat(struct super_block *sb, s32 mode)
  1028. {
  1029. AMAP_T *amap = SDFAT_SB(sb)->fsi.amap;
  1030. if (!amap)
  1031. return 0;
  1032. if (mode == VOL_AU_STAT_TOTAL)
  1033. return amap->n_au;
  1034. else if (mode == VOL_AU_STAT_CLEAN)
  1035. return amap->n_clean_au;
  1036. else if (mode == VOL_AU_STAT_FULL)
  1037. return amap->n_full_au;
  1038. return 0;
  1039. }