xfs_iext_tree.c 23 KB

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