extent_map.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 %llu of inode %llu goes past ip_clusters of %u\n",
  166. i,
  167. (unsigned long long)le64_to_cpu(rec->e_blkno),
  168. (unsigned long long)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 %llu; e_blkno %llu and rec %d at e_blkno %llu\n",
  213. cpos, clusters,
  214. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  215. (unsigned long long)blkno, i,
  216. (unsigned long long)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 %llu\n",
  229. cpos, clusters,
  230. (unsigned long long)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 %llu of inode %llu goes past ip_clusters of %u\n",
  262. i,
  263. (unsigned long long)le64_to_cpu(rec->e_blkno),
  264. (unsigned long long)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 && (ret != -EEXIST)) {
  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. * Simple rule #2: A return code of -EEXIST from this function or
  383. * its calls to ocfs2_extent_map_insert_entry() signifies that another
  384. * thread beat us to the insert. It is not an actual error, but it
  385. * tells the caller we have no more work to do.
  386. */
  387. static int ocfs2_extent_map_try_insert(struct inode *inode,
  388. struct ocfs2_extent_rec *rec,
  389. int tree_depth,
  390. struct ocfs2_em_insert_context *ctxt)
  391. {
  392. int ret;
  393. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  394. struct ocfs2_extent_map_entry *old_ent;
  395. ctxt->need_left = 0;
  396. ctxt->need_right = 0;
  397. ctxt->old_ent = NULL;
  398. spin_lock(&OCFS2_I(inode)->ip_lock);
  399. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  400. if (!ret) {
  401. ctxt->new_ent = NULL;
  402. goto out_unlock;
  403. }
  404. /* Since insert_entry failed, the map MUST have old_ent */
  405. old_ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos),
  406. le32_to_cpu(rec->e_clusters),
  407. NULL, NULL);
  408. BUG_ON(!old_ent);
  409. if (old_ent->e_tree_depth < tree_depth) {
  410. /* Another thread beat us to the lower tree_depth */
  411. ret = -EEXIST;
  412. goto out_unlock;
  413. }
  414. if (old_ent->e_tree_depth == tree_depth) {
  415. /*
  416. * Another thread beat us to this tree_depth.
  417. * Let's make sure we agree with that thread (the
  418. * extent_rec should be identical).
  419. */
  420. if (!memcmp(rec, &old_ent->e_rec,
  421. sizeof(struct ocfs2_extent_rec)))
  422. ret = 0;
  423. else
  424. /* FIXME: Should this be ESRCH/EBADR??? */
  425. ret = -EEXIST;
  426. goto out_unlock;
  427. }
  428. /*
  429. * We do it in this order specifically so that no actual tree
  430. * changes occur until we have all the pieces we need. We
  431. * don't want malloc failures to leave an inconsistent tree.
  432. * Whenever we drop the lock, another process could be
  433. * inserting. Also note that, if another process just beat us
  434. * to an insert, we might not need the same pieces we needed
  435. * the first go round. In the end, the pieces we need will
  436. * be used, and the pieces we don't will be freed.
  437. */
  438. ctxt->need_left = !!(le32_to_cpu(rec->e_cpos) >
  439. le32_to_cpu(old_ent->e_rec.e_cpos));
  440. ctxt->need_right = !!((le32_to_cpu(old_ent->e_rec.e_cpos) +
  441. le32_to_cpu(old_ent->e_rec.e_clusters)) >
  442. (le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)));
  443. ret = -EAGAIN;
  444. if (ctxt->need_left) {
  445. if (!ctxt->left_ent)
  446. goto out_unlock;
  447. *(ctxt->left_ent) = *old_ent;
  448. ctxt->left_ent->e_rec.e_clusters =
  449. cpu_to_le32(le32_to_cpu(rec->e_cpos) -
  450. le32_to_cpu(ctxt->left_ent->e_rec.e_cpos));
  451. }
  452. if (ctxt->need_right) {
  453. if (!ctxt->right_ent)
  454. goto out_unlock;
  455. *(ctxt->right_ent) = *old_ent;
  456. ctxt->right_ent->e_rec.e_cpos =
  457. cpu_to_le32(le32_to_cpu(rec->e_cpos) +
  458. le32_to_cpu(rec->e_clusters));
  459. ctxt->right_ent->e_rec.e_clusters =
  460. cpu_to_le32((le32_to_cpu(old_ent->e_rec.e_cpos) +
  461. le32_to_cpu(old_ent->e_rec.e_clusters)) -
  462. le32_to_cpu(ctxt->right_ent->e_rec.e_cpos));
  463. }
  464. rb_erase(&old_ent->e_node, &em->em_extents);
  465. /* Now that he's erased, set him up for deletion */
  466. ctxt->old_ent = old_ent;
  467. if (ctxt->need_left) {
  468. ret = ocfs2_extent_map_insert_entry(em,
  469. ctxt->left_ent);
  470. if (ret)
  471. goto out_unlock;
  472. ctxt->left_ent = NULL;
  473. }
  474. if (ctxt->need_right) {
  475. ret = ocfs2_extent_map_insert_entry(em,
  476. ctxt->right_ent);
  477. if (ret)
  478. goto out_unlock;
  479. ctxt->right_ent = NULL;
  480. }
  481. ret = ocfs2_extent_map_insert_entry(em, ctxt->new_ent);
  482. if (!ret)
  483. ctxt->new_ent = NULL;
  484. out_unlock:
  485. spin_unlock(&OCFS2_I(inode)->ip_lock);
  486. return ret;
  487. }
  488. static int ocfs2_extent_map_insert(struct inode *inode,
  489. struct ocfs2_extent_rec *rec,
  490. int tree_depth)
  491. {
  492. int ret;
  493. struct ocfs2_em_insert_context ctxt = {0, };
  494. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) >
  495. OCFS2_I(inode)->ip_map.em_clusters) {
  496. ret = -EBADR;
  497. mlog_errno(ret);
  498. return ret;
  499. }
  500. /* Zero e_clusters means a truncated tail record. It better be EOF */
  501. if (!rec->e_clusters) {
  502. if ((le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters)) !=
  503. OCFS2_I(inode)->ip_map.em_clusters) {
  504. ret = -EBADR;
  505. mlog_errno(ret);
  506. ocfs2_error(inode->i_sb,
  507. "Zero e_clusters on non-tail extent record at e_blkno %llu on inode %llu\n",
  508. (unsigned long long)le64_to_cpu(rec->e_blkno),
  509. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  510. return ret;
  511. }
  512. /* Ignore the truncated tail */
  513. return 0;
  514. }
  515. ret = -ENOMEM;
  516. ctxt.new_ent = kmem_cache_alloc(ocfs2_em_ent_cachep,
  517. GFP_NOFS);
  518. if (!ctxt.new_ent) {
  519. mlog_errno(ret);
  520. return ret;
  521. }
  522. ctxt.new_ent->e_rec = *rec;
  523. ctxt.new_ent->e_tree_depth = tree_depth;
  524. do {
  525. ret = -ENOMEM;
  526. if (ctxt.need_left && !ctxt.left_ent) {
  527. ctxt.left_ent =
  528. kmem_cache_alloc(ocfs2_em_ent_cachep,
  529. GFP_NOFS);
  530. if (!ctxt.left_ent)
  531. break;
  532. }
  533. if (ctxt.need_right && !ctxt.right_ent) {
  534. ctxt.right_ent =
  535. kmem_cache_alloc(ocfs2_em_ent_cachep,
  536. GFP_NOFS);
  537. if (!ctxt.right_ent)
  538. break;
  539. }
  540. ret = ocfs2_extent_map_try_insert(inode, rec,
  541. tree_depth, &ctxt);
  542. } while (ret == -EAGAIN);
  543. if ((ret < 0) && (ret != -EEXIST))
  544. mlog_errno(ret);
  545. if (ctxt.left_ent)
  546. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.left_ent);
  547. if (ctxt.right_ent)
  548. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.right_ent);
  549. if (ctxt.old_ent)
  550. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.old_ent);
  551. if (ctxt.new_ent)
  552. kmem_cache_free(ocfs2_em_ent_cachep, ctxt.new_ent);
  553. return ret;
  554. }
  555. /*
  556. * Append this record to the tail of the extent map. It must be
  557. * tree_depth 0. The record might be an extension of an existing
  558. * record, and as such that needs to be handled. eg:
  559. *
  560. * Existing record in the extent map:
  561. *
  562. * cpos = 10, len = 10
  563. * |---------|
  564. *
  565. * New Record:
  566. *
  567. * cpos = 10, len = 20
  568. * |------------------|
  569. *
  570. * The passed record is the new on-disk record. The new_clusters value
  571. * is how many clusters were added to the file. If the append is a
  572. * contiguous append, the new_clusters has been added to
  573. * rec->e_clusters. If the append is an entirely new extent, then
  574. * rec->e_clusters is == new_clusters.
  575. */
  576. int ocfs2_extent_map_append(struct inode *inode,
  577. struct ocfs2_extent_rec *rec,
  578. u32 new_clusters)
  579. {
  580. int ret;
  581. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  582. struct ocfs2_extent_map_entry *ent;
  583. struct ocfs2_extent_rec *old;
  584. BUG_ON(!new_clusters);
  585. BUG_ON(le32_to_cpu(rec->e_clusters) < new_clusters);
  586. if (em->em_clusters < OCFS2_I(inode)->ip_clusters) {
  587. /*
  588. * Size changed underneath us on disk. Drop any
  589. * straddling records and update our idea of
  590. * i_clusters
  591. */
  592. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  593. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  594. }
  595. mlog_bug_on_msg((le32_to_cpu(rec->e_cpos) +
  596. le32_to_cpu(rec->e_clusters)) !=
  597. (em->em_clusters + new_clusters),
  598. "Inode %llu:\n"
  599. "rec->e_cpos = %u + rec->e_clusters = %u = %u\n"
  600. "em->em_clusters = %u + new_clusters = %u = %u\n",
  601. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  602. le32_to_cpu(rec->e_cpos), le32_to_cpu(rec->e_clusters),
  603. le32_to_cpu(rec->e_cpos) + le32_to_cpu(rec->e_clusters),
  604. em->em_clusters, new_clusters,
  605. em->em_clusters + new_clusters);
  606. em->em_clusters += new_clusters;
  607. ret = -ENOENT;
  608. if (le32_to_cpu(rec->e_clusters) > new_clusters) {
  609. /* This is a contiguous append */
  610. ent = ocfs2_extent_map_lookup(em, le32_to_cpu(rec->e_cpos), 1,
  611. NULL, NULL);
  612. if (ent) {
  613. old = &ent->e_rec;
  614. BUG_ON((le32_to_cpu(rec->e_cpos) +
  615. le32_to_cpu(rec->e_clusters)) !=
  616. (le32_to_cpu(old->e_cpos) +
  617. le32_to_cpu(old->e_clusters) +
  618. new_clusters));
  619. if (ent->e_tree_depth == 0) {
  620. BUG_ON(le32_to_cpu(old->e_cpos) !=
  621. le32_to_cpu(rec->e_cpos));
  622. BUG_ON(le64_to_cpu(old->e_blkno) !=
  623. le64_to_cpu(rec->e_blkno));
  624. ret = 0;
  625. }
  626. /*
  627. * Let non-leafs fall through as -ENOENT to
  628. * force insertion of the new leaf.
  629. */
  630. le32_add_cpu(&old->e_clusters, new_clusters);
  631. }
  632. }
  633. if (ret == -ENOENT)
  634. ret = ocfs2_extent_map_insert(inode, rec, 0);
  635. if (ret < 0)
  636. mlog_errno(ret);
  637. return ret;
  638. }
  639. #if 0
  640. /* Code here is included but defined out as it completes the extent
  641. * map api and may be used in the future. */
  642. /*
  643. * Look up the record containing this cluster offset. This record is
  644. * part of the extent map. Do not free it. Any changes you make to
  645. * it will reflect in the extent map. So, if your last extent
  646. * is (cpos = 10, clusters = 10) and you truncate the file by 5
  647. * clusters, you can do:
  648. *
  649. * ret = ocfs2_extent_map_get_rec(em, orig_size - 5, &rec);
  650. * rec->e_clusters -= 5;
  651. *
  652. * The lookup does not read from disk. If the map isn't filled in for
  653. * an entry, you won't find it.
  654. *
  655. * Also note that the returned record is valid until alloc_sem is
  656. * dropped. After that, truncate and extend can happen. Caveat Emptor.
  657. */
  658. int ocfs2_extent_map_get_rec(struct inode *inode, u32 cpos,
  659. struct ocfs2_extent_rec **rec,
  660. int *tree_depth)
  661. {
  662. int ret = -ENOENT;
  663. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  664. struct ocfs2_extent_map_entry *ent;
  665. *rec = NULL;
  666. if (cpos >= OCFS2_I(inode)->ip_clusters)
  667. return -EINVAL;
  668. if (cpos >= em->em_clusters) {
  669. /*
  670. * Size changed underneath us on disk. Drop any
  671. * straddling records and update our idea of
  672. * i_clusters
  673. */
  674. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  675. em->em_clusters = OCFS2_I(inode)->ip_clusters ;
  676. }
  677. ent = ocfs2_extent_map_lookup(&OCFS2_I(inode)->ip_map, cpos, 1,
  678. NULL, NULL);
  679. if (ent) {
  680. *rec = &ent->e_rec;
  681. if (tree_depth)
  682. *tree_depth = ent->e_tree_depth;
  683. ret = 0;
  684. }
  685. return ret;
  686. }
  687. int ocfs2_extent_map_get_clusters(struct inode *inode,
  688. u32 v_cpos, int count,
  689. u32 *p_cpos, int *ret_count)
  690. {
  691. int ret;
  692. u32 coff, ccount;
  693. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  694. struct ocfs2_extent_map_entry *ent = NULL;
  695. *p_cpos = ccount = 0;
  696. if ((v_cpos + count) > OCFS2_I(inode)->ip_clusters)
  697. return -EINVAL;
  698. if ((v_cpos + count) > em->em_clusters) {
  699. /*
  700. * Size changed underneath us on disk. Drop any
  701. * straddling records and update our idea of
  702. * i_clusters
  703. */
  704. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  705. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  706. }
  707. ret = ocfs2_extent_map_lookup_read(inode, v_cpos, count, &ent);
  708. if (ret)
  709. return ret;
  710. if (ent) {
  711. /* We should never find ourselves straddling an interval */
  712. if (!ocfs2_extent_rec_contains_clusters(&ent->e_rec,
  713. v_cpos,
  714. count))
  715. return -ESRCH;
  716. coff = v_cpos - le32_to_cpu(ent->e_rec.e_cpos);
  717. *p_cpos = ocfs2_blocks_to_clusters(inode->i_sb,
  718. le64_to_cpu(ent->e_rec.e_blkno)) +
  719. coff;
  720. if (ret_count)
  721. *ret_count = le32_to_cpu(ent->e_rec.e_clusters) - coff;
  722. return 0;
  723. }
  724. return -ENOENT;
  725. }
  726. #endif /* 0 */
  727. int ocfs2_extent_map_get_blocks(struct inode *inode,
  728. u64 v_blkno, int count,
  729. u64 *p_blkno, int *ret_count)
  730. {
  731. int ret;
  732. u64 boff;
  733. u32 cpos, clusters;
  734. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  735. struct ocfs2_extent_map_entry *ent = NULL;
  736. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  737. struct ocfs2_extent_rec *rec;
  738. *p_blkno = 0;
  739. cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
  740. clusters = ocfs2_blocks_to_clusters(inode->i_sb,
  741. (u64)count + bpc - 1);
  742. if ((cpos + clusters) > OCFS2_I(inode)->ip_clusters) {
  743. ret = -EINVAL;
  744. mlog_errno(ret);
  745. return ret;
  746. }
  747. if ((cpos + clusters) > em->em_clusters) {
  748. /*
  749. * Size changed underneath us on disk. Drop any
  750. * straddling records and update our idea of
  751. * i_clusters
  752. */
  753. ocfs2_extent_map_drop(inode, em->em_clusters - 1);
  754. em->em_clusters = OCFS2_I(inode)->ip_clusters;
  755. }
  756. ret = ocfs2_extent_map_lookup_read(inode, cpos, clusters, &ent);
  757. if (ret) {
  758. mlog_errno(ret);
  759. return ret;
  760. }
  761. if (ent)
  762. {
  763. rec = &ent->e_rec;
  764. /* We should never find ourselves straddling an interval */
  765. if (!ocfs2_extent_rec_contains_clusters(rec, cpos, clusters)) {
  766. ret = -ESRCH;
  767. mlog_errno(ret);
  768. return ret;
  769. }
  770. boff = ocfs2_clusters_to_blocks(inode->i_sb, cpos -
  771. le32_to_cpu(rec->e_cpos));
  772. boff += (v_blkno & (u64)(bpc - 1));
  773. *p_blkno = le64_to_cpu(rec->e_blkno) + boff;
  774. if (ret_count) {
  775. *ret_count = ocfs2_clusters_to_blocks(inode->i_sb,
  776. le32_to_cpu(rec->e_clusters)) - boff;
  777. }
  778. return 0;
  779. }
  780. return -ENOENT;
  781. }
  782. int ocfs2_extent_map_init(struct inode *inode)
  783. {
  784. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  785. em->em_extents = RB_ROOT;
  786. em->em_clusters = 0;
  787. return 0;
  788. }
  789. /* Needs the lock */
  790. static void __ocfs2_extent_map_drop(struct inode *inode,
  791. u32 new_clusters,
  792. struct rb_node **free_head,
  793. struct ocfs2_extent_map_entry **tail_ent)
  794. {
  795. struct rb_node *node, *next;
  796. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  797. struct ocfs2_extent_map_entry *ent;
  798. *free_head = NULL;
  799. ent = NULL;
  800. node = rb_last(&em->em_extents);
  801. while (node)
  802. {
  803. next = rb_prev(node);
  804. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  805. e_node);
  806. if (le32_to_cpu(ent->e_rec.e_cpos) < new_clusters)
  807. break;
  808. rb_erase(&ent->e_node, &em->em_extents);
  809. node->rb_right = *free_head;
  810. *free_head = node;
  811. ent = NULL;
  812. node = next;
  813. }
  814. /* Do we have an entry straddling new_clusters? */
  815. if (tail_ent) {
  816. if (ent &&
  817. ((le32_to_cpu(ent->e_rec.e_cpos) +
  818. le32_to_cpu(ent->e_rec.e_clusters)) > new_clusters))
  819. *tail_ent = ent;
  820. else
  821. *tail_ent = NULL;
  822. }
  823. }
  824. static void __ocfs2_extent_map_drop_cleanup(struct rb_node *free_head)
  825. {
  826. struct rb_node *node;
  827. struct ocfs2_extent_map_entry *ent;
  828. while (free_head) {
  829. node = free_head;
  830. free_head = node->rb_right;
  831. ent = rb_entry(node, struct ocfs2_extent_map_entry,
  832. e_node);
  833. kmem_cache_free(ocfs2_em_ent_cachep, ent);
  834. }
  835. }
  836. /*
  837. * Remove all entries past new_clusters, inclusive of an entry that
  838. * contains new_clusters. This is effectively a cache forget.
  839. *
  840. * If you want to also clip the last extent by some number of clusters,
  841. * you need to call ocfs2_extent_map_trunc().
  842. * This code does not check or modify ip_clusters.
  843. */
  844. int ocfs2_extent_map_drop(struct inode *inode, u32 new_clusters)
  845. {
  846. struct rb_node *free_head = NULL;
  847. struct ocfs2_extent_map *em = &OCFS2_I(inode)->ip_map;
  848. struct ocfs2_extent_map_entry *ent;
  849. spin_lock(&OCFS2_I(inode)->ip_lock);
  850. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  851. if (ent) {
  852. rb_erase(&ent->e_node, &em->em_extents);
  853. ent->e_node.rb_right = free_head;
  854. free_head = &ent->e_node;
  855. }
  856. spin_unlock(&OCFS2_I(inode)->ip_lock);
  857. if (free_head)
  858. __ocfs2_extent_map_drop_cleanup(free_head);
  859. return 0;
  860. }
  861. /*
  862. * Remove all entries past new_clusters and also clip any extent
  863. * straddling new_clusters, if there is one. This does not check
  864. * or modify ip_clusters
  865. */
  866. int ocfs2_extent_map_trunc(struct inode *inode, u32 new_clusters)
  867. {
  868. struct rb_node *free_head = NULL;
  869. struct ocfs2_extent_map_entry *ent = NULL;
  870. spin_lock(&OCFS2_I(inode)->ip_lock);
  871. __ocfs2_extent_map_drop(inode, new_clusters, &free_head, &ent);
  872. if (ent)
  873. ent->e_rec.e_clusters = cpu_to_le32(new_clusters -
  874. le32_to_cpu(ent->e_rec.e_cpos));
  875. OCFS2_I(inode)->ip_map.em_clusters = new_clusters;
  876. spin_unlock(&OCFS2_I(inode)->ip_lock);
  877. if (free_head)
  878. __ocfs2_extent_map_drop_cleanup(free_head);
  879. return 0;
  880. }
  881. int __init init_ocfs2_extent_maps(void)
  882. {
  883. ocfs2_em_ent_cachep =
  884. kmem_cache_create("ocfs2_em_ent",
  885. sizeof(struct ocfs2_extent_map_entry),
  886. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  887. if (!ocfs2_em_ent_cachep)
  888. return -ENOMEM;
  889. return 0;
  890. }
  891. void exit_ocfs2_extent_maps(void)
  892. {
  893. kmem_cache_destroy(ocfs2_em_ent_cachep);
  894. }