extent_map.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * extent_map.c
  5. *
  6. * Block/Cluster mapping functions
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License, version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program; if not, write to the
  21. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 021110-1307, USA.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #define MLOG_MASK_PREFIX ML_EXTENT_MAP
  28. #include <cluster/masklog.h>
  29. #include "ocfs2.h"
  30. #include "alloc.h"
  31. #include "extent_map.h"
  32. #include "inode.h"
  33. #include "super.h"
  34. #include "buffer_head_io.h"
  35. /*
  36. * The extent caching implementation is intentionally trivial.
  37. *
  38. * We only cache a small number of extents stored directly on the
  39. * inode, so linear order operations are acceptable. If we ever want
  40. * to increase the size of the extent map, then these algorithms must
  41. * get smarter.
  42. */
  43. void ocfs2_extent_map_init(struct inode *inode)
  44. {
  45. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  46. oi->ip_extent_map.em_num_items = 0;
  47. INIT_LIST_HEAD(&oi->ip_extent_map.em_list);
  48. }
  49. static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
  50. unsigned int cpos,
  51. struct ocfs2_extent_map_item **ret_emi)
  52. {
  53. unsigned int range;
  54. struct ocfs2_extent_map_item *emi;
  55. *ret_emi = NULL;
  56. list_for_each_entry(emi, &em->em_list, ei_list) {
  57. range = emi->ei_cpos + emi->ei_clusters;
  58. if (cpos >= emi->ei_cpos && cpos < range) {
  59. list_move(&emi->ei_list, &em->em_list);
  60. *ret_emi = emi;
  61. break;
  62. }
  63. }
  64. }
  65. static int ocfs2_extent_map_lookup(struct inode *inode, unsigned int cpos,
  66. unsigned int *phys, unsigned int *len,
  67. unsigned int *flags)
  68. {
  69. unsigned int coff;
  70. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  71. struct ocfs2_extent_map_item *emi;
  72. spin_lock(&oi->ip_lock);
  73. __ocfs2_extent_map_lookup(&oi->ip_extent_map, cpos, &emi);
  74. if (emi) {
  75. coff = cpos - emi->ei_cpos;
  76. *phys = emi->ei_phys + coff;
  77. if (len)
  78. *len = emi->ei_clusters - coff;
  79. if (flags)
  80. *flags = emi->ei_flags;
  81. }
  82. spin_unlock(&oi->ip_lock);
  83. if (emi == NULL)
  84. return -ENOENT;
  85. return 0;
  86. }
  87. /*
  88. * Forget about all clusters equal to or greater than cpos.
  89. */
  90. void ocfs2_extent_map_trunc(struct inode *inode, unsigned int cpos)
  91. {
  92. struct list_head *p, *n;
  93. struct ocfs2_extent_map_item *emi;
  94. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  95. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  96. LIST_HEAD(tmp_list);
  97. unsigned int range;
  98. spin_lock(&oi->ip_lock);
  99. list_for_each_safe(p, n, &em->em_list) {
  100. emi = list_entry(p, struct ocfs2_extent_map_item, ei_list);
  101. if (emi->ei_cpos >= cpos) {
  102. /* Full truncate of this record. */
  103. list_move(&emi->ei_list, &tmp_list);
  104. BUG_ON(em->em_num_items == 0);
  105. em->em_num_items--;
  106. continue;
  107. }
  108. range = emi->ei_cpos + emi->ei_clusters;
  109. if (range > cpos) {
  110. /* Partial truncate */
  111. emi->ei_clusters = cpos - emi->ei_cpos;
  112. }
  113. }
  114. spin_unlock(&oi->ip_lock);
  115. list_for_each_safe(p, n, &tmp_list) {
  116. emi = list_entry(p, struct ocfs2_extent_map_item, ei_list);
  117. list_del(&emi->ei_list);
  118. kfree(emi);
  119. }
  120. }
  121. /*
  122. * Is any part of emi2 contained within emi1
  123. */
  124. static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
  125. struct ocfs2_extent_map_item *emi2)
  126. {
  127. unsigned int range1, range2;
  128. /*
  129. * Check if logical start of emi2 is inside emi1
  130. */
  131. range1 = emi1->ei_cpos + emi1->ei_clusters;
  132. if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
  133. return 1;
  134. /*
  135. * Check if logical end of emi2 is inside emi1
  136. */
  137. range2 = emi2->ei_cpos + emi2->ei_clusters;
  138. if (range2 > emi1->ei_cpos && range2 <= range1)
  139. return 1;
  140. return 0;
  141. }
  142. static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
  143. struct ocfs2_extent_map_item *src)
  144. {
  145. dest->ei_cpos = src->ei_cpos;
  146. dest->ei_phys = src->ei_phys;
  147. dest->ei_clusters = src->ei_clusters;
  148. dest->ei_flags = src->ei_flags;
  149. }
  150. /*
  151. * Try to merge emi with ins. Returns 1 if merge succeeds, zero
  152. * otherwise.
  153. */
  154. static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
  155. struct ocfs2_extent_map_item *ins)
  156. {
  157. /*
  158. * Handle contiguousness
  159. */
  160. if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
  161. ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
  162. ins->ei_flags == emi->ei_flags) {
  163. emi->ei_clusters += ins->ei_clusters;
  164. return 1;
  165. } else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
  166. (ins->ei_cpos + ins->ei_clusters) == emi->ei_phys &&
  167. ins->ei_flags == emi->ei_flags) {
  168. emi->ei_phys = ins->ei_phys;
  169. emi->ei_cpos = ins->ei_cpos;
  170. emi->ei_clusters += ins->ei_clusters;
  171. return 1;
  172. }
  173. /*
  174. * Overlapping extents - this shouldn't happen unless we've
  175. * split an extent to change it's flags. That is exceedingly
  176. * rare, so there's no sense in trying to optimize it yet.
  177. */
  178. if (ocfs2_ei_is_contained(emi, ins) ||
  179. ocfs2_ei_is_contained(ins, emi)) {
  180. ocfs2_copy_emi_fields(emi, ins);
  181. return 1;
  182. }
  183. /* No merge was possible. */
  184. return 0;
  185. }
  186. /*
  187. * In order to reduce complexity on the caller, this insert function
  188. * is intentionally liberal in what it will accept.
  189. *
  190. * The only rule is that the truncate call *must* be used whenever
  191. * records have been deleted. This avoids inserting overlapping
  192. * records with different physical mappings.
  193. */
  194. void ocfs2_extent_map_insert_rec(struct inode *inode,
  195. struct ocfs2_extent_rec *rec)
  196. {
  197. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  198. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  199. struct ocfs2_extent_map_item *emi, *new_emi = NULL;
  200. struct ocfs2_extent_map_item ins;
  201. ins.ei_cpos = le32_to_cpu(rec->e_cpos);
  202. ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
  203. le64_to_cpu(rec->e_blkno));
  204. ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
  205. ins.ei_flags = rec->e_flags;
  206. search:
  207. spin_lock(&oi->ip_lock);
  208. list_for_each_entry(emi, &em->em_list, ei_list) {
  209. if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
  210. list_move(&emi->ei_list, &em->em_list);
  211. spin_unlock(&oi->ip_lock);
  212. goto out;
  213. }
  214. }
  215. /*
  216. * No item could be merged.
  217. *
  218. * Either allocate and add a new item, or overwrite the last recently
  219. * inserted.
  220. */
  221. if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
  222. if (new_emi == NULL) {
  223. spin_unlock(&oi->ip_lock);
  224. new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
  225. if (new_emi == NULL)
  226. goto out;
  227. goto search;
  228. }
  229. ocfs2_copy_emi_fields(new_emi, &ins);
  230. list_add(&new_emi->ei_list, &em->em_list);
  231. em->em_num_items++;
  232. new_emi = NULL;
  233. } else {
  234. BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
  235. emi = list_entry(em->em_list.prev,
  236. struct ocfs2_extent_map_item, ei_list);
  237. list_move(&emi->ei_list, &em->em_list);
  238. ocfs2_copy_emi_fields(emi, &ins);
  239. }
  240. spin_unlock(&oi->ip_lock);
  241. out:
  242. if (new_emi)
  243. kfree(new_emi);
  244. }
  245. /*
  246. * Return the 1st index within el which contains an extent start
  247. * larger than v_cluster.
  248. */
  249. static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
  250. u32 v_cluster)
  251. {
  252. int i;
  253. struct ocfs2_extent_rec *rec;
  254. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  255. rec = &el->l_recs[i];
  256. if (v_cluster < le32_to_cpu(rec->e_cpos))
  257. break;
  258. }
  259. return i;
  260. }
  261. /*
  262. * Figure out the size of a hole which starts at v_cluster within the given
  263. * extent list.
  264. *
  265. * If there is no more allocation past v_cluster, we return the maximum
  266. * cluster size minus v_cluster.
  267. *
  268. * If we have in-inode extents, then el points to the dinode list and
  269. * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
  270. * containing el.
  271. */
  272. static int ocfs2_figure_hole_clusters(struct inode *inode,
  273. struct ocfs2_extent_list *el,
  274. struct buffer_head *eb_bh,
  275. u32 v_cluster,
  276. u32 *num_clusters)
  277. {
  278. int ret, i;
  279. struct buffer_head *next_eb_bh = NULL;
  280. struct ocfs2_extent_block *eb, *next_eb;
  281. i = ocfs2_search_for_hole_index(el, v_cluster);
  282. if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
  283. eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  284. /*
  285. * Check the next leaf for any extents.
  286. */
  287. if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
  288. goto no_more_extents;
  289. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  290. le64_to_cpu(eb->h_next_leaf_blk),
  291. &next_eb_bh, OCFS2_BH_CACHED, inode);
  292. if (ret) {
  293. mlog_errno(ret);
  294. goto out;
  295. }
  296. next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
  297. if (!OCFS2_IS_VALID_EXTENT_BLOCK(next_eb)) {
  298. ret = -EROFS;
  299. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, next_eb);
  300. goto out;
  301. }
  302. el = &next_eb->h_list;
  303. i = ocfs2_search_for_hole_index(el, v_cluster);
  304. }
  305. no_more_extents:
  306. if (i == le16_to_cpu(el->l_next_free_rec)) {
  307. /*
  308. * We're at the end of our existing allocation. Just
  309. * return the maximum number of clusters we could
  310. * possibly allocate.
  311. */
  312. *num_clusters = UINT_MAX - v_cluster;
  313. } else {
  314. *num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
  315. }
  316. ret = 0;
  317. out:
  318. brelse(next_eb_bh);
  319. return ret;
  320. }
  321. /*
  322. * Return the index of the extent record which contains cluster #v_cluster.
  323. * -1 is returned if it was not found.
  324. *
  325. * Should work fine on interior and exterior nodes.
  326. */
  327. static int ocfs2_search_extent_list(struct ocfs2_extent_list *el,
  328. u32 v_cluster)
  329. {
  330. int ret = -1;
  331. int i;
  332. struct ocfs2_extent_rec *rec;
  333. u32 rec_end, rec_start, clusters;
  334. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  335. rec = &el->l_recs[i];
  336. rec_start = le32_to_cpu(rec->e_cpos);
  337. clusters = ocfs2_rec_clusters(el, rec);
  338. rec_end = rec_start + clusters;
  339. if (v_cluster >= rec_start && v_cluster < rec_end) {
  340. ret = i;
  341. break;
  342. }
  343. }
  344. return ret;
  345. }
  346. int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  347. u32 *p_cluster, u32 *num_clusters,
  348. unsigned int *extent_flags)
  349. {
  350. int ret, i;
  351. unsigned int flags = 0;
  352. struct buffer_head *di_bh = NULL;
  353. struct buffer_head *eb_bh = NULL;
  354. struct ocfs2_dinode *di;
  355. struct ocfs2_extent_block *eb;
  356. struct ocfs2_extent_list *el;
  357. struct ocfs2_extent_rec *rec;
  358. u32 coff;
  359. ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
  360. num_clusters, extent_flags);
  361. if (ret == 0)
  362. goto out;
  363. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  364. &di_bh, OCFS2_BH_CACHED, inode);
  365. if (ret) {
  366. mlog_errno(ret);
  367. goto out;
  368. }
  369. di = (struct ocfs2_dinode *) di_bh->b_data;
  370. el = &di->id2.i_list;
  371. if (el->l_tree_depth) {
  372. ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
  373. if (ret) {
  374. mlog_errno(ret);
  375. goto out;
  376. }
  377. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  378. el = &eb->h_list;
  379. if (el->l_tree_depth) {
  380. ocfs2_error(inode->i_sb,
  381. "Inode %lu has non zero tree depth in "
  382. "leaf block %llu\n", inode->i_ino,
  383. (unsigned long long)eb_bh->b_blocknr);
  384. ret = -EROFS;
  385. goto out;
  386. }
  387. }
  388. i = ocfs2_search_extent_list(el, v_cluster);
  389. if (i == -1) {
  390. /*
  391. * A hole was found. Return some canned values that
  392. * callers can key on. If asked for, num_clusters will
  393. * be populated with the size of the hole.
  394. */
  395. *p_cluster = 0;
  396. if (num_clusters) {
  397. ret = ocfs2_figure_hole_clusters(inode, el, eb_bh,
  398. v_cluster,
  399. num_clusters);
  400. if (ret) {
  401. mlog_errno(ret);
  402. goto out;
  403. }
  404. }
  405. } else {
  406. rec = &el->l_recs[i];
  407. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  408. if (!rec->e_blkno) {
  409. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  410. "record (%u, %u, 0)", inode->i_ino,
  411. le32_to_cpu(rec->e_cpos),
  412. ocfs2_rec_clusters(el, rec));
  413. ret = -EROFS;
  414. goto out;
  415. }
  416. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  417. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  418. le64_to_cpu(rec->e_blkno));
  419. *p_cluster = *p_cluster + coff;
  420. if (num_clusters)
  421. *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
  422. flags = rec->e_flags;
  423. ocfs2_extent_map_insert_rec(inode, rec);
  424. }
  425. if (extent_flags)
  426. *extent_flags = flags;
  427. out:
  428. brelse(di_bh);
  429. brelse(eb_bh);
  430. return ret;
  431. }
  432. /*
  433. * This expects alloc_sem to be held. The allocation cannot change at
  434. * all while the map is in the process of being updated.
  435. */
  436. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  437. u64 *ret_count, unsigned int *extent_flags)
  438. {
  439. int ret;
  440. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  441. u32 cpos, num_clusters, p_cluster;
  442. u64 boff = 0;
  443. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  444. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
  445. extent_flags);
  446. if (ret) {
  447. mlog_errno(ret);
  448. goto out;
  449. }
  450. /*
  451. * p_cluster == 0 indicates a hole.
  452. */
  453. if (p_cluster) {
  454. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  455. boff += (v_blkno & (u64)(bpc - 1));
  456. }
  457. *p_blkno = boff;
  458. if (ret_count) {
  459. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  460. *ret_count -= v_blkno & (u64)(bpc - 1);
  461. }
  462. out:
  463. return ret;
  464. }