xfs_iext_tree.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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_data;
  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_data;
  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_data;
  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 inline void *
  338. xfs_iext_alloc_node(
  339. int size)
  340. {
  341. return kzalloc(size, GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  342. }
  343. static void
  344. xfs_iext_grow(
  345. struct xfs_ifork *ifp)
  346. {
  347. struct xfs_iext_node *node = xfs_iext_alloc_node(NODE_SIZE);
  348. int i;
  349. if (ifp->if_height == 1) {
  350. struct xfs_iext_leaf *prev = ifp->if_data;
  351. node->keys[0] = xfs_iext_leaf_key(prev, 0);
  352. node->ptrs[0] = prev;
  353. } else {
  354. struct xfs_iext_node *prev = ifp->if_data;
  355. ASSERT(ifp->if_height > 1);
  356. node->keys[0] = prev->keys[0];
  357. node->ptrs[0] = prev;
  358. }
  359. for (i = 1; i < KEYS_PER_NODE; i++)
  360. node->keys[i] = XFS_IEXT_KEY_INVALID;
  361. ifp->if_data = node;
  362. ifp->if_height++;
  363. }
  364. static void
  365. xfs_iext_update_node(
  366. struct xfs_ifork *ifp,
  367. xfs_fileoff_t old_offset,
  368. xfs_fileoff_t new_offset,
  369. int level,
  370. void *ptr)
  371. {
  372. struct xfs_iext_node *node = ifp->if_data;
  373. int height, i;
  374. for (height = ifp->if_height; height > level; height--) {
  375. for (i = 0; i < KEYS_PER_NODE; i++) {
  376. if (i > 0 && xfs_iext_key_cmp(node, i, old_offset) > 0)
  377. break;
  378. if (node->keys[i] == old_offset)
  379. node->keys[i] = new_offset;
  380. }
  381. node = node->ptrs[i - 1];
  382. ASSERT(node);
  383. }
  384. ASSERT(node == ptr);
  385. }
  386. static struct xfs_iext_node *
  387. xfs_iext_split_node(
  388. struct xfs_iext_node **nodep,
  389. int *pos,
  390. int *nr_entries)
  391. {
  392. struct xfs_iext_node *node = *nodep;
  393. struct xfs_iext_node *new = xfs_iext_alloc_node(NODE_SIZE);
  394. const int nr_move = KEYS_PER_NODE / 2;
  395. int nr_keep = nr_move + (KEYS_PER_NODE & 1);
  396. int i = 0;
  397. /* for sequential append operations just spill over into the new node */
  398. if (*pos == KEYS_PER_NODE) {
  399. *nodep = new;
  400. *pos = 0;
  401. *nr_entries = 0;
  402. goto done;
  403. }
  404. for (i = 0; i < nr_move; i++) {
  405. new->keys[i] = node->keys[nr_keep + i];
  406. new->ptrs[i] = node->ptrs[nr_keep + i];
  407. node->keys[nr_keep + i] = XFS_IEXT_KEY_INVALID;
  408. node->ptrs[nr_keep + i] = NULL;
  409. }
  410. if (*pos >= nr_keep) {
  411. *nodep = new;
  412. *pos -= nr_keep;
  413. *nr_entries = nr_move;
  414. } else {
  415. *nr_entries = nr_keep;
  416. }
  417. done:
  418. for (; i < KEYS_PER_NODE; i++)
  419. new->keys[i] = XFS_IEXT_KEY_INVALID;
  420. return new;
  421. }
  422. static void
  423. xfs_iext_insert_node(
  424. struct xfs_ifork *ifp,
  425. uint64_t offset,
  426. void *ptr,
  427. int level)
  428. {
  429. struct xfs_iext_node *node, *new;
  430. int i, pos, nr_entries;
  431. again:
  432. if (ifp->if_height < level)
  433. xfs_iext_grow(ifp);
  434. new = NULL;
  435. node = xfs_iext_find_level(ifp, offset, level);
  436. pos = xfs_iext_node_insert_pos(node, offset);
  437. nr_entries = xfs_iext_node_nr_entries(node, pos);
  438. ASSERT(pos >= nr_entries || xfs_iext_key_cmp(node, pos, offset) != 0);
  439. ASSERT(nr_entries <= KEYS_PER_NODE);
  440. if (nr_entries == KEYS_PER_NODE)
  441. new = xfs_iext_split_node(&node, &pos, &nr_entries);
  442. /*
  443. * Update the pointers in higher levels if the first entry changes
  444. * in an existing node.
  445. */
  446. if (node != new && pos == 0 && nr_entries > 0)
  447. xfs_iext_update_node(ifp, node->keys[0], offset, level, node);
  448. for (i = nr_entries; i > pos; i--) {
  449. node->keys[i] = node->keys[i - 1];
  450. node->ptrs[i] = node->ptrs[i - 1];
  451. }
  452. node->keys[pos] = offset;
  453. node->ptrs[pos] = ptr;
  454. if (new) {
  455. offset = new->keys[0];
  456. ptr = new;
  457. level++;
  458. goto again;
  459. }
  460. }
  461. static struct xfs_iext_leaf *
  462. xfs_iext_split_leaf(
  463. struct xfs_iext_cursor *cur,
  464. int *nr_entries)
  465. {
  466. struct xfs_iext_leaf *leaf = cur->leaf;
  467. struct xfs_iext_leaf *new = xfs_iext_alloc_node(NODE_SIZE);
  468. const int nr_move = RECS_PER_LEAF / 2;
  469. int nr_keep = nr_move + (RECS_PER_LEAF & 1);
  470. int i;
  471. /* for sequential append operations just spill over into the new node */
  472. if (cur->pos == RECS_PER_LEAF) {
  473. cur->leaf = new;
  474. cur->pos = 0;
  475. *nr_entries = 0;
  476. goto done;
  477. }
  478. for (i = 0; i < nr_move; i++) {
  479. new->recs[i] = leaf->recs[nr_keep + i];
  480. xfs_iext_rec_clear(&leaf->recs[nr_keep + i]);
  481. }
  482. if (cur->pos >= nr_keep) {
  483. cur->leaf = new;
  484. cur->pos -= nr_keep;
  485. *nr_entries = nr_move;
  486. } else {
  487. *nr_entries = nr_keep;
  488. }
  489. done:
  490. if (leaf->next)
  491. leaf->next->prev = new;
  492. new->next = leaf->next;
  493. new->prev = leaf;
  494. leaf->next = new;
  495. return new;
  496. }
  497. static void
  498. xfs_iext_alloc_root(
  499. struct xfs_ifork *ifp,
  500. struct xfs_iext_cursor *cur)
  501. {
  502. ASSERT(ifp->if_bytes == 0);
  503. ifp->if_data = xfs_iext_alloc_node(sizeof(struct xfs_iext_rec));
  504. ifp->if_height = 1;
  505. /* now that we have a node step into it */
  506. cur->leaf = ifp->if_data;
  507. cur->pos = 0;
  508. }
  509. static void
  510. xfs_iext_realloc_root(
  511. struct xfs_ifork *ifp,
  512. struct xfs_iext_cursor *cur)
  513. {
  514. int64_t new_size = ifp->if_bytes + sizeof(struct xfs_iext_rec);
  515. void *new;
  516. /* account for the prev/next pointers */
  517. if (new_size / sizeof(struct xfs_iext_rec) == RECS_PER_LEAF)
  518. new_size = NODE_SIZE;
  519. new = krealloc(ifp->if_data, new_size,
  520. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  521. memset(new + ifp->if_bytes, 0, new_size - ifp->if_bytes);
  522. ifp->if_data = new;
  523. cur->leaf = new;
  524. }
  525. /*
  526. * Increment the sequence counter on extent tree changes. If we are on a COW
  527. * fork, this allows the writeback code to skip looking for a COW extent if the
  528. * COW fork hasn't changed. We use WRITE_ONCE here to ensure the update to the
  529. * sequence counter is seen before the modifications to the extent tree itself
  530. * take effect.
  531. */
  532. static inline void xfs_iext_inc_seq(struct xfs_ifork *ifp)
  533. {
  534. WRITE_ONCE(ifp->if_seq, READ_ONCE(ifp->if_seq) + 1);
  535. }
  536. void
  537. xfs_iext_insert_raw(
  538. struct xfs_ifork *ifp,
  539. struct xfs_iext_cursor *cur,
  540. struct xfs_bmbt_irec *irec)
  541. {
  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);
  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. if (new)
  569. xfs_iext_insert_node(ifp, xfs_iext_leaf_key(new, 0), new, 2);
  570. }
  571. void
  572. xfs_iext_insert(
  573. struct xfs_inode *ip,
  574. struct xfs_iext_cursor *cur,
  575. struct xfs_bmbt_irec *irec,
  576. int state)
  577. {
  578. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  579. xfs_iext_insert_raw(ifp, cur, irec);
  580. trace_xfs_iext_insert(ip, cur, state, _RET_IP_);
  581. }
  582. static struct xfs_iext_node *
  583. xfs_iext_rebalance_node(
  584. struct xfs_iext_node *parent,
  585. int *pos,
  586. struct xfs_iext_node *node,
  587. int nr_entries)
  588. {
  589. /*
  590. * If the neighbouring nodes are completely full, or have different
  591. * parents, we might never be able to merge our node, and will only
  592. * delete it once the number of entries hits zero.
  593. */
  594. if (nr_entries == 0)
  595. return node;
  596. if (*pos > 0) {
  597. struct xfs_iext_node *prev = parent->ptrs[*pos - 1];
  598. int nr_prev = xfs_iext_node_nr_entries(prev, 0), i;
  599. if (nr_prev + nr_entries <= KEYS_PER_NODE) {
  600. for (i = 0; i < nr_entries; i++) {
  601. prev->keys[nr_prev + i] = node->keys[i];
  602. prev->ptrs[nr_prev + i] = node->ptrs[i];
  603. }
  604. return node;
  605. }
  606. }
  607. if (*pos + 1 < xfs_iext_node_nr_entries(parent, *pos)) {
  608. struct xfs_iext_node *next = parent->ptrs[*pos + 1];
  609. int nr_next = xfs_iext_node_nr_entries(next, 0), i;
  610. if (nr_entries + nr_next <= KEYS_PER_NODE) {
  611. /*
  612. * Merge the next node into this node so that we don't
  613. * have to do an additional update of the keys in the
  614. * higher levels.
  615. */
  616. for (i = 0; i < nr_next; i++) {
  617. node->keys[nr_entries + i] = next->keys[i];
  618. node->ptrs[nr_entries + i] = next->ptrs[i];
  619. }
  620. ++*pos;
  621. return next;
  622. }
  623. }
  624. return NULL;
  625. }
  626. static void
  627. xfs_iext_remove_node(
  628. struct xfs_ifork *ifp,
  629. xfs_fileoff_t offset,
  630. void *victim)
  631. {
  632. struct xfs_iext_node *node, *parent;
  633. int level = 2, pos, nr_entries, i;
  634. ASSERT(level <= ifp->if_height);
  635. node = xfs_iext_find_level(ifp, offset, level);
  636. pos = xfs_iext_node_pos(node, offset);
  637. again:
  638. ASSERT(node->ptrs[pos]);
  639. ASSERT(node->ptrs[pos] == victim);
  640. kfree(victim);
  641. nr_entries = xfs_iext_node_nr_entries(node, pos) - 1;
  642. offset = node->keys[0];
  643. for (i = pos; i < nr_entries; i++) {
  644. node->keys[i] = node->keys[i + 1];
  645. node->ptrs[i] = node->ptrs[i + 1];
  646. }
  647. node->keys[nr_entries] = XFS_IEXT_KEY_INVALID;
  648. node->ptrs[nr_entries] = NULL;
  649. if (pos == 0 && nr_entries > 0) {
  650. xfs_iext_update_node(ifp, offset, node->keys[0], level, node);
  651. offset = node->keys[0];
  652. }
  653. if (nr_entries >= KEYS_PER_NODE / 2)
  654. return;
  655. if (level < ifp->if_height) {
  656. /*
  657. * If we aren't at the root yet try to find a neighbour node to
  658. * merge with (or delete the node if it is empty), and then
  659. * recurse up to the next level.
  660. */
  661. level++;
  662. parent = xfs_iext_find_level(ifp, offset, level);
  663. pos = xfs_iext_node_pos(parent, offset);
  664. ASSERT(pos != KEYS_PER_NODE);
  665. ASSERT(parent->ptrs[pos] == node);
  666. node = xfs_iext_rebalance_node(parent, &pos, node, nr_entries);
  667. if (node) {
  668. victim = node;
  669. node = parent;
  670. goto again;
  671. }
  672. } else if (nr_entries == 1) {
  673. /*
  674. * If we are at the root and only one entry is left we can just
  675. * free this node and update the root pointer.
  676. */
  677. ASSERT(node == ifp->if_data);
  678. ifp->if_data = node->ptrs[0];
  679. ifp->if_height--;
  680. kfree(node);
  681. }
  682. }
  683. static void
  684. xfs_iext_rebalance_leaf(
  685. struct xfs_ifork *ifp,
  686. struct xfs_iext_cursor *cur,
  687. struct xfs_iext_leaf *leaf,
  688. xfs_fileoff_t offset,
  689. int nr_entries)
  690. {
  691. /*
  692. * If the neighbouring nodes are completely full we might never be able
  693. * to merge our node, and will only delete it once the number of
  694. * entries hits zero.
  695. */
  696. if (nr_entries == 0)
  697. goto remove_node;
  698. if (leaf->prev) {
  699. int nr_prev = xfs_iext_leaf_nr_entries(ifp, leaf->prev, 0), i;
  700. if (nr_prev + nr_entries <= RECS_PER_LEAF) {
  701. for (i = 0; i < nr_entries; i++)
  702. leaf->prev->recs[nr_prev + i] = leaf->recs[i];
  703. if (cur->leaf == leaf) {
  704. cur->leaf = leaf->prev;
  705. cur->pos += nr_prev;
  706. }
  707. goto remove_node;
  708. }
  709. }
  710. if (leaf->next) {
  711. int nr_next = xfs_iext_leaf_nr_entries(ifp, leaf->next, 0), i;
  712. if (nr_entries + nr_next <= RECS_PER_LEAF) {
  713. /*
  714. * Merge the next node into this node so that we don't
  715. * have to do an additional update of the keys in the
  716. * higher levels.
  717. */
  718. for (i = 0; i < nr_next; i++) {
  719. leaf->recs[nr_entries + i] =
  720. leaf->next->recs[i];
  721. }
  722. if (cur->leaf == leaf->next) {
  723. cur->leaf = leaf;
  724. cur->pos += nr_entries;
  725. }
  726. offset = xfs_iext_leaf_key(leaf->next, 0);
  727. leaf = leaf->next;
  728. goto remove_node;
  729. }
  730. }
  731. return;
  732. remove_node:
  733. if (leaf->prev)
  734. leaf->prev->next = leaf->next;
  735. if (leaf->next)
  736. leaf->next->prev = leaf->prev;
  737. xfs_iext_remove_node(ifp, offset, leaf);
  738. }
  739. static void
  740. xfs_iext_free_last_leaf(
  741. struct xfs_ifork *ifp)
  742. {
  743. ifp->if_height--;
  744. kfree(ifp->if_data);
  745. ifp->if_data = NULL;
  746. }
  747. void
  748. xfs_iext_remove(
  749. struct xfs_inode *ip,
  750. struct xfs_iext_cursor *cur,
  751. int state)
  752. {
  753. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  754. struct xfs_iext_leaf *leaf = cur->leaf;
  755. xfs_fileoff_t offset = xfs_iext_leaf_key(leaf, 0);
  756. int i, nr_entries;
  757. trace_xfs_iext_remove(ip, cur, state, _RET_IP_);
  758. ASSERT(ifp->if_height > 0);
  759. ASSERT(ifp->if_data != NULL);
  760. ASSERT(xfs_iext_valid(ifp, cur));
  761. xfs_iext_inc_seq(ifp);
  762. nr_entries = xfs_iext_leaf_nr_entries(ifp, leaf, cur->pos) - 1;
  763. for (i = cur->pos; i < nr_entries; i++)
  764. leaf->recs[i] = leaf->recs[i + 1];
  765. xfs_iext_rec_clear(&leaf->recs[nr_entries]);
  766. ifp->if_bytes -= sizeof(struct xfs_iext_rec);
  767. if (cur->pos == 0 && nr_entries > 0) {
  768. xfs_iext_update_node(ifp, offset, xfs_iext_leaf_key(leaf, 0), 1,
  769. leaf);
  770. offset = xfs_iext_leaf_key(leaf, 0);
  771. } else if (cur->pos == nr_entries) {
  772. if (ifp->if_height > 1 && leaf->next)
  773. cur->leaf = leaf->next;
  774. else
  775. cur->leaf = NULL;
  776. cur->pos = 0;
  777. }
  778. if (nr_entries >= RECS_PER_LEAF / 2)
  779. return;
  780. if (ifp->if_height > 1)
  781. xfs_iext_rebalance_leaf(ifp, cur, leaf, offset, nr_entries);
  782. else if (nr_entries == 0)
  783. xfs_iext_free_last_leaf(ifp);
  784. }
  785. /*
  786. * Lookup the extent covering bno.
  787. *
  788. * If there is an extent covering bno return the extent index, and store the
  789. * expanded extent structure in *gotp, and the extent cursor in *cur.
  790. * If there is no extent covering bno, but there is an extent after it (e.g.
  791. * it lies in a hole) return that extent in *gotp and its cursor in *cur
  792. * instead.
  793. * If bno is beyond the last extent return false, and return an invalid
  794. * cursor value.
  795. */
  796. bool
  797. xfs_iext_lookup_extent(
  798. struct xfs_inode *ip,
  799. struct xfs_ifork *ifp,
  800. xfs_fileoff_t offset,
  801. struct xfs_iext_cursor *cur,
  802. struct xfs_bmbt_irec *gotp)
  803. {
  804. XFS_STATS_INC(ip->i_mount, xs_look_exlist);
  805. cur->leaf = xfs_iext_find_level(ifp, offset, 1);
  806. if (!cur->leaf) {
  807. cur->pos = 0;
  808. return false;
  809. }
  810. for (cur->pos = 0; cur->pos < xfs_iext_max_recs(ifp); cur->pos++) {
  811. struct xfs_iext_rec *rec = cur_rec(cur);
  812. if (xfs_iext_rec_is_empty(rec))
  813. break;
  814. if (xfs_iext_rec_cmp(rec, offset) >= 0)
  815. goto found;
  816. }
  817. /* Try looking in the next node for an entry > offset */
  818. if (ifp->if_height == 1 || !cur->leaf->next)
  819. return false;
  820. cur->leaf = cur->leaf->next;
  821. cur->pos = 0;
  822. if (!xfs_iext_valid(ifp, cur))
  823. return false;
  824. found:
  825. xfs_iext_get(gotp, cur_rec(cur));
  826. return true;
  827. }
  828. /*
  829. * Returns the last extent before end, and if this extent doesn't cover
  830. * end, update end to the end of the extent.
  831. */
  832. bool
  833. xfs_iext_lookup_extent_before(
  834. struct xfs_inode *ip,
  835. struct xfs_ifork *ifp,
  836. xfs_fileoff_t *end,
  837. struct xfs_iext_cursor *cur,
  838. struct xfs_bmbt_irec *gotp)
  839. {
  840. /* could be optimized to not even look up the next on a match.. */
  841. if (xfs_iext_lookup_extent(ip, ifp, *end - 1, cur, gotp) &&
  842. gotp->br_startoff <= *end - 1)
  843. return true;
  844. if (!xfs_iext_prev_extent(ifp, cur, gotp))
  845. return false;
  846. *end = gotp->br_startoff + gotp->br_blockcount;
  847. return true;
  848. }
  849. void
  850. xfs_iext_update_extent(
  851. struct xfs_inode *ip,
  852. int state,
  853. struct xfs_iext_cursor *cur,
  854. struct xfs_bmbt_irec *new)
  855. {
  856. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  857. xfs_iext_inc_seq(ifp);
  858. if (cur->pos == 0) {
  859. struct xfs_bmbt_irec old;
  860. xfs_iext_get(&old, cur_rec(cur));
  861. if (new->br_startoff != old.br_startoff) {
  862. xfs_iext_update_node(ifp, old.br_startoff,
  863. new->br_startoff, 1, cur->leaf);
  864. }
  865. }
  866. trace_xfs_bmap_pre_update(ip, cur, state, _RET_IP_);
  867. xfs_iext_set(cur_rec(cur), new);
  868. trace_xfs_bmap_post_update(ip, cur, state, _RET_IP_);
  869. }
  870. /*
  871. * Return true if the cursor points at an extent and return the extent structure
  872. * in gotp. Else return false.
  873. */
  874. bool
  875. xfs_iext_get_extent(
  876. struct xfs_ifork *ifp,
  877. struct xfs_iext_cursor *cur,
  878. struct xfs_bmbt_irec *gotp)
  879. {
  880. if (!xfs_iext_valid(ifp, cur))
  881. return false;
  882. xfs_iext_get(gotp, cur_rec(cur));
  883. return true;
  884. }
  885. /*
  886. * This is a recursive function, because of that we need to be extremely
  887. * careful with stack usage.
  888. */
  889. static void
  890. xfs_iext_destroy_node(
  891. struct xfs_iext_node *node,
  892. int level)
  893. {
  894. int i;
  895. if (level > 1) {
  896. for (i = 0; i < KEYS_PER_NODE; i++) {
  897. if (node->keys[i] == XFS_IEXT_KEY_INVALID)
  898. break;
  899. xfs_iext_destroy_node(node->ptrs[i], level - 1);
  900. }
  901. }
  902. kfree(node);
  903. }
  904. void
  905. xfs_iext_destroy(
  906. struct xfs_ifork *ifp)
  907. {
  908. xfs_iext_destroy_node(ifp->if_data, ifp->if_height);
  909. ifp->if_bytes = 0;
  910. ifp->if_height = 0;
  911. ifp->if_data = NULL;
  912. }