extent_map.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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_obj(*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. if (le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)) {
  370. ocfs2_error(inode->i_sb,
  371. "Inode %lu has an invalid extent (next_free_rec %u, count %u)\n",
  372. inode->i_ino,
  373. le16_to_cpu(el->l_next_free_rec),
  374. le16_to_cpu(el->l_count));
  375. ret = -EROFS;
  376. goto out;
  377. }
  378. i = ocfs2_search_extent_list(el, v_cluster);
  379. if (i == -1) {
  380. /*
  381. * Holes can be larger than the maximum size of an
  382. * extent, so we return their lengths in a separate
  383. * field.
  384. */
  385. if (hole_len) {
  386. ret = ocfs2_figure_hole_clusters(INODE_CACHE(inode),
  387. el, eb_bh,
  388. v_cluster, &len);
  389. if (ret) {
  390. mlog_errno(ret);
  391. goto out;
  392. }
  393. *hole_len = len;
  394. }
  395. goto out_hole;
  396. }
  397. rec = &el->l_recs[i];
  398. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  399. if (!rec->e_blkno) {
  400. ocfs2_error(inode->i_sb,
  401. "Inode %lu has bad extent record (%u, %u, 0)\n",
  402. inode->i_ino,
  403. le32_to_cpu(rec->e_cpos),
  404. ocfs2_rec_clusters(el, rec));
  405. ret = -EROFS;
  406. goto out;
  407. }
  408. *ret_rec = *rec;
  409. /*
  410. * Checking for last extent is potentially expensive - we
  411. * might have to look at the next leaf over to see if it's
  412. * empty.
  413. *
  414. * The first two checks are to see whether the caller even
  415. * cares for this information, and if the extent is at least
  416. * the last in it's list.
  417. *
  418. * If those hold true, then the extent is last if any of the
  419. * additional conditions hold true:
  420. * - Extent list is in-inode
  421. * - Extent list is right-most
  422. * - Extent list is 2nd to rightmost, with empty right-most
  423. */
  424. if (is_last) {
  425. if (i == (le16_to_cpu(el->l_next_free_rec) - 1)) {
  426. if (tree_height == 0)
  427. *is_last = 1;
  428. else if (eb->h_blkno == di->i_last_eb_blk)
  429. *is_last = 1;
  430. else if (eb->h_next_leaf_blk == di->i_last_eb_blk) {
  431. ret = ocfs2_last_eb_is_empty(inode, di);
  432. if (ret < 0) {
  433. mlog_errno(ret);
  434. goto out;
  435. }
  436. if (ret == 1)
  437. *is_last = 1;
  438. }
  439. }
  440. }
  441. out_hole:
  442. ret = 0;
  443. out:
  444. brelse(eb_bh);
  445. return ret;
  446. }
  447. static void ocfs2_relative_extent_offsets(struct super_block *sb,
  448. u32 v_cluster,
  449. struct ocfs2_extent_rec *rec,
  450. u32 *p_cluster, u32 *num_clusters)
  451. {
  452. u32 coff = v_cluster - le32_to_cpu(rec->e_cpos);
  453. *p_cluster = ocfs2_blocks_to_clusters(sb, le64_to_cpu(rec->e_blkno));
  454. *p_cluster = *p_cluster + coff;
  455. if (num_clusters)
  456. *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
  457. }
  458. int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
  459. u32 *p_cluster, u32 *num_clusters,
  460. struct ocfs2_extent_list *el,
  461. unsigned int *extent_flags)
  462. {
  463. int ret = 0, i;
  464. struct buffer_head *eb_bh = NULL;
  465. struct ocfs2_extent_block *eb;
  466. struct ocfs2_extent_rec *rec;
  467. u32 coff;
  468. if (el->l_tree_depth) {
  469. ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
  470. &eb_bh);
  471. if (ret) {
  472. mlog_errno(ret);
  473. goto out;
  474. }
  475. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  476. el = &eb->h_list;
  477. if (el->l_tree_depth) {
  478. ocfs2_error(inode->i_sb,
  479. "Inode %lu has non zero tree depth in xattr leaf block %llu\n",
  480. inode->i_ino,
  481. (unsigned long long)eb_bh->b_blocknr);
  482. ret = -EROFS;
  483. goto out;
  484. }
  485. }
  486. i = ocfs2_search_extent_list(el, v_cluster);
  487. if (i == -1) {
  488. ret = -EROFS;
  489. mlog_errno(ret);
  490. goto out;
  491. } else {
  492. rec = &el->l_recs[i];
  493. BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  494. if (!rec->e_blkno) {
  495. ocfs2_error(inode->i_sb,
  496. "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
  497. inode->i_ino,
  498. le32_to_cpu(rec->e_cpos),
  499. ocfs2_rec_clusters(el, rec));
  500. ret = -EROFS;
  501. goto out;
  502. }
  503. coff = v_cluster - le32_to_cpu(rec->e_cpos);
  504. *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  505. le64_to_cpu(rec->e_blkno));
  506. *p_cluster = *p_cluster + coff;
  507. if (num_clusters)
  508. *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
  509. if (extent_flags)
  510. *extent_flags = rec->e_flags;
  511. }
  512. out:
  513. brelse(eb_bh);
  514. return ret;
  515. }
  516. int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  517. u32 *p_cluster, u32 *num_clusters,
  518. unsigned int *extent_flags)
  519. {
  520. int ret;
  521. unsigned int hole_len, flags = 0;
  522. struct buffer_head *di_bh = NULL;
  523. struct ocfs2_extent_rec rec;
  524. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  525. ret = -ERANGE;
  526. mlog_errno(ret);
  527. goto out;
  528. }
  529. ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
  530. num_clusters, extent_flags);
  531. if (ret == 0)
  532. goto out;
  533. ret = ocfs2_read_inode_block(inode, &di_bh);
  534. if (ret) {
  535. mlog_errno(ret);
  536. goto out;
  537. }
  538. ret = ocfs2_get_clusters_nocache(inode, di_bh, v_cluster, &hole_len,
  539. &rec, NULL);
  540. if (ret) {
  541. mlog_errno(ret);
  542. goto out;
  543. }
  544. if (rec.e_blkno == 0ULL) {
  545. /*
  546. * A hole was found. Return some canned values that
  547. * callers can key on. If asked for, num_clusters will
  548. * be populated with the size of the hole.
  549. */
  550. *p_cluster = 0;
  551. if (num_clusters) {
  552. *num_clusters = hole_len;
  553. }
  554. } else {
  555. ocfs2_relative_extent_offsets(inode->i_sb, v_cluster, &rec,
  556. p_cluster, num_clusters);
  557. flags = rec.e_flags;
  558. ocfs2_extent_map_insert_rec(inode, &rec);
  559. }
  560. if (extent_flags)
  561. *extent_flags = flags;
  562. out:
  563. brelse(di_bh);
  564. return ret;
  565. }
  566. /*
  567. * This expects alloc_sem to be held. The allocation cannot change at
  568. * all while the map is in the process of being updated.
  569. */
  570. int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
  571. u64 *ret_count, unsigned int *extent_flags)
  572. {
  573. int ret;
  574. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  575. u32 cpos, num_clusters, p_cluster;
  576. u64 boff = 0;
  577. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  578. ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
  579. extent_flags);
  580. if (ret) {
  581. mlog_errno(ret);
  582. goto out;
  583. }
  584. /*
  585. * p_cluster == 0 indicates a hole.
  586. */
  587. if (p_cluster) {
  588. boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  589. boff += (v_blkno & (u64)(bpc - 1));
  590. }
  591. *p_blkno = boff;
  592. if (ret_count) {
  593. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  594. *ret_count -= v_blkno & (u64)(bpc - 1);
  595. }
  596. out:
  597. return ret;
  598. }
  599. /*
  600. * The ocfs2_fiemap_inline() may be a little bit misleading, since
  601. * it not only handles the fiemap for inlined files, but also deals
  602. * with the fast symlink, cause they have no difference for extent
  603. * mapping per se.
  604. *
  605. * Must be called with ip_alloc_sem semaphore held.
  606. */
  607. static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
  608. struct fiemap_extent_info *fieinfo,
  609. u64 map_start)
  610. {
  611. int ret;
  612. unsigned int id_count;
  613. struct ocfs2_dinode *di;
  614. u64 phys;
  615. u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
  616. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  617. lockdep_assert_held_read(&oi->ip_alloc_sem);
  618. di = (struct ocfs2_dinode *)di_bh->b_data;
  619. if (ocfs2_inode_is_fast_symlink(inode))
  620. id_count = ocfs2_fast_symlink_chars(inode->i_sb);
  621. else
  622. id_count = le16_to_cpu(di->id2.i_data.id_count);
  623. if (map_start < id_count) {
  624. phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
  625. if (ocfs2_inode_is_fast_symlink(inode))
  626. phys += offsetof(struct ocfs2_dinode, id2.i_symlink);
  627. else
  628. phys += offsetof(struct ocfs2_dinode,
  629. id2.i_data.id_data);
  630. /* Release the ip_alloc_sem to prevent deadlock on page fault */
  631. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  632. ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
  633. flags);
  634. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  635. if (ret < 0)
  636. return ret;
  637. }
  638. return 0;
  639. }
  640. int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  641. u64 map_start, u64 map_len)
  642. {
  643. int ret, is_last;
  644. u32 mapping_end, cpos;
  645. unsigned int hole_size;
  646. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  647. u64 len_bytes, phys_bytes, virt_bytes;
  648. struct buffer_head *di_bh = NULL;
  649. struct ocfs2_extent_rec rec;
  650. ret = fiemap_prep(inode, fieinfo, map_start, &map_len, 0);
  651. if (ret)
  652. return ret;
  653. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  654. if (ret) {
  655. mlog_errno(ret);
  656. goto out;
  657. }
  658. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  659. /*
  660. * Handle inline-data and fast symlink separately.
  661. */
  662. if ((OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  663. ocfs2_inode_is_fast_symlink(inode)) {
  664. ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
  665. goto out_unlock;
  666. }
  667. cpos = map_start >> osb->s_clustersize_bits;
  668. mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  669. map_start + map_len);
  670. is_last = 0;
  671. while (cpos < mapping_end && !is_last) {
  672. u32 fe_flags;
  673. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  674. &hole_size, &rec, &is_last);
  675. if (ret) {
  676. mlog_errno(ret);
  677. goto out_unlock;
  678. }
  679. if (rec.e_blkno == 0ULL) {
  680. cpos += hole_size;
  681. continue;
  682. }
  683. fe_flags = 0;
  684. if (rec.e_flags & OCFS2_EXT_UNWRITTEN)
  685. fe_flags |= FIEMAP_EXTENT_UNWRITTEN;
  686. if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  687. fe_flags |= FIEMAP_EXTENT_SHARED;
  688. if (is_last)
  689. fe_flags |= FIEMAP_EXTENT_LAST;
  690. len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
  691. phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
  692. virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
  693. /* Release the ip_alloc_sem to prevent deadlock on page fault */
  694. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  695. ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
  696. len_bytes, fe_flags);
  697. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  698. if (ret)
  699. break;
  700. cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
  701. }
  702. if (ret > 0)
  703. ret = 0;
  704. out_unlock:
  705. brelse(di_bh);
  706. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  707. ocfs2_inode_unlock(inode, 0);
  708. out:
  709. return ret;
  710. }
  711. /* Is IO overwriting allocated blocks? */
  712. int ocfs2_overwrite_io(struct inode *inode, struct buffer_head *di_bh,
  713. u64 map_start, u64 map_len)
  714. {
  715. int ret = 0, is_last;
  716. u32 mapping_end, cpos;
  717. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  718. struct ocfs2_extent_rec rec;
  719. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  720. if (ocfs2_size_fits_inline_data(di_bh, map_start + map_len))
  721. return ret;
  722. else
  723. return -EAGAIN;
  724. }
  725. cpos = map_start >> osb->s_clustersize_bits;
  726. mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  727. map_start + map_len);
  728. is_last = 0;
  729. while (cpos < mapping_end && !is_last) {
  730. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  731. NULL, &rec, &is_last);
  732. if (ret) {
  733. mlog_errno(ret);
  734. goto out;
  735. }
  736. if (rec.e_blkno == 0ULL)
  737. break;
  738. if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  739. break;
  740. cpos = le32_to_cpu(rec.e_cpos) +
  741. le16_to_cpu(rec.e_leaf_clusters);
  742. }
  743. if (cpos < mapping_end)
  744. ret = -EAGAIN;
  745. out:
  746. return ret;
  747. }
  748. int ocfs2_seek_data_hole_offset(struct file *file, loff_t *offset, int whence)
  749. {
  750. struct inode *inode = file->f_mapping->host;
  751. int ret;
  752. unsigned int is_last = 0, is_data = 0;
  753. u16 cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  754. u32 cpos, cend, clen, hole_size;
  755. u64 extoff, extlen;
  756. struct buffer_head *di_bh = NULL;
  757. struct ocfs2_extent_rec rec;
  758. BUG_ON(whence != SEEK_DATA && whence != SEEK_HOLE);
  759. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  760. if (ret) {
  761. mlog_errno(ret);
  762. goto out;
  763. }
  764. down_read(&OCFS2_I(inode)->ip_alloc_sem);
  765. if (*offset >= i_size_read(inode)) {
  766. ret = -ENXIO;
  767. goto out_unlock;
  768. }
  769. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  770. if (whence == SEEK_HOLE)
  771. *offset = i_size_read(inode);
  772. goto out_unlock;
  773. }
  774. clen = 0;
  775. cpos = *offset >> cs_bits;
  776. cend = ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
  777. while (cpos < cend && !is_last) {
  778. ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos, &hole_size,
  779. &rec, &is_last);
  780. if (ret) {
  781. mlog_errno(ret);
  782. goto out_unlock;
  783. }
  784. extoff = cpos;
  785. extoff <<= cs_bits;
  786. if (rec.e_blkno == 0ULL) {
  787. clen = hole_size;
  788. is_data = 0;
  789. } else {
  790. clen = le16_to_cpu(rec.e_leaf_clusters) -
  791. (cpos - le32_to_cpu(rec.e_cpos));
  792. is_data = (rec.e_flags & OCFS2_EXT_UNWRITTEN) ? 0 : 1;
  793. }
  794. if ((!is_data && whence == SEEK_HOLE) ||
  795. (is_data && whence == SEEK_DATA)) {
  796. if (extoff > *offset)
  797. *offset = extoff;
  798. goto out_unlock;
  799. }
  800. if (!is_last)
  801. cpos += clen;
  802. }
  803. if (whence == SEEK_HOLE) {
  804. extoff = cpos;
  805. extoff <<= cs_bits;
  806. extlen = clen;
  807. extlen <<= cs_bits;
  808. if ((extoff + extlen) > i_size_read(inode))
  809. extlen = i_size_read(inode) - extoff;
  810. extoff += extlen;
  811. if (extoff > *offset)
  812. *offset = extoff;
  813. goto out_unlock;
  814. }
  815. ret = -ENXIO;
  816. out_unlock:
  817. brelse(di_bh);
  818. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  819. ocfs2_inode_unlock(inode, 0);
  820. out:
  821. return ret;
  822. }
  823. int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
  824. struct buffer_head *bhs[], int flags,
  825. int (*validate)(struct super_block *sb,
  826. struct buffer_head *bh))
  827. {
  828. int rc = 0;
  829. u64 p_block, p_count;
  830. int i, count, done = 0;
  831. trace_ocfs2_read_virt_blocks(
  832. inode, (unsigned long long)v_block, nr, bhs, flags,
  833. validate);
  834. if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
  835. i_size_read(inode)) {
  836. BUG_ON(!(flags & OCFS2_BH_READAHEAD));
  837. goto out;
  838. }
  839. while (done < nr) {
  840. if (!down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem)) {
  841. rc = -EAGAIN;
  842. mlog(ML_ERROR,
  843. "Inode #%llu ip_alloc_sem is temporarily unavailable\n",
  844. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  845. break;
  846. }
  847. rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
  848. &p_block, &p_count, NULL);
  849. up_read(&OCFS2_I(inode)->ip_alloc_sem);
  850. if (rc) {
  851. mlog_errno(rc);
  852. break;
  853. }
  854. if (!p_block) {
  855. rc = -EIO;
  856. mlog(ML_ERROR,
  857. "Inode #%llu contains a hole at offset %llu\n",
  858. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  859. (unsigned long long)(v_block + done) <<
  860. inode->i_sb->s_blocksize_bits);
  861. break;
  862. }
  863. count = nr - done;
  864. if (p_count < count)
  865. count = p_count;
  866. /*
  867. * If the caller passed us bhs, they should have come
  868. * from a previous readahead call to this function. Thus,
  869. * they should have the right b_blocknr.
  870. */
  871. for (i = 0; i < count; i++) {
  872. if (!bhs[done + i])
  873. continue;
  874. BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
  875. }
  876. rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
  877. bhs + done, flags, validate);
  878. if (rc) {
  879. mlog_errno(rc);
  880. break;
  881. }
  882. done += count;
  883. }
  884. out:
  885. return rc;
  886. }