extent_map.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 ocfs2_extent_map_item *emi, *n;
  93. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  94. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  95. LIST_HEAD(tmp_list);
  96. unsigned int range;
  97. spin_lock(&oi->ip_lock);
  98. list_for_each_entry_safe(emi, n, &em->em_list, ei_list) {
  99. if (emi->ei_cpos >= cpos) {
  100. /* Full truncate of this record. */
  101. list_move(&emi->ei_list, &tmp_list);
  102. BUG_ON(em->em_num_items == 0);
  103. em->em_num_items--;
  104. continue;
  105. }
  106. range = emi->ei_cpos + emi->ei_clusters;
  107. if (range > cpos) {
  108. /* Partial truncate */
  109. emi->ei_clusters = cpos - emi->ei_cpos;
  110. }
  111. }
  112. spin_unlock(&oi->ip_lock);
  113. list_for_each_entry_safe(emi, n, &tmp_list, ei_list) {
  114. list_del(&emi->ei_list);
  115. kfree(emi);
  116. }
  117. }
  118. /*
  119. * Is any part of emi2 contained within emi1
  120. */
  121. static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
  122. struct ocfs2_extent_map_item *emi2)
  123. {
  124. unsigned int range1, range2;
  125. /*
  126. * Check if logical start of emi2 is inside emi1
  127. */
  128. range1 = emi1->ei_cpos + emi1->ei_clusters;
  129. if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
  130. return 1;
  131. /*
  132. * Check if logical end of emi2 is inside emi1
  133. */
  134. range2 = emi2->ei_cpos + emi2->ei_clusters;
  135. if (range2 > emi1->ei_cpos && range2 <= range1)
  136. return 1;
  137. return 0;
  138. }
  139. static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
  140. struct ocfs2_extent_map_item *src)
  141. {
  142. dest->ei_cpos = src->ei_cpos;
  143. dest->ei_phys = src->ei_phys;
  144. dest->ei_clusters = src->ei_clusters;
  145. dest->ei_flags = src->ei_flags;
  146. }
  147. /*
  148. * Try to merge emi with ins. Returns 1 if merge succeeds, zero
  149. * otherwise.
  150. */
  151. static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
  152. struct ocfs2_extent_map_item *ins)
  153. {
  154. /*
  155. * Handle contiguousness
  156. */
  157. if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
  158. ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
  159. ins->ei_flags == emi->ei_flags) {
  160. emi->ei_clusters += ins->ei_clusters;
  161. return 1;
  162. } else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
  163. (ins->ei_cpos + ins->ei_clusters) == emi->ei_phys &&
  164. ins->ei_flags == emi->ei_flags) {
  165. emi->ei_phys = ins->ei_phys;
  166. emi->ei_cpos = ins->ei_cpos;
  167. emi->ei_clusters += ins->ei_clusters;
  168. return 1;
  169. }
  170. /*
  171. * Overlapping extents - this shouldn't happen unless we've
  172. * split an extent to change it's flags. That is exceedingly
  173. * rare, so there's no sense in trying to optimize it yet.
  174. */
  175. if (ocfs2_ei_is_contained(emi, ins) ||
  176. ocfs2_ei_is_contained(ins, emi)) {
  177. ocfs2_copy_emi_fields(emi, ins);
  178. return 1;
  179. }
  180. /* No merge was possible. */
  181. return 0;
  182. }
  183. /*
  184. * In order to reduce complexity on the caller, this insert function
  185. * is intentionally liberal in what it will accept.
  186. *
  187. * The only rule is that the truncate call *must* be used whenever
  188. * records have been deleted. This avoids inserting overlapping
  189. * records with different physical mappings.
  190. */
  191. void ocfs2_extent_map_insert_rec(struct inode *inode,
  192. struct ocfs2_extent_rec *rec)
  193. {
  194. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  195. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  196. struct ocfs2_extent_map_item *emi, *new_emi = NULL;
  197. struct ocfs2_extent_map_item ins;
  198. ins.ei_cpos = le32_to_cpu(rec->e_cpos);
  199. ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
  200. le64_to_cpu(rec->e_blkno));
  201. ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
  202. ins.ei_flags = rec->e_flags;
  203. search:
  204. spin_lock(&oi->ip_lock);
  205. list_for_each_entry(emi, &em->em_list, ei_list) {
  206. if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
  207. list_move(&emi->ei_list, &em->em_list);
  208. spin_unlock(&oi->ip_lock);
  209. goto out;
  210. }
  211. }
  212. /*
  213. * No item could be merged.
  214. *
  215. * Either allocate and add a new item, or overwrite the last recently
  216. * inserted.
  217. */
  218. if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
  219. if (new_emi == NULL) {
  220. spin_unlock(&oi->ip_lock);
  221. new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
  222. if (new_emi == NULL)
  223. goto out;
  224. goto search;
  225. }
  226. ocfs2_copy_emi_fields(new_emi, &ins);
  227. list_add(&new_emi->ei_list, &em->em_list);
  228. em->em_num_items++;
  229. new_emi = NULL;
  230. } else {
  231. BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
  232. emi = list_entry(em->em_list.prev,
  233. struct ocfs2_extent_map_item, ei_list);
  234. list_move(&emi->ei_list, &em->em_list);
  235. ocfs2_copy_emi_fields(emi, &ins);
  236. }
  237. spin_unlock(&oi->ip_lock);
  238. out:
  239. if (new_emi)
  240. kfree(new_emi);
  241. }
  242. /*
  243. * Return the 1st index within el which contains an extent start
  244. * larger than v_cluster.
  245. */
  246. static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
  247. u32 v_cluster)
  248. {
  249. int i;
  250. struct ocfs2_extent_rec *rec;
  251. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  252. rec = &el->l_recs[i];
  253. if (v_cluster < le32_to_cpu(rec->e_cpos))
  254. break;
  255. }
  256. return i;
  257. }
  258. /*
  259. * Figure out the size of a hole which starts at v_cluster within the given
  260. * extent list.
  261. *
  262. * If there is no more allocation past v_cluster, we return the maximum
  263. * cluster size minus v_cluster.
  264. *
  265. * If we have in-inode extents, then el points to the dinode list and
  266. * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
  267. * containing el.
  268. */
  269. static int ocfs2_figure_hole_clusters(struct inode *inode,
  270. struct ocfs2_extent_list *el,
  271. struct buffer_head *eb_bh,
  272. u32 v_cluster,
  273. u32 *num_clusters)
  274. {
  275. int ret, i;
  276. struct buffer_head *next_eb_bh = NULL;
  277. struct ocfs2_extent_block *eb, *next_eb;
  278. i = ocfs2_search_for_hole_index(el, v_cluster);
  279. if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
  280. eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  281. /*
  282. * Check the next leaf for any extents.
  283. */
  284. if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
  285. goto no_more_extents;
  286. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  287. le64_to_cpu(eb->h_next_leaf_blk),
  288. &next_eb_bh, OCFS2_BH_CACHED, inode);
  289. if (ret) {
  290. mlog_errno(ret);
  291. goto out;
  292. }
  293. next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
  294. if (!OCFS2_IS_VALID_EXTENT_BLOCK(next_eb)) {
  295. ret = -EROFS;
  296. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, next_eb);
  297. goto out;
  298. }
  299. el = &next_eb->h_list;
  300. i = ocfs2_search_for_hole_index(el, v_cluster);
  301. }
  302. no_more_extents:
  303. if (i == le16_to_cpu(el->l_next_free_rec)) {
  304. /*
  305. * We're at the end of our existing allocation. Just
  306. * return the maximum number of clusters we could
  307. * possibly allocate.
  308. */
  309. *num_clusters = UINT_MAX - v_cluster;
  310. } else {
  311. *num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
  312. }
  313. ret = 0;
  314. out:
  315. brelse(next_eb_bh);
  316. return ret;
  317. }
  318. int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  319. u32 *p_cluster, u32 *num_clusters,
  320. unsigned int *extent_flags)
  321. {
  322. int ret, i;
  323. unsigned int flags = 0;
  324. struct buffer_head *di_bh = NULL;
  325. struct buffer_head *eb_bh = NULL;
  326. struct ocfs2_dinode *di;
  327. struct ocfs2_extent_block *eb;
  328. struct ocfs2_extent_list *el;
  329. struct ocfs2_extent_rec *rec;
  330. u32 coff;
  331. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  332. ret = -ERANGE;
  333. mlog_errno(ret);
  334. goto out;
  335. }
  336. ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
  337. num_clusters, extent_flags);
  338. if (ret == 0)
  339. goto out;
  340. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  341. &di_bh, OCFS2_BH_CACHED, inode);
  342. if (ret) {
  343. mlog_errno(ret);
  344. goto out;
  345. }
  346. di = (struct ocfs2_dinode *) di_bh->b_data;
  347. el = &di->id2.i_list;
  348. if (el->l_tree_depth) {
  349. ret = ocfs2_find_leaf(inode, el, v_cluster, &eb_bh);
  350. if (ret) {
  351. mlog_errno(ret);
  352. goto out;
  353. }
  354. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  355. el = &eb->h_list;
  356. if (el->l_tree_depth) {
  357. ocfs2_error(inode->i_sb,
  358. "Inode %lu has non zero tree depth in "
  359. "leaf block %llu\n", inode->i_ino,
  360. (unsigned long long)eb_bh->b_blocknr);
  361. ret = -EROFS;
  362. goto out;
  363. }
  364. }
  365. i = ocfs2_search_extent_list(el, v_cluster);
  366. if (i == -1) {
  367. /*
  368. * A hole was found. Return some canned values that
  369. * callers can key on. If asked for, num_clusters will
  370. * be populated with the size of the hole.
  371. */
  372. *p_cluster = 0;
  373. if (num_clusters) {
  374. ret = ocfs2_figure_hole_clusters(inode, el, eb_bh,
  375. v_cluster,
  376. num_clusters);
  377. if (ret) {
  378. mlog_errno(ret);
  379. goto out;
  380. }
  381. }
  382. } else {
  383. rec = &el->l_recs[i];
  384. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  385. if (!rec->e_blkno) {
  386. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  387. "record (%u, %u, 0)", inode->i_ino,
  388. le32_to_cpu(rec->e_cpos),
  389. ocfs2_rec_clusters(el, rec));
  390. ret = -EROFS;
  391. goto out;
  392. }
  393. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  394. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  395. le64_to_cpu(rec->e_blkno));
  396. *p_cluster = *p_cluster + coff;
  397. if (num_clusters)
  398. *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
  399. flags = rec->e_flags;
  400. ocfs2_extent_map_insert_rec(inode, rec);
  401. }
  402. if (extent_flags)
  403. *extent_flags = flags;
  404. out:
  405. brelse(di_bh);
  406. brelse(eb_bh);
  407. return ret;
  408. }
  409. /*
  410. * This expects alloc_sem to be held. The allocation cannot change at
  411. * all while the map is in the process of being updated.
  412. */
  413. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  414. u64 *ret_count, unsigned int *extent_flags)
  415. {
  416. int ret;
  417. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  418. u32 cpos, num_clusters, p_cluster;
  419. u64 boff = 0;
  420. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  421. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
  422. extent_flags);
  423. if (ret) {
  424. mlog_errno(ret);
  425. goto out;
  426. }
  427. /*
  428. * p_cluster == 0 indicates a hole.
  429. */
  430. if (p_cluster) {
  431. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  432. boff += (v_blkno & (u64)(bpc - 1));
  433. }
  434. *p_blkno = boff;
  435. if (ret_count) {
  436. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  437. *ret_count -= v_blkno & (u64)(bpc - 1);
  438. }
  439. out:
  440. return ret;
  441. }