xfs_iext_tree.c 23 KB

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