extent_map.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. #include <linux/fiemap.h>
  28. #define MLOG_MASK_PREFIX ML_EXTENT_MAP
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "dlmglue.h"
  33. #include "extent_map.h"
  34. #include "inode.h"
  35. #include "super.h"
  36. #include "buffer_head_io.h"
  37. /*
  38. * The extent caching implementation is intentionally trivial.
  39. *
  40. * We only cache a small number of extents stored directly on the
  41. * inode, so linear order operations are acceptable. If we ever want
  42. * to increase the size of the extent map, then these algorithms must
  43. * get smarter.
  44. */
  45. void ocfs2_extent_map_init(struct inode *inode)
  46. {
  47. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  48. oi->ip_extent_map.em_num_items = 0;
  49. INIT_LIST_HEAD(&oi->ip_extent_map.em_list);
  50. }
  51. static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
  52. unsigned int cpos,
  53. struct ocfs2_extent_map_item **ret_emi)
  54. {
  55. unsigned int range;
  56. struct ocfs2_extent_map_item *emi;
  57. *ret_emi = NULL;
  58. list_for_each_entry(emi, &em->em_list, ei_list) {
  59. range = emi->ei_cpos + emi->ei_clusters;
  60. if (cpos >= emi->ei_cpos && cpos < range) {
  61. list_move(&emi->ei_list, &em->em_list);
  62. *ret_emi = emi;
  63. break;
  64. }
  65. }
  66. }
  67. static int ocfs2_extent_map_lookup(struct inode *inode, unsigned int cpos,
  68. unsigned int *phys, unsigned int *len,
  69. unsigned int *flags)
  70. {
  71. unsigned int coff;
  72. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  73. struct ocfs2_extent_map_item *emi;
  74. spin_lock(&oi->ip_lock);
  75. __ocfs2_extent_map_lookup(&oi->ip_extent_map, cpos, &emi);
  76. if (emi) {
  77. coff = cpos - emi->ei_cpos;
  78. *phys = emi->ei_phys + coff;
  79. if (len)
  80. *len = emi->ei_clusters - coff;
  81. if (flags)
  82. *flags = emi->ei_flags;
  83. }
  84. spin_unlock(&oi->ip_lock);
  85. if (emi == NULL)
  86. return -ENOENT;
  87. return 0;
  88. }
  89. /*
  90. * Forget about all clusters equal to or greater than cpos.
  91. */
  92. void ocfs2_extent_map_trunc(struct inode *inode, unsigned int cpos)
  93. {
  94. struct ocfs2_extent_map_item *emi, *n;
  95. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  96. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  97. LIST_HEAD(tmp_list);
  98. unsigned int range;
  99. spin_lock(&oi->ip_lock);
  100. list_for_each_entry_safe(emi, n, &em->em_list, 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_entry_safe(emi, n, &tmp_list, ei_list) {
  116. list_del(&emi->ei_list);
  117. kfree(emi);
  118. }
  119. }
  120. /*
  121. * Is any part of emi2 contained within emi1
  122. */
  123. static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
  124. struct ocfs2_extent_map_item *emi2)
  125. {
  126. unsigned int range1, range2;
  127. /*
  128. * Check if logical start of emi2 is inside emi1
  129. */
  130. range1 = emi1->ei_cpos + emi1->ei_clusters;
  131. if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
  132. return 1;
  133. /*
  134. * Check if logical end of emi2 is inside emi1
  135. */
  136. range2 = emi2->ei_cpos + emi2->ei_clusters;
  137. if (range2 > emi1->ei_cpos && range2 <= range1)
  138. return 1;
  139. return 0;
  140. }
  141. static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
  142. struct ocfs2_extent_map_item *src)
  143. {
  144. dest->ei_cpos = src->ei_cpos;
  145. dest->ei_phys = src->ei_phys;
  146. dest->ei_clusters = src->ei_clusters;
  147. dest->ei_flags = src->ei_flags;
  148. }
  149. /*
  150. * Try to merge emi with ins. Returns 1 if merge succeeds, zero
  151. * otherwise.
  152. */
  153. static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
  154. struct ocfs2_extent_map_item *ins)
  155. {
  156. /*
  157. * Handle contiguousness
  158. */
  159. if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
  160. ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
  161. ins->ei_flags == emi->ei_flags) {
  162. emi->ei_clusters += ins->ei_clusters;
  163. return 1;
  164. } else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
  165. (ins->ei_cpos + ins->ei_clusters) == emi->ei_phys &&
  166. ins->ei_flags == emi->ei_flags) {
  167. emi->ei_phys = ins->ei_phys;
  168. emi->ei_cpos = ins->ei_cpos;
  169. emi->ei_clusters += ins->ei_clusters;
  170. return 1;
  171. }
  172. /*
  173. * Overlapping extents - this shouldn't happen unless we've
  174. * split an extent to change it's flags. That is exceedingly
  175. * rare, so there's no sense in trying to optimize it yet.
  176. */
  177. if (ocfs2_ei_is_contained(emi, ins) ||
  178. ocfs2_ei_is_contained(ins, emi)) {
  179. ocfs2_copy_emi_fields(emi, ins);
  180. return 1;
  181. }
  182. /* No merge was possible. */
  183. return 0;
  184. }
  185. /*
  186. * In order to reduce complexity on the caller, this insert function
  187. * is intentionally liberal in what it will accept.
  188. *
  189. * The only rule is that the truncate call *must* be used whenever
  190. * records have been deleted. This avoids inserting overlapping
  191. * records with different physical mappings.
  192. */
  193. void ocfs2_extent_map_insert_rec(struct inode *inode,
  194. struct ocfs2_extent_rec *rec)
  195. {
  196. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  197. struct ocfs2_extent_map *em = &oi->ip_extent_map;
  198. struct ocfs2_extent_map_item *emi, *new_emi = NULL;
  199. struct ocfs2_extent_map_item ins;
  200. ins.ei_cpos = le32_to_cpu(rec->e_cpos);
  201. ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
  202. le64_to_cpu(rec->e_blkno));
  203. ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
  204. ins.ei_flags = rec->e_flags;
  205. search:
  206. spin_lock(&oi->ip_lock);
  207. list_for_each_entry(emi, &em->em_list, ei_list) {
  208. if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
  209. list_move(&emi->ei_list, &em->em_list);
  210. spin_unlock(&oi->ip_lock);
  211. goto out;
  212. }
  213. }
  214. /*
  215. * No item could be merged.
  216. *
  217. * Either allocate and add a new item, or overwrite the last recently
  218. * inserted.
  219. */
  220. if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
  221. if (new_emi == NULL) {
  222. spin_unlock(&oi->ip_lock);
  223. new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
  224. if (new_emi == NULL)
  225. goto out;
  226. goto search;
  227. }
  228. ocfs2_copy_emi_fields(new_emi, &ins);
  229. list_add(&new_emi->ei_list, &em->em_list);
  230. em->em_num_items++;
  231. new_emi = NULL;
  232. } else {
  233. BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
  234. emi = list_entry(em->em_list.prev,
  235. struct ocfs2_extent_map_item, ei_list);
  236. list_move(&emi->ei_list, &em->em_list);
  237. ocfs2_copy_emi_fields(emi, &ins);
  238. }
  239. spin_unlock(&oi->ip_lock);
  240. out:
  241. if (new_emi)
  242. kfree(new_emi);
  243. }
  244. static int ocfs2_last_eb_is_empty(struct inode *inode,
  245. struct ocfs2_dinode *di)
  246. {
  247. int ret, next_free;
  248. u64 last_eb_blk = le64_to_cpu(di->i_last_eb_blk);
  249. struct buffer_head *eb_bh = NULL;
  250. struct ocfs2_extent_block *eb;
  251. struct ocfs2_extent_list *el;
  252. ret = ocfs2_read_extent_block(INODE_CACHE(inode), last_eb_blk, &eb_bh);
  253. if (ret) {
  254. mlog_errno(ret);
  255. goto out;
  256. }
  257. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  258. el = &eb->h_list;
  259. if (el->l_tree_depth) {
  260. ocfs2_error(inode->i_sb,
  261. "Inode %lu has non zero tree depth in "
  262. "leaf block %llu\n", inode->i_ino,
  263. (unsigned long long)eb_bh->b_blocknr);
  264. ret = -EROFS;
  265. goto out;
  266. }
  267. next_free = le16_to_cpu(el->l_next_free_rec);
  268. if (next_free == 0 ||
  269. (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0])))
  270. ret = 1;
  271. out:
  272. brelse(eb_bh);
  273. return ret;
  274. }
  275. /*
  276. * Return the 1st index within el which contains an extent start
  277. * larger than v_cluster.
  278. */
  279. static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
  280. u32 v_cluster)
  281. {
  282. int i;
  283. struct ocfs2_extent_rec *rec;
  284. for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  285. rec = &el->l_recs[i];
  286. if (v_cluster < le32_to_cpu(rec->e_cpos))
  287. break;
  288. }
  289. return i;
  290. }
  291. /*
  292. * Figure out the size of a hole which starts at v_cluster within the given
  293. * extent list.
  294. *
  295. * If there is no more allocation past v_cluster, we return the maximum
  296. * cluster size minus v_cluster.
  297. *
  298. * If we have in-inode extents, then el points to the dinode list and
  299. * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
  300. * containing el.
  301. */
  302. int ocfs2_figure_hole_clusters(struct ocfs2_caching_info *ci,
  303. struct ocfs2_extent_list *el,
  304. struct buffer_head *eb_bh,
  305. u32 v_cluster,
  306. u32 *num_clusters)
  307. {
  308. int ret, i;
  309. struct buffer_head *next_eb_bh = NULL;
  310. struct ocfs2_extent_block *eb, *next_eb;
  311. i = ocfs2_search_for_hole_index(el, v_cluster);
  312. if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
  313. eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  314. /*
  315. * Check the next leaf for any extents.
  316. */
  317. if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
  318. goto no_more_extents;
  319. ret = ocfs2_read_extent_block(ci,
  320. le64_to_cpu(eb->h_next_leaf_blk),
  321. &next_eb_bh);
  322. if (ret) {
  323. mlog_errno(ret);
  324. goto out;
  325. }
  326. next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
  327. el = &next_eb->h_list;
  328. i = ocfs2_search_for_hole_index(el, v_cluster);
  329. }
  330. no_more_extents:
  331. if (i == le16_to_cpu(el->l_next_free_rec)) {
  332. /*
  333. * We're at the end of our existing allocation. Just
  334. * return the maximum number of clusters we could
  335. * possibly allocate.
  336. */
  337. *num_clusters = UINT_MAX - v_cluster;
  338. } else {
  339. *num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
  340. }
  341. ret = 0;
  342. out:
  343. brelse(next_eb_bh);
  344. return ret;
  345. }
  346. static int ocfs2_get_clusters_nocache(struct inode *inode,
  347. struct buffer_head *di_bh,
  348. u32 v_cluster, unsigned int *hole_len,
  349. struct ocfs2_extent_rec *ret_rec,
  350. unsigned int *is_last)
  351. {
  352. int i, ret, tree_height, len;
  353. struct ocfs2_dinode *di;
  354. struct ocfs2_extent_block *uninitialized_var(eb);
  355. struct ocfs2_extent_list *el;
  356. struct ocfs2_extent_rec *rec;
  357. struct buffer_head *eb_bh = NULL;
  358. memset(ret_rec, 0, sizeof(*ret_rec));
  359. if (is_last)
  360. *is_last = 0;
  361. di = (struct ocfs2_dinode *) di_bh->b_data;
  362. el = &di->id2.i_list;
  363. tree_height = le16_to_cpu(el->l_tree_depth);
  364. if (tree_height > 0) {
  365. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
  366. &eb_bh);
  367. if (ret) {
  368. mlog_errno(ret);
  369. goto out;
  370. }
  371. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  372. el = &eb->h_list;
  373. if (el->l_tree_depth) {
  374. ocfs2_error(inode->i_sb,
  375. "Inode %lu has non zero tree depth in "
  376. "leaf block %llu\n", inode->i_ino,
  377. (unsigned long long)eb_bh->b_blocknr);
  378. ret = -EROFS;
  379. goto out;
  380. }
  381. }
  382. i = ocfs2_search_extent_list(el, v_cluster);
  383. if (i == -1) {
  384. /*
  385. * Holes can be larger than the maximum size of an
  386. * extent, so we return their lengths in a seperate
  387. * field.
  388. */
  389. if (hole_len) {
  390. ret = ocfs2_figure_hole_clusters(INODE_CACHE(inode),
  391. el, eb_bh,
  392. v_cluster, &len);
  393. if (ret) {
  394. mlog_errno(ret);
  395. goto out;
  396. }
  397. *hole_len = len;
  398. }
  399. goto out_hole;
  400. }
  401. rec = &el->l_recs[i];
  402. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  403. if (!rec->e_blkno) {
  404. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  405. "record (%u, %u, 0)", inode->i_ino,
  406. le32_to_cpu(rec->e_cpos),
  407. ocfs2_rec_clusters(el, rec));
  408. ret = -EROFS;
  409. goto out;
  410. }
  411. *ret_rec = *rec;
  412. /*
  413. * Checking for last extent is potentially expensive - we
  414. * might have to look at the next leaf over to see if it's
  415. * empty.
  416. *
  417. * The first two checks are to see whether the caller even
  418. * cares for this information, and if the extent is at least
  419. * the last in it's list.
  420. *
  421. * If those hold true, then the extent is last if any of the
  422. * additional conditions hold true:
  423. * - Extent list is in-inode
  424. * - Extent list is right-most
  425. * - Extent list is 2nd to rightmost, with empty right-most
  426. */
  427. if (is_last) {
  428. if (i == (le16_to_cpu(el->l_next_free_rec) - 1)) {
  429. if (tree_height == 0)
  430. *is_last = 1;
  431. else if (eb->h_blkno == di->i_last_eb_blk)
  432. *is_last = 1;
  433. else if (eb->h_next_leaf_blk == di->i_last_eb_blk) {
  434. ret = ocfs2_last_eb_is_empty(inode, di);
  435. if (ret < 0) {
  436. mlog_errno(ret);
  437. goto out;
  438. }
  439. if (ret == 1)
  440. *is_last = 1;
  441. }
  442. }
  443. }
  444. out_hole:
  445. ret = 0;
  446. out:
  447. brelse(eb_bh);
  448. return ret;
  449. }
  450. static void ocfs2_relative_extent_offsets(struct super_block *sb,
  451. u32 v_cluster,
  452. struct ocfs2_extent_rec *rec,
  453. u32 *p_cluster, u32 *num_clusters)
  454. {
  455. u32 coff = v_cluster - le32_to_cpu(rec->e_cpos);
  456. *p_cluster = ocfs2_blocks_to_clusters(sb, le64_to_cpu(rec->e_blkno));
  457. *p_cluster = *p_cluster + coff;
  458. if (num_clusters)
  459. *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
  460. }
  461. int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
  462. u32 *p_cluster, u32 *num_clusters,
  463. struct ocfs2_extent_list *el,
  464. unsigned int *extent_flags)
  465. {
  466. int ret = 0, i;
  467. struct buffer_head *eb_bh = NULL;
  468. struct ocfs2_extent_block *eb;
  469. struct ocfs2_extent_rec *rec;
  470. u32 coff;
  471. if (el->l_tree_depth) {
  472. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
  473. &eb_bh);
  474. if (ret) {
  475. mlog_errno(ret);
  476. goto out;
  477. }
  478. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  479. el = &eb->h_list;
  480. if (el->l_tree_depth) {
  481. ocfs2_error(inode->i_sb,
  482. "Inode %lu has non zero tree depth in "
  483. "xattr leaf block %llu\n", inode->i_ino,
  484. (unsigned long long)eb_bh->b_blocknr);
  485. ret = -EROFS;
  486. goto out;
  487. }
  488. }
  489. i = ocfs2_search_extent_list(el, v_cluster);
  490. if (i == -1) {
  491. ret = -EROFS;
  492. mlog_errno(ret);
  493. goto out;
  494. } else {
  495. rec = &el->l_recs[i];
  496. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  497. if (!rec->e_blkno) {
  498. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  499. "record (%u, %u, 0) in xattr", inode->i_ino,
  500. le32_to_cpu(rec->e_cpos),
  501. ocfs2_rec_clusters(el, rec));
  502. ret = -EROFS;
  503. goto out;
  504. }
  505. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  506. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  507. le64_to_cpu(rec->e_blkno));
  508. *p_cluster = *p_cluster + coff;
  509. if (num_clusters)
  510. *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
  511. if (extent_flags)
  512. *extent_flags = rec->e_flags;
  513. }
  514. out:
  515. if (eb_bh)
  516. brelse(eb_bh);
  517. return ret;
  518. }
  519. int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  520. u32 *p_cluster, u32 *num_clusters,
  521. unsigned int *extent_flags)
  522. {
  523. int ret;
  524. unsigned int uninitialized_var(hole_len), flags = 0;
  525. struct buffer_head *di_bh = NULL;
  526. struct ocfs2_extent_rec rec;
  527. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  528. ret = -ERANGE;
  529. mlog_errno(ret);
  530. goto out;
  531. }
  532. ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
  533. num_clusters, extent_flags);
  534. if (ret == 0)
  535. goto out;
  536. ret = ocfs2_read_inode_block(inode, &di_bh);
  537. if (ret) {
  538. mlog_errno(ret);
  539. goto out;
  540. }
  541. ret = ocfs2_get_clusters_nocache(inode, di_bh, v_cluster, &hole_len,
  542. &rec, NULL);
  543. if (ret) {
  544. mlog_errno(ret);
  545. goto out;
  546. }
  547. if (rec.e_blkno == 0ULL) {
  548. /*
  549. * A hole was found. Return some canned values that
  550. * callers can key on. If asked for, num_clusters will
  551. * be populated with the size of the hole.
  552. */
  553. *p_cluster = 0;
  554. if (num_clusters) {
  555. *num_clusters = hole_len;
  556. }
  557. } else {
  558. ocfs2_relative_extent_offsets(inode->i_sb, v_cluster, &rec,
  559. p_cluster, num_clusters);
  560. flags = rec.e_flags;
  561. ocfs2_extent_map_insert_rec(inode, &rec);
  562. }
  563. if (extent_flags)
  564. *extent_flags = flags;
  565. out:
  566. brelse(di_bh);
  567. return ret;
  568. }
  569. /*
  570. * This expects alloc_sem to be held. The allocation cannot change at
  571. * all while the map is in the process of being updated.
  572. */
  573. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  574. u64 *ret_count, unsigned int *extent_flags)
  575. {
  576. int ret;
  577. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  578. u32 cpos, num_clusters, p_cluster;
  579. u64 boff = 0;
  580. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  581. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
  582. extent_flags);
  583. if (ret) {
  584. mlog_errno(ret);
  585. goto out;
  586. }
  587. /*
  588. * p_cluster == 0 indicates a hole.
  589. */
  590. if (p_cluster) {
  591. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  592. boff += (v_blkno & (u64)(bpc - 1));
  593. }
  594. *p_blkno = boff;
  595. if (ret_count) {
  596. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  597. *ret_count -= v_blkno & (u64)(bpc - 1);
  598. }
  599. out:
  600. return ret;
  601. }
  602. static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
  603. struct fiemap_extent_info *fieinfo,
  604. u64 map_start)
  605. {
  606. int ret;
  607. unsigned int id_count;
  608. struct ocfs2_dinode *di;
  609. u64 phys;
  610. u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
  611. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  612. di = (struct ocfs2_dinode *)di_bh->b_data;
  613. id_count = le16_to_cpu(di->id2.i_data.id_count);
  614. if (map_start < id_count) {
  615. phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
  616. phys += offsetof(struct ocfs2_dinode, id2.i_data.id_data);
  617. ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
  618. flags);
  619. if (ret < 0)
  620. return ret;
  621. }
  622. return 0;
  623. }
  624. #define OCFS2_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  625. int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  626. u64 map_start, u64 map_len)
  627. {
  628. int ret, is_last;
  629. u32 mapping_end, cpos;
  630. unsigned int hole_size;
  631. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  632. u64 len_bytes, phys_bytes, virt_bytes;
  633. struct buffer_head *di_bh = NULL;
  634. struct ocfs2_extent_rec rec;
  635. ret = fiemap_check_flags(fieinfo, OCFS2_FIEMAP_FLAGS);
  636. if (ret)
  637. return ret;
  638. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  639. if (ret) {
  640. mlog_errno(ret);
  641. goto out;
  642. }
  643. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  644. /*
  645. * Handle inline-data separately.
  646. */
  647. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  648. ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
  649. goto out_unlock;
  650. }
  651. cpos = map_start >> osb->s_clustersize_bits;
  652. mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  653. map_start + map_len);
  654. mapping_end -= cpos;
  655. is_last = 0;
  656. while (cpos < mapping_end && !is_last) {
  657. u32 fe_flags;
  658. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  659. &hole_size, &rec, &is_last);
  660. if (ret) {
  661. mlog_errno(ret);
  662. goto out;
  663. }
  664. if (rec.e_blkno == 0ULL) {
  665. cpos += hole_size;
  666. continue;
  667. }
  668. fe_flags = 0;
  669. if (rec.e_flags & OCFS2_EXT_UNWRITTEN)
  670. fe_flags |= FIEMAP_EXTENT_UNWRITTEN;
  671. if (is_last)
  672. fe_flags |= FIEMAP_EXTENT_LAST;
  673. len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
  674. phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
  675. virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
  676. ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
  677. len_bytes, fe_flags);
  678. if (ret)
  679. break;
  680. cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
  681. }
  682. if (ret > 0)
  683. ret = 0;
  684. out_unlock:
  685. brelse(di_bh);
  686. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  687. ocfs2_inode_unlock(inode, 0);
  688. out:
  689. return ret;
  690. }
  691. int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
  692. struct buffer_head *bhs[], int flags,
  693. int (*validate)(struct super_block *sb,
  694. struct buffer_head *bh))
  695. {
  696. int rc = 0;
  697. u64 p_block, p_count;
  698. int i, count, done = 0;
  699. mlog_entry("(inode = %p, v_block = %llu, nr = %d, bhs = %p, "
  700. "flags = %x, validate = %p)\n",
  701. inode, (unsigned long long)v_block, nr, bhs, flags,
  702. validate);
  703. if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
  704. i_size_read(inode)) {
  705. BUG_ON(!(flags & OCFS2_BH_READAHEAD));
  706. goto out;
  707. }
  708. while (done < nr) {
  709. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  710. rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
  711. &p_block, &p_count, NULL);
  712. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  713. if (rc) {
  714. mlog_errno(rc);
  715. break;
  716. }
  717. if (!p_block) {
  718. rc = -EIO;
  719. mlog(ML_ERROR,
  720. "Inode #%llu contains a hole at offset %llu\n",
  721. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  722. (unsigned long long)(v_block + done) <<
  723. inode->i_sb->s_blocksize_bits);
  724. break;
  725. }
  726. count = nr - done;
  727. if (p_count < count)
  728. count = p_count;
  729. /*
  730. * If the caller passed us bhs, they should have come
  731. * from a previous readahead call to this function. Thus,
  732. * they should have the right b_blocknr.
  733. */
  734. for (i = 0; i < count; i++) {
  735. if (!bhs[done + i])
  736. continue;
  737. BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
  738. }
  739. rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
  740. bhs + done, flags, validate);
  741. if (rc) {
  742. mlog_errno(rc);
  743. break;
  744. }
  745. done += count;
  746. }
  747. out:
  748. mlog_exit(rc);
  749. return rc;
  750. }