extent_map.c 24 KB

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