extent_map.c 24 KB

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