extent_map.c 26 KB

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