xfs_iext_tree.c 23 KB

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