extent_map.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * extent_map.c
  5. *
  6. * In-memory extent map for OCFS2. Man, this code was prettier in
  7. * the library.
  8. *
  9. * Copyright (C) 2004 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License, version 2, as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/rbtree.h>
  30. #define MLOG_MASK_PREFIX ML_EXTENT_MAP
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "extent_map.h"
  34. #include "inode.h"
  35. #include "super.h"
  36. #include "buffer_head_io.h"
  37. /*
  38. * SUCK SUCK SUCK
  39. * Our headers are so bad that struct ocfs2_extent_map is in ocfs.h
  40. */
  41. struct ocfs2_extent_map_entry {
  42. struct rb_node e_node;
  43. int e_tree_depth;
  44. struct ocfs2_extent_rec e_rec;
  45. };
  46. struct ocfs2_em_insert_context {
  47. int need_left;
  48. int need_right;
  49. struct ocfs2_extent_map_entry *new_ent;
  50. struct ocfs2_extent_map_entry *old_ent;
  51. struct ocfs2_extent_map_entry *left_ent;
  52. struct ocfs2_extent_map_entry *right_ent;
  53. };
  54. static kmem_cache_t *ocfs2_em_ent_cachep = NULL;
  55. static struct ocfs2_extent_map_entry *
  56. ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
  57. u32 cpos, u32 clusters,
  58. struct rb_node ***ret_p,
  59. struct rb_node **ret_parent);
  60. static int ocfs2_extent_map_insert(struct inode *inode,
  61. struct ocfs2_extent_rec *rec,
  62. int tree_depth);
  63. static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map *em,
  64. struct ocfs2_extent_map_entry *ent);
  65. static int ocfs2_extent_map_find_leaf(struct inode *inode,
  66. u32 cpos, u32 clusters,
  67. struct ocfs2_extent_list *el);
  68. static int ocfs2_extent_map_lookup_read(struct inode *inode,
  69. u32 cpos, u32 clusters,
  70. struct ocfs2_extent_map_entry **ret_ent);
  71. static int ocfs2_extent_map_try_insert(struct inode *inode,
  72. struct ocfs2_extent_rec *rec,
  73. int tree_depth,
  74. struct ocfs2_em_insert_context *ctxt);
  75. /* returns 1 only if the rec contains all the given clusters -- that is that
  76. * rec's cpos is <= the cluster cpos and that the rec endpoint (cpos +
  77. * clusters) is >= the argument's endpoint */
  78. static int ocfs2_extent_rec_contains_clusters(struct ocfs2_extent_rec *rec,
  79. u32 cpos, u32 clusters)
  80. {
  81. if (le32_to_cpu(rec->e_cpos) > cpos)
  82. return 0;
  83. if (cpos + clusters > le32_to_cpu(rec->e_cpos) +
  84. le32_to_cpu(rec->e_clusters))
  85. return 0;
  86. return 1;
  87. }
  88. /*
  89. * Find an entry in the tree that intersects the region passed in.
  90. * Note that this will find straddled intervals, it is up to the
  91. * callers to enforce any boundary conditions.
  92. *
  93. * Callers must hold ip_lock. This lookup is not guaranteed to return
  94. * a tree_depth 0 match, and as such can race inserts if the lock
  95. * were not held.
  96. *
  97. * The rb_node garbage lets insertion share the search. Trivial
  98. * callers pass NULL.
  99. */
  100. static struct ocfs2_extent_map_entry *
  101. ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
  102. u32 cpos, u32 clusters,
  103. struct rb_node ***ret_p,
  104. struct rb_node **ret_parent)
  105. {
  106. struct rb_node **p = &em->em_extents.rb_node;
  107. struct rb_node *parent = NULL;
  108. struct ocfs2_extent_map_entry *ent = NULL;
  109. while (*p)
  110. {
  111. parent = *p;
  112. ent = rb_entry(parent, struct ocfs2_extent_map_entry,
  113. e_node);
  114. if ((cpos + clusters) <= le32_to_cpu(ent->e_rec.e_cpos)) {
  115. p = &(*p)->rb_left;
  116. ent = NULL;
  117. } else if (cpos >= (le32_to_cpu(ent->e_rec.e_cpos) +
  118. le32_to_cpu(ent->e_rec.e_clusters))) {
  119. p = &(*p)->rb_right;
  120. ent = NULL;
  121. } else
  122. break;
  123. }
  124. if (ret_p != NULL)
  125. *ret_p = p;
  126. if (ret_parent != NULL)
  127. *ret_parent = parent;
  128. return ent;
  129. }
  130. /*
  131. * Find the leaf containing the interval we want. While we're on our
  132. * way down the tree, fill in every record we see at any depth, because
  133. * we might want it later.
  134. *
  135. * Note that this code is run without ip_lock. That's because it
  136. * sleeps while reading. If someone is also filling the extent list at
  137. * the same time we are, we might have to restart.
  138. */
  139. static int ocfs2_extent_map_find_leaf(struct inode *inode,
  140. u32 cpos, u32 clusters,
  141. struct ocfs2_extent_list *el)
  142. {
  143. int i, ret;
  144. struct buffer_head *eb_bh = NULL;
  145. u64 blkno;
  146. u32 rec_end;
  147. struct ocfs2_extent_block *eb;
  148. struct ocfs2_extent_rec *rec;
  149. /*
  150. * The bh data containing the el cannot change here, because
  151. * we hold alloc_sem. So we can do this without other
  152. * locks.
  153. */
  154. while (el->l_tree_depth)
  155. {
  156. blkno = 0;
  157. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  158. rec = &el->l_recs[i];
  159. rec_end = (le32_to_cpu(rec->e_cpos) +
  160. le32_to_cpu(rec->e_clusters));
  161. ret = -EBADR;
  162. if (rec_end > OCFS2_I(inode)->ip_clusters) {
  163. mlog_errno(ret);
  164. goto out_free;
  165. }
  166. if (rec_end <= cpos) {
  167. ret = ocfs2_extent_map_insert(inode, rec,
  168. le16_to_cpu(el->l_tree_depth));
  169. if (ret && (ret != -EEXIST)) {
  170. mlog_errno(ret);
  171. goto out_free;
  172. }
  173. continue;
  174. }
  175. if ((cpos + clusters) <= le32_to_cpu(rec->e_cpos)) {
  176. ret = ocfs2_extent_map_insert(inode, rec,
  177. le16_to_cpu(el->l_tree_depth));
  178. if (ret && (ret != -EEXIST)) {
  179. mlog_errno(ret);
  180. goto out_free;
  181. }
  182. continue;
  183. }
  184. /*
  185. * We've found a record that matches our
  186. * interval. We don't insert it because we're
  187. * about to traverse it.
  188. */
  189. /* Check to see if we're stradling */
  190. ret = -ESRCH;
  191. if (!ocfs2_extent_rec_contains_clusters(rec,
  192. cpos,
  193. clusters)) {
  194. mlog_errno(ret);
  195. goto out_free;
  196. }
  197. /*
  198. * If we've already found a record, the el has
  199. * two records covering the same interval.
  200. * EEEK!
  201. */
  202. ret = -EBADR;
  203. if (blkno) {
  204. mlog_errno(ret);
  205. goto out_free;
  206. }
  207. blkno = le64_to_cpu(rec->e_blkno);
  208. }
  209. /*
  210. * We don't support holes, and we're still up
  211. * in the branches, so we'd better have found someone
  212. */
  213. ret = -EBADR;
  214. if (!blkno) {
  215. mlog_errno(ret);
  216. goto out_free;
  217. }
  218. if (eb_bh) {
  219. brelse(eb_bh);
  220. eb_bh = NULL;
  221. }
  222. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  223. blkno, &eb_bh, OCFS2_BH_CACHED,
  224. inode);
  225. if (ret) {
  226. mlog_errno(ret);
  227. goto out_free;
  228. }
  229. eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  230. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  231. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  232. ret = -EIO;
  233. goto out_free;
  234. }
  235. el = &eb->h_list;
  236. }
  237. BUG_ON(el->l_tree_depth);
  238. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  239. rec = &el->l_recs[i];
  240. ret = ocfs2_extent_map_insert(inode, rec,
  241. le16_to_cpu(el->l_tree_depth));
  242. if (ret) {
  243. mlog_errno(ret);
  244. goto out_free;
  245. }
  246. }
  247. ret = 0;
  248. out_free:
  249. if (eb_bh)
  250. brelse(eb_bh);
  251. return ret;
  252. }
  253. /*
  254. * This lookup actually will read from disk. It has one invariant:
  255. * It will never re-traverse blocks. This means that all inserts should
  256. * be new regions or more granular regions (both allowed by insert).
  257. */
  258. static int ocfs2_extent_map_lookup_read(struct inode *inode,
  259. u32 cpos,
  260. u32 clusters,
  261. struct ocfs2_extent_map_entry **ret_ent)
  262. {
  263. int ret;
  264. u64 blkno;
  265. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  266. struct ocfs2_extent_map_entry *ent;
  267. struct buffer_head *bh = NULL;
  268. struct ocfs2_extent_block *eb;
  269. struct ocfs2_dinode *di;
  270. struct ocfs2_extent_list *el;
  271. spin_lock(&OCFS2_I(inode)->ip_lock);
  272. ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
  273. if (ent) {
  274. if (!ent->e_tree_depth) {
  275. spin_unlock(&OCFS2_I(inode)->ip_lock);
  276. *ret_ent = ent;
  277. return 0;
  278. }
  279. blkno = le64_to_cpu(ent->e_rec.e_blkno);
  280. spin_unlock(&OCFS2_I(inode)->ip_lock);
  281. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, &bh,
  282. OCFS2_BH_CACHED, inode);
  283. if (ret) {
  284. mlog_errno(ret);
  285. if (bh)
  286. brelse(bh);
  287. return ret;
  288. }
  289. eb = (struct ocfs2_extent_block *)bh->b_data;
  290. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  291. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  292. brelse(bh);
  293. return -EIO;
  294. }
  295. el = &eb->h_list;
  296. } else {
  297. spin_unlock(&OCFS2_I(inode)->ip_lock);
  298. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  299. OCFS2_I(inode)->ip_blkno, &bh,
  300. OCFS2_BH_CACHED, inode);
  301. if (ret) {
  302. mlog_errno(ret);
  303. if (bh)
  304. brelse(bh);
  305. return ret;
  306. }
  307. di = (struct ocfs2_dinode *)bh->b_data;
  308. if (!OCFS2_IS_VALID_DINODE(di)) {
  309. brelse(bh);
  310. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, di);
  311. return -EIO;
  312. }
  313. el = &di->id2.i_list;
  314. }
  315. ret = ocfs2_extent_map_find_leaf(inode, cpos, clusters, el);
  316. brelse(bh);
  317. if (ret) {
  318. mlog_errno(ret);
  319. return ret;
  320. }
  321. ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
  322. if (!ent) {
  323. ret = -ESRCH;
  324. mlog_errno(ret);
  325. return ret;
  326. }
  327. /* FIXME: Make sure this isn't a corruption */
  328. BUG_ON(ent->e_tree_depth);
  329. *ret_ent = ent;
  330. return 0;
  331. }
  332. /*
  333. * Callers must hold ip_lock. This can insert pieces of the tree,
  334. * thus racing lookup if the lock weren't held.
  335. */
  336. static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map *em,
  337. struct ocfs2_extent_map_entry *ent)
  338. {
  339. struct rb_node **p, *parent;
  340. struct ocfs2_extent_map_entry *old_ent;
  341. old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(ent->e_rec.e_cpos),
  342. le32_to_cpu(ent->e_rec.e_clusters),
  343. &p, &parent);
  344. if (old_ent)
  345. return -EEXIST;
  346. rb_link_node(&ent->e_node, parent, p);
  347. rb_insert_color(&ent->e_node, &em->em_extents);
  348. return 0;
  349. }
  350. /*
  351. * Simple rule: on any return code other than -EAGAIN, anything left
  352. * in the insert_context will be freed.
  353. */
  354. static int ocfs2_extent_map_try_insert(struct inode *inode,
  355. struct ocfs2_extent_rec *rec,
  356. int tree_depth,
  357. struct ocfs2_em_insert_context *ctxt)
  358. {
  359. int ret;
  360. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  361. struct ocfs2_extent_map_entry *old_ent;
  362. ctxt->need_left = 0;
  363. ctxt->need_right = 0;
  364. ctxt->old_ent = NULL;
  365. spin_lock(&OCFS2_I(inode)->ip_lock);
  366. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  367. if (!ret) {
  368. ctxt->new_ent = NULL;
  369. goto out_unlock;
  370. }
  371. old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos),
  372. le32_to_cpu(rec->e_clusters), NULL,
  373. NULL);
  374. BUG_ON(!old_ent);
  375. ret = -EEXIST;
  376. if (old_ent->e_tree_depth < tree_depth)
  377. goto out_unlock;
  378. if (old_ent->e_tree_depth == tree_depth) {
  379. if (!memcmp(rec, &old_ent->e_rec,
  380. sizeof(struct ocfs2_extent_rec)))
  381. ret = 0;
  382. /* FIXME: Should this be ESRCH/EBADR??? */
  383. goto out_unlock;
  384. }
  385. /*
  386. * We do it in this order specifically so that no actual tree
  387. * changes occur until we have all the pieces we need. We
  388. * don't want malloc failures to leave an inconsistent tree.
  389. * Whenever we drop the lock, another process could be
  390. * inserting. Also note that, if another process just beat us
  391. * to an insert, we might not need the same pieces we needed
  392. * the first go round. In the end, the pieces we need will
  393. * be used, and the pieces we don't will be freed.
  394. */
  395. ctxt->need_left = !!(le32_to_cpu(rec->e_cpos) >
  396. le32_to_cpu(old_ent->e_rec.e_cpos));
  397. ctxt->need_right = !!((le32_to_cpu(old_ent->e_rec.e_cpos) +
  398. le32_to_cpu(old_ent->e_rec.e_clusters)) >
  399. (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)));
  400. ret = -EAGAIN;
  401. if (ctxt->need_left) {
  402. if (!ctxt->left_ent)
  403. goto out_unlock;
  404. *(ctxt->left_ent) = *old_ent;
  405. ctxt->left_ent->e_rec.e_clusters =
  406. cpu_to_le32(le32_to_cpu(rec->e_cpos) -
  407. le32_to_cpu(ctxt->left_ent->e_rec.e_cpos));
  408. }
  409. if (ctxt->need_right) {
  410. if (!ctxt->right_ent)
  411. goto out_unlock;
  412. *(ctxt->right_ent) = *old_ent;
  413. ctxt->right_ent->e_rec.e_cpos =
  414. cpu_to_le32(le32_to_cpu(rec->e_cpos) +
  415. le32_to_cpu(rec->e_clusters));
  416. ctxt->right_ent->e_rec.e_clusters =
  417. cpu_to_le32((le32_to_cpu(old_ent->e_rec.e_cpos) +
  418. le32_to_cpu(old_ent->e_rec.e_clusters)) -
  419. le32_to_cpu(ctxt->right_ent->e_rec.e_cpos));
  420. }
  421. rb_erase(&old_ent->e_node, &em->em_extents);
  422. /* Now that he's erased, set him up for deletion */
  423. ctxt->old_ent = old_ent;
  424. if (ctxt->need_left) {
  425. ret = ocfs2_extent_map_insert_entry(em,
  426. ctxt->left_ent);
  427. if (ret)
  428. goto out_unlock;
  429. ctxt->left_ent = NULL;
  430. }
  431. if (ctxt->need_right) {
  432. ret = ocfs2_extent_map_insert_entry(em,
  433. ctxt->right_ent);
  434. if (ret)
  435. goto out_unlock;
  436. ctxt->right_ent = NULL;
  437. }
  438. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  439. if (!ret)
  440. ctxt->new_ent = NULL;
  441. out_unlock:
  442. spin_unlock(&OCFS2_I(inode)->ip_lock);
  443. return ret;
  444. }
  445. static int ocfs2_extent_map_insert(struct inode *inode,
  446. struct ocfs2_extent_rec *rec,
  447. int tree_depth)
  448. {
  449. int ret;
  450. struct ocfs2_em_insert_context ctxt = {0, };
  451. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) >
  452. OCFS2_I(inode)->ip_map.em_clusters) {
  453. ret = -EBADR;
  454. mlog_errno(ret);
  455. return ret;
  456. }
  457. /* Zero e_clusters means a truncated tail record. It better be EOF */
  458. if (!rec->e_clusters) {
  459. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) !=
  460. OCFS2_I(inode)->ip_map.em_clusters) {
  461. ret = -EBADR;
  462. mlog_errno(ret);
  463. return ret;
  464. }
  465. /* Ignore the truncated tail */
  466. return 0;
  467. }
  468. ret = -ENOMEM;
  469. ctxt.new_ent = kmem_cache_alloc(ocfs2_em_ent_cachep,
  470. GFP_KERNEL);
  471. if (!ctxt.new_ent) {
  472. mlog_errno(ret);
  473. return ret;
  474. }
  475. ctxt.new_ent->e_rec = *rec;
  476. ctxt.new_ent->e_tree_depth = tree_depth;
  477. do {
  478. ret = -ENOMEM;
  479. if (ctxt.need_left && !ctxt.left_ent) {
  480. ctxt.left_ent =
  481. kmem_cache_alloc(ocfs2_em_ent_cachep,
  482. GFP_KERNEL);
  483. if (!ctxt.left_ent)
  484. break;
  485. }
  486. if (ctxt.need_right && !ctxt.right_ent) {
  487. ctxt.right_ent =
  488. kmem_cache_alloc(ocfs2_em_ent_cachep,
  489. GFP_KERNEL);
  490. if (!ctxt.right_ent)
  491. break;
  492. }
  493. ret = ocfs2_extent_map_try_insert(inode, rec,
  494. tree_depth, &ctxt);
  495. } while (ret == -EAGAIN);
  496. if (ret < 0)
  497. mlog_errno(ret);
  498. if (ctxt.left_ent)
  499. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.left_ent);
  500. if (ctxt.right_ent)
  501. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.right_ent);
  502. if (ctxt.old_ent)
  503. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.old_ent);
  504. if (ctxt.new_ent)
  505. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.new_ent);
  506. return ret;
  507. }
  508. /*
  509. * Append this record to the tail of the extent map. It must be
  510. * tree_depth 0. The record might be an extension of an existing
  511. * record, and as such that needs to be handled. eg:
  512. *
  513. * Existing record in the extent map:
  514. *
  515. * cpos = 10, len = 10
  516. * |---------|
  517. *
  518. * New Record:
  519. *
  520. * cpos = 10, len = 20
  521. * |------------------|
  522. *
  523. * The passed record is the new on-disk record. The new_clusters value
  524. * is how many clusters were added to the file. If the append is a
  525. * contiguous append, the new_clusters has been added to
  526. * rec->e_clusters. If the append is an entirely new extent, then
  527. * rec->e_clusters is == new_clusters.
  528. */
  529. int ocfs2_extent_map_append(struct inode *inode,
  530. struct ocfs2_extent_rec *rec,
  531. u32 new_clusters)
  532. {
  533. int ret;
  534. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  535. struct ocfs2_extent_map_entry *ent;
  536. struct ocfs2_extent_rec *old;
  537. BUG_ON(!new_clusters);
  538. BUG_ON(le32_to_cpu(rec->e_clusters) < new_clusters);
  539. if (em->em_clusters < OCFS2_I(inode)->ip_clusters) {
  540. /*
  541. * Size changed underneath us on disk. Drop any
  542. * straddling records and update our idea of
  543. * i_clusters
  544. */
  545. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  546. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  547. }
  548. mlog_bug_on_msg((le32_to_cpu(rec->e_cpos) +
  549. le32_to_cpu(rec->e_clusters)) !=
  550. (em->em_clusters + new_clusters),
  551. "Inode %"MLFu64":\n"
  552. "rec->e_cpos = %u + rec->e_clusters = %u = %u\n"
  553. "em->em_clusters = %u + new_clusters = %u = %u\n",
  554. OCFS2_I(inode)->ip_blkno,
  555. le32_to_cpu(rec->e_cpos), le32_to_cpu(rec->e_clusters),
  556. le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters),
  557. em->em_clusters, new_clusters,
  558. em->em_clusters + new_clusters);
  559. em->em_clusters += new_clusters;
  560. ret = -ENOENT;
  561. if (le32_to_cpu(rec->e_clusters) > new_clusters) {
  562. /* This is a contiguous append */
  563. ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos), 1,
  564. NULL, NULL);
  565. if (ent) {
  566. old = &ent->e_rec;
  567. BUG_ON((le32_to_cpu(rec->e_cpos) +
  568. le32_to_cpu(rec->e_clusters)) !=
  569. (le32_to_cpu(old->e_cpos) +
  570. le32_to_cpu(old->e_clusters) +
  571. new_clusters));
  572. if (ent->e_tree_depth == 0) {
  573. BUG_ON(le32_to_cpu(old->e_cpos) !=
  574. le32_to_cpu(rec->e_cpos));
  575. BUG_ON(le64_to_cpu(old->e_blkno) !=
  576. le64_to_cpu(rec->e_blkno));
  577. ret = 0;
  578. }
  579. /*
  580. * Let non-leafs fall through as -ENOENT to
  581. * force insertion of the new leaf.
  582. */
  583. le32_add_cpu(&old->e_clusters, new_clusters);
  584. }
  585. }
  586. if (ret == -ENOENT)
  587. ret = ocfs2_extent_map_insert(inode, rec, 0);
  588. if (ret < 0)
  589. mlog_errno(ret);
  590. return ret;
  591. }
  592. #if 0
  593. /* Code here is included but defined out as it completes the extent
  594. * map api and may be used in the future. */
  595. /*
  596. * Look up the record containing this cluster offset. This record is
  597. * part of the extent map. Do not free it. Any changes you make to
  598. * it will reflect in the extent map. So, if your last extent
  599. * is (cpos = 10, clusters = 10) and you truncate the file by 5
  600. * clusters, you can do:
  601. *
  602. * ret = ocfs2_extent_map_get_rec(em, orig_size - 5, &rec);
  603. * rec->e_clusters -= 5;
  604. *
  605. * The lookup does not read from disk. If the map isn't filled in for
  606. * an entry, you won't find it.
  607. *
  608. * Also note that the returned record is valid until alloc_sem is
  609. * dropped. After that, truncate and extend can happen. Caveat Emptor.
  610. */
  611. int ocfs2_extent_map_get_rec(struct inode *inode, u32 cpos,
  612. struct ocfs2_extent_rec **rec,
  613. int *tree_depth)
  614. {
  615. int ret = -ENOENT;
  616. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  617. struct ocfs2_extent_map_entry *ent;
  618. *rec = NULL;
  619. if (cpos >= OCFS2_I(inode)->ip_clusters)
  620. return -EINVAL;
  621. if (cpos >= em->em_clusters) {
  622. /*
  623. * Size changed underneath us on disk. Drop any
  624. * straddling records and update our idea of
  625. * i_clusters
  626. */
  627. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  628. em->em_clusters = OCFS2_I(inode)->ip_clusters ;
  629. }
  630. ent = ocfs2_extent_map_lookup(&OCFS2_I(inode)->ip_map, cpos, 1,
  631. NULL, NULL);
  632. if (ent) {
  633. *rec = &ent->e_rec;
  634. if (tree_depth)
  635. *tree_depth = ent->e_tree_depth;
  636. ret = 0;
  637. }
  638. return ret;
  639. }
  640. int ocfs2_extent_map_get_clusters(struct inode *inode,
  641. u32 v_cpos, int count,
  642. u32 *p_cpos, int *ret_count)
  643. {
  644. int ret;
  645. u32 coff, ccount;
  646. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  647. struct ocfs2_extent_map_entry *ent = NULL;
  648. *p_cpos = ccount = 0;
  649. if ((v_cpos + count) > OCFS2_I(inode)->ip_clusters)
  650. return -EINVAL;
  651. if ((v_cpos + count) > em->em_clusters) {
  652. /*
  653. * Size changed underneath us on disk. Drop any
  654. * straddling records and update our idea of
  655. * i_clusters
  656. */
  657. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  658. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  659. }
  660. ret = ocfs2_extent_map_lookup_read(inode, v_cpos, count, &ent);
  661. if (ret)
  662. return ret;
  663. if (ent) {
  664. /* We should never find ourselves straddling an interval */
  665. if (!ocfs2_extent_rec_contains_clusters(&ent->e_rec,
  666. v_cpos,
  667. count))
  668. return -ESRCH;
  669. coff = v_cpos - le32_to_cpu(ent->e_rec.e_cpos);
  670. *p_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  671. le64_to_cpu(ent->e_rec.e_blkno)) +
  672. coff;
  673. if (ret_count)
  674. *ret_count = le32_to_cpu(ent->e_rec.e_clusters) - coff;
  675. return 0;
  676. }
  677. return -ENOENT;
  678. }
  679. #endif /* 0 */
  680. int ocfs2_extent_map_get_blocks(struct inode *inode,
  681. u64 v_blkno, int count,
  682. u64 *p_blkno, int *ret_count)
  683. {
  684. int ret;
  685. u64 boff;
  686. u32 cpos, clusters;
  687. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  688. struct ocfs2_extent_map_entry *ent = NULL;
  689. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  690. struct ocfs2_extent_rec *rec;
  691. *p_blkno = 0;
  692. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  693. clusters = ocfs2_blocks_to_clusters(inode->i_sb,
  694. (u64)count + bpc - 1);
  695. if ((cpos + clusters) > OCFS2_I(inode)->ip_clusters) {
  696. ret = -EINVAL;
  697. mlog_errno(ret);
  698. return ret;
  699. }
  700. if ((cpos + clusters) > em->em_clusters) {
  701. /*
  702. * Size changed underneath us on disk. Drop any
  703. * straddling records and update our idea of
  704. * i_clusters
  705. */
  706. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  707. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  708. }
  709. ret = ocfs2_extent_map_lookup_read(inode, cpos, clusters, &ent);
  710. if (ret) {
  711. mlog_errno(ret);
  712. return ret;
  713. }
  714. if (ent)
  715. {
  716. rec = &ent->e_rec;
  717. /* We should never find ourselves straddling an interval */
  718. if (!ocfs2_extent_rec_contains_clusters(rec, cpos, clusters)) {
  719. ret = -ESRCH;
  720. mlog_errno(ret);
  721. return ret;
  722. }
  723. boff = ocfs2_clusters_to_blocks(inode->i_sb, cpos -
  724. le32_to_cpu(rec->e_cpos));
  725. boff += (v_blkno & (u64)(bpc - 1));
  726. *p_blkno = le64_to_cpu(rec->e_blkno) + boff;
  727. if (ret_count) {
  728. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb,
  729. le32_to_cpu(rec->e_clusters)) - boff;
  730. }
  731. return 0;
  732. }
  733. return -ENOENT;
  734. }
  735. int ocfs2_extent_map_init(struct inode *inode)
  736. {
  737. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  738. em->em_extents = RB_ROOT;
  739. em->em_clusters = 0;
  740. return 0;
  741. }
  742. /* Needs the lock */
  743. static void __ocfs2_extent_map_drop(struct inode *inode,
  744. u32 new_clusters,
  745. struct rb_node **free_head,
  746. struct ocfs2_extent_map_entry **tail_ent)
  747. {
  748. struct rb_node *node, *next;
  749. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  750. struct ocfs2_extent_map_entry *ent;
  751. *free_head = NULL;
  752. ent = NULL;
  753. node = rb_last(&em->em_extents);
  754. while (node)
  755. {
  756. next = rb_prev(node);
  757. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  758. e_node);
  759. if (le32_to_cpu(ent->e_rec.e_cpos) < new_clusters)
  760. break;
  761. rb_erase(&ent->e_node, &em->em_extents);
  762. node->rb_right = *free_head;
  763. *free_head = node;
  764. ent = NULL;
  765. node = next;
  766. }
  767. /* Do we have an entry straddling new_clusters? */
  768. if (tail_ent) {
  769. if (ent &&
  770. ((le32_to_cpu(ent->e_rec.e_cpos) +
  771. le32_to_cpu(ent->e_rec.e_clusters)) > new_clusters))
  772. *tail_ent = ent;
  773. else
  774. *tail_ent = NULL;
  775. }
  776. }
  777. static void __ocfs2_extent_map_drop_cleanup(struct rb_node *free_head)
  778. {
  779. struct rb_node *node;
  780. struct ocfs2_extent_map_entry *ent;
  781. while (free_head) {
  782. node = free_head;
  783. free_head = node->rb_right;
  784. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  785. e_node);
  786. kmem_cache_free(ocfs2_em_ent_cachep, ent);
  787. }
  788. }
  789. /*
  790. * Remove all entries past new_clusters, inclusive of an entry that
  791. * contains new_clusters. This is effectively a cache forget.
  792. *
  793. * If you want to also clip the last extent by some number of clusters,
  794. * you need to call ocfs2_extent_map_trunc().
  795. * This code does not check or modify ip_clusters.
  796. */
  797. int ocfs2_extent_map_drop(struct inode *inode, u32 new_clusters)
  798. {
  799. struct rb_node *free_head = NULL;
  800. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  801. struct ocfs2_extent_map_entry *ent;
  802. spin_lock(&OCFS2_I(inode)->ip_lock);
  803. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  804. if (ent) {
  805. rb_erase(&ent->e_node, &em->em_extents);
  806. ent->e_node.rb_right = free_head;
  807. free_head = &ent->e_node;
  808. }
  809. spin_unlock(&OCFS2_I(inode)->ip_lock);
  810. if (free_head)
  811. __ocfs2_extent_map_drop_cleanup(free_head);
  812. return 0;
  813. }
  814. /*
  815. * Remove all entries past new_clusters and also clip any extent
  816. * straddling new_clusters, if there is one. This does not check
  817. * or modify ip_clusters
  818. */
  819. int ocfs2_extent_map_trunc(struct inode *inode, u32 new_clusters)
  820. {
  821. struct rb_node *free_head = NULL;
  822. struct ocfs2_extent_map_entry *ent = NULL;
  823. spin_lock(&OCFS2_I(inode)->ip_lock);
  824. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  825. if (ent)
  826. ent->e_rec.e_clusters = cpu_to_le32(new_clusters -
  827. le32_to_cpu(ent->e_rec.e_cpos));
  828. OCFS2_I(inode)->ip_map.em_clusters = new_clusters;
  829. spin_unlock(&OCFS2_I(inode)->ip_lock);
  830. if (free_head)
  831. __ocfs2_extent_map_drop_cleanup(free_head);
  832. return 0;
  833. }
  834. int __init init_ocfs2_extent_maps(void)
  835. {
  836. ocfs2_em_ent_cachep =
  837. kmem_cache_create("ocfs2_em_ent",
  838. sizeof(struct ocfs2_extent_map_entry),
  839. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  840. if (!ocfs2_em_ent_cachep)
  841. return -ENOMEM;
  842. return 0;
  843. }
  844. void exit_ocfs2_extent_maps(void)
  845. {
  846. kmem_cache_destroy(ocfs2_em_ent_cachep);
  847. }