xfs_iext_tree.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017 Christoph Hellwig.
  4. */
  5. #include <linux/cache.h>
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include "xfs.h"
  9. #include "xfs_format.h"
  10. #include "xfs_bit.h"
  11. #include "xfs_log_format.h"
  12. #include "xfs_inode.h"
  13. #include "xfs_inode_fork.h"
  14. #include "xfs_trans_resv.h"
  15. #include "xfs_mount.h"
  16. #include "xfs_trace.h"
  17. /*
  18. * In-core extent record layout:
  19. *
  20. * +-------+----------------------------+
  21. * | 00:53 | all 54 bits of startoff |
  22. * | 54:63 | low 10 bits of startblock |
  23. * +-------+----------------------------+
  24. * | 00:20 | all 21 bits of length |
  25. * | 21 | unwritten extent bit |
  26. * | 22:63 | high 42 bits of startblock |
  27. * +-------+----------------------------+
  28. */
  29. #define XFS_IEXT_STARTOFF_MASK xfs_mask64lo(BMBT_STARTOFF_BITLEN)
  30. #define XFS_IEXT_LENGTH_MASK xfs_mask64lo(BMBT_BLOCKCOUNT_BITLEN)
  31. #define XFS_IEXT_STARTBLOCK_MASK xfs_mask64lo(BMBT_STARTBLOCK_BITLEN)
  32. struct xfs_iext_rec {
  33. uint64_t lo;
  34. uint64_t hi;
  35. };
  36. /*
  37. * Given that the length can't be a zero, only an empty hi value indicates an
  38. * unused record.
  39. */
  40. static bool xfs_iext_rec_is_empty(struct xfs_iext_rec *rec)
  41. {
  42. return rec->hi == 0;
  43. }
  44. static inline void xfs_iext_rec_clear(struct xfs_iext_rec *rec)
  45. {
  46. rec->lo = 0;
  47. rec->hi = 0;
  48. }
  49. static void
  50. xfs_iext_set(
  51. struct xfs_iext_rec *rec,
  52. struct xfs_bmbt_irec *irec)
  53. {
  54. ASSERT((irec->br_startoff & ~XFS_IEXT_STARTOFF_MASK) == 0);
  55. ASSERT((irec->br_blockcount & ~XFS_IEXT_LENGTH_MASK) == 0);
  56. ASSERT((irec->br_startblock & ~XFS_IEXT_STARTBLOCK_MASK) == 0);
  57. rec->lo = irec->br_startoff & XFS_IEXT_STARTOFF_MASK;
  58. rec->hi = irec->br_blockcount & XFS_IEXT_LENGTH_MASK;
  59. rec->lo |= (irec->br_startblock << 54);
  60. rec->hi |= ((irec->br_startblock & ~xfs_mask64lo(10)) << (22 - 10));
  61. if (irec->br_state == XFS_EXT_UNWRITTEN)
  62. rec->hi |= (1 << 21);
  63. }
  64. static void
  65. xfs_iext_get(
  66. struct xfs_bmbt_irec *irec,
  67. struct xfs_iext_rec *rec)
  68. {
  69. irec->br_startoff = rec->lo & XFS_IEXT_STARTOFF_MASK;
  70. irec->br_blockcount = rec->hi & XFS_IEXT_LENGTH_MASK;
  71. irec->br_startblock = rec->lo >> 54;
  72. irec->br_startblock |= (rec->hi & xfs_mask64hi(42)) >> (22 - 10);
  73. if (rec->hi & (1 << 21))
  74. irec->br_state = XFS_EXT_UNWRITTEN;
  75. else
  76. irec->br_state = XFS_EXT_NORM;
  77. }
  78. enum {
  79. NODE_SIZE = 256,
  80. KEYS_PER_NODE = NODE_SIZE / (sizeof(uint64_t) + sizeof(void *)),
  81. RECS_PER_LEAF = (NODE_SIZE - (2 * sizeof(struct xfs_iext_leaf *))) /
  82. sizeof(struct xfs_iext_rec),
  83. };
  84. /*
  85. * In-core extent btree block layout:
  86. *
  87. * There are two types of blocks in the btree: leaf and inner (non-leaf) blocks.
  88. *
  89. * The leaf blocks are made up by %KEYS_PER_NODE extent records, which each
  90. * contain the startoffset, blockcount, startblock and unwritten extent flag.
  91. * See above for the exact format, followed by pointers to the previous and next
  92. * leaf blocks (if there are any).
  93. *
  94. * The inner (non-leaf) blocks first contain KEYS_PER_NODE lookup keys, followed
  95. * by an equal number of pointers to the btree blocks at the next lower level.
  96. *
  97. * +-------+-------+-------+-------+-------+----------+----------+
  98. * Leaf: | rec 1 | rec 2 | rec 3 | rec 4 | rec N | prev-ptr | next-ptr |
  99. * +-------+-------+-------+-------+-------+----------+----------+
  100. *
  101. * +-------+-------+-------+-------+-------+-------+------+-------+
  102. * Inner: | key 1 | key 2 | key 3 | key N | ptr 1 | ptr 2 | ptr3 | ptr N |
  103. * +-------+-------+-------+-------+-------+-------+------+-------+
  104. */
  105. struct xfs_iext_node {
  106. uint64_t keys[KEYS_PER_NODE];
  107. #define XFS_IEXT_KEY_INVALID (1ULL << 63)
  108. void *ptrs[KEYS_PER_NODE];
  109. };
  110. struct xfs_iext_leaf {
  111. struct xfs_iext_rec recs[RECS_PER_LEAF];
  112. struct xfs_iext_leaf *prev;
  113. struct xfs_iext_leaf *next;
  114. };
  115. inline xfs_extnum_t xfs_iext_count(struct xfs_ifork *ifp)
  116. {
  117. return ifp->if_bytes / sizeof(struct xfs_iext_rec);
  118. }
  119. static inline int xfs_iext_max_recs(struct xfs_ifork *ifp)
  120. {
  121. if (ifp->if_height == 1)
  122. return xfs_iext_count(ifp);
  123. return RECS_PER_LEAF;
  124. }
  125. static inline struct xfs_iext_rec *cur_rec(struct xfs_iext_cursor *cur)
  126. {
  127. return &cur->leaf->recs[cur->pos];
  128. }
  129. static inline bool xfs_iext_valid(struct xfs_ifork *ifp,
  130. struct xfs_iext_cursor *cur)
  131. {
  132. if (!cur->leaf)
  133. return false;
  134. if (cur->pos < 0 || cur->pos >= xfs_iext_max_recs(ifp))
  135. return false;
  136. if (xfs_iext_rec_is_empty(cur_rec(cur)))
  137. return false;
  138. return true;
  139. }
  140. static void *
  141. xfs_iext_find_first_leaf(
  142. struct xfs_ifork *ifp)
  143. {
  144. struct xfs_iext_node *node = ifp->if_u1.if_root;
  145. int height;
  146. if (!ifp->if_height)
  147. return NULL;
  148. for (height = ifp->if_height; height > 1; height--) {
  149. node = node->ptrs[0];
  150. ASSERT(node);
  151. }
  152. return node;
  153. }
  154. static void *
  155. xfs_iext_find_last_leaf(
  156. struct xfs_ifork *ifp)
  157. {
  158. struct xfs_iext_node *node = ifp->if_u1.if_root;
  159. int height, i;
  160. if (!ifp->if_height)
  161. return NULL;
  162. for (height = ifp->if_height; height > 1; height--) {
  163. for (i = 1; i < KEYS_PER_NODE; i++)
  164. if (!node->ptrs[i])
  165. break;
  166. node = node->ptrs[i - 1];
  167. ASSERT(node);
  168. }
  169. return node;
  170. }
  171. void
  172. xfs_iext_first(
  173. struct xfs_ifork *ifp,
  174. struct xfs_iext_cursor *cur)
  175. {
  176. cur->pos = 0;
  177. cur->leaf = xfs_iext_find_first_leaf(ifp);
  178. }
  179. void
  180. xfs_iext_last(
  181. struct xfs_ifork *ifp,
  182. struct xfs_iext_cursor *cur)
  183. {
  184. int i;
  185. cur->leaf = xfs_iext_find_last_leaf(ifp);
  186. if (!cur->leaf) {
  187. cur->pos = 0;
  188. return;
  189. }
  190. for (i = 1; i < xfs_iext_max_recs(ifp); i++) {
  191. if (xfs_iext_rec_is_empty(&cur->leaf->recs[i]))
  192. break;
  193. }
  194. cur->pos = i - 1;
  195. }
  196. void
  197. xfs_iext_next(
  198. struct xfs_ifork *ifp,
  199. struct xfs_iext_cursor *cur)
  200. {
  201. if (!cur->leaf) {
  202. ASSERT(cur->pos <= 0 || cur->pos >= RECS_PER_LEAF);
  203. xfs_iext_first(ifp, cur);
  204. return;
  205. }
  206. ASSERT(cur->pos >= 0);
  207. ASSERT(cur->pos < xfs_iext_max_recs(ifp));
  208. cur->pos++;
  209. if (ifp->if_height > 1 && !xfs_iext_valid(ifp, cur) &&
  210. cur->leaf->next) {
  211. cur->leaf = cur->leaf->next;
  212. cur->pos = 0;
  213. }
  214. }
  215. void
  216. xfs_iext_prev(
  217. struct xfs_ifork *ifp,
  218. struct xfs_iext_cursor *cur)
  219. {
  220. if (!cur->leaf) {
  221. ASSERT(cur->pos <= 0 || cur->pos >= RECS_PER_LEAF);
  222. xfs_iext_last(ifp, cur);
  223. return;
  224. }
  225. ASSERT(cur->pos >= 0);
  226. ASSERT(cur->pos <= RECS_PER_LEAF);
  227. recurse:
  228. do {
  229. cur->pos--;
  230. if (xfs_iext_valid(ifp, cur))
  231. return;
  232. } while (cur->pos > 0);
  233. if (ifp->if_height > 1 && cur->leaf->prev) {
  234. cur->leaf = cur->leaf->prev;
  235. cur->pos = RECS_PER_LEAF;
  236. goto recurse;
  237. }
  238. }
  239. static inline int
  240. xfs_iext_key_cmp(
  241. struct xfs_iext_node *node,
  242. int n,
  243. xfs_fileoff_t offset)
  244. {
  245. if (node->keys[n] > offset)
  246. return 1;
  247. if (node->keys[n] < offset)
  248. return -1;
  249. return 0;
  250. }
  251. static inline int
  252. xfs_iext_rec_cmp(
  253. struct xfs_iext_rec *rec,
  254. xfs_fileoff_t offset)
  255. {
  256. uint64_t rec_offset = rec->lo & XFS_IEXT_STARTOFF_MASK;
  257. uint32_t rec_len = rec->hi & XFS_IEXT_LENGTH_MASK;
  258. if (rec_offset > offset)
  259. return 1;
  260. if (rec_offset + rec_len <= offset)
  261. return -1;
  262. return 0;
  263. }
  264. static void *
  265. xfs_iext_find_level(
  266. struct xfs_ifork *ifp,
  267. xfs_fileoff_t offset,
  268. int level)
  269. {
  270. struct xfs_iext_node *node = ifp->if_u1.if_root;
  271. int height, i;
  272. if (!ifp->if_height)
  273. return NULL;
  274. for (height = ifp->if_height; height > level; height--) {
  275. for (i = 1; i < KEYS_PER_NODE; i++)
  276. if (xfs_iext_key_cmp(node, i, offset) > 0)
  277. break;
  278. node = node->ptrs[i - 1];
  279. if (!node)
  280. break;
  281. }
  282. return node;
  283. }
  284. static int
  285. xfs_iext_node_pos(
  286. struct xfs_iext_node *node,
  287. xfs_fileoff_t offset)
  288. {
  289. int i;
  290. for (i = 1; i < KEYS_PER_NODE; i++) {
  291. if (xfs_iext_key_cmp(node, i, offset) > 0)
  292. break;
  293. }
  294. return i - 1;
  295. }
  296. static int
  297. xfs_iext_node_insert_pos(
  298. struct xfs_iext_node *node,
  299. xfs_fileoff_t offset)
  300. {
  301. int i;
  302. for (i = 0; i < KEYS_PER_NODE; i++) {
  303. if (xfs_iext_key_cmp(node, i, offset) > 0)
  304. return i;
  305. }
  306. return KEYS_PER_NODE;
  307. }
  308. static int
  309. xfs_iext_node_nr_entries(
  310. struct xfs_iext_node *node,
  311. int start)
  312. {
  313. int i;
  314. for (i = start; i < KEYS_PER_NODE; i++) {
  315. if (node->keys[i] == XFS_IEXT_KEY_INVALID)
  316. break;
  317. }
  318. return i;
  319. }
  320. static int
  321. xfs_iext_leaf_nr_entries(
  322. struct xfs_ifork *ifp,
  323. struct xfs_iext_leaf *leaf,
  324. int start)
  325. {
  326. int i;
  327. for (i = start; i < xfs_iext_max_recs(ifp); i++) {
  328. if (xfs_iext_rec_is_empty(&leaf->recs[i]))
  329. break;
  330. }
  331. return i;
  332. }
  333. static inline uint64_t
  334. xfs_iext_leaf_key(
  335. struct xfs_iext_leaf *leaf,
  336. int n)
  337. {
  338. return leaf->recs[n].lo & XFS_IEXT_STARTOFF_MASK;
  339. }
  340. static void
  341. xfs_iext_grow(
  342. struct xfs_ifork *ifp)
  343. {
  344. struct xfs_iext_node *node = kmem_zalloc(NODE_SIZE, KM_NOFS);
  345. int i;
  346. if (ifp->if_height == 1) {
  347. struct xfs_iext_leaf *prev = ifp->if_u1.if_root;
  348. node->keys[0] = xfs_iext_leaf_key(prev, 0);
  349. node->ptrs[0] = prev;
  350. } else {
  351. struct xfs_iext_node *prev = ifp->if_u1.if_root;
  352. ASSERT(ifp->if_height > 1);
  353. node->keys[0] = prev->keys[0];
  354. node->ptrs[0] = prev;
  355. }
  356. for (i = 1; i < KEYS_PER_NODE; i++)
  357. node->keys[i] = XFS_IEXT_KEY_INVALID;
  358. ifp->if_u1.if_root = node;
  359. ifp->if_height++;
  360. }
  361. static void
  362. xfs_iext_update_node(
  363. struct xfs_ifork *ifp,
  364. xfs_fileoff_t old_offset,
  365. xfs_fileoff_t new_offset,
  366. int level,
  367. void *ptr)
  368. {
  369. struct xfs_iext_node *node = ifp->if_u1.if_root;
  370. int height, i;
  371. for (height = ifp->if_height; height > level; height--) {
  372. for (i = 0; i < KEYS_PER_NODE; i++) {
  373. if (i > 0 && xfs_iext_key_cmp(node, i, old_offset) > 0)
  374. break;
  375. if (node->keys[i] == old_offset)
  376. node->keys[i] = new_offset;
  377. }
  378. node = node->ptrs[i - 1];
  379. ASSERT(node);
  380. }
  381. ASSERT(node == ptr);
  382. }
  383. static struct xfs_iext_node *
  384. xfs_iext_split_node(
  385. struct xfs_iext_node **nodep,
  386. int *pos,
  387. int *nr_entries)
  388. {
  389. struct xfs_iext_node *node = *nodep;
  390. struct xfs_iext_node *new = kmem_zalloc(NODE_SIZE, KM_NOFS);
  391. const int nr_move = KEYS_PER_NODE / 2;
  392. int nr_keep = nr_move + (KEYS_PER_NODE & 1);
  393. int i = 0;
  394. /* for sequential append operations just spill over into the new node */
  395. if (*pos == KEYS_PER_NODE) {
  396. *nodep = new;
  397. *pos = 0;
  398. *nr_entries = 0;
  399. goto done;
  400. }
  401. for (i = 0; i < nr_move; i++) {
  402. new->keys[i] = node->keys[nr_keep + i];
  403. new->ptrs[i] = node->ptrs[nr_keep + i];
  404. node->keys[nr_keep + i] = XFS_IEXT_KEY_INVALID;
  405. node->ptrs[nr_keep + i] = NULL;
  406. }
  407. if (*pos >= nr_keep) {
  408. *nodep = new;
  409. *pos -= nr_keep;
  410. *nr_entries = nr_move;
  411. } else {
  412. *nr_entries = nr_keep;
  413. }
  414. done:
  415. for (; i < KEYS_PER_NODE; i++)
  416. new->keys[i] = XFS_IEXT_KEY_INVALID;
  417. return new;
  418. }
  419. static void
  420. xfs_iext_insert_node(
  421. struct xfs_ifork *ifp,
  422. uint64_t offset,
  423. void *ptr,
  424. int level)
  425. {
  426. struct xfs_iext_node *node, *new;
  427. int i, pos, nr_entries;
  428. again:
  429. if (ifp->if_height < level)
  430. xfs_iext_grow(ifp);
  431. new = NULL;
  432. node = xfs_iext_find_level(ifp, offset, level);
  433. pos = xfs_iext_node_insert_pos(node, offset);
  434. nr_entries = xfs_iext_node_nr_entries(node, pos);
  435. ASSERT(pos >= nr_entries || xfs_iext_key_cmp(node, pos, offset) != 0);
  436. ASSERT(nr_entries <= KEYS_PER_NODE);
  437. if (nr_entries == KEYS_PER_NODE)
  438. new = xfs_iext_split_node(&node, &pos, &nr_entries);
  439. /*
  440. * Update the pointers in higher levels if the first entry changes
  441. * in an existing node.
  442. */
  443. if (node != new && pos == 0 && nr_entries > 0)
  444. xfs_iext_update_node(ifp, node->keys[0], offset, level, node);
  445. for (i = nr_entries; i > pos; i--) {
  446. node->keys[i] = node->keys[i - 1];
  447. node->ptrs[i] = node->ptrs[i - 1];
  448. }
  449. node->keys[pos] = offset;
  450. node->ptrs[pos] = ptr;
  451. if (new) {
  452. offset = new->keys[0];
  453. ptr = new;
  454. level++;
  455. goto again;
  456. }
  457. }
  458. static struct xfs_iext_leaf *
  459. xfs_iext_split_leaf(
  460. struct xfs_iext_cursor *cur,
  461. int *nr_entries)
  462. {
  463. struct xfs_iext_leaf *leaf = cur->leaf;
  464. struct xfs_iext_leaf *new = kmem_zalloc(NODE_SIZE, KM_NOFS);
  465. const int nr_move = RECS_PER_LEAF / 2;
  466. int nr_keep = nr_move + (RECS_PER_LEAF & 1);
  467. int i;
  468. /* for sequential append operations just spill over into the new node */
  469. if (cur->pos == RECS_PER_LEAF) {
  470. cur->leaf = new;
  471. cur->pos = 0;
  472. *nr_entries = 0;
  473. goto done;
  474. }
  475. for (i = 0; i < nr_move; i++) {
  476. new->recs[i] = leaf->recs[nr_keep + i];
  477. xfs_iext_rec_clear(&leaf->recs[nr_keep + i]);
  478. }
  479. if (cur->pos >= nr_keep) {
  480. cur->leaf = new;
  481. cur->pos -= nr_keep;
  482. *nr_entries = nr_move;
  483. } else {
  484. *nr_entries = nr_keep;
  485. }
  486. done:
  487. if (leaf->next)
  488. leaf->next->prev = new;
  489. new->next = leaf->next;
  490. new->prev = leaf;
  491. leaf->next = new;
  492. return new;
  493. }
  494. static void
  495. xfs_iext_alloc_root(
  496. struct xfs_ifork *ifp,
  497. struct xfs_iext_cursor *cur)
  498. {
  499. ASSERT(ifp->if_bytes == 0);
  500. ifp->if_u1.if_root = kmem_zalloc(sizeof(struct xfs_iext_rec), KM_NOFS);
  501. ifp->if_height = 1;
  502. /* now that we have a node step into it */
  503. cur->leaf = ifp->if_u1.if_root;
  504. cur->pos = 0;
  505. }
  506. static void
  507. xfs_iext_realloc_root(
  508. struct xfs_ifork *ifp,
  509. struct xfs_iext_cursor *cur)
  510. {
  511. size_t new_size = ifp->if_bytes + sizeof(struct xfs_iext_rec);
  512. void *new;
  513. /* account for the prev/next pointers */
  514. if (new_size / sizeof(struct xfs_iext_rec) == RECS_PER_LEAF)
  515. new_size = NODE_SIZE;
  516. new = kmem_realloc(ifp->if_u1.if_root, new_size, KM_NOFS);
  517. memset(new + ifp->if_bytes, 0, new_size - ifp->if_bytes);
  518. ifp->if_u1.if_root = new;
  519. cur->leaf = new;
  520. }
  521. void
  522. xfs_iext_insert(
  523. struct xfs_inode *ip,
  524. struct xfs_iext_cursor *cur,
  525. struct xfs_bmbt_irec *irec,
  526. int state)
  527. {
  528. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  529. xfs_fileoff_t offset = irec->br_startoff;
  530. struct xfs_iext_leaf *new = NULL;
  531. int nr_entries, i;
  532. if (ifp->if_height == 0)
  533. xfs_iext_alloc_root(ifp, cur);
  534. else if (ifp->if_height == 1)
  535. xfs_iext_realloc_root(ifp, cur);
  536. nr_entries = xfs_iext_leaf_nr_entries(ifp, cur->leaf, cur->pos);
  537. ASSERT(nr_entries <= RECS_PER_LEAF);
  538. ASSERT(cur->pos >= nr_entries ||
  539. xfs_iext_rec_cmp(cur_rec(cur), irec->br_startoff) != 0);
  540. if (nr_entries == RECS_PER_LEAF)
  541. new = xfs_iext_split_leaf(cur, &nr_entries);
  542. /*
  543. * Update the pointers in higher levels if the first entry changes
  544. * in an existing node.
  545. */
  546. if (cur->leaf != new && cur->pos == 0 && nr_entries > 0) {
  547. xfs_iext_update_node(ifp, xfs_iext_leaf_key(cur->leaf, 0),
  548. offset, 1, cur->leaf);
  549. }
  550. for (i = nr_entries; i > cur->pos; i--)
  551. cur->leaf->recs[i] = cur->leaf->recs[i - 1];
  552. xfs_iext_set(cur_rec(cur), irec);
  553. ifp->if_bytes += sizeof(struct xfs_iext_rec);
  554. trace_xfs_iext_insert(ip, cur, state, _RET_IP_);
  555. if (new)
  556. xfs_iext_insert_node(ifp, xfs_iext_leaf_key(new, 0), new, 2);
  557. }
  558. static struct xfs_iext_node *
  559. xfs_iext_rebalance_node(
  560. struct xfs_iext_node *parent,
  561. int *pos,
  562. struct xfs_iext_node *node,
  563. int nr_entries)
  564. {
  565. /*
  566. * If the neighbouring nodes are completely full, or have different
  567. * parents, we might never be able to merge our node, and will only
  568. * delete it once the number of entries hits zero.
  569. */
  570. if (nr_entries == 0)
  571. return node;
  572. if (*pos > 0) {
  573. struct xfs_iext_node *prev = parent->ptrs[*pos - 1];
  574. int nr_prev = xfs_iext_node_nr_entries(prev, 0), i;
  575. if (nr_prev + nr_entries <= KEYS_PER_NODE) {
  576. for (i = 0; i < nr_entries; i++) {
  577. prev->keys[nr_prev + i] = node->keys[i];
  578. prev->ptrs[nr_prev + i] = node->ptrs[i];
  579. }
  580. return node;
  581. }
  582. }
  583. if (*pos + 1 < xfs_iext_node_nr_entries(parent, *pos)) {
  584. struct xfs_iext_node *next = parent->ptrs[*pos + 1];
  585. int nr_next = xfs_iext_node_nr_entries(next, 0), i;
  586. if (nr_entries + nr_next <= KEYS_PER_NODE) {
  587. /*
  588. * Merge the next node into this node so that we don't
  589. * have to do an additional update of the keys in the
  590. * higher levels.
  591. */
  592. for (i = 0; i < nr_next; i++) {
  593. node->keys[nr_entries + i] = next->keys[i];
  594. node->ptrs[nr_entries + i] = next->ptrs[i];
  595. }
  596. ++*pos;
  597. return next;
  598. }
  599. }
  600. return NULL;
  601. }
  602. static void
  603. xfs_iext_remove_node(
  604. struct xfs_ifork *ifp,
  605. xfs_fileoff_t offset,
  606. void *victim)
  607. {
  608. struct xfs_iext_node *node, *parent;
  609. int level = 2, pos, nr_entries, i;
  610. ASSERT(level <= ifp->if_height);
  611. node = xfs_iext_find_level(ifp, offset, level);
  612. pos = xfs_iext_node_pos(node, offset);
  613. again:
  614. ASSERT(node->ptrs[pos]);
  615. ASSERT(node->ptrs[pos] == victim);
  616. kmem_free(victim);
  617. nr_entries = xfs_iext_node_nr_entries(node, pos) - 1;
  618. offset = node->keys[0];
  619. for (i = pos; i < nr_entries; i++) {
  620. node->keys[i] = node->keys[i + 1];
  621. node->ptrs[i] = node->ptrs[i + 1];
  622. }
  623. node->keys[nr_entries] = XFS_IEXT_KEY_INVALID;
  624. node->ptrs[nr_entries] = NULL;
  625. if (pos == 0 && nr_entries > 0) {
  626. xfs_iext_update_node(ifp, offset, node->keys[0], level, node);
  627. offset = node->keys[0];
  628. }
  629. if (nr_entries >= KEYS_PER_NODE / 2)
  630. return;
  631. if (level < ifp->if_height) {
  632. /*
  633. * If we aren't at the root yet try to find a neighbour node to
  634. * merge with (or delete the node if it is empty), and then
  635. * recurse up to the next level.
  636. */
  637. level++;
  638. parent = xfs_iext_find_level(ifp, offset, level);
  639. pos = xfs_iext_node_pos(parent, offset);
  640. ASSERT(pos != KEYS_PER_NODE);
  641. ASSERT(parent->ptrs[pos] == node);
  642. node = xfs_iext_rebalance_node(parent, &pos, node, nr_entries);
  643. if (node) {
  644. victim = node;
  645. node = parent;
  646. goto again;
  647. }
  648. } else if (nr_entries == 1) {
  649. /*
  650. * If we are at the root and only one entry is left we can just
  651. * free this node and update the root pointer.
  652. */
  653. ASSERT(node == ifp->if_u1.if_root);
  654. ifp->if_u1.if_root = node->ptrs[0];
  655. ifp->if_height--;
  656. kmem_free(node);
  657. }
  658. }
  659. static void
  660. xfs_iext_rebalance_leaf(
  661. struct xfs_ifork *ifp,
  662. struct xfs_iext_cursor *cur,
  663. struct xfs_iext_leaf *leaf,
  664. xfs_fileoff_t offset,
  665. int nr_entries)
  666. {
  667. /*
  668. * If the neighbouring nodes are completely full we might never be able
  669. * to merge our node, and will only delete it once the number of
  670. * entries hits zero.
  671. */
  672. if (nr_entries == 0)
  673. goto remove_node;
  674. if (leaf->prev) {
  675. int nr_prev = xfs_iext_leaf_nr_entries(ifp, leaf->prev, 0), i;
  676. if (nr_prev + nr_entries <= RECS_PER_LEAF) {
  677. for (i = 0; i < nr_entries; i++)
  678. leaf->prev->recs[nr_prev + i] = leaf->recs[i];
  679. if (cur->leaf == leaf) {
  680. cur->leaf = leaf->prev;
  681. cur->pos += nr_prev;
  682. }
  683. goto remove_node;
  684. }
  685. }
  686. if (leaf->next) {
  687. int nr_next = xfs_iext_leaf_nr_entries(ifp, leaf->next, 0), i;
  688. if (nr_entries + nr_next <= RECS_PER_LEAF) {
  689. /*
  690. * Merge the next node into this node so that we don't
  691. * have to do an additional update of the keys in the
  692. * higher levels.
  693. */
  694. for (i = 0; i < nr_next; i++) {
  695. leaf->recs[nr_entries + i] =
  696. leaf->next->recs[i];
  697. }
  698. if (cur->leaf == leaf->next) {
  699. cur->leaf = leaf;
  700. cur->pos += nr_entries;
  701. }
  702. offset = xfs_iext_leaf_key(leaf->next, 0);
  703. leaf = leaf->next;
  704. goto remove_node;
  705. }
  706. }
  707. return;
  708. remove_node:
  709. if (leaf->prev)
  710. leaf->prev->next = leaf->next;
  711. if (leaf->next)
  712. leaf->next->prev = leaf->prev;
  713. xfs_iext_remove_node(ifp, offset, leaf);
  714. }
  715. static void
  716. xfs_iext_free_last_leaf(
  717. struct xfs_ifork *ifp)
  718. {
  719. ifp->if_height--;
  720. kmem_free(ifp->if_u1.if_root);
  721. ifp->if_u1.if_root = NULL;
  722. }
  723. void
  724. xfs_iext_remove(
  725. struct xfs_inode *ip,
  726. struct xfs_iext_cursor *cur,
  727. int state)
  728. {
  729. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  730. struct xfs_iext_leaf *leaf = cur->leaf;
  731. xfs_fileoff_t offset = xfs_iext_leaf_key(leaf, 0);
  732. int i, nr_entries;
  733. trace_xfs_iext_remove(ip, cur, state, _RET_IP_);
  734. ASSERT(ifp->if_height > 0);
  735. ASSERT(ifp->if_u1.if_root != NULL);
  736. ASSERT(xfs_iext_valid(ifp, cur));
  737. nr_entries = xfs_iext_leaf_nr_entries(ifp, leaf, cur->pos) - 1;
  738. for (i = cur->pos; i < nr_entries; i++)
  739. leaf->recs[i] = leaf->recs[i + 1];
  740. xfs_iext_rec_clear(&leaf->recs[nr_entries]);
  741. ifp->if_bytes -= sizeof(struct xfs_iext_rec);
  742. if (cur->pos == 0 && nr_entries > 0) {
  743. xfs_iext_update_node(ifp, offset, xfs_iext_leaf_key(leaf, 0), 1,
  744. leaf);
  745. offset = xfs_iext_leaf_key(leaf, 0);
  746. } else if (cur->pos == nr_entries) {
  747. if (ifp->if_height > 1 && leaf->next)
  748. cur->leaf = leaf->next;
  749. else
  750. cur->leaf = NULL;
  751. cur->pos = 0;
  752. }
  753. if (nr_entries >= RECS_PER_LEAF / 2)
  754. return;
  755. if (ifp->if_height > 1)
  756. xfs_iext_rebalance_leaf(ifp, cur, leaf, offset, nr_entries);
  757. else if (nr_entries == 0)
  758. xfs_iext_free_last_leaf(ifp);
  759. }
  760. /*
  761. * Lookup the extent covering bno.
  762. *
  763. * If there is an extent covering bno return the extent index, and store the
  764. * expanded extent structure in *gotp, and the extent cursor in *cur.
  765. * If there is no extent covering bno, but there is an extent after it (e.g.
  766. * it lies in a hole) return that extent in *gotp and its cursor in *cur
  767. * instead.
  768. * If bno is beyond the last extent return false, and return an invalid
  769. * cursor value.
  770. */
  771. bool
  772. xfs_iext_lookup_extent(
  773. struct xfs_inode *ip,
  774. struct xfs_ifork *ifp,
  775. xfs_fileoff_t offset,
  776. struct xfs_iext_cursor *cur,
  777. struct xfs_bmbt_irec *gotp)
  778. {
  779. XFS_STATS_INC(ip->i_mount, xs_look_exlist);
  780. cur->leaf = xfs_iext_find_level(ifp, offset, 1);
  781. if (!cur->leaf) {
  782. cur->pos = 0;
  783. return false;
  784. }
  785. for (cur->pos = 0; cur->pos < xfs_iext_max_recs(ifp); cur->pos++) {
  786. struct xfs_iext_rec *rec = cur_rec(cur);
  787. if (xfs_iext_rec_is_empty(rec))
  788. break;
  789. if (xfs_iext_rec_cmp(rec, offset) >= 0)
  790. goto found;
  791. }
  792. /* Try looking in the next node for an entry > offset */
  793. if (ifp->if_height == 1 || !cur->leaf->next)
  794. return false;
  795. cur->leaf = cur->leaf->next;
  796. cur->pos = 0;
  797. if (!xfs_iext_valid(ifp, cur))
  798. return false;
  799. found:
  800. xfs_iext_get(gotp, cur_rec(cur));
  801. return true;
  802. }
  803. /*
  804. * Returns the last extent before end, and if this extent doesn't cover
  805. * end, update end to the end of the extent.
  806. */
  807. bool
  808. xfs_iext_lookup_extent_before(
  809. struct xfs_inode *ip,
  810. struct xfs_ifork *ifp,
  811. xfs_fileoff_t *end,
  812. struct xfs_iext_cursor *cur,
  813. struct xfs_bmbt_irec *gotp)
  814. {
  815. /* could be optimized to not even look up the next on a match.. */
  816. if (xfs_iext_lookup_extent(ip, ifp, *end - 1, cur, gotp) &&
  817. gotp->br_startoff <= *end - 1)
  818. return true;
  819. if (!xfs_iext_prev_extent(ifp, cur, gotp))
  820. return false;
  821. *end = gotp->br_startoff + gotp->br_blockcount;
  822. return true;
  823. }
  824. void
  825. xfs_iext_update_extent(
  826. struct xfs_inode *ip,
  827. int state,
  828. struct xfs_iext_cursor *cur,
  829. struct xfs_bmbt_irec *new)
  830. {
  831. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  832. if (cur->pos == 0) {
  833. struct xfs_bmbt_irec old;
  834. xfs_iext_get(&old, cur_rec(cur));
  835. if (new->br_startoff != old.br_startoff) {
  836. xfs_iext_update_node(ifp, old.br_startoff,
  837. new->br_startoff, 1, cur->leaf);
  838. }
  839. }
  840. trace_xfs_bmap_pre_update(ip, cur, state, _RET_IP_);
  841. xfs_iext_set(cur_rec(cur), new);
  842. trace_xfs_bmap_post_update(ip, cur, state, _RET_IP_);
  843. }
  844. /*
  845. * Return true if the cursor points at an extent and return the extent structure
  846. * in gotp. Else return false.
  847. */
  848. bool
  849. xfs_iext_get_extent(
  850. struct xfs_ifork *ifp,
  851. struct xfs_iext_cursor *cur,
  852. struct xfs_bmbt_irec *gotp)
  853. {
  854. if (!xfs_iext_valid(ifp, cur))
  855. return false;
  856. xfs_iext_get(gotp, cur_rec(cur));
  857. return true;
  858. }
  859. /*
  860. * This is a recursive function, because of that we need to be extremely
  861. * careful with stack usage.
  862. */
  863. static void
  864. xfs_iext_destroy_node(
  865. struct xfs_iext_node *node,
  866. int level)
  867. {
  868. int i;
  869. if (level > 1) {
  870. for (i = 0; i < KEYS_PER_NODE; i++) {
  871. if (node->keys[i] == XFS_IEXT_KEY_INVALID)
  872. break;
  873. xfs_iext_destroy_node(node->ptrs[i], level - 1);
  874. }
  875. }
  876. kmem_free(node);
  877. }
  878. void
  879. xfs_iext_destroy(
  880. struct xfs_ifork *ifp)
  881. {
  882. xfs_iext_destroy_node(ifp->if_u1.if_root, ifp->if_height);
  883. ifp->if_bytes = 0;
  884. ifp->if_height = 0;
  885. ifp->if_u1.if_root = NULL;
  886. }