extent_map.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
  597. struct fiemap_extent_info *fieinfo,
  598. u64 map_start)
  599. {
  600. int ret;
  601. unsigned int id_count;
  602. struct ocfs2_dinode *di;
  603. u64 phys;
  604. u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
  605. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  606. di = (struct ocfs2_dinode *)di_bh->b_data;
  607. if (ocfs2_inode_is_fast_symlink(inode))
  608. id_count = ocfs2_fast_symlink_chars(inode->i_sb);
  609. else
  610. id_count = le16_to_cpu(di->id2.i_data.id_count);
  611. if (map_start < id_count) {
  612. phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
  613. if (ocfs2_inode_is_fast_symlink(inode))
  614. phys += offsetof(struct ocfs2_dinode, id2.i_symlink);
  615. else
  616. phys += offsetof(struct ocfs2_dinode,
  617. id2.i_data.id_data);
  618. ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
  619. flags);
  620. if (ret < 0)
  621. return ret;
  622. }
  623. return 0;
  624. }
  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_prep(inode, fieinfo, map_start, &map_len, 0);
  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 and fast symlink separately.
  646. */
  647. if ((OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  648. ocfs2_inode_is_fast_symlink(inode)) {
  649. ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
  650. goto out_unlock;
  651. }
  652. cpos = map_start >> osb->s_clustersize_bits;
  653. mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  654. map_start + map_len);
  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_unlock;
  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 (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  672. fe_flags |= FIEMAP_EXTENT_SHARED;
  673. if (is_last)
  674. fe_flags |= FIEMAP_EXTENT_LAST;
  675. len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
  676. phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
  677. virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
  678. ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
  679. len_bytes, fe_flags);
  680. if (ret)
  681. break;
  682. cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
  683. }
  684. if (ret > 0)
  685. ret = 0;
  686. out_unlock:
  687. brelse(di_bh);
  688. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  689. ocfs2_inode_unlock(inode, 0);
  690. out:
  691. return ret;
  692. }
  693. /* Is IO overwriting allocated blocks? */
  694. int ocfs2_overwrite_io(struct inode *inode, struct buffer_head *di_bh,
  695. u64 map_start, u64 map_len)
  696. {
  697. int ret = 0, is_last;
  698. u32 mapping_end, cpos;
  699. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  700. struct ocfs2_extent_rec rec;
  701. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  702. if (ocfs2_size_fits_inline_data(di_bh, map_start + map_len))
  703. return ret;
  704. else
  705. return -EAGAIN;
  706. }
  707. cpos = map_start >> osb->s_clustersize_bits;
  708. mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  709. map_start + map_len);
  710. is_last = 0;
  711. while (cpos < mapping_end && !is_last) {
  712. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  713. NULL, &rec, &is_last);
  714. if (ret) {
  715. mlog_errno(ret);
  716. goto out;
  717. }
  718. if (rec.e_blkno == 0ULL)
  719. break;
  720. if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  721. break;
  722. cpos = le32_to_cpu(rec.e_cpos) +
  723. le16_to_cpu(rec.e_leaf_clusters);
  724. }
  725. if (cpos < mapping_end)
  726. ret = -EAGAIN;
  727. out:
  728. return ret;
  729. }
  730. int ocfs2_seek_data_hole_offset(struct file *file, loff_t *offset, int whence)
  731. {
  732. struct inode *inode = file->f_mapping->host;
  733. int ret;
  734. unsigned int is_last = 0, is_data = 0;
  735. u16 cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  736. u32 cpos, cend, clen, hole_size;
  737. u64 extoff, extlen;
  738. struct buffer_head *di_bh = NULL;
  739. struct ocfs2_extent_rec rec;
  740. BUG_ON(whence != SEEK_DATA && whence != SEEK_HOLE);
  741. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  742. if (ret) {
  743. mlog_errno(ret);
  744. goto out;
  745. }
  746. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  747. if (*offset >= i_size_read(inode)) {
  748. ret = -ENXIO;
  749. goto out_unlock;
  750. }
  751. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  752. if (whence == SEEK_HOLE)
  753. *offset = i_size_read(inode);
  754. goto out_unlock;
  755. }
  756. clen = 0;
  757. cpos = *offset >> cs_bits;
  758. cend = ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
  759. while (cpos < cend && !is_last) {
  760. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos, &hole_size,
  761. &rec, &is_last);
  762. if (ret) {
  763. mlog_errno(ret);
  764. goto out_unlock;
  765. }
  766. extoff = cpos;
  767. extoff <<= cs_bits;
  768. if (rec.e_blkno == 0ULL) {
  769. clen = hole_size;
  770. is_data = 0;
  771. } else {
  772. clen = le16_to_cpu(rec.e_leaf_clusters) -
  773. (cpos - le32_to_cpu(rec.e_cpos));
  774. is_data = (rec.e_flags & OCFS2_EXT_UNWRITTEN) ? 0 : 1;
  775. }
  776. if ((!is_data && whence == SEEK_HOLE) ||
  777. (is_data && whence == SEEK_DATA)) {
  778. if (extoff > *offset)
  779. *offset = extoff;
  780. goto out_unlock;
  781. }
  782. if (!is_last)
  783. cpos += clen;
  784. }
  785. if (whence == SEEK_HOLE) {
  786. extoff = cpos;
  787. extoff <<= cs_bits;
  788. extlen = clen;
  789. extlen <<= cs_bits;
  790. if ((extoff + extlen) > i_size_read(inode))
  791. extlen = i_size_read(inode) - extoff;
  792. extoff += extlen;
  793. if (extoff > *offset)
  794. *offset = extoff;
  795. goto out_unlock;
  796. }
  797. ret = -ENXIO;
  798. out_unlock:
  799. brelse(di_bh);
  800. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  801. ocfs2_inode_unlock(inode, 0);
  802. out:
  803. return ret;
  804. }
  805. int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
  806. struct buffer_head *bhs[], int flags,
  807. int (*validate)(struct super_block *sb,
  808. struct buffer_head *bh))
  809. {
  810. int rc = 0;
  811. u64 p_block, p_count;
  812. int i, count, done = 0;
  813. trace_ocfs2_read_virt_blocks(
  814. inode, (unsigned long long)v_block, nr, bhs, flags,
  815. validate);
  816. if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
  817. i_size_read(inode)) {
  818. BUG_ON(!(flags & OCFS2_BH_READAHEAD));
  819. goto out;
  820. }
  821. while (done < nr) {
  822. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  823. rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
  824. &p_block, &p_count, NULL);
  825. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  826. if (rc) {
  827. mlog_errno(rc);
  828. break;
  829. }
  830. if (!p_block) {
  831. rc = -EIO;
  832. mlog(ML_ERROR,
  833. "Inode #%llu contains a hole at offset %llu\n",
  834. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  835. (unsigned long long)(v_block + done) <<
  836. inode->i_sb->s_blocksize_bits);
  837. break;
  838. }
  839. count = nr - done;
  840. if (p_count < count)
  841. count = p_count;
  842. /*
  843. * If the caller passed us bhs, they should have come
  844. * from a previous readahead call to this function. Thus,
  845. * they should have the right b_blocknr.
  846. */
  847. for (i = 0; i < count; i++) {
  848. if (!bhs[done + i])
  849. continue;
  850. BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
  851. }
  852. rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
  853. bhs + done, flags, validate);
  854. if (rc) {
  855. mlog_errno(rc);
  856. break;
  857. }
  858. done += count;
  859. }
  860. out:
  861. return rc;
  862. }