extent_map.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. if (el->l_tree_depth)
  238. BUG();
  239. for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  240. rec = &el->l_recs[i];
  241. ret = ocfs2_extent_map_insert(inode, rec,
  242. le16_to_cpu(el->l_tree_depth));
  243. if (ret) {
  244. mlog_errno(ret);
  245. goto out_free;
  246. }
  247. }
  248. ret = 0;
  249. out_free:
  250. if (eb_bh)
  251. brelse(eb_bh);
  252. return ret;
  253. }
  254. /*
  255. * This lookup actually will read from disk. It has one invariant:
  256. * It will never re-traverse blocks. This means that all inserts should
  257. * be new regions or more granular regions (both allowed by insert).
  258. */
  259. static int ocfs2_extent_map_lookup_read(struct inode *inode,
  260. u32 cpos,
  261. u32 clusters,
  262. struct ocfs2_extent_map_entry **ret_ent)
  263. {
  264. int ret;
  265. u64 blkno;
  266. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  267. struct ocfs2_extent_map_entry *ent;
  268. struct buffer_head *bh = NULL;
  269. struct ocfs2_extent_block *eb;
  270. struct ocfs2_dinode *di;
  271. struct ocfs2_extent_list *el;
  272. spin_lock(&OCFS2_I(inode)->ip_lock);
  273. ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
  274. if (ent) {
  275. if (!ent->e_tree_depth) {
  276. spin_unlock(&OCFS2_I(inode)->ip_lock);
  277. *ret_ent = ent;
  278. return 0;
  279. }
  280. blkno = le64_to_cpu(ent->e_rec.e_blkno);
  281. spin_unlock(&OCFS2_I(inode)->ip_lock);
  282. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, &bh,
  283. OCFS2_BH_CACHED, inode);
  284. if (ret) {
  285. mlog_errno(ret);
  286. if (bh)
  287. brelse(bh);
  288. return ret;
  289. }
  290. eb = (struct ocfs2_extent_block *)bh->b_data;
  291. if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
  292. OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
  293. brelse(bh);
  294. return -EIO;
  295. }
  296. el = &eb->h_list;
  297. } else {
  298. spin_unlock(&OCFS2_I(inode)->ip_lock);
  299. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  300. OCFS2_I(inode)->ip_blkno, &bh,
  301. OCFS2_BH_CACHED, inode);
  302. if (ret) {
  303. mlog_errno(ret);
  304. if (bh)
  305. brelse(bh);
  306. return ret;
  307. }
  308. di = (struct ocfs2_dinode *)bh->b_data;
  309. if (!OCFS2_IS_VALID_DINODE(di)) {
  310. brelse(bh);
  311. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, di);
  312. return -EIO;
  313. }
  314. el = &di->id2.i_list;
  315. }
  316. ret = ocfs2_extent_map_find_leaf(inode, cpos, clusters, el);
  317. brelse(bh);
  318. if (ret) {
  319. mlog_errno(ret);
  320. return ret;
  321. }
  322. ent = ocfs2_extent_map_lookup(em, cpos, clusters, NULL, NULL);
  323. if (!ent) {
  324. ret = -ESRCH;
  325. mlog_errno(ret);
  326. return ret;
  327. }
  328. if (ent->e_tree_depth)
  329. BUG(); /* FIXME: Make sure this isn't a corruption */
  330. *ret_ent = ent;
  331. return 0;
  332. }
  333. /*
  334. * Callers must hold ip_lock. This can insert pieces of the tree,
  335. * thus racing lookup if the lock weren't held.
  336. */
  337. static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map *em,
  338. struct ocfs2_extent_map_entry *ent)
  339. {
  340. struct rb_node **p, *parent;
  341. struct ocfs2_extent_map_entry *old_ent;
  342. old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(ent->e_rec.e_cpos),
  343. le32_to_cpu(ent->e_rec.e_clusters),
  344. &p, &parent);
  345. if (old_ent)
  346. return -EEXIST;
  347. rb_link_node(&ent->e_node, parent, p);
  348. rb_insert_color(&ent->e_node, &em->em_extents);
  349. return 0;
  350. }
  351. /*
  352. * Simple rule: on any return code other than -EAGAIN, anything left
  353. * in the insert_context will be freed.
  354. */
  355. static int ocfs2_extent_map_try_insert(struct inode *inode,
  356. struct ocfs2_extent_rec *rec,
  357. int tree_depth,
  358. struct ocfs2_em_insert_context *ctxt)
  359. {
  360. int ret;
  361. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  362. struct ocfs2_extent_map_entry *old_ent;
  363. ctxt->need_left = 0;
  364. ctxt->need_right = 0;
  365. ctxt->old_ent = NULL;
  366. spin_lock(&OCFS2_I(inode)->ip_lock);
  367. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  368. if (!ret) {
  369. ctxt->new_ent = NULL;
  370. goto out_unlock;
  371. }
  372. old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos),
  373. le32_to_cpu(rec->e_clusters), NULL,
  374. NULL);
  375. if (!old_ent)
  376. BUG();
  377. ret = -EEXIST;
  378. if (old_ent->e_tree_depth < tree_depth)
  379. goto out_unlock;
  380. if (old_ent->e_tree_depth == tree_depth) {
  381. if (!memcmp(rec, &old_ent->e_rec,
  382. sizeof(struct ocfs2_extent_rec)))
  383. ret = 0;
  384. /* FIXME: Should this be ESRCH/EBADR??? */
  385. goto out_unlock;
  386. }
  387. /*
  388. * We do it in this order specifically so that no actual tree
  389. * changes occur until we have all the pieces we need. We
  390. * don't want malloc failures to leave an inconsistent tree.
  391. * Whenever we drop the lock, another process could be
  392. * inserting. Also note that, if another process just beat us
  393. * to an insert, we might not need the same pieces we needed
  394. * the first go round. In the end, the pieces we need will
  395. * be used, and the pieces we don't will be freed.
  396. */
  397. ctxt->need_left = !!(le32_to_cpu(rec->e_cpos) >
  398. le32_to_cpu(old_ent->e_rec.e_cpos));
  399. ctxt->need_right = !!((le32_to_cpu(old_ent->e_rec.e_cpos) +
  400. le32_to_cpu(old_ent->e_rec.e_clusters)) >
  401. (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)));
  402. ret = -EAGAIN;
  403. if (ctxt->need_left) {
  404. if (!ctxt->left_ent)
  405. goto out_unlock;
  406. *(ctxt->left_ent) = *old_ent;
  407. ctxt->left_ent->e_rec.e_clusters =
  408. cpu_to_le32(le32_to_cpu(rec->e_cpos) -
  409. le32_to_cpu(ctxt->left_ent->e_rec.e_cpos));
  410. }
  411. if (ctxt->need_right) {
  412. if (!ctxt->right_ent)
  413. goto out_unlock;
  414. *(ctxt->right_ent) = *old_ent;
  415. ctxt->right_ent->e_rec.e_cpos =
  416. cpu_to_le32(le32_to_cpu(rec->e_cpos) +
  417. le32_to_cpu(rec->e_clusters));
  418. ctxt->right_ent->e_rec.e_clusters =
  419. cpu_to_le32((le32_to_cpu(old_ent->e_rec.e_cpos) +
  420. le32_to_cpu(old_ent->e_rec.e_clusters)) -
  421. le32_to_cpu(ctxt->right_ent->e_rec.e_cpos));
  422. }
  423. rb_erase(&old_ent->e_node, &em->em_extents);
  424. /* Now that he's erased, set him up for deletion */
  425. ctxt->old_ent = old_ent;
  426. if (ctxt->need_left) {
  427. ret = ocfs2_extent_map_insert_entry(em,
  428. ctxt->left_ent);
  429. if (ret)
  430. goto out_unlock;
  431. ctxt->left_ent = NULL;
  432. }
  433. if (ctxt->need_right) {
  434. ret = ocfs2_extent_map_insert_entry(em,
  435. ctxt->right_ent);
  436. if (ret)
  437. goto out_unlock;
  438. ctxt->right_ent = NULL;
  439. }
  440. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  441. if (!ret)
  442. ctxt->new_ent = NULL;
  443. out_unlock:
  444. spin_unlock(&OCFS2_I(inode)->ip_lock);
  445. return ret;
  446. }
  447. static int ocfs2_extent_map_insert(struct inode *inode,
  448. struct ocfs2_extent_rec *rec,
  449. int tree_depth)
  450. {
  451. int ret;
  452. struct ocfs2_em_insert_context ctxt = {0, };
  453. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) >
  454. OCFS2_I(inode)->ip_map.em_clusters) {
  455. ret = -EBADR;
  456. mlog_errno(ret);
  457. return ret;
  458. }
  459. /* Zero e_clusters means a truncated tail record. It better be EOF */
  460. if (!rec->e_clusters) {
  461. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) !=
  462. OCFS2_I(inode)->ip_map.em_clusters) {
  463. ret = -EBADR;
  464. mlog_errno(ret);
  465. return ret;
  466. }
  467. /* Ignore the truncated tail */
  468. return 0;
  469. }
  470. ret = -ENOMEM;
  471. ctxt.new_ent = kmem_cache_alloc(ocfs2_em_ent_cachep,
  472. GFP_KERNEL);
  473. if (!ctxt.new_ent) {
  474. mlog_errno(ret);
  475. return ret;
  476. }
  477. ctxt.new_ent->e_rec = *rec;
  478. ctxt.new_ent->e_tree_depth = tree_depth;
  479. do {
  480. ret = -ENOMEM;
  481. if (ctxt.need_left && !ctxt.left_ent) {
  482. ctxt.left_ent =
  483. kmem_cache_alloc(ocfs2_em_ent_cachep,
  484. GFP_KERNEL);
  485. if (!ctxt.left_ent)
  486. break;
  487. }
  488. if (ctxt.need_right && !ctxt.right_ent) {
  489. ctxt.right_ent =
  490. kmem_cache_alloc(ocfs2_em_ent_cachep,
  491. GFP_KERNEL);
  492. if (!ctxt.right_ent)
  493. break;
  494. }
  495. ret = ocfs2_extent_map_try_insert(inode, rec,
  496. tree_depth, &ctxt);
  497. } while (ret == -EAGAIN);
  498. if (ret < 0)
  499. mlog_errno(ret);
  500. if (ctxt.left_ent)
  501. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.left_ent);
  502. if (ctxt.right_ent)
  503. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.right_ent);
  504. if (ctxt.old_ent)
  505. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.old_ent);
  506. if (ctxt.new_ent)
  507. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.new_ent);
  508. return ret;
  509. }
  510. /*
  511. * Append this record to the tail of the extent map. It must be
  512. * tree_depth 0. The record might be an extension of an existing
  513. * record, and as such that needs to be handled. eg:
  514. *
  515. * Existing record in the extent map:
  516. *
  517. * cpos = 10, len = 10
  518. * |---------|
  519. *
  520. * New Record:
  521. *
  522. * cpos = 10, len = 20
  523. * |------------------|
  524. *
  525. * The passed record is the new on-disk record. The new_clusters value
  526. * is how many clusters were added to the file. If the append is a
  527. * contiguous append, the new_clusters has been added to
  528. * rec->e_clusters. If the append is an entirely new extent, then
  529. * rec->e_clusters is == new_clusters.
  530. */
  531. int ocfs2_extent_map_append(struct inode *inode,
  532. struct ocfs2_extent_rec *rec,
  533. u32 new_clusters)
  534. {
  535. int ret;
  536. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  537. struct ocfs2_extent_map_entry *ent;
  538. struct ocfs2_extent_rec *old;
  539. BUG_ON(!new_clusters);
  540. BUG_ON(le32_to_cpu(rec->e_clusters) < new_clusters);
  541. if (em->em_clusters < OCFS2_I(inode)->ip_clusters) {
  542. /*
  543. * Size changed underneath us on disk. Drop any
  544. * straddling records and update our idea of
  545. * i_clusters
  546. */
  547. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  548. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  549. }
  550. mlog_bug_on_msg((le32_to_cpu(rec->e_cpos) +
  551. le32_to_cpu(rec->e_clusters)) !=
  552. (em->em_clusters + new_clusters),
  553. "Inode %"MLFu64":\n"
  554. "rec->e_cpos = %u + rec->e_clusters = %u = %u\n"
  555. "em->em_clusters = %u + new_clusters = %u = %u\n",
  556. OCFS2_I(inode)->ip_blkno,
  557. le32_to_cpu(rec->e_cpos), le32_to_cpu(rec->e_clusters),
  558. le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters),
  559. em->em_clusters, new_clusters,
  560. em->em_clusters + new_clusters);
  561. em->em_clusters += new_clusters;
  562. ret = -ENOENT;
  563. if (le32_to_cpu(rec->e_clusters) > new_clusters) {
  564. /* This is a contiguous append */
  565. ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos), 1,
  566. NULL, NULL);
  567. if (ent) {
  568. old = &ent->e_rec;
  569. BUG_ON((le32_to_cpu(rec->e_cpos) +
  570. le32_to_cpu(rec->e_clusters)) !=
  571. (le32_to_cpu(old->e_cpos) +
  572. le32_to_cpu(old->e_clusters) +
  573. new_clusters));
  574. if (ent->e_tree_depth == 0) {
  575. BUG_ON(le32_to_cpu(old->e_cpos) !=
  576. le32_to_cpu(rec->e_cpos));
  577. BUG_ON(le64_to_cpu(old->e_blkno) !=
  578. le64_to_cpu(rec->e_blkno));
  579. ret = 0;
  580. }
  581. /*
  582. * Let non-leafs fall through as -ENOENT to
  583. * force insertion of the new leaf.
  584. */
  585. le32_add_cpu(&old->e_clusters, new_clusters);
  586. }
  587. }
  588. if (ret == -ENOENT)
  589. ret = ocfs2_extent_map_insert(inode, rec, 0);
  590. if (ret < 0)
  591. mlog_errno(ret);
  592. return ret;
  593. }
  594. #if 0
  595. /* Code here is included but defined out as it completes the extent
  596. * map api and may be used in the future. */
  597. /*
  598. * Look up the record containing this cluster offset. This record is
  599. * part of the extent map. Do not free it. Any changes you make to
  600. * it will reflect in the extent map. So, if your last extent
  601. * is (cpos = 10, clusters = 10) and you truncate the file by 5
  602. * clusters, you can do:
  603. *
  604. * ret = ocfs2_extent_map_get_rec(em, orig_size - 5, &rec);
  605. * rec->e_clusters -= 5;
  606. *
  607. * The lookup does not read from disk. If the map isn't filled in for
  608. * an entry, you won't find it.
  609. *
  610. * Also note that the returned record is valid until alloc_sem is
  611. * dropped. After that, truncate and extend can happen. Caveat Emptor.
  612. */
  613. int ocfs2_extent_map_get_rec(struct inode *inode, u32 cpos,
  614. struct ocfs2_extent_rec **rec,
  615. int *tree_depth)
  616. {
  617. int ret = -ENOENT;
  618. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  619. struct ocfs2_extent_map_entry *ent;
  620. *rec = NULL;
  621. if (cpos >= OCFS2_I(inode)->ip_clusters)
  622. return -EINVAL;
  623. if (cpos >= em->em_clusters) {
  624. /*
  625. * Size changed underneath us on disk. Drop any
  626. * straddling records and update our idea of
  627. * i_clusters
  628. */
  629. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  630. em->em_clusters = OCFS2_I(inode)->ip_clusters ;
  631. }
  632. ent = ocfs2_extent_map_lookup(&OCFS2_I(inode)->ip_map, cpos, 1,
  633. NULL, NULL);
  634. if (ent) {
  635. *rec = &ent->e_rec;
  636. if (tree_depth)
  637. *tree_depth = ent->e_tree_depth;
  638. ret = 0;
  639. }
  640. return ret;
  641. }
  642. int ocfs2_extent_map_get_clusters(struct inode *inode,
  643. u32 v_cpos, int count,
  644. u32 *p_cpos, int *ret_count)
  645. {
  646. int ret;
  647. u32 coff, ccount;
  648. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  649. struct ocfs2_extent_map_entry *ent = NULL;
  650. *p_cpos = ccount = 0;
  651. if ((v_cpos + count) > OCFS2_I(inode)->ip_clusters)
  652. return -EINVAL;
  653. if ((v_cpos + count) > em->em_clusters) {
  654. /*
  655. * Size changed underneath us on disk. Drop any
  656. * straddling records and update our idea of
  657. * i_clusters
  658. */
  659. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  660. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  661. }
  662. ret = ocfs2_extent_map_lookup_read(inode, v_cpos, count, &ent);
  663. if (ret)
  664. return ret;
  665. if (ent) {
  666. /* We should never find ourselves straddling an interval */
  667. if (!ocfs2_extent_rec_contains_clusters(&ent->e_rec,
  668. v_cpos,
  669. count))
  670. return -ESRCH;
  671. coff = v_cpos - le32_to_cpu(ent->e_rec.e_cpos);
  672. *p_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  673. le64_to_cpu(ent->e_rec.e_blkno)) +
  674. coff;
  675. if (ret_count)
  676. *ret_count = le32_to_cpu(ent->e_rec.e_clusters) - coff;
  677. return 0;
  678. }
  679. return -ENOENT;
  680. }
  681. #endif /* 0 */
  682. int ocfs2_extent_map_get_blocks(struct inode *inode,
  683. u64 v_blkno, int count,
  684. u64 *p_blkno, int *ret_count)
  685. {
  686. int ret;
  687. u64 boff;
  688. u32 cpos, clusters;
  689. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  690. struct ocfs2_extent_map_entry *ent = NULL;
  691. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  692. struct ocfs2_extent_rec *rec;
  693. *p_blkno = 0;
  694. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  695. clusters = ocfs2_blocks_to_clusters(inode->i_sb,
  696. (u64)count + bpc - 1);
  697. if ((cpos + clusters) > OCFS2_I(inode)->ip_clusters) {
  698. ret = -EINVAL;
  699. mlog_errno(ret);
  700. return ret;
  701. }
  702. if ((cpos + clusters) > em->em_clusters) {
  703. /*
  704. * Size changed underneath us on disk. Drop any
  705. * straddling records and update our idea of
  706. * i_clusters
  707. */
  708. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  709. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  710. }
  711. ret = ocfs2_extent_map_lookup_read(inode, cpos, clusters, &ent);
  712. if (ret) {
  713. mlog_errno(ret);
  714. return ret;
  715. }
  716. if (ent)
  717. {
  718. rec = &ent->e_rec;
  719. /* We should never find ourselves straddling an interval */
  720. if (!ocfs2_extent_rec_contains_clusters(rec, cpos, clusters)) {
  721. ret = -ESRCH;
  722. mlog_errno(ret);
  723. return ret;
  724. }
  725. boff = ocfs2_clusters_to_blocks(inode->i_sb, cpos -
  726. le32_to_cpu(rec->e_cpos));
  727. boff += (v_blkno & (u64)(bpc - 1));
  728. *p_blkno = le64_to_cpu(rec->e_blkno) + boff;
  729. if (ret_count) {
  730. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb,
  731. le32_to_cpu(rec->e_clusters)) - boff;
  732. }
  733. return 0;
  734. }
  735. return -ENOENT;
  736. }
  737. int ocfs2_extent_map_init(struct inode *inode)
  738. {
  739. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  740. em->em_extents = RB_ROOT;
  741. em->em_clusters = 0;
  742. return 0;
  743. }
  744. /* Needs the lock */
  745. static void __ocfs2_extent_map_drop(struct inode *inode,
  746. u32 new_clusters,
  747. struct rb_node **free_head,
  748. struct ocfs2_extent_map_entry **tail_ent)
  749. {
  750. struct rb_node *node, *next;
  751. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  752. struct ocfs2_extent_map_entry *ent;
  753. *free_head = NULL;
  754. ent = NULL;
  755. node = rb_last(&em->em_extents);
  756. while (node)
  757. {
  758. next = rb_prev(node);
  759. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  760. e_node);
  761. if (le32_to_cpu(ent->e_rec.e_cpos) < new_clusters)
  762. break;
  763. rb_erase(&ent->e_node, &em->em_extents);
  764. node->rb_right = *free_head;
  765. *free_head = node;
  766. ent = NULL;
  767. node = next;
  768. }
  769. /* Do we have an entry straddling new_clusters? */
  770. if (tail_ent) {
  771. if (ent &&
  772. ((le32_to_cpu(ent->e_rec.e_cpos) +
  773. le32_to_cpu(ent->e_rec.e_clusters)) > new_clusters))
  774. *tail_ent = ent;
  775. else
  776. *tail_ent = NULL;
  777. }
  778. }
  779. static void __ocfs2_extent_map_drop_cleanup(struct rb_node *free_head)
  780. {
  781. struct rb_node *node;
  782. struct ocfs2_extent_map_entry *ent;
  783. while (free_head) {
  784. node = free_head;
  785. free_head = node->rb_right;
  786. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  787. e_node);
  788. kmem_cache_free(ocfs2_em_ent_cachep, ent);
  789. }
  790. }
  791. /*
  792. * Remove all entries past new_clusters, inclusive of an entry that
  793. * contains new_clusters. This is effectively a cache forget.
  794. *
  795. * If you want to also clip the last extent by some number of clusters,
  796. * you need to call ocfs2_extent_map_trunc().
  797. * This code does not check or modify ip_clusters.
  798. */
  799. int ocfs2_extent_map_drop(struct inode *inode, u32 new_clusters)
  800. {
  801. struct rb_node *free_head = NULL;
  802. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  803. struct ocfs2_extent_map_entry *ent;
  804. spin_lock(&OCFS2_I(inode)->ip_lock);
  805. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  806. if (ent) {
  807. rb_erase(&ent->e_node, &em->em_extents);
  808. ent->e_node.rb_right = free_head;
  809. free_head = &ent->e_node;
  810. }
  811. spin_unlock(&OCFS2_I(inode)->ip_lock);
  812. if (free_head)
  813. __ocfs2_extent_map_drop_cleanup(free_head);
  814. return 0;
  815. }
  816. /*
  817. * Remove all entries past new_clusters and also clip any extent
  818. * straddling new_clusters, if there is one. This does not check
  819. * or modify ip_clusters
  820. */
  821. int ocfs2_extent_map_trunc(struct inode *inode, u32 new_clusters)
  822. {
  823. struct rb_node *free_head = NULL;
  824. struct ocfs2_extent_map_entry *ent = NULL;
  825. spin_lock(&OCFS2_I(inode)->ip_lock);
  826. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  827. if (ent)
  828. ent->e_rec.e_clusters = cpu_to_le32(new_clusters -
  829. le32_to_cpu(ent->e_rec.e_cpos));
  830. OCFS2_I(inode)->ip_map.em_clusters = new_clusters;
  831. spin_unlock(&OCFS2_I(inode)->ip_lock);
  832. if (free_head)
  833. __ocfs2_extent_map_drop_cleanup(free_head);
  834. return 0;
  835. }
  836. int __init init_ocfs2_extent_maps(void)
  837. {
  838. ocfs2_em_ent_cachep =
  839. kmem_cache_create("ocfs2_em_ent",
  840. sizeof(struct ocfs2_extent_map_entry),
  841. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  842. if (!ocfs2_em_ent_cachep)
  843. return -ENOMEM;
  844. return 0;
  845. }
  846. void __exit exit_ocfs2_extent_maps(void)
  847. {
  848. kmem_cache_destroy(ocfs2_em_ent_cachep);
  849. }