xfs_iext_tree.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. u32 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. if (node != new && pos == 0 && nr_entries > 0)
  448. xfs_iext_update_node(ifp, node->keys[0], offset, level, node);
  449. for (i = nr_entries; i > pos; i--) {
  450. node->keys[i] = node->keys[i - 1];
  451. node->ptrs[i] = node->ptrs[i - 1];
  452. }
  453. node->keys[pos] = offset;
  454. node->ptrs[pos] = ptr;
  455. if (new) {
  456. offset = new->keys[0];
  457. ptr = new;
  458. level++;
  459. goto again;
  460. }
  461. }
  462. static struct xfs_iext_leaf *
  463. xfs_iext_split_leaf(
  464. struct xfs_iext_cursor *cur,
  465. int *nr_entries)
  466. {
  467. struct xfs_iext_leaf *leaf = cur->leaf;
  468. struct xfs_iext_leaf *new = kmem_zalloc(NODE_SIZE, KM_NOFS);
  469. const int nr_move = RECS_PER_LEAF / 2;
  470. int nr_keep = nr_move + (RECS_PER_LEAF & 1);
  471. int i;
  472. /* for sequential append operations just spill over into the new node */
  473. if (cur->pos == KEYS_PER_NODE) {
  474. cur->leaf = new;
  475. cur->pos = 0;
  476. *nr_entries = 0;
  477. goto done;
  478. }
  479. if (nr_keep & 1)
  480. nr_keep++;
  481. for (i = 0; i < nr_move; i++) {
  482. new->recs[i] = leaf->recs[nr_keep + i];
  483. xfs_iext_rec_clear(&leaf->recs[nr_keep + i]);
  484. }
  485. if (cur->pos >= nr_keep) {
  486. cur->leaf = new;
  487. cur->pos -= nr_keep;
  488. *nr_entries = nr_move;
  489. } else {
  490. *nr_entries = nr_keep;
  491. }
  492. done:
  493. if (leaf->next)
  494. leaf->next->prev = new;
  495. new->next = leaf->next;
  496. new->prev = leaf;
  497. leaf->next = new;
  498. return new;
  499. }
  500. static void
  501. xfs_iext_alloc_root(
  502. struct xfs_ifork *ifp,
  503. struct xfs_iext_cursor *cur)
  504. {
  505. ASSERT(ifp->if_bytes == 0);
  506. ifp->if_u1.if_root = kmem_zalloc(sizeof(struct xfs_iext_rec), KM_NOFS);
  507. ifp->if_height = 1;
  508. /* now that we have a node step into it */
  509. cur->leaf = ifp->if_u1.if_root;
  510. cur->pos = 0;
  511. }
  512. static void
  513. xfs_iext_realloc_root(
  514. struct xfs_ifork *ifp,
  515. struct xfs_iext_cursor *cur)
  516. {
  517. size_t new_size = ifp->if_bytes + sizeof(struct xfs_iext_rec);
  518. void *new;
  519. /* account for the prev/next pointers */
  520. if (new_size / sizeof(struct xfs_iext_rec) == RECS_PER_LEAF)
  521. new_size = NODE_SIZE;
  522. new = kmem_realloc(ifp->if_u1.if_root, new_size, KM_NOFS);
  523. memset(new + ifp->if_bytes, 0, new_size - ifp->if_bytes);
  524. ifp->if_u1.if_root = new;
  525. cur->leaf = new;
  526. }
  527. void
  528. xfs_iext_insert(
  529. struct xfs_inode *ip,
  530. struct xfs_iext_cursor *cur,
  531. struct xfs_bmbt_irec *irec,
  532. int state)
  533. {
  534. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  535. xfs_fileoff_t offset = irec->br_startoff;
  536. struct xfs_iext_leaf *new = NULL;
  537. int nr_entries, i;
  538. trace_xfs_iext_insert(ip, cur, state, _RET_IP_);
  539. if (ifp->if_height == 0)
  540. xfs_iext_alloc_root(ifp, cur);
  541. else if (ifp->if_height == 1)
  542. xfs_iext_realloc_root(ifp, cur);
  543. nr_entries = xfs_iext_leaf_nr_entries(ifp, cur->leaf, cur->pos);
  544. ASSERT(nr_entries <= RECS_PER_LEAF);
  545. ASSERT(cur->pos >= nr_entries ||
  546. xfs_iext_rec_cmp(cur_rec(cur), irec->br_startoff) != 0);
  547. if (nr_entries == RECS_PER_LEAF)
  548. new = xfs_iext_split_leaf(cur, &nr_entries);
  549. if (cur->leaf != new && cur->pos == 0 && nr_entries > 0) {
  550. xfs_iext_update_node(ifp, xfs_iext_leaf_key(cur->leaf, 0),
  551. offset, 1, cur->leaf);
  552. }
  553. for (i = nr_entries; i > cur->pos; i--)
  554. cur->leaf->recs[i] = cur->leaf->recs[i - 1];
  555. xfs_iext_set(cur_rec(cur), irec);
  556. ifp->if_bytes += sizeof(struct xfs_iext_rec);
  557. if (new)
  558. xfs_iext_insert_node(ifp, xfs_iext_leaf_key(new, 0), new, 2);
  559. }
  560. static struct xfs_iext_node *
  561. xfs_iext_rebalance_node(
  562. struct xfs_iext_node *parent,
  563. int *pos,
  564. struct xfs_iext_node *node,
  565. int nr_entries)
  566. {
  567. if (nr_entries == 0)
  568. return node;
  569. if (*pos > 0) {
  570. struct xfs_iext_node *prev = parent->ptrs[*pos - 1];
  571. int nr_prev = xfs_iext_node_nr_entries(prev, 0), i;
  572. if (nr_prev + nr_entries <= KEYS_PER_NODE) {
  573. for (i = 0; i < nr_entries; i++) {
  574. prev->keys[nr_prev + i] = node->keys[i];
  575. prev->ptrs[nr_prev + i] = node->ptrs[i];
  576. }
  577. return node;
  578. }
  579. }
  580. if (*pos + 1 < xfs_iext_node_nr_entries(parent, *pos)) {
  581. struct xfs_iext_node *next = parent->ptrs[*pos + 1];
  582. int nr_next = xfs_iext_node_nr_entries(next, 0), i;
  583. if (nr_entries + nr_next <= KEYS_PER_NODE) {
  584. for (i = 0; i < nr_next; i++) {
  585. node->keys[nr_entries + i] = next->keys[i];
  586. node->ptrs[nr_entries + i] = next->ptrs[i];
  587. }
  588. ++*pos;
  589. return next;
  590. }
  591. }
  592. return NULL;
  593. }
  594. static void
  595. xfs_iext_remove_node(
  596. struct xfs_ifork *ifp,
  597. xfs_fileoff_t offset,
  598. void *victim)
  599. {
  600. struct xfs_iext_node *node, *parent;
  601. int level = 2, pos, nr_entries, i;
  602. ASSERT(level <= ifp->if_height);
  603. node = xfs_iext_find_level(ifp, offset, level);
  604. pos = xfs_iext_node_pos(node, offset);
  605. again:
  606. ASSERT(node->ptrs[pos]);
  607. ASSERT(node->ptrs[pos] == victim);
  608. kmem_free(victim);
  609. nr_entries = xfs_iext_node_nr_entries(node, pos) - 1;
  610. offset = node->keys[0];
  611. for (i = pos; i < nr_entries; i++) {
  612. node->keys[i] = node->keys[i + 1];
  613. node->ptrs[i] = node->ptrs[i + 1];
  614. }
  615. node->keys[nr_entries] = XFS_IEXT_KEY_INVALID;
  616. node->ptrs[nr_entries] = NULL;
  617. if (pos == 0 && nr_entries > 0) {
  618. xfs_iext_update_node(ifp, offset, node->keys[0], level,
  619. node);
  620. offset = node->keys[0];
  621. }
  622. if (nr_entries >= KEYS_PER_NODE / 2)
  623. return;
  624. if (level < ifp->if_height) {
  625. level++;
  626. parent = xfs_iext_find_level(ifp, offset, level);
  627. pos = xfs_iext_node_pos(parent, offset);
  628. ASSERT(pos != KEYS_PER_NODE);
  629. ASSERT(parent->ptrs[pos] == node);
  630. node = xfs_iext_rebalance_node(parent, &pos, node, nr_entries);
  631. if (node) {
  632. offset = node->keys[0];
  633. victim = node;
  634. node = parent;
  635. goto again;
  636. }
  637. } else if (nr_entries == 1) {
  638. ASSERT(node == ifp->if_u1.if_root);
  639. ifp->if_u1.if_root = node->ptrs[0];
  640. ifp->if_height--;
  641. kmem_free(node);
  642. }
  643. }
  644. static void
  645. xfs_iext_rebalance_leaf(
  646. struct xfs_ifork *ifp,
  647. struct xfs_iext_cursor *cur,
  648. struct xfs_iext_leaf *leaf,
  649. xfs_fileoff_t offset,
  650. int fill)
  651. {
  652. if (leaf->prev) {
  653. int nr_prev = xfs_iext_leaf_nr_entries(ifp, leaf->prev, 0), i;
  654. if (nr_prev + fill <= RECS_PER_LEAF) {
  655. for (i = 0; i < fill; i++)
  656. leaf->prev->recs[nr_prev + i] = leaf->recs[i];
  657. if (cur->leaf == leaf) {
  658. cur->leaf = leaf->prev;
  659. cur->pos += nr_prev;
  660. }
  661. goto remove_node;
  662. }
  663. }
  664. if (leaf->next) {
  665. int nr_next = xfs_iext_leaf_nr_entries(ifp, leaf->next, 0), i;
  666. if (fill + nr_next <= RECS_PER_LEAF) {
  667. for (i = 0; i < nr_next; i++)
  668. leaf->recs[fill + i] = leaf->next->recs[i];
  669. if (cur->leaf == leaf->next) {
  670. cur->leaf = leaf;
  671. cur->pos += fill;
  672. }
  673. offset = xfs_iext_leaf_key(leaf->next, 0);
  674. leaf = leaf->next;
  675. goto remove_node;
  676. }
  677. }
  678. return;
  679. remove_node:
  680. if (leaf->prev)
  681. leaf->prev->next = leaf->next;
  682. if (leaf->next)
  683. leaf->next->prev = leaf->prev;
  684. xfs_iext_remove_node(ifp, offset, leaf);
  685. }
  686. static void
  687. xfs_iext_free_last_leaf(
  688. struct xfs_ifork *ifp)
  689. {
  690. ifp->if_u1.if_root = NULL;
  691. ifp->if_height--;
  692. kmem_free(ifp->if_u1.if_root);
  693. }
  694. void
  695. xfs_iext_remove(
  696. struct xfs_inode *ip,
  697. struct xfs_iext_cursor *cur,
  698. int state)
  699. {
  700. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  701. struct xfs_iext_leaf *leaf = cur->leaf;
  702. xfs_fileoff_t offset = xfs_iext_leaf_key(leaf, 0);
  703. int i, nr_entries;
  704. trace_xfs_iext_remove(ip, cur, state, _RET_IP_);
  705. ASSERT(ifp->if_height > 0);
  706. ASSERT(ifp->if_u1.if_root != NULL);
  707. ASSERT(xfs_iext_valid(ifp, cur));
  708. nr_entries = xfs_iext_leaf_nr_entries(ifp, leaf, cur->pos) - 1;
  709. for (i = cur->pos; i < nr_entries; i++)
  710. leaf->recs[i] = leaf->recs[i + 1];
  711. xfs_iext_rec_clear(&leaf->recs[nr_entries]);
  712. ifp->if_bytes -= sizeof(struct xfs_iext_rec);
  713. if (cur->pos == 0 && nr_entries > 0) {
  714. xfs_iext_update_node(ifp, offset, xfs_iext_leaf_key(leaf, 0), 1,
  715. leaf);
  716. offset = xfs_iext_leaf_key(leaf, 0);
  717. } else if (cur->pos == nr_entries) {
  718. if (ifp->if_height > 1 && leaf->next)
  719. cur->leaf = leaf->next;
  720. else
  721. cur->leaf = NULL;
  722. cur->pos = 0;
  723. }
  724. if (nr_entries >= RECS_PER_LEAF / 2)
  725. return;
  726. if (ifp->if_height > 1)
  727. xfs_iext_rebalance_leaf(ifp, cur, leaf, offset, nr_entries);
  728. else if (nr_entries == 0)
  729. xfs_iext_free_last_leaf(ifp);
  730. }
  731. /*
  732. * Lookup the extent covering bno.
  733. *
  734. * If there is an extent covering bno return the extent index, and store the
  735. * expanded extent structure in *gotp, and the extent cursor in *cur.
  736. * If there is no extent covering bno, but there is an extent after it (e.g.
  737. * it lies in a hole) return that extent in *gotp and its cursor in *cur
  738. * instead.
  739. * If bno is beyond the last extent return false, and return an invalid
  740. * cursor value.
  741. */
  742. bool
  743. xfs_iext_lookup_extent(
  744. struct xfs_inode *ip,
  745. struct xfs_ifork *ifp,
  746. xfs_fileoff_t offset,
  747. struct xfs_iext_cursor *cur,
  748. struct xfs_bmbt_irec *gotp)
  749. {
  750. XFS_STATS_INC(ip->i_mount, xs_look_exlist);
  751. cur->leaf = xfs_iext_find_level(ifp, offset, 1);
  752. if (!cur->leaf) {
  753. cur->pos = 0;
  754. return false;
  755. }
  756. for (cur->pos = 0; cur->pos < xfs_iext_max_recs(ifp); cur->pos++) {
  757. struct xfs_iext_rec *rec = cur_rec(cur);
  758. if (xfs_iext_rec_is_empty(rec))
  759. break;
  760. if (xfs_iext_rec_cmp(rec, offset) >= 0)
  761. goto found;
  762. }
  763. /* Try looking in the next node for an entry > offset */
  764. if (ifp->if_height == 1 || !cur->leaf->next)
  765. return false;
  766. cur->leaf = cur->leaf->next;
  767. cur->pos = 0;
  768. if (!xfs_iext_valid(ifp, cur))
  769. return false;
  770. found:
  771. xfs_iext_get(gotp, cur_rec(cur));
  772. return true;
  773. }
  774. /*
  775. * Returns the last extent before end, and if this extent doesn't cover
  776. * end, update end to the end of the extent.
  777. */
  778. bool
  779. xfs_iext_lookup_extent_before(
  780. struct xfs_inode *ip,
  781. struct xfs_ifork *ifp,
  782. xfs_fileoff_t *end,
  783. struct xfs_iext_cursor *cur,
  784. struct xfs_bmbt_irec *gotp)
  785. {
  786. /* could be optimized to not even look up the next on a match.. */
  787. if (xfs_iext_lookup_extent(ip, ifp, *end - 1, cur, gotp) &&
  788. gotp->br_startoff <= *end - 1)
  789. return true;
  790. if (!xfs_iext_prev_extent(ifp, cur, gotp))
  791. return false;
  792. *end = gotp->br_startoff + gotp->br_blockcount;
  793. return true;
  794. }
  795. void
  796. xfs_iext_update_extent(
  797. struct xfs_inode *ip,
  798. int state,
  799. struct xfs_iext_cursor *cur,
  800. struct xfs_bmbt_irec *new)
  801. {
  802. struct xfs_ifork *ifp = xfs_iext_state_to_fork(ip, state);
  803. if (cur->pos == 0) {
  804. struct xfs_bmbt_irec old;
  805. xfs_iext_get(&old, cur_rec(cur));
  806. if (new->br_startoff != old.br_startoff) {
  807. xfs_iext_update_node(ifp, old.br_startoff,
  808. new->br_startoff, 1, cur->leaf);
  809. }
  810. }
  811. trace_xfs_bmap_pre_update(ip, cur, state, _RET_IP_);
  812. xfs_iext_set(cur_rec(cur), new);
  813. trace_xfs_bmap_post_update(ip, cur, state, _RET_IP_);
  814. }
  815. /*
  816. * Return true if the cursor points at an extent and return the extent structure
  817. * in gotp. Else return false.
  818. */
  819. bool
  820. xfs_iext_get_extent(
  821. struct xfs_ifork *ifp,
  822. struct xfs_iext_cursor *cur,
  823. struct xfs_bmbt_irec *gotp)
  824. {
  825. if (!xfs_iext_valid(ifp, cur))
  826. return false;
  827. xfs_iext_get(gotp, cur_rec(cur));
  828. return true;
  829. }
  830. /*
  831. * This is a recursive function, because of that we need to be extremely
  832. * careful with stack usage.
  833. */
  834. static void
  835. xfs_iext_destroy_node(
  836. struct xfs_iext_node *node,
  837. int level)
  838. {
  839. int i;
  840. if (level > 1) {
  841. for (i = 0; i < KEYS_PER_NODE; i++) {
  842. if (node->keys[i] == XFS_IEXT_KEY_INVALID)
  843. break;
  844. xfs_iext_destroy_node(node->ptrs[i], level - 1);
  845. }
  846. }
  847. kmem_free(node);
  848. }
  849. void
  850. xfs_iext_destroy(
  851. struct xfs_ifork *ifp)
  852. {
  853. xfs_iext_destroy_node(ifp->if_u1.if_root, ifp->if_height);
  854. ifp->if_bytes = 0;
  855. ifp->if_height = 0;
  856. ifp->if_u1.if_root = NULL;
  857. }