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