xfs: add a btree entries pointer to struct xfs_da3_icnode_hdr
[linux-block.git] / fs / xfs / libxfs / xfs_da_btree.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_bit.h"
14 #include "xfs_mount.h"
15 #include "xfs_dir2.h"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_inode.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap.h"
20 #include "xfs_attr_leaf.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_buf_item.h"
24 #include "xfs_log.h"
25
26 /*
27  * xfs_da_btree.c
28  *
29  * Routines to implement directories as Btrees of hashed names.
30  */
31
32 /*========================================================================
33  * Function prototypes for the kernel.
34  *========================================================================*/
35
36 /*
37  * Routines used for growing the Btree.
38  */
39 STATIC int xfs_da3_root_split(xfs_da_state_t *state,
40                                             xfs_da_state_blk_t *existing_root,
41                                             xfs_da_state_blk_t *new_child);
42 STATIC int xfs_da3_node_split(xfs_da_state_t *state,
43                                             xfs_da_state_blk_t *existing_blk,
44                                             xfs_da_state_blk_t *split_blk,
45                                             xfs_da_state_blk_t *blk_to_add,
46                                             int treelevel,
47                                             int *result);
48 STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
49                                          xfs_da_state_blk_t *node_blk_1,
50                                          xfs_da_state_blk_t *node_blk_2);
51 STATIC void xfs_da3_node_add(xfs_da_state_t *state,
52                                    xfs_da_state_blk_t *old_node_blk,
53                                    xfs_da_state_blk_t *new_node_blk);
54
55 /*
56  * Routines used for shrinking the Btree.
57  */
58 STATIC int xfs_da3_root_join(xfs_da_state_t *state,
59                                            xfs_da_state_blk_t *root_blk);
60 STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
61 STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
62                                               xfs_da_state_blk_t *drop_blk);
63 STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
64                                          xfs_da_state_blk_t *src_node_blk,
65                                          xfs_da_state_blk_t *dst_node_blk);
66
67 /*
68  * Utility routines.
69  */
70 STATIC int      xfs_da3_blk_unlink(xfs_da_state_t *state,
71                                   xfs_da_state_blk_t *drop_blk,
72                                   xfs_da_state_blk_t *save_blk);
73
74
75 kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
76
77 /*
78  * Allocate a dir-state structure.
79  * We don't put them on the stack since they're large.
80  */
81 xfs_da_state_t *
82 xfs_da_state_alloc(void)
83 {
84         return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
85 }
86
87 /*
88  * Kill the altpath contents of a da-state structure.
89  */
90 STATIC void
91 xfs_da_state_kill_altpath(xfs_da_state_t *state)
92 {
93         int     i;
94
95         for (i = 0; i < state->altpath.active; i++)
96                 state->altpath.blk[i].bp = NULL;
97         state->altpath.active = 0;
98 }
99
100 /*
101  * Free a da-state structure.
102  */
103 void
104 xfs_da_state_free(xfs_da_state_t *state)
105 {
106         xfs_da_state_kill_altpath(state);
107 #ifdef DEBUG
108         memset((char *)state, 0, sizeof(*state));
109 #endif /* DEBUG */
110         kmem_zone_free(xfs_da_state_zone, state);
111 }
112
113 void
114 xfs_da3_node_hdr_from_disk(
115         struct xfs_mount                *mp,
116         struct xfs_da3_icnode_hdr       *to,
117         struct xfs_da_intnode           *from)
118 {
119         if (xfs_sb_version_hascrc(&mp->m_sb)) {
120                 struct xfs_da3_intnode  *from3 = (struct xfs_da3_intnode *)from;
121
122                 to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
123                 to->back = be32_to_cpu(from3->hdr.info.hdr.back);
124                 to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
125                 to->count = be16_to_cpu(from3->hdr.__count);
126                 to->level = be16_to_cpu(from3->hdr.__level);
127                 to->btree = from3->__btree;
128                 ASSERT(to->magic == XFS_DA3_NODE_MAGIC);
129         } else {
130                 to->forw = be32_to_cpu(from->hdr.info.forw);
131                 to->back = be32_to_cpu(from->hdr.info.back);
132                 to->magic = be16_to_cpu(from->hdr.info.magic);
133                 to->count = be16_to_cpu(from->hdr.__count);
134                 to->level = be16_to_cpu(from->hdr.__level);
135                 to->btree = from->__btree;
136                 ASSERT(to->magic == XFS_DA_NODE_MAGIC);
137         }
138 }
139
140 void
141 xfs_da3_node_hdr_to_disk(
142         struct xfs_mount                *mp,
143         struct xfs_da_intnode           *to,
144         struct xfs_da3_icnode_hdr       *from)
145 {
146         if (xfs_sb_version_hascrc(&mp->m_sb)) {
147                 struct xfs_da3_intnode  *to3 = (struct xfs_da3_intnode *)to;
148
149                 ASSERT(from->magic == XFS_DA3_NODE_MAGIC);
150                 to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
151                 to3->hdr.info.hdr.back = cpu_to_be32(from->back);
152                 to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
153                 to3->hdr.__count = cpu_to_be16(from->count);
154                 to3->hdr.__level = cpu_to_be16(from->level);
155         } else {
156                 ASSERT(from->magic == XFS_DA_NODE_MAGIC);
157                 to->hdr.info.forw = cpu_to_be32(from->forw);
158                 to->hdr.info.back = cpu_to_be32(from->back);
159                 to->hdr.info.magic = cpu_to_be16(from->magic);
160                 to->hdr.__count = cpu_to_be16(from->count);
161                 to->hdr.__level = cpu_to_be16(from->level);
162         }
163 }
164
165 /*
166  * Verify an xfs_da3_blkinfo structure. Note that the da3 fields are only
167  * accessible on v5 filesystems. This header format is common across da node,
168  * attr leaf and dir leaf blocks.
169  */
170 xfs_failaddr_t
171 xfs_da3_blkinfo_verify(
172         struct xfs_buf          *bp,
173         struct xfs_da3_blkinfo  *hdr3)
174 {
175         struct xfs_mount        *mp = bp->b_mount;
176         struct xfs_da_blkinfo   *hdr = &hdr3->hdr;
177
178         if (!xfs_verify_magic16(bp, hdr->magic))
179                 return __this_address;
180
181         if (xfs_sb_version_hascrc(&mp->m_sb)) {
182                 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
183                         return __this_address;
184                 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
185                         return __this_address;
186                 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
187                         return __this_address;
188         }
189
190         return NULL;
191 }
192
193 static xfs_failaddr_t
194 xfs_da3_node_verify(
195         struct xfs_buf          *bp)
196 {
197         struct xfs_mount        *mp = bp->b_mount;
198         struct xfs_da_intnode   *hdr = bp->b_addr;
199         struct xfs_da3_icnode_hdr ichdr;
200         xfs_failaddr_t          fa;
201
202         xfs_da3_node_hdr_from_disk(mp, &ichdr, hdr);
203
204         fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
205         if (fa)
206                 return fa;
207
208         if (ichdr.level == 0)
209                 return __this_address;
210         if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
211                 return __this_address;
212         if (ichdr.count == 0)
213                 return __this_address;
214
215         /*
216          * we don't know if the node is for and attribute or directory tree,
217          * so only fail if the count is outside both bounds
218          */
219         if (ichdr.count > mp->m_dir_geo->node_ents &&
220             ichdr.count > mp->m_attr_geo->node_ents)
221                 return __this_address;
222
223         /* XXX: hash order check? */
224
225         return NULL;
226 }
227
228 static void
229 xfs_da3_node_write_verify(
230         struct xfs_buf  *bp)
231 {
232         struct xfs_mount        *mp = bp->b_mount;
233         struct xfs_buf_log_item *bip = bp->b_log_item;
234         struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
235         xfs_failaddr_t          fa;
236
237         fa = xfs_da3_node_verify(bp);
238         if (fa) {
239                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
240                 return;
241         }
242
243         if (!xfs_sb_version_hascrc(&mp->m_sb))
244                 return;
245
246         if (bip)
247                 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
248
249         xfs_buf_update_cksum(bp, XFS_DA3_NODE_CRC_OFF);
250 }
251
252 /*
253  * leaf/node format detection on trees is sketchy, so a node read can be done on
254  * leaf level blocks when detection identifies the tree as a node format tree
255  * incorrectly. In this case, we need to swap the verifier to match the correct
256  * format of the block being read.
257  */
258 static void
259 xfs_da3_node_read_verify(
260         struct xfs_buf          *bp)
261 {
262         struct xfs_da_blkinfo   *info = bp->b_addr;
263         xfs_failaddr_t          fa;
264
265         switch (be16_to_cpu(info->magic)) {
266                 case XFS_DA3_NODE_MAGIC:
267                         if (!xfs_buf_verify_cksum(bp, XFS_DA3_NODE_CRC_OFF)) {
268                                 xfs_verifier_error(bp, -EFSBADCRC,
269                                                 __this_address);
270                                 break;
271                         }
272                         /* fall through */
273                 case XFS_DA_NODE_MAGIC:
274                         fa = xfs_da3_node_verify(bp);
275                         if (fa)
276                                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
277                         return;
278                 case XFS_ATTR_LEAF_MAGIC:
279                 case XFS_ATTR3_LEAF_MAGIC:
280                         bp->b_ops = &xfs_attr3_leaf_buf_ops;
281                         bp->b_ops->verify_read(bp);
282                         return;
283                 case XFS_DIR2_LEAFN_MAGIC:
284                 case XFS_DIR3_LEAFN_MAGIC:
285                         bp->b_ops = &xfs_dir3_leafn_buf_ops;
286                         bp->b_ops->verify_read(bp);
287                         return;
288                 default:
289                         xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
290                         break;
291         }
292 }
293
294 /* Verify the structure of a da3 block. */
295 static xfs_failaddr_t
296 xfs_da3_node_verify_struct(
297         struct xfs_buf          *bp)
298 {
299         struct xfs_da_blkinfo   *info = bp->b_addr;
300
301         switch (be16_to_cpu(info->magic)) {
302         case XFS_DA3_NODE_MAGIC:
303         case XFS_DA_NODE_MAGIC:
304                 return xfs_da3_node_verify(bp);
305         case XFS_ATTR_LEAF_MAGIC:
306         case XFS_ATTR3_LEAF_MAGIC:
307                 bp->b_ops = &xfs_attr3_leaf_buf_ops;
308                 return bp->b_ops->verify_struct(bp);
309         case XFS_DIR2_LEAFN_MAGIC:
310         case XFS_DIR3_LEAFN_MAGIC:
311                 bp->b_ops = &xfs_dir3_leafn_buf_ops;
312                 return bp->b_ops->verify_struct(bp);
313         default:
314                 return __this_address;
315         }
316 }
317
318 const struct xfs_buf_ops xfs_da3_node_buf_ops = {
319         .name = "xfs_da3_node",
320         .magic16 = { cpu_to_be16(XFS_DA_NODE_MAGIC),
321                      cpu_to_be16(XFS_DA3_NODE_MAGIC) },
322         .verify_read = xfs_da3_node_read_verify,
323         .verify_write = xfs_da3_node_write_verify,
324         .verify_struct = xfs_da3_node_verify_struct,
325 };
326
327 int
328 xfs_da3_node_read(
329         struct xfs_trans        *tp,
330         struct xfs_inode        *dp,
331         xfs_dablk_t             bno,
332         xfs_daddr_t             mappedbno,
333         struct xfs_buf          **bpp,
334         int                     which_fork)
335 {
336         int                     err;
337
338         err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
339                                         which_fork, &xfs_da3_node_buf_ops);
340         if (!err && tp && *bpp) {
341                 struct xfs_da_blkinfo   *info = (*bpp)->b_addr;
342                 int                     type;
343
344                 switch (be16_to_cpu(info->magic)) {
345                 case XFS_DA_NODE_MAGIC:
346                 case XFS_DA3_NODE_MAGIC:
347                         type = XFS_BLFT_DA_NODE_BUF;
348                         break;
349                 case XFS_ATTR_LEAF_MAGIC:
350                 case XFS_ATTR3_LEAF_MAGIC:
351                         type = XFS_BLFT_ATTR_LEAF_BUF;
352                         break;
353                 case XFS_DIR2_LEAFN_MAGIC:
354                 case XFS_DIR3_LEAFN_MAGIC:
355                         type = XFS_BLFT_DIR_LEAFN_BUF;
356                         break;
357                 default:
358                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
359                                         tp->t_mountp, info, sizeof(*info));
360                         xfs_trans_brelse(tp, *bpp);
361                         *bpp = NULL;
362                         return -EFSCORRUPTED;
363                 }
364                 xfs_trans_buf_set_type(tp, *bpp, type);
365         }
366         return err;
367 }
368
369 /*========================================================================
370  * Routines used for growing the Btree.
371  *========================================================================*/
372
373 /*
374  * Create the initial contents of an intermediate node.
375  */
376 int
377 xfs_da3_node_create(
378         struct xfs_da_args      *args,
379         xfs_dablk_t             blkno,
380         int                     level,
381         struct xfs_buf          **bpp,
382         int                     whichfork)
383 {
384         struct xfs_da_intnode   *node;
385         struct xfs_trans        *tp = args->trans;
386         struct xfs_mount        *mp = tp->t_mountp;
387         struct xfs_da3_icnode_hdr ichdr = {0};
388         struct xfs_buf          *bp;
389         int                     error;
390         struct xfs_inode        *dp = args->dp;
391
392         trace_xfs_da_node_create(args);
393         ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
394
395         error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, whichfork);
396         if (error)
397                 return error;
398         bp->b_ops = &xfs_da3_node_buf_ops;
399         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
400         node = bp->b_addr;
401
402         if (xfs_sb_version_hascrc(&mp->m_sb)) {
403                 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
404
405                 memset(hdr3, 0, sizeof(struct xfs_da3_node_hdr));
406                 ichdr.magic = XFS_DA3_NODE_MAGIC;
407                 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
408                 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
409                 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid);
410         } else {
411                 ichdr.magic = XFS_DA_NODE_MAGIC;
412         }
413         ichdr.level = level;
414
415         xfs_da3_node_hdr_to_disk(dp->i_mount, node, &ichdr);
416         xfs_trans_log_buf(tp, bp,
417                 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
418
419         *bpp = bp;
420         return 0;
421 }
422
423 /*
424  * Split a leaf node, rebalance, then possibly split
425  * intermediate nodes, rebalance, etc.
426  */
427 int                                                     /* error */
428 xfs_da3_split(
429         struct xfs_da_state     *state)
430 {
431         struct xfs_da_state_blk *oldblk;
432         struct xfs_da_state_blk *newblk;
433         struct xfs_da_state_blk *addblk;
434         struct xfs_da_intnode   *node;
435         int                     max;
436         int                     action = 0;
437         int                     error;
438         int                     i;
439
440         trace_xfs_da_split(state->args);
441
442         /*
443          * Walk back up the tree splitting/inserting/adjusting as necessary.
444          * If we need to insert and there isn't room, split the node, then
445          * decide which fragment to insert the new block from below into.
446          * Note that we may split the root this way, but we need more fixup.
447          */
448         max = state->path.active - 1;
449         ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
450         ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
451                state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
452
453         addblk = &state->path.blk[max];         /* initial dummy value */
454         for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
455                 oldblk = &state->path.blk[i];
456                 newblk = &state->altpath.blk[i];
457
458                 /*
459                  * If a leaf node then
460                  *     Allocate a new leaf node, then rebalance across them.
461                  * else if an intermediate node then
462                  *     We split on the last layer, must we split the node?
463                  */
464                 switch (oldblk->magic) {
465                 case XFS_ATTR_LEAF_MAGIC:
466                         error = xfs_attr3_leaf_split(state, oldblk, newblk);
467                         if ((error != 0) && (error != -ENOSPC)) {
468                                 return error;   /* GROT: attr is inconsistent */
469                         }
470                         if (!error) {
471                                 addblk = newblk;
472                                 break;
473                         }
474                         /*
475                          * Entry wouldn't fit, split the leaf again. The new
476                          * extrablk will be consumed by xfs_da3_node_split if
477                          * the node is split.
478                          */
479                         state->extravalid = 1;
480                         if (state->inleaf) {
481                                 state->extraafter = 0;  /* before newblk */
482                                 trace_xfs_attr_leaf_split_before(state->args);
483                                 error = xfs_attr3_leaf_split(state, oldblk,
484                                                             &state->extrablk);
485                         } else {
486                                 state->extraafter = 1;  /* after newblk */
487                                 trace_xfs_attr_leaf_split_after(state->args);
488                                 error = xfs_attr3_leaf_split(state, newblk,
489                                                             &state->extrablk);
490                         }
491                         if (error)
492                                 return error;   /* GROT: attr inconsistent */
493                         addblk = newblk;
494                         break;
495                 case XFS_DIR2_LEAFN_MAGIC:
496                         error = xfs_dir2_leafn_split(state, oldblk, newblk);
497                         if (error)
498                                 return error;
499                         addblk = newblk;
500                         break;
501                 case XFS_DA_NODE_MAGIC:
502                         error = xfs_da3_node_split(state, oldblk, newblk, addblk,
503                                                          max - i, &action);
504                         addblk->bp = NULL;
505                         if (error)
506                                 return error;   /* GROT: dir is inconsistent */
507                         /*
508                          * Record the newly split block for the next time thru?
509                          */
510                         if (action)
511                                 addblk = newblk;
512                         else
513                                 addblk = NULL;
514                         break;
515                 }
516
517                 /*
518                  * Update the btree to show the new hashval for this child.
519                  */
520                 xfs_da3_fixhashpath(state, &state->path);
521         }
522         if (!addblk)
523                 return 0;
524
525         /*
526          * xfs_da3_node_split() should have consumed any extra blocks we added
527          * during a double leaf split in the attr fork. This is guaranteed as
528          * we can't be here if the attr fork only has a single leaf block.
529          */
530         ASSERT(state->extravalid == 0 ||
531                state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
532
533         /*
534          * Split the root node.
535          */
536         ASSERT(state->path.active == 0);
537         oldblk = &state->path.blk[0];
538         error = xfs_da3_root_split(state, oldblk, addblk);
539         if (error)
540                 goto out;
541
542         /*
543          * Update pointers to the node which used to be block 0 and just got
544          * bumped because of the addition of a new root node.  Note that the
545          * original block 0 could be at any position in the list of blocks in
546          * the tree.
547          *
548          * Note: the magic numbers and sibling pointers are in the same physical
549          * place for both v2 and v3 headers (by design). Hence it doesn't matter
550          * which version of the xfs_da_intnode structure we use here as the
551          * result will be the same using either structure.
552          */
553         node = oldblk->bp->b_addr;
554         if (node->hdr.info.forw) {
555                 if (be32_to_cpu(node->hdr.info.forw) != addblk->blkno) {
556                         xfs_buf_corruption_error(oldblk->bp);
557                         error = -EFSCORRUPTED;
558                         goto out;
559                 }
560                 node = addblk->bp->b_addr;
561                 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
562                 xfs_trans_log_buf(state->args->trans, addblk->bp,
563                                   XFS_DA_LOGRANGE(node, &node->hdr.info,
564                                   sizeof(node->hdr.info)));
565         }
566         node = oldblk->bp->b_addr;
567         if (node->hdr.info.back) {
568                 if (be32_to_cpu(node->hdr.info.back) != addblk->blkno) {
569                         xfs_buf_corruption_error(oldblk->bp);
570                         error = -EFSCORRUPTED;
571                         goto out;
572                 }
573                 node = addblk->bp->b_addr;
574                 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
575                 xfs_trans_log_buf(state->args->trans, addblk->bp,
576                                   XFS_DA_LOGRANGE(node, &node->hdr.info,
577                                   sizeof(node->hdr.info)));
578         }
579 out:
580         addblk->bp = NULL;
581         return error;
582 }
583
584 /*
585  * Split the root.  We have to create a new root and point to the two
586  * parts (the split old root) that we just created.  Copy block zero to
587  * the EOF, extending the inode in process.
588  */
589 STATIC int                                              /* error */
590 xfs_da3_root_split(
591         struct xfs_da_state     *state,
592         struct xfs_da_state_blk *blk1,
593         struct xfs_da_state_blk *blk2)
594 {
595         struct xfs_da_intnode   *node;
596         struct xfs_da_intnode   *oldroot;
597         struct xfs_da_node_entry *btree;
598         struct xfs_da3_icnode_hdr nodehdr;
599         struct xfs_da_args      *args;
600         struct xfs_buf          *bp;
601         struct xfs_inode        *dp;
602         struct xfs_trans        *tp;
603         struct xfs_dir2_leaf    *leaf;
604         xfs_dablk_t             blkno;
605         int                     level;
606         int                     error;
607         int                     size;
608
609         trace_xfs_da_root_split(state->args);
610
611         /*
612          * Copy the existing (incorrect) block from the root node position
613          * to a free space somewhere.
614          */
615         args = state->args;
616         error = xfs_da_grow_inode(args, &blkno);
617         if (error)
618                 return error;
619
620         dp = args->dp;
621         tp = args->trans;
622         error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
623         if (error)
624                 return error;
625         node = bp->b_addr;
626         oldroot = blk1->bp->b_addr;
627         if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
628             oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
629                 struct xfs_da3_icnode_hdr icnodehdr;
630
631                 xfs_da3_node_hdr_from_disk(dp->i_mount, &icnodehdr, oldroot);
632                 btree = icnodehdr.btree;
633                 size = (int)((char *)&btree[icnodehdr.count] - (char *)oldroot);
634                 level = icnodehdr.level;
635
636                 /*
637                  * we are about to copy oldroot to bp, so set up the type
638                  * of bp while we know exactly what it will be.
639                  */
640                 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
641         } else {
642                 struct xfs_dir3_icleaf_hdr leafhdr;
643                 struct xfs_dir2_leaf_entry *ents;
644
645                 leaf = (xfs_dir2_leaf_t *)oldroot;
646                 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
647                 ents = dp->d_ops->leaf_ents_p(leaf);
648
649                 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
650                        leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
651                 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
652                 level = 0;
653
654                 /*
655                  * we are about to copy oldroot to bp, so set up the type
656                  * of bp while we know exactly what it will be.
657                  */
658                 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
659         }
660
661         /*
662          * we can copy most of the information in the node from one block to
663          * another, but for CRC enabled headers we have to make sure that the
664          * block specific identifiers are kept intact. We update the buffer
665          * directly for this.
666          */
667         memcpy(node, oldroot, size);
668         if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
669             oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
670                 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
671
672                 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
673         }
674         xfs_trans_log_buf(tp, bp, 0, size - 1);
675
676         bp->b_ops = blk1->bp->b_ops;
677         xfs_trans_buf_copy_type(bp, blk1->bp);
678         blk1->bp = bp;
679         blk1->blkno = blkno;
680
681         /*
682          * Set up the new root node.
683          */
684         error = xfs_da3_node_create(args,
685                 (args->whichfork == XFS_DATA_FORK) ? args->geo->leafblk : 0,
686                 level + 1, &bp, args->whichfork);
687         if (error)
688                 return error;
689
690         node = bp->b_addr;
691         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
692         btree = nodehdr.btree;
693         btree[0].hashval = cpu_to_be32(blk1->hashval);
694         btree[0].before = cpu_to_be32(blk1->blkno);
695         btree[1].hashval = cpu_to_be32(blk2->hashval);
696         btree[1].before = cpu_to_be32(blk2->blkno);
697         nodehdr.count = 2;
698         xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
699
700 #ifdef DEBUG
701         if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
702             oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
703                 ASSERT(blk1->blkno >= args->geo->leafblk &&
704                        blk1->blkno < args->geo->freeblk);
705                 ASSERT(blk2->blkno >= args->geo->leafblk &&
706                        blk2->blkno < args->geo->freeblk);
707         }
708 #endif
709
710         /* Header is already logged by xfs_da_node_create */
711         xfs_trans_log_buf(tp, bp,
712                 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
713
714         return 0;
715 }
716
717 /*
718  * Split the node, rebalance, then add the new entry.
719  */
720 STATIC int                                              /* error */
721 xfs_da3_node_split(
722         struct xfs_da_state     *state,
723         struct xfs_da_state_blk *oldblk,
724         struct xfs_da_state_blk *newblk,
725         struct xfs_da_state_blk *addblk,
726         int                     treelevel,
727         int                     *result)
728 {
729         struct xfs_da_intnode   *node;
730         struct xfs_da3_icnode_hdr nodehdr;
731         xfs_dablk_t             blkno;
732         int                     newcount;
733         int                     error;
734         int                     useextra;
735         struct xfs_inode        *dp = state->args->dp;
736
737         trace_xfs_da_node_split(state->args);
738
739         node = oldblk->bp->b_addr;
740         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
741
742         /*
743          * With V2 dirs the extra block is data or freespace.
744          */
745         useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
746         newcount = 1 + useextra;
747         /*
748          * Do we have to split the node?
749          */
750         if (nodehdr.count + newcount > state->args->geo->node_ents) {
751                 /*
752                  * Allocate a new node, add to the doubly linked chain of
753                  * nodes, then move some of our excess entries into it.
754                  */
755                 error = xfs_da_grow_inode(state->args, &blkno);
756                 if (error)
757                         return error;   /* GROT: dir is inconsistent */
758
759                 error = xfs_da3_node_create(state->args, blkno, treelevel,
760                                            &newblk->bp, state->args->whichfork);
761                 if (error)
762                         return error;   /* GROT: dir is inconsistent */
763                 newblk->blkno = blkno;
764                 newblk->magic = XFS_DA_NODE_MAGIC;
765                 xfs_da3_node_rebalance(state, oldblk, newblk);
766                 error = xfs_da3_blk_link(state, oldblk, newblk);
767                 if (error)
768                         return error;
769                 *result = 1;
770         } else {
771                 *result = 0;
772         }
773
774         /*
775          * Insert the new entry(s) into the correct block
776          * (updating last hashval in the process).
777          *
778          * xfs_da3_node_add() inserts BEFORE the given index,
779          * and as a result of using node_lookup_int() we always
780          * point to a valid entry (not after one), but a split
781          * operation always results in a new block whose hashvals
782          * FOLLOW the current block.
783          *
784          * If we had double-split op below us, then add the extra block too.
785          */
786         node = oldblk->bp->b_addr;
787         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
788         if (oldblk->index <= nodehdr.count) {
789                 oldblk->index++;
790                 xfs_da3_node_add(state, oldblk, addblk);
791                 if (useextra) {
792                         if (state->extraafter)
793                                 oldblk->index++;
794                         xfs_da3_node_add(state, oldblk, &state->extrablk);
795                         state->extravalid = 0;
796                 }
797         } else {
798                 newblk->index++;
799                 xfs_da3_node_add(state, newblk, addblk);
800                 if (useextra) {
801                         if (state->extraafter)
802                                 newblk->index++;
803                         xfs_da3_node_add(state, newblk, &state->extrablk);
804                         state->extravalid = 0;
805                 }
806         }
807
808         return 0;
809 }
810
811 /*
812  * Balance the btree elements between two intermediate nodes,
813  * usually one full and one empty.
814  *
815  * NOTE: if blk2 is empty, then it will get the upper half of blk1.
816  */
817 STATIC void
818 xfs_da3_node_rebalance(
819         struct xfs_da_state     *state,
820         struct xfs_da_state_blk *blk1,
821         struct xfs_da_state_blk *blk2)
822 {
823         struct xfs_da_intnode   *node1;
824         struct xfs_da_intnode   *node2;
825         struct xfs_da_intnode   *tmpnode;
826         struct xfs_da_node_entry *btree1;
827         struct xfs_da_node_entry *btree2;
828         struct xfs_da_node_entry *btree_s;
829         struct xfs_da_node_entry *btree_d;
830         struct xfs_da3_icnode_hdr nodehdr1;
831         struct xfs_da3_icnode_hdr nodehdr2;
832         struct xfs_trans        *tp;
833         int                     count;
834         int                     tmp;
835         int                     swap = 0;
836         struct xfs_inode        *dp = state->args->dp;
837
838         trace_xfs_da_node_rebalance(state->args);
839
840         node1 = blk1->bp->b_addr;
841         node2 = blk2->bp->b_addr;
842         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
843         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
844         btree1 = nodehdr1.btree;
845         btree2 = nodehdr2.btree;
846
847         /*
848          * Figure out how many entries need to move, and in which direction.
849          * Swap the nodes around if that makes it simpler.
850          */
851         if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
852             ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
853              (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
854                         be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
855                 tmpnode = node1;
856                 node1 = node2;
857                 node2 = tmpnode;
858                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
859                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
860                 btree1 = nodehdr1.btree;
861                 btree2 = nodehdr2.btree;
862                 swap = 1;
863         }
864
865         count = (nodehdr1.count - nodehdr2.count) / 2;
866         if (count == 0)
867                 return;
868         tp = state->args->trans;
869         /*
870          * Two cases: high-to-low and low-to-high.
871          */
872         if (count > 0) {
873                 /*
874                  * Move elements in node2 up to make a hole.
875                  */
876                 tmp = nodehdr2.count;
877                 if (tmp > 0) {
878                         tmp *= (uint)sizeof(xfs_da_node_entry_t);
879                         btree_s = &btree2[0];
880                         btree_d = &btree2[count];
881                         memmove(btree_d, btree_s, tmp);
882                 }
883
884                 /*
885                  * Move the req'd B-tree elements from high in node1 to
886                  * low in node2.
887                  */
888                 nodehdr2.count += count;
889                 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
890                 btree_s = &btree1[nodehdr1.count - count];
891                 btree_d = &btree2[0];
892                 memcpy(btree_d, btree_s, tmp);
893                 nodehdr1.count -= count;
894         } else {
895                 /*
896                  * Move the req'd B-tree elements from low in node2 to
897                  * high in node1.
898                  */
899                 count = -count;
900                 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
901                 btree_s = &btree2[0];
902                 btree_d = &btree1[nodehdr1.count];
903                 memcpy(btree_d, btree_s, tmp);
904                 nodehdr1.count += count;
905
906                 xfs_trans_log_buf(tp, blk1->bp,
907                         XFS_DA_LOGRANGE(node1, btree_d, tmp));
908
909                 /*
910                  * Move elements in node2 down to fill the hole.
911                  */
912                 tmp  = nodehdr2.count - count;
913                 tmp *= (uint)sizeof(xfs_da_node_entry_t);
914                 btree_s = &btree2[count];
915                 btree_d = &btree2[0];
916                 memmove(btree_d, btree_s, tmp);
917                 nodehdr2.count -= count;
918         }
919
920         /*
921          * Log header of node 1 and all current bits of node 2.
922          */
923         xfs_da3_node_hdr_to_disk(dp->i_mount, node1, &nodehdr1);
924         xfs_trans_log_buf(tp, blk1->bp,
925                 XFS_DA_LOGRANGE(node1, &node1->hdr, dp->d_ops->node_hdr_size));
926
927         xfs_da3_node_hdr_to_disk(dp->i_mount, node2, &nodehdr2);
928         xfs_trans_log_buf(tp, blk2->bp,
929                 XFS_DA_LOGRANGE(node2, &node2->hdr,
930                                 dp->d_ops->node_hdr_size +
931                                 (sizeof(btree2[0]) * nodehdr2.count)));
932
933         /*
934          * Record the last hashval from each block for upward propagation.
935          * (note: don't use the swapped node pointers)
936          */
937         if (swap) {
938                 node1 = blk1->bp->b_addr;
939                 node2 = blk2->bp->b_addr;
940                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr1, node1);
941                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr2, node2);
942                 btree1 = nodehdr1.btree;
943                 btree2 = nodehdr2.btree;
944         }
945         blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
946         blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
947
948         /*
949          * Adjust the expected index for insertion.
950          */
951         if (blk1->index >= nodehdr1.count) {
952                 blk2->index = blk1->index - nodehdr1.count;
953                 blk1->index = nodehdr1.count + 1;       /* make it invalid */
954         }
955 }
956
957 /*
958  * Add a new entry to an intermediate node.
959  */
960 STATIC void
961 xfs_da3_node_add(
962         struct xfs_da_state     *state,
963         struct xfs_da_state_blk *oldblk,
964         struct xfs_da_state_blk *newblk)
965 {
966         struct xfs_da_intnode   *node;
967         struct xfs_da3_icnode_hdr nodehdr;
968         struct xfs_da_node_entry *btree;
969         int                     tmp;
970         struct xfs_inode        *dp = state->args->dp;
971
972         trace_xfs_da_node_add(state->args);
973
974         node = oldblk->bp->b_addr;
975         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
976         btree = nodehdr.btree;
977
978         ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
979         ASSERT(newblk->blkno != 0);
980         if (state->args->whichfork == XFS_DATA_FORK)
981                 ASSERT(newblk->blkno >= state->args->geo->leafblk &&
982                        newblk->blkno < state->args->geo->freeblk);
983
984         /*
985          * We may need to make some room before we insert the new node.
986          */
987         tmp = 0;
988         if (oldblk->index < nodehdr.count) {
989                 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
990                 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
991         }
992         btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
993         btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
994         xfs_trans_log_buf(state->args->trans, oldblk->bp,
995                 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
996                                 tmp + sizeof(*btree)));
997
998         nodehdr.count += 1;
999         xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1000         xfs_trans_log_buf(state->args->trans, oldblk->bp,
1001                 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
1002
1003         /*
1004          * Copy the last hash value from the oldblk to propagate upwards.
1005          */
1006         oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1007 }
1008
1009 /*========================================================================
1010  * Routines used for shrinking the Btree.
1011  *========================================================================*/
1012
1013 /*
1014  * Deallocate an empty leaf node, remove it from its parent,
1015  * possibly deallocating that block, etc...
1016  */
1017 int
1018 xfs_da3_join(
1019         struct xfs_da_state     *state)
1020 {
1021         struct xfs_da_state_blk *drop_blk;
1022         struct xfs_da_state_blk *save_blk;
1023         int                     action = 0;
1024         int                     error;
1025
1026         trace_xfs_da_join(state->args);
1027
1028         drop_blk = &state->path.blk[ state->path.active-1 ];
1029         save_blk = &state->altpath.blk[ state->path.active-1 ];
1030         ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
1031         ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
1032                drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1033
1034         /*
1035          * Walk back up the tree joining/deallocating as necessary.
1036          * When we stop dropping blocks, break out.
1037          */
1038         for (  ; state->path.active >= 2; drop_blk--, save_blk--,
1039                  state->path.active--) {
1040                 /*
1041                  * See if we can combine the block with a neighbor.
1042                  *   (action == 0) => no options, just leave
1043                  *   (action == 1) => coalesce, then unlink
1044                  *   (action == 2) => block empty, unlink it
1045                  */
1046                 switch (drop_blk->magic) {
1047                 case XFS_ATTR_LEAF_MAGIC:
1048                         error = xfs_attr3_leaf_toosmall(state, &action);
1049                         if (error)
1050                                 return error;
1051                         if (action == 0)
1052                                 return 0;
1053                         xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
1054                         break;
1055                 case XFS_DIR2_LEAFN_MAGIC:
1056                         error = xfs_dir2_leafn_toosmall(state, &action);
1057                         if (error)
1058                                 return error;
1059                         if (action == 0)
1060                                 return 0;
1061                         xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1062                         break;
1063                 case XFS_DA_NODE_MAGIC:
1064                         /*
1065                          * Remove the offending node, fixup hashvals,
1066                          * check for a toosmall neighbor.
1067                          */
1068                         xfs_da3_node_remove(state, drop_blk);
1069                         xfs_da3_fixhashpath(state, &state->path);
1070                         error = xfs_da3_node_toosmall(state, &action);
1071                         if (error)
1072                                 return error;
1073                         if (action == 0)
1074                                 return 0;
1075                         xfs_da3_node_unbalance(state, drop_blk, save_blk);
1076                         break;
1077                 }
1078                 xfs_da3_fixhashpath(state, &state->altpath);
1079                 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
1080                 xfs_da_state_kill_altpath(state);
1081                 if (error)
1082                         return error;
1083                 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1084                                                          drop_blk->bp);
1085                 drop_blk->bp = NULL;
1086                 if (error)
1087                         return error;
1088         }
1089         /*
1090          * We joined all the way to the top.  If it turns out that
1091          * we only have one entry in the root, make the child block
1092          * the new root.
1093          */
1094         xfs_da3_node_remove(state, drop_blk);
1095         xfs_da3_fixhashpath(state, &state->path);
1096         error = xfs_da3_root_join(state, &state->path.blk[0]);
1097         return error;
1098 }
1099
1100 #ifdef  DEBUG
1101 static void
1102 xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1103 {
1104         __be16  magic = blkinfo->magic;
1105
1106         if (level == 1) {
1107                 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1108                        magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
1109                        magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1110                        magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
1111         } else {
1112                 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1113                        magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1114         }
1115         ASSERT(!blkinfo->forw);
1116         ASSERT(!blkinfo->back);
1117 }
1118 #else   /* !DEBUG */
1119 #define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1120 #endif  /* !DEBUG */
1121
1122 /*
1123  * We have only one entry in the root.  Copy the only remaining child of
1124  * the old root to block 0 as the new root node.
1125  */
1126 STATIC int
1127 xfs_da3_root_join(
1128         struct xfs_da_state     *state,
1129         struct xfs_da_state_blk *root_blk)
1130 {
1131         struct xfs_da_intnode   *oldroot;
1132         struct xfs_da_args      *args;
1133         xfs_dablk_t             child;
1134         struct xfs_buf          *bp;
1135         struct xfs_da3_icnode_hdr oldroothdr;
1136         int                     error;
1137         struct xfs_inode        *dp = state->args->dp;
1138
1139         trace_xfs_da_root_join(state->args);
1140
1141         ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
1142
1143         args = state->args;
1144         oldroot = root_blk->bp->b_addr;
1145         xfs_da3_node_hdr_from_disk(dp->i_mount, &oldroothdr, oldroot);
1146         ASSERT(oldroothdr.forw == 0);
1147         ASSERT(oldroothdr.back == 0);
1148
1149         /*
1150          * If the root has more than one child, then don't do anything.
1151          */
1152         if (oldroothdr.count > 1)
1153                 return 0;
1154
1155         /*
1156          * Read in the (only) child block, then copy those bytes into
1157          * the root block's buffer and free the original child block.
1158          */
1159         child = be32_to_cpu(oldroothdr.btree[0].before);
1160         ASSERT(child != 0);
1161         error = xfs_da3_node_read(args->trans, dp, child, -1, &bp,
1162                                              args->whichfork);
1163         if (error)
1164                 return error;
1165         xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
1166
1167         /*
1168          * This could be copying a leaf back into the root block in the case of
1169          * there only being a single leaf block left in the tree. Hence we have
1170          * to update the b_ops pointer as well to match the buffer type change
1171          * that could occur. For dir3 blocks we also need to update the block
1172          * number in the buffer header.
1173          */
1174         memcpy(root_blk->bp->b_addr, bp->b_addr, args->geo->blksize);
1175         root_blk->bp->b_ops = bp->b_ops;
1176         xfs_trans_buf_copy_type(root_blk->bp, bp);
1177         if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1178                 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1179                 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1180         }
1181         xfs_trans_log_buf(args->trans, root_blk->bp, 0,
1182                           args->geo->blksize - 1);
1183         error = xfs_da_shrink_inode(args, child, bp);
1184         return error;
1185 }
1186
1187 /*
1188  * Check a node block and its neighbors to see if the block should be
1189  * collapsed into one or the other neighbor.  Always keep the block
1190  * with the smaller block number.
1191  * If the current block is over 50% full, don't try to join it, return 0.
1192  * If the block is empty, fill in the state structure and return 2.
1193  * If it can be collapsed, fill in the state structure and return 1.
1194  * If nothing can be done, return 0.
1195  */
1196 STATIC int
1197 xfs_da3_node_toosmall(
1198         struct xfs_da_state     *state,
1199         int                     *action)
1200 {
1201         struct xfs_da_intnode   *node;
1202         struct xfs_da_state_blk *blk;
1203         struct xfs_da_blkinfo   *info;
1204         xfs_dablk_t             blkno;
1205         struct xfs_buf          *bp;
1206         struct xfs_da3_icnode_hdr nodehdr;
1207         int                     count;
1208         int                     forward;
1209         int                     error;
1210         int                     retval;
1211         int                     i;
1212         struct xfs_inode        *dp = state->args->dp;
1213
1214         trace_xfs_da_node_toosmall(state->args);
1215
1216         /*
1217          * Check for the degenerate case of the block being over 50% full.
1218          * If so, it's not worth even looking to see if we might be able
1219          * to coalesce with a sibling.
1220          */
1221         blk = &state->path.blk[ state->path.active-1 ];
1222         info = blk->bp->b_addr;
1223         node = (xfs_da_intnode_t *)info;
1224         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1225         if (nodehdr.count > (state->args->geo->node_ents >> 1)) {
1226                 *action = 0;    /* blk over 50%, don't try to join */
1227                 return 0;       /* blk over 50%, don't try to join */
1228         }
1229
1230         /*
1231          * Check for the degenerate case of the block being empty.
1232          * If the block is empty, we'll simply delete it, no need to
1233          * coalesce it with a sibling block.  We choose (arbitrarily)
1234          * to merge with the forward block unless it is NULL.
1235          */
1236         if (nodehdr.count == 0) {
1237                 /*
1238                  * Make altpath point to the block we want to keep and
1239                  * path point to the block we want to drop (this one).
1240                  */
1241                 forward = (info->forw != 0);
1242                 memcpy(&state->altpath, &state->path, sizeof(state->path));
1243                 error = xfs_da3_path_shift(state, &state->altpath, forward,
1244                                                  0, &retval);
1245                 if (error)
1246                         return error;
1247                 if (retval) {
1248                         *action = 0;
1249                 } else {
1250                         *action = 2;
1251                 }
1252                 return 0;
1253         }
1254
1255         /*
1256          * Examine each sibling block to see if we can coalesce with
1257          * at least 25% free space to spare.  We need to figure out
1258          * whether to merge with the forward or the backward block.
1259          * We prefer coalescing with the lower numbered sibling so as
1260          * to shrink a directory over time.
1261          */
1262         count  = state->args->geo->node_ents;
1263         count -= state->args->geo->node_ents >> 2;
1264         count -= nodehdr.count;
1265
1266         /* start with smaller blk num */
1267         forward = nodehdr.forw < nodehdr.back;
1268         for (i = 0; i < 2; forward = !forward, i++) {
1269                 struct xfs_da3_icnode_hdr thdr;
1270                 if (forward)
1271                         blkno = nodehdr.forw;
1272                 else
1273                         blkno = nodehdr.back;
1274                 if (blkno == 0)
1275                         continue;
1276                 error = xfs_da3_node_read(state->args->trans, dp,
1277                                         blkno, -1, &bp, state->args->whichfork);
1278                 if (error)
1279                         return error;
1280
1281                 node = bp->b_addr;
1282                 xfs_da3_node_hdr_from_disk(dp->i_mount, &thdr, node);
1283                 xfs_trans_brelse(state->args->trans, bp);
1284
1285                 if (count - thdr.count >= 0)
1286                         break;  /* fits with at least 25% to spare */
1287         }
1288         if (i >= 2) {
1289                 *action = 0;
1290                 return 0;
1291         }
1292
1293         /*
1294          * Make altpath point to the block we want to keep (the lower
1295          * numbered block) and path point to the block we want to drop.
1296          */
1297         memcpy(&state->altpath, &state->path, sizeof(state->path));
1298         if (blkno < blk->blkno) {
1299                 error = xfs_da3_path_shift(state, &state->altpath, forward,
1300                                                  0, &retval);
1301         } else {
1302                 error = xfs_da3_path_shift(state, &state->path, forward,
1303                                                  0, &retval);
1304         }
1305         if (error)
1306                 return error;
1307         if (retval) {
1308                 *action = 0;
1309                 return 0;
1310         }
1311         *action = 1;
1312         return 0;
1313 }
1314
1315 /*
1316  * Pick up the last hashvalue from an intermediate node.
1317  */
1318 STATIC uint
1319 xfs_da3_node_lasthash(
1320         struct xfs_inode        *dp,
1321         struct xfs_buf          *bp,
1322         int                     *count)
1323 {
1324         struct xfs_da3_icnode_hdr nodehdr;
1325
1326         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, bp->b_addr);
1327         if (count)
1328                 *count = nodehdr.count;
1329         if (!nodehdr.count)
1330                 return 0;
1331         return be32_to_cpu(nodehdr.btree[nodehdr.count - 1].hashval);
1332 }
1333
1334 /*
1335  * Walk back up the tree adjusting hash values as necessary,
1336  * when we stop making changes, return.
1337  */
1338 void
1339 xfs_da3_fixhashpath(
1340         struct xfs_da_state     *state,
1341         struct xfs_da_state_path *path)
1342 {
1343         struct xfs_da_state_blk *blk;
1344         struct xfs_da_intnode   *node;
1345         struct xfs_da_node_entry *btree;
1346         xfs_dahash_t            lasthash=0;
1347         int                     level;
1348         int                     count;
1349         struct xfs_inode        *dp = state->args->dp;
1350
1351         trace_xfs_da_fixhashpath(state->args);
1352
1353         level = path->active-1;
1354         blk = &path->blk[ level ];
1355         switch (blk->magic) {
1356         case XFS_ATTR_LEAF_MAGIC:
1357                 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1358                 if (count == 0)
1359                         return;
1360                 break;
1361         case XFS_DIR2_LEAFN_MAGIC:
1362                 lasthash = xfs_dir2_leaf_lasthash(dp, blk->bp, &count);
1363                 if (count == 0)
1364                         return;
1365                 break;
1366         case XFS_DA_NODE_MAGIC:
1367                 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
1368                 if (count == 0)
1369                         return;
1370                 break;
1371         }
1372         for (blk--, level--; level >= 0; blk--, level--) {
1373                 struct xfs_da3_icnode_hdr nodehdr;
1374
1375                 node = blk->bp->b_addr;
1376                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1377                 btree = nodehdr.btree;
1378                 if (be32_to_cpu(btree[blk->index].hashval) == lasthash)
1379                         break;
1380                 blk->hashval = lasthash;
1381                 btree[blk->index].hashval = cpu_to_be32(lasthash);
1382                 xfs_trans_log_buf(state->args->trans, blk->bp,
1383                                   XFS_DA_LOGRANGE(node, &btree[blk->index],
1384                                                   sizeof(*btree)));
1385
1386                 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
1387         }
1388 }
1389
1390 /*
1391  * Remove an entry from an intermediate node.
1392  */
1393 STATIC void
1394 xfs_da3_node_remove(
1395         struct xfs_da_state     *state,
1396         struct xfs_da_state_blk *drop_blk)
1397 {
1398         struct xfs_da_intnode   *node;
1399         struct xfs_da3_icnode_hdr nodehdr;
1400         struct xfs_da_node_entry *btree;
1401         int                     index;
1402         int                     tmp;
1403         struct xfs_inode        *dp = state->args->dp;
1404
1405         trace_xfs_da_node_remove(state->args);
1406
1407         node = drop_blk->bp->b_addr;
1408         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1409         ASSERT(drop_blk->index < nodehdr.count);
1410         ASSERT(drop_blk->index >= 0);
1411
1412         /*
1413          * Copy over the offending entry, or just zero it out.
1414          */
1415         index = drop_blk->index;
1416         btree = nodehdr.btree;
1417         if (index < nodehdr.count - 1) {
1418                 tmp  = nodehdr.count - index - 1;
1419                 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1420                 memmove(&btree[index], &btree[index + 1], tmp);
1421                 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1422                     XFS_DA_LOGRANGE(node, &btree[index], tmp));
1423                 index = nodehdr.count - 1;
1424         }
1425         memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
1426         xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1427             XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1428         nodehdr.count -= 1;
1429         xfs_da3_node_hdr_to_disk(dp->i_mount, node, &nodehdr);
1430         xfs_trans_log_buf(state->args->trans, drop_blk->bp,
1431             XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size));
1432
1433         /*
1434          * Copy the last hash value from the block to propagate upwards.
1435          */
1436         drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
1437 }
1438
1439 /*
1440  * Unbalance the elements between two intermediate nodes,
1441  * move all Btree elements from one node into another.
1442  */
1443 STATIC void
1444 xfs_da3_node_unbalance(
1445         struct xfs_da_state     *state,
1446         struct xfs_da_state_blk *drop_blk,
1447         struct xfs_da_state_blk *save_blk)
1448 {
1449         struct xfs_da_intnode   *drop_node;
1450         struct xfs_da_intnode   *save_node;
1451         struct xfs_da_node_entry *drop_btree;
1452         struct xfs_da_node_entry *save_btree;
1453         struct xfs_da3_icnode_hdr drop_hdr;
1454         struct xfs_da3_icnode_hdr save_hdr;
1455         struct xfs_trans        *tp;
1456         int                     sindex;
1457         int                     tmp;
1458         struct xfs_inode        *dp = state->args->dp;
1459
1460         trace_xfs_da_node_unbalance(state->args);
1461
1462         drop_node = drop_blk->bp->b_addr;
1463         save_node = save_blk->bp->b_addr;
1464         xfs_da3_node_hdr_from_disk(dp->i_mount, &drop_hdr, drop_node);
1465         xfs_da3_node_hdr_from_disk(dp->i_mount, &save_hdr, save_node);
1466         drop_btree = drop_hdr.btree;
1467         save_btree = save_hdr.btree;
1468         tp = state->args->trans;
1469
1470         /*
1471          * If the dying block has lower hashvals, then move all the
1472          * elements in the remaining block up to make a hole.
1473          */
1474         if ((be32_to_cpu(drop_btree[0].hashval) <
1475                         be32_to_cpu(save_btree[0].hashval)) ||
1476             (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1477                         be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1478                 /* XXX: check this - is memmove dst correct? */
1479                 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1480                 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1481
1482                 sindex = 0;
1483                 xfs_trans_log_buf(tp, save_blk->bp,
1484                         XFS_DA_LOGRANGE(save_node, &save_btree[0],
1485                                 (save_hdr.count + drop_hdr.count) *
1486                                                 sizeof(xfs_da_node_entry_t)));
1487         } else {
1488                 sindex = save_hdr.count;
1489                 xfs_trans_log_buf(tp, save_blk->bp,
1490                         XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1491                                 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
1492         }
1493
1494         /*
1495          * Move all the B-tree elements from drop_blk to save_blk.
1496          */
1497         tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1498         memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1499         save_hdr.count += drop_hdr.count;
1500
1501         xfs_da3_node_hdr_to_disk(dp->i_mount, save_node, &save_hdr);
1502         xfs_trans_log_buf(tp, save_blk->bp,
1503                 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1504                                 dp->d_ops->node_hdr_size));
1505
1506         /*
1507          * Save the last hashval in the remaining block for upward propagation.
1508          */
1509         save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
1510 }
1511
1512 /*========================================================================
1513  * Routines used for finding things in the Btree.
1514  *========================================================================*/
1515
1516 /*
1517  * Walk down the Btree looking for a particular filename, filling
1518  * in the state structure as we go.
1519  *
1520  * We will set the state structure to point to each of the elements
1521  * in each of the nodes where either the hashval is or should be.
1522  *
1523  * We support duplicate hashval's so for each entry in the current
1524  * node that could contain the desired hashval, descend.  This is a
1525  * pruned depth-first tree search.
1526  */
1527 int                                                     /* error */
1528 xfs_da3_node_lookup_int(
1529         struct xfs_da_state     *state,
1530         int                     *result)
1531 {
1532         struct xfs_da_state_blk *blk;
1533         struct xfs_da_blkinfo   *curr;
1534         struct xfs_da_intnode   *node;
1535         struct xfs_da_node_entry *btree;
1536         struct xfs_da3_icnode_hdr nodehdr;
1537         struct xfs_da_args      *args;
1538         xfs_dablk_t             blkno;
1539         xfs_dahash_t            hashval;
1540         xfs_dahash_t            btreehashval;
1541         int                     probe;
1542         int                     span;
1543         int                     max;
1544         int                     error;
1545         int                     retval;
1546         unsigned int            expected_level = 0;
1547         uint16_t                magic;
1548         struct xfs_inode        *dp = state->args->dp;
1549
1550         args = state->args;
1551
1552         /*
1553          * Descend thru the B-tree searching each level for the right
1554          * node to use, until the right hashval is found.
1555          */
1556         blkno = args->geo->leafblk;
1557         for (blk = &state->path.blk[0], state->path.active = 1;
1558                          state->path.active <= XFS_DA_NODE_MAXDEPTH;
1559                          blk++, state->path.active++) {
1560                 /*
1561                  * Read the next node down in the tree.
1562                  */
1563                 blk->blkno = blkno;
1564                 error = xfs_da3_node_read(args->trans, args->dp, blkno,
1565                                         -1, &blk->bp, args->whichfork);
1566                 if (error) {
1567                         blk->blkno = 0;
1568                         state->path.active--;
1569                         return error;
1570                 }
1571                 curr = blk->bp->b_addr;
1572                 magic = be16_to_cpu(curr->magic);
1573
1574                 if (magic == XFS_ATTR_LEAF_MAGIC ||
1575                     magic == XFS_ATTR3_LEAF_MAGIC) {
1576                         blk->magic = XFS_ATTR_LEAF_MAGIC;
1577                         blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1578                         break;
1579                 }
1580
1581                 if (magic == XFS_DIR2_LEAFN_MAGIC ||
1582                     magic == XFS_DIR3_LEAFN_MAGIC) {
1583                         blk->magic = XFS_DIR2_LEAFN_MAGIC;
1584                         blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
1585                                                               blk->bp, NULL);
1586                         break;
1587                 }
1588
1589                 if (magic != XFS_DA_NODE_MAGIC && magic != XFS_DA3_NODE_MAGIC) {
1590                         xfs_buf_corruption_error(blk->bp);
1591                         return -EFSCORRUPTED;
1592                 }
1593
1594                 blk->magic = XFS_DA_NODE_MAGIC;
1595
1596                 /*
1597                  * Search an intermediate node for a match.
1598                  */
1599                 node = blk->bp->b_addr;
1600                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr, node);
1601                 btree = nodehdr.btree;
1602
1603                 /* Tree taller than we can handle; bail out! */
1604                 if (nodehdr.level >= XFS_DA_NODE_MAXDEPTH) {
1605                         xfs_buf_corruption_error(blk->bp);
1606                         return -EFSCORRUPTED;
1607                 }
1608
1609                 /* Check the level from the root. */
1610                 if (blkno == args->geo->leafblk)
1611                         expected_level = nodehdr.level - 1;
1612                 else if (expected_level != nodehdr.level) {
1613                         xfs_buf_corruption_error(blk->bp);
1614                         return -EFSCORRUPTED;
1615                 } else
1616                         expected_level--;
1617
1618                 max = nodehdr.count;
1619                 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
1620
1621                 /*
1622                  * Binary search.  (note: small blocks will skip loop)
1623                  */
1624                 probe = span = max / 2;
1625                 hashval = args->hashval;
1626                 while (span > 4) {
1627                         span /= 2;
1628                         btreehashval = be32_to_cpu(btree[probe].hashval);
1629                         if (btreehashval < hashval)
1630                                 probe += span;
1631                         else if (btreehashval > hashval)
1632                                 probe -= span;
1633                         else
1634                                 break;
1635                 }
1636                 ASSERT((probe >= 0) && (probe < max));
1637                 ASSERT((span <= 4) ||
1638                         (be32_to_cpu(btree[probe].hashval) == hashval));
1639
1640                 /*
1641                  * Since we may have duplicate hashval's, find the first
1642                  * matching hashval in the node.
1643                  */
1644                 while (probe > 0 &&
1645                        be32_to_cpu(btree[probe].hashval) >= hashval) {
1646                         probe--;
1647                 }
1648                 while (probe < max &&
1649                        be32_to_cpu(btree[probe].hashval) < hashval) {
1650                         probe++;
1651                 }
1652
1653                 /*
1654                  * Pick the right block to descend on.
1655                  */
1656                 if (probe == max) {
1657                         blk->index = max - 1;
1658                         blkno = be32_to_cpu(btree[max - 1].before);
1659                 } else {
1660                         blk->index = probe;
1661                         blkno = be32_to_cpu(btree[probe].before);
1662                 }
1663
1664                 /* We can't point back to the root. */
1665                 if (blkno == args->geo->leafblk) {
1666                         XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW,
1667                                         dp->i_mount);
1668                         return -EFSCORRUPTED;
1669                 }
1670         }
1671
1672         if (expected_level != 0) {
1673                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, dp->i_mount);
1674                 return -EFSCORRUPTED;
1675         }
1676
1677         /*
1678          * A leaf block that ends in the hashval that we are interested in
1679          * (final hashval == search hashval) means that the next block may
1680          * contain more entries with the same hashval, shift upward to the
1681          * next leaf and keep searching.
1682          */
1683         for (;;) {
1684                 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
1685                         retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1686                                                         &blk->index, state);
1687                 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1688                         retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
1689                         blk->index = args->index;
1690                         args->blkno = blk->blkno;
1691                 } else {
1692                         ASSERT(0);
1693                         return -EFSCORRUPTED;
1694                 }
1695                 if (((retval == -ENOENT) || (retval == -ENOATTR)) &&
1696                     (blk->hashval == args->hashval)) {
1697                         error = xfs_da3_path_shift(state, &state->path, 1, 1,
1698                                                          &retval);
1699                         if (error)
1700                                 return error;
1701                         if (retval == 0) {
1702                                 continue;
1703                         } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
1704                                 /* path_shift() gives ENOENT */
1705                                 retval = -ENOATTR;
1706                         }
1707                 }
1708                 break;
1709         }
1710         *result = retval;
1711         return 0;
1712 }
1713
1714 /*========================================================================
1715  * Utility routines.
1716  *========================================================================*/
1717
1718 /*
1719  * Compare two intermediate nodes for "order".
1720  */
1721 STATIC int
1722 xfs_da3_node_order(
1723         struct xfs_inode *dp,
1724         struct xfs_buf  *node1_bp,
1725         struct xfs_buf  *node2_bp)
1726 {
1727         struct xfs_da_intnode   *node1;
1728         struct xfs_da_intnode   *node2;
1729         struct xfs_da_node_entry *btree1;
1730         struct xfs_da_node_entry *btree2;
1731         struct xfs_da3_icnode_hdr node1hdr;
1732         struct xfs_da3_icnode_hdr node2hdr;
1733
1734         node1 = node1_bp->b_addr;
1735         node2 = node2_bp->b_addr;
1736         xfs_da3_node_hdr_from_disk(dp->i_mount, &node1hdr, node1);
1737         xfs_da3_node_hdr_from_disk(dp->i_mount, &node2hdr, node2);
1738         btree1 = node1hdr.btree;
1739         btree2 = node2hdr.btree;
1740
1741         if (node1hdr.count > 0 && node2hdr.count > 0 &&
1742             ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1743              (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1744               be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1745                 return 1;
1746         }
1747         return 0;
1748 }
1749
1750 /*
1751  * Link a new block into a doubly linked list of blocks (of whatever type).
1752  */
1753 int                                                     /* error */
1754 xfs_da3_blk_link(
1755         struct xfs_da_state     *state,
1756         struct xfs_da_state_blk *old_blk,
1757         struct xfs_da_state_blk *new_blk)
1758 {
1759         struct xfs_da_blkinfo   *old_info;
1760         struct xfs_da_blkinfo   *new_info;
1761         struct xfs_da_blkinfo   *tmp_info;
1762         struct xfs_da_args      *args;
1763         struct xfs_buf          *bp;
1764         int                     before = 0;
1765         int                     error;
1766         struct xfs_inode        *dp = state->args->dp;
1767
1768         /*
1769          * Set up environment.
1770          */
1771         args = state->args;
1772         ASSERT(args != NULL);
1773         old_info = old_blk->bp->b_addr;
1774         new_info = new_blk->bp->b_addr;
1775         ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
1776                old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1777                old_blk->magic == XFS_ATTR_LEAF_MAGIC);
1778
1779         switch (old_blk->magic) {
1780         case XFS_ATTR_LEAF_MAGIC:
1781                 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1782                 break;
1783         case XFS_DIR2_LEAFN_MAGIC:
1784                 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
1785                 break;
1786         case XFS_DA_NODE_MAGIC:
1787                 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
1788                 break;
1789         }
1790
1791         /*
1792          * Link blocks in appropriate order.
1793          */
1794         if (before) {
1795                 /*
1796                  * Link new block in before existing block.
1797                  */
1798                 trace_xfs_da_link_before(args);
1799                 new_info->forw = cpu_to_be32(old_blk->blkno);
1800                 new_info->back = old_info->back;
1801                 if (old_info->back) {
1802                         error = xfs_da3_node_read(args->trans, dp,
1803                                                 be32_to_cpu(old_info->back),
1804                                                 -1, &bp, args->whichfork);
1805                         if (error)
1806                                 return error;
1807                         ASSERT(bp != NULL);
1808                         tmp_info = bp->b_addr;
1809                         ASSERT(tmp_info->magic == old_info->magic);
1810                         ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1811                         tmp_info->forw = cpu_to_be32(new_blk->blkno);
1812                         xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1813                 }
1814                 old_info->back = cpu_to_be32(new_blk->blkno);
1815         } else {
1816                 /*
1817                  * Link new block in after existing block.
1818                  */
1819                 trace_xfs_da_link_after(args);
1820                 new_info->forw = old_info->forw;
1821                 new_info->back = cpu_to_be32(old_blk->blkno);
1822                 if (old_info->forw) {
1823                         error = xfs_da3_node_read(args->trans, dp,
1824                                                 be32_to_cpu(old_info->forw),
1825                                                 -1, &bp, args->whichfork);
1826                         if (error)
1827                                 return error;
1828                         ASSERT(bp != NULL);
1829                         tmp_info = bp->b_addr;
1830                         ASSERT(tmp_info->magic == old_info->magic);
1831                         ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1832                         tmp_info->back = cpu_to_be32(new_blk->blkno);
1833                         xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
1834                 }
1835                 old_info->forw = cpu_to_be32(new_blk->blkno);
1836         }
1837
1838         xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1839         xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
1840         return 0;
1841 }
1842
1843 /*
1844  * Unlink a block from a doubly linked list of blocks.
1845  */
1846 STATIC int                                              /* error */
1847 xfs_da3_blk_unlink(
1848         struct xfs_da_state     *state,
1849         struct xfs_da_state_blk *drop_blk,
1850         struct xfs_da_state_blk *save_blk)
1851 {
1852         struct xfs_da_blkinfo   *drop_info;
1853         struct xfs_da_blkinfo   *save_info;
1854         struct xfs_da_blkinfo   *tmp_info;
1855         struct xfs_da_args      *args;
1856         struct xfs_buf          *bp;
1857         int                     error;
1858
1859         /*
1860          * Set up environment.
1861          */
1862         args = state->args;
1863         ASSERT(args != NULL);
1864         save_info = save_blk->bp->b_addr;
1865         drop_info = drop_blk->bp->b_addr;
1866         ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
1867                save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1868                save_blk->magic == XFS_ATTR_LEAF_MAGIC);
1869         ASSERT(save_blk->magic == drop_blk->magic);
1870         ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1871                (be32_to_cpu(save_info->back) == drop_blk->blkno));
1872         ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1873                (be32_to_cpu(drop_info->back) == save_blk->blkno));
1874
1875         /*
1876          * Unlink the leaf block from the doubly linked chain of leaves.
1877          */
1878         if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
1879                 trace_xfs_da_unlink_back(args);
1880                 save_info->back = drop_info->back;
1881                 if (drop_info->back) {
1882                         error = xfs_da3_node_read(args->trans, args->dp,
1883                                                 be32_to_cpu(drop_info->back),
1884                                                 -1, &bp, args->whichfork);
1885                         if (error)
1886                                 return error;
1887                         ASSERT(bp != NULL);
1888                         tmp_info = bp->b_addr;
1889                         ASSERT(tmp_info->magic == save_info->magic);
1890                         ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1891                         tmp_info->forw = cpu_to_be32(save_blk->blkno);
1892                         xfs_trans_log_buf(args->trans, bp, 0,
1893                                                     sizeof(*tmp_info) - 1);
1894                 }
1895         } else {
1896                 trace_xfs_da_unlink_forward(args);
1897                 save_info->forw = drop_info->forw;
1898                 if (drop_info->forw) {
1899                         error = xfs_da3_node_read(args->trans, args->dp,
1900                                                 be32_to_cpu(drop_info->forw),
1901                                                 -1, &bp, args->whichfork);
1902                         if (error)
1903                                 return error;
1904                         ASSERT(bp != NULL);
1905                         tmp_info = bp->b_addr;
1906                         ASSERT(tmp_info->magic == save_info->magic);
1907                         ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1908                         tmp_info->back = cpu_to_be32(save_blk->blkno);
1909                         xfs_trans_log_buf(args->trans, bp, 0,
1910                                                     sizeof(*tmp_info) - 1);
1911                 }
1912         }
1913
1914         xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
1915         return 0;
1916 }
1917
1918 /*
1919  * Move a path "forward" or "!forward" one block at the current level.
1920  *
1921  * This routine will adjust a "path" to point to the next block
1922  * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1923  * Btree, including updating pointers to the intermediate nodes between
1924  * the new bottom and the root.
1925  */
1926 int                                                     /* error */
1927 xfs_da3_path_shift(
1928         struct xfs_da_state     *state,
1929         struct xfs_da_state_path *path,
1930         int                     forward,
1931         int                     release,
1932         int                     *result)
1933 {
1934         struct xfs_da_state_blk *blk;
1935         struct xfs_da_blkinfo   *info;
1936         struct xfs_da_args      *args;
1937         struct xfs_da_node_entry *btree;
1938         struct xfs_da3_icnode_hdr nodehdr;
1939         struct xfs_buf          *bp;
1940         xfs_dablk_t             blkno = 0;
1941         int                     level;
1942         int                     error;
1943         struct xfs_inode        *dp = state->args->dp;
1944
1945         trace_xfs_da_path_shift(state->args);
1946
1947         /*
1948          * Roll up the Btree looking for the first block where our
1949          * current index is not at the edge of the block.  Note that
1950          * we skip the bottom layer because we want the sibling block.
1951          */
1952         args = state->args;
1953         ASSERT(args != NULL);
1954         ASSERT(path != NULL);
1955         ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1956         level = (path->active-1) - 1;   /* skip bottom layer in path */
1957         for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1958                 xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
1959                                            blk->bp->b_addr);
1960
1961                 if (forward && (blk->index < nodehdr.count - 1)) {
1962                         blk->index++;
1963                         blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
1964                         break;
1965                 } else if (!forward && (blk->index > 0)) {
1966                         blk->index--;
1967                         blkno = be32_to_cpu(nodehdr.btree[blk->index].before);
1968                         break;
1969                 }
1970         }
1971         if (level < 0) {
1972                 *result = -ENOENT;      /* we're out of our tree */
1973                 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1974                 return 0;
1975         }
1976
1977         /*
1978          * Roll down the edge of the subtree until we reach the
1979          * same depth we were at originally.
1980          */
1981         for (blk++, level++; level < path->active; blk++, level++) {
1982                 /*
1983                  * Read the next child block into a local buffer.
1984                  */
1985                 error = xfs_da3_node_read(args->trans, dp, blkno, -1, &bp,
1986                                           args->whichfork);
1987                 if (error)
1988                         return error;
1989
1990                 /*
1991                  * Release the old block (if it's dirty, the trans doesn't
1992                  * actually let go) and swap the local buffer into the path
1993                  * structure. This ensures failure of the above read doesn't set
1994                  * a NULL buffer in an active slot in the path.
1995                  */
1996                 if (release)
1997                         xfs_trans_brelse(args->trans, blk->bp);
1998                 blk->blkno = blkno;
1999                 blk->bp = bp;
2000
2001                 info = blk->bp->b_addr;
2002                 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
2003                        info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
2004                        info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2005                        info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
2006                        info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
2007                        info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
2008
2009
2010                 /*
2011                  * Note: we flatten the magic number to a single type so we
2012                  * don't have to compare against crc/non-crc types elsewhere.
2013                  */
2014                 switch (be16_to_cpu(info->magic)) {
2015                 case XFS_DA_NODE_MAGIC:
2016                 case XFS_DA3_NODE_MAGIC:
2017                         blk->magic = XFS_DA_NODE_MAGIC;
2018                         xfs_da3_node_hdr_from_disk(dp->i_mount, &nodehdr,
2019                                                    bp->b_addr);
2020                         btree = nodehdr.btree;
2021                         blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
2022                         if (forward)
2023                                 blk->index = 0;
2024                         else
2025                                 blk->index = nodehdr.count - 1;
2026                         blkno = be32_to_cpu(btree[blk->index].before);
2027                         break;
2028                 case XFS_ATTR_LEAF_MAGIC:
2029                 case XFS_ATTR3_LEAF_MAGIC:
2030                         blk->magic = XFS_ATTR_LEAF_MAGIC;
2031                         ASSERT(level == path->active-1);
2032                         blk->index = 0;
2033                         blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
2034                         break;
2035                 case XFS_DIR2_LEAFN_MAGIC:
2036                 case XFS_DIR3_LEAFN_MAGIC:
2037                         blk->magic = XFS_DIR2_LEAFN_MAGIC;
2038                         ASSERT(level == path->active-1);
2039                         blk->index = 0;
2040                         blk->hashval = xfs_dir2_leaf_lasthash(args->dp,
2041                                                               blk->bp, NULL);
2042                         break;
2043                 default:
2044                         ASSERT(0);
2045                         break;
2046                 }
2047         }
2048         *result = 0;
2049         return 0;
2050 }
2051
2052
2053 /*========================================================================
2054  * Utility routines.
2055  *========================================================================*/
2056
2057 /*
2058  * Implement a simple hash on a character string.
2059  * Rotate the hash value by 7 bits, then XOR each character in.
2060  * This is implemented with some source-level loop unrolling.
2061  */
2062 xfs_dahash_t
2063 xfs_da_hashname(const uint8_t *name, int namelen)
2064 {
2065         xfs_dahash_t hash;
2066
2067         /*
2068          * Do four characters at a time as long as we can.
2069          */
2070         for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2071                 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2072                        (name[3] << 0) ^ rol32(hash, 7 * 4);
2073
2074         /*
2075          * Now do the rest of the characters.
2076          */
2077         switch (namelen) {
2078         case 3:
2079                 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2080                        rol32(hash, 7 * 3);
2081         case 2:
2082                 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2083         case 1:
2084                 return (name[0] << 0) ^ rol32(hash, 7 * 1);
2085         default: /* case 0: */
2086                 return hash;
2087         }
2088 }
2089
2090 enum xfs_dacmp
2091 xfs_da_compname(
2092         struct xfs_da_args *args,
2093         const unsigned char *name,
2094         int             len)
2095 {
2096         return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2097                                         XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2098 }
2099
2100 static xfs_dahash_t
2101 xfs_default_hashname(
2102         struct xfs_name *name)
2103 {
2104         return xfs_da_hashname(name->name, name->len);
2105 }
2106
2107 const struct xfs_nameops xfs_default_nameops = {
2108         .hashname       = xfs_default_hashname,
2109         .compname       = xfs_da_compname
2110 };
2111
2112 int
2113 xfs_da_grow_inode_int(
2114         struct xfs_da_args      *args,
2115         xfs_fileoff_t           *bno,
2116         int                     count)
2117 {
2118         struct xfs_trans        *tp = args->trans;
2119         struct xfs_inode        *dp = args->dp;
2120         int                     w = args->whichfork;
2121         xfs_rfsblock_t          nblks = dp->i_d.di_nblocks;
2122         struct xfs_bmbt_irec    map, *mapp;
2123         int                     nmap, error, got, i, mapi;
2124
2125         /*
2126          * Find a spot in the file space to put the new block.
2127          */
2128         error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2129         if (error)
2130                 return error;
2131
2132         /*
2133          * Try mapping it in one filesystem block.
2134          */
2135         nmap = 1;
2136         error = xfs_bmapi_write(tp, dp, *bno, count,
2137                         xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
2138                         args->total, &map, &nmap);
2139         if (error)
2140                 return error;
2141
2142         ASSERT(nmap <= 1);
2143         if (nmap == 1) {
2144                 mapp = &map;
2145                 mapi = 1;
2146         } else if (nmap == 0 && count > 1) {
2147                 xfs_fileoff_t           b;
2148                 int                     c;
2149
2150                 /*
2151                  * If we didn't get it and the block might work if fragmented,
2152                  * try without the CONTIG flag.  Loop until we get it all.
2153                  */
2154                 mapp = kmem_alloc(sizeof(*mapp) * count, 0);
2155                 for (b = *bno, mapi = 0; b < *bno + count; ) {
2156                         nmap = min(XFS_BMAP_MAX_NMAP, count);
2157                         c = (int)(*bno + count - b);
2158                         error = xfs_bmapi_write(tp, dp, b, c,
2159                                         xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2160                                         args->total, &mapp[mapi], &nmap);
2161                         if (error)
2162                                 goto out_free_map;
2163                         if (nmap < 1)
2164                                 break;
2165                         mapi += nmap;
2166                         b = mapp[mapi - 1].br_startoff +
2167                             mapp[mapi - 1].br_blockcount;
2168                 }
2169         } else {
2170                 mapi = 0;
2171                 mapp = NULL;
2172         }
2173
2174         /*
2175          * Count the blocks we got, make sure it matches the total.
2176          */
2177         for (i = 0, got = 0; i < mapi; i++)
2178                 got += mapp[i].br_blockcount;
2179         if (got != count || mapp[0].br_startoff != *bno ||
2180             mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
2181             *bno + count) {
2182                 error = -ENOSPC;
2183                 goto out_free_map;
2184         }
2185
2186         /* account for newly allocated blocks in reserved blocks total */
2187         args->total -= dp->i_d.di_nblocks - nblks;
2188
2189 out_free_map:
2190         if (mapp != &map)
2191                 kmem_free(mapp);
2192         return error;
2193 }
2194
2195 /*
2196  * Add a block to the btree ahead of the file.
2197  * Return the new block number to the caller.
2198  */
2199 int
2200 xfs_da_grow_inode(
2201         struct xfs_da_args      *args,
2202         xfs_dablk_t             *new_blkno)
2203 {
2204         xfs_fileoff_t           bno;
2205         int                     error;
2206
2207         trace_xfs_da_grow_inode(args);
2208
2209         bno = args->geo->leafblk;
2210         error = xfs_da_grow_inode_int(args, &bno, args->geo->fsbcount);
2211         if (!error)
2212                 *new_blkno = (xfs_dablk_t)bno;
2213         return error;
2214 }
2215
2216 /*
2217  * Ick.  We need to always be able to remove a btree block, even
2218  * if there's no space reservation because the filesystem is full.
2219  * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2220  * It swaps the target block with the last block in the file.  The
2221  * last block in the file can always be removed since it can't cause
2222  * a bmap btree split to do that.
2223  */
2224 STATIC int
2225 xfs_da3_swap_lastblock(
2226         struct xfs_da_args      *args,
2227         xfs_dablk_t             *dead_blknop,
2228         struct xfs_buf          **dead_bufp)
2229 {
2230         struct xfs_da_blkinfo   *dead_info;
2231         struct xfs_da_blkinfo   *sib_info;
2232         struct xfs_da_intnode   *par_node;
2233         struct xfs_da_intnode   *dead_node;
2234         struct xfs_dir2_leaf    *dead_leaf2;
2235         struct xfs_da_node_entry *btree;
2236         struct xfs_da3_icnode_hdr par_hdr;
2237         struct xfs_inode        *dp;
2238         struct xfs_trans        *tp;
2239         struct xfs_mount        *mp;
2240         struct xfs_buf          *dead_buf;
2241         struct xfs_buf          *last_buf;
2242         struct xfs_buf          *sib_buf;
2243         struct xfs_buf          *par_buf;
2244         xfs_dahash_t            dead_hash;
2245         xfs_fileoff_t           lastoff;
2246         xfs_dablk_t             dead_blkno;
2247         xfs_dablk_t             last_blkno;
2248         xfs_dablk_t             sib_blkno;
2249         xfs_dablk_t             par_blkno;
2250         int                     error;
2251         int                     w;
2252         int                     entno;
2253         int                     level;
2254         int                     dead_level;
2255
2256         trace_xfs_da_swap_lastblock(args);
2257
2258         dead_buf = *dead_bufp;
2259         dead_blkno = *dead_blknop;
2260         tp = args->trans;
2261         dp = args->dp;
2262         w = args->whichfork;
2263         ASSERT(w == XFS_DATA_FORK);
2264         mp = dp->i_mount;
2265         lastoff = args->geo->freeblk;
2266         error = xfs_bmap_last_before(tp, dp, &lastoff, w);
2267         if (error)
2268                 return error;
2269         if (unlikely(lastoff == 0)) {
2270                 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2271                                  mp);
2272                 return -EFSCORRUPTED;
2273         }
2274         /*
2275          * Read the last block in the btree space.
2276          */
2277         last_blkno = (xfs_dablk_t)lastoff - args->geo->fsbcount;
2278         error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
2279         if (error)
2280                 return error;
2281         /*
2282          * Copy the last block into the dead buffer and log it.
2283          */
2284         memcpy(dead_buf->b_addr, last_buf->b_addr, args->geo->blksize);
2285         xfs_trans_log_buf(tp, dead_buf, 0, args->geo->blksize - 1);
2286         dead_info = dead_buf->b_addr;
2287         /*
2288          * Get values from the moved block.
2289          */
2290         if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2291             dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2292                 struct xfs_dir3_icleaf_hdr leafhdr;
2293                 struct xfs_dir2_leaf_entry *ents;
2294
2295                 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
2296                 dp->d_ops->leaf_hdr_from_disk(&leafhdr, dead_leaf2);
2297                 ents = dp->d_ops->leaf_ents_p(dead_leaf2);
2298                 dead_level = 0;
2299                 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
2300         } else {
2301                 struct xfs_da3_icnode_hdr deadhdr;
2302
2303                 dead_node = (xfs_da_intnode_t *)dead_info;
2304                 xfs_da3_node_hdr_from_disk(dp->i_mount, &deadhdr, dead_node);
2305                 btree = deadhdr.btree;
2306                 dead_level = deadhdr.level;
2307                 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
2308         }
2309         sib_buf = par_buf = NULL;
2310         /*
2311          * If the moved block has a left sibling, fix up the pointers.
2312          */
2313         if ((sib_blkno = be32_to_cpu(dead_info->back))) {
2314                 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2315                 if (error)
2316                         goto done;
2317                 sib_info = sib_buf->b_addr;
2318                 if (unlikely(
2319                     be32_to_cpu(sib_info->forw) != last_blkno ||
2320                     sib_info->magic != dead_info->magic)) {
2321                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2322                                          XFS_ERRLEVEL_LOW, mp);
2323                         error = -EFSCORRUPTED;
2324                         goto done;
2325                 }
2326                 sib_info->forw = cpu_to_be32(dead_blkno);
2327                 xfs_trans_log_buf(tp, sib_buf,
2328                         XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2329                                         sizeof(sib_info->forw)));
2330                 sib_buf = NULL;
2331         }
2332         /*
2333          * If the moved block has a right sibling, fix up the pointers.
2334          */
2335         if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
2336                 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
2337                 if (error)
2338                         goto done;
2339                 sib_info = sib_buf->b_addr;
2340                 if (unlikely(
2341                        be32_to_cpu(sib_info->back) != last_blkno ||
2342                        sib_info->magic != dead_info->magic)) {
2343                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2344                                          XFS_ERRLEVEL_LOW, mp);
2345                         error = -EFSCORRUPTED;
2346                         goto done;
2347                 }
2348                 sib_info->back = cpu_to_be32(dead_blkno);
2349                 xfs_trans_log_buf(tp, sib_buf,
2350                         XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2351                                         sizeof(sib_info->back)));
2352                 sib_buf = NULL;
2353         }
2354         par_blkno = args->geo->leafblk;
2355         level = -1;
2356         /*
2357          * Walk down the tree looking for the parent of the moved block.
2358          */
2359         for (;;) {
2360                 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2361                 if (error)
2362                         goto done;
2363                 par_node = par_buf->b_addr;
2364                 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2365                 if (level >= 0 && level != par_hdr.level + 1) {
2366                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2367                                          XFS_ERRLEVEL_LOW, mp);
2368                         error = -EFSCORRUPTED;
2369                         goto done;
2370                 }
2371                 level = par_hdr.level;
2372                 btree = par_hdr.btree;
2373                 for (entno = 0;
2374                      entno < par_hdr.count &&
2375                      be32_to_cpu(btree[entno].hashval) < dead_hash;
2376                      entno++)
2377                         continue;
2378                 if (entno == par_hdr.count) {
2379                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2380                                          XFS_ERRLEVEL_LOW, mp);
2381                         error = -EFSCORRUPTED;
2382                         goto done;
2383                 }
2384                 par_blkno = be32_to_cpu(btree[entno].before);
2385                 if (level == dead_level + 1)
2386                         break;
2387                 xfs_trans_brelse(tp, par_buf);
2388                 par_buf = NULL;
2389         }
2390         /*
2391          * We're in the right parent block.
2392          * Look for the right entry.
2393          */
2394         for (;;) {
2395                 for (;
2396                      entno < par_hdr.count &&
2397                      be32_to_cpu(btree[entno].before) != last_blkno;
2398                      entno++)
2399                         continue;
2400                 if (entno < par_hdr.count)
2401                         break;
2402                 par_blkno = par_hdr.forw;
2403                 xfs_trans_brelse(tp, par_buf);
2404                 par_buf = NULL;
2405                 if (unlikely(par_blkno == 0)) {
2406                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2407                                          XFS_ERRLEVEL_LOW, mp);
2408                         error = -EFSCORRUPTED;
2409                         goto done;
2410                 }
2411                 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
2412                 if (error)
2413                         goto done;
2414                 par_node = par_buf->b_addr;
2415                 xfs_da3_node_hdr_from_disk(dp->i_mount, &par_hdr, par_node);
2416                 if (par_hdr.level != level) {
2417                         XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2418                                          XFS_ERRLEVEL_LOW, mp);
2419                         error = -EFSCORRUPTED;
2420                         goto done;
2421                 }
2422                 btree = par_hdr.btree;
2423                 entno = 0;
2424         }
2425         /*
2426          * Update the parent entry pointing to the moved block.
2427          */
2428         btree[entno].before = cpu_to_be32(dead_blkno);
2429         xfs_trans_log_buf(tp, par_buf,
2430                 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2431                                 sizeof(btree[entno].before)));
2432         *dead_blknop = last_blkno;
2433         *dead_bufp = last_buf;
2434         return 0;
2435 done:
2436         if (par_buf)
2437                 xfs_trans_brelse(tp, par_buf);
2438         if (sib_buf)
2439                 xfs_trans_brelse(tp, sib_buf);
2440         xfs_trans_brelse(tp, last_buf);
2441         return error;
2442 }
2443
2444 /*
2445  * Remove a btree block from a directory or attribute.
2446  */
2447 int
2448 xfs_da_shrink_inode(
2449         struct xfs_da_args      *args,
2450         xfs_dablk_t             dead_blkno,
2451         struct xfs_buf          *dead_buf)
2452 {
2453         struct xfs_inode        *dp;
2454         int                     done, error, w, count;
2455         struct xfs_trans        *tp;
2456
2457         trace_xfs_da_shrink_inode(args);
2458
2459         dp = args->dp;
2460         w = args->whichfork;
2461         tp = args->trans;
2462         count = args->geo->fsbcount;
2463         for (;;) {
2464                 /*
2465                  * Remove extents.  If we get ENOSPC for a dir we have to move
2466                  * the last block to the place we want to kill.
2467                  */
2468                 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2469                                     xfs_bmapi_aflag(w), 0, &done);
2470                 if (error == -ENOSPC) {
2471                         if (w != XFS_DATA_FORK)
2472                                 break;
2473                         error = xfs_da3_swap_lastblock(args, &dead_blkno,
2474                                                       &dead_buf);
2475                         if (error)
2476                                 break;
2477                 } else {
2478                         break;
2479                 }
2480         }
2481         xfs_trans_binval(tp, dead_buf);
2482         return error;
2483 }
2484
2485 /*
2486  * See if the mapping(s) for this btree block are valid, i.e.
2487  * don't contain holes, are logically contiguous, and cover the whole range.
2488  */
2489 STATIC int
2490 xfs_da_map_covers_blocks(
2491         int             nmap,
2492         xfs_bmbt_irec_t *mapp,
2493         xfs_dablk_t     bno,
2494         int             count)
2495 {
2496         int             i;
2497         xfs_fileoff_t   off;
2498
2499         for (i = 0, off = bno; i < nmap; i++) {
2500                 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2501                     mapp[i].br_startblock == DELAYSTARTBLOCK) {
2502                         return 0;
2503                 }
2504                 if (off != mapp[i].br_startoff) {
2505                         return 0;
2506                 }
2507                 off += mapp[i].br_blockcount;
2508         }
2509         return off == bno + count;
2510 }
2511
2512 /*
2513  * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2514  *
2515  * For the single map case, it is assumed that the caller has provided a pointer
2516  * to a valid xfs_buf_map.  For the multiple map case, this function will
2517  * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2518  * map pointer with the allocated map.
2519  */
2520 static int
2521 xfs_buf_map_from_irec(
2522         struct xfs_mount        *mp,
2523         struct xfs_buf_map      **mapp,
2524         int                     *nmaps,
2525         struct xfs_bmbt_irec    *irecs,
2526         int                     nirecs)
2527 {
2528         struct xfs_buf_map      *map;
2529         int                     i;
2530
2531         ASSERT(*nmaps == 1);
2532         ASSERT(nirecs >= 1);
2533
2534         if (nirecs > 1) {
2535                 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
2536                                   KM_NOFS);
2537                 if (!map)
2538                         return -ENOMEM;
2539                 *mapp = map;
2540         }
2541
2542         *nmaps = nirecs;
2543         map = *mapp;
2544         for (i = 0; i < *nmaps; i++) {
2545                 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2546                        irecs[i].br_startblock != HOLESTARTBLOCK);
2547                 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2548                 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2549         }
2550         return 0;
2551 }
2552
2553 /*
2554  * Map the block we are given ready for reading. There are three possible return
2555  * values:
2556  *      -1 - will be returned if we land in a hole and mappedbno == -2 so the
2557  *           caller knows not to execute a subsequent read.
2558  *       0 - if we mapped the block successfully
2559  *      >0 - positive error number if there was an error.
2560  */
2561 static int
2562 xfs_dabuf_map(
2563         struct xfs_inode        *dp,
2564         xfs_dablk_t             bno,
2565         xfs_daddr_t             mappedbno,
2566         int                     whichfork,
2567         struct xfs_buf_map      **map,
2568         int                     *nmaps)
2569 {
2570         struct xfs_mount        *mp = dp->i_mount;
2571         int                     nfsb;
2572         int                     error = 0;
2573         struct xfs_bmbt_irec    irec;
2574         struct xfs_bmbt_irec    *irecs = &irec;
2575         int                     nirecs;
2576
2577         ASSERT(map && *map);
2578         ASSERT(*nmaps == 1);
2579
2580         if (whichfork == XFS_DATA_FORK)
2581                 nfsb = mp->m_dir_geo->fsbcount;
2582         else
2583                 nfsb = mp->m_attr_geo->fsbcount;
2584
2585         /*
2586          * Caller doesn't have a mapping.  -2 means don't complain
2587          * if we land in a hole.
2588          */
2589         if (mappedbno == -1 || mappedbno == -2) {
2590                 /*
2591                  * Optimize the one-block case.
2592                  */
2593                 if (nfsb != 1)
2594                         irecs = kmem_zalloc(sizeof(irec) * nfsb,
2595                                             KM_NOFS);
2596
2597                 nirecs = nfsb;
2598                 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2599                                        &nirecs, xfs_bmapi_aflag(whichfork));
2600                 if (error)
2601                         goto out;
2602         } else {
2603                 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2604                 irecs->br_startoff = (xfs_fileoff_t)bno;
2605                 irecs->br_blockcount = nfsb;
2606                 irecs->br_state = 0;
2607                 nirecs = 1;
2608         }
2609
2610         if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2611                 /* Caller ok with no mapping. */
2612                 if (mappedbno == -2) {
2613                         error = -1;
2614                         goto out;
2615                 }
2616
2617                 /* Caller expected a mapping, so abort. */
2618                 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
2619                         int i;
2620
2621                         xfs_alert(mp, "%s: bno %lld dir: inode %lld", __func__,
2622                                         (long long)bno, (long long)dp->i_ino);
2623                         for (i = 0; i < *nmaps; i++) {
2624                                 xfs_alert(mp,
2625 "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
2626                                         i,
2627                                         (long long)irecs[i].br_startoff,
2628                                         (long long)irecs[i].br_startblock,
2629                                         (long long)irecs[i].br_blockcount,
2630                                         irecs[i].br_state);
2631                         }
2632                 }
2633                 XFS_ERROR_REPORT("xfs_da_do_buf(1)", XFS_ERRLEVEL_LOW, mp);
2634                 error = -EFSCORRUPTED;
2635                 goto out;
2636         }
2637         error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2638 out:
2639         if (irecs != &irec)
2640                 kmem_free(irecs);
2641         return error;
2642 }
2643
2644 /*
2645  * Get a buffer for the dir/attr block.
2646  */
2647 int
2648 xfs_da_get_buf(
2649         struct xfs_trans        *trans,
2650         struct xfs_inode        *dp,
2651         xfs_dablk_t             bno,
2652         xfs_daddr_t             mappedbno,
2653         struct xfs_buf          **bpp,
2654         int                     whichfork)
2655 {
2656         struct xfs_buf          *bp;
2657         struct xfs_buf_map      map;
2658         struct xfs_buf_map      *mapp;
2659         int                     nmap;
2660         int                     error;
2661
2662         *bpp = NULL;
2663         mapp = &map;
2664         nmap = 1;
2665         error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2666                                 &mapp, &nmap);
2667         if (error) {
2668                 /* mapping a hole is not an error, but we don't continue */
2669                 if (error == -1)
2670                         error = 0;
2671                 goto out_free;
2672         }
2673
2674         bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2675                                     mapp, nmap, 0);
2676         error = bp ? bp->b_error : -EIO;
2677         if (error) {
2678                 if (bp)
2679                         xfs_trans_brelse(trans, bp);
2680                 goto out_free;
2681         }
2682
2683         *bpp = bp;
2684
2685 out_free:
2686         if (mapp != &map)
2687                 kmem_free(mapp);
2688
2689         return error;
2690 }
2691
2692 /*
2693  * Get a buffer for the dir/attr block, fill in the contents.
2694  */
2695 int
2696 xfs_da_read_buf(
2697         struct xfs_trans        *trans,
2698         struct xfs_inode        *dp,
2699         xfs_dablk_t             bno,
2700         xfs_daddr_t             mappedbno,
2701         struct xfs_buf          **bpp,
2702         int                     whichfork,
2703         const struct xfs_buf_ops *ops)
2704 {
2705         struct xfs_buf          *bp;
2706         struct xfs_buf_map      map;
2707         struct xfs_buf_map      *mapp;
2708         int                     nmap;
2709         int                     error;
2710
2711         *bpp = NULL;
2712         mapp = &map;
2713         nmap = 1;
2714         error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2715                                 &mapp, &nmap);
2716         if (error) {
2717                 /* mapping a hole is not an error, but we don't continue */
2718                 if (error == -1)
2719                         error = 0;
2720                 goto out_free;
2721         }
2722
2723         error = xfs_trans_read_buf_map(dp->i_mount, trans,
2724                                         dp->i_mount->m_ddev_targp,
2725                                         mapp, nmap, 0, &bp, ops);
2726         if (error)
2727                 goto out_free;
2728
2729         if (whichfork == XFS_ATTR_FORK)
2730                 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
2731         else
2732                 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2733         *bpp = bp;
2734 out_free:
2735         if (mapp != &map)
2736                 kmem_free(mapp);
2737
2738         return error;
2739 }
2740
2741 /*
2742  * Readahead the dir/attr block.
2743  */
2744 int
2745 xfs_da_reada_buf(
2746         struct xfs_inode        *dp,
2747         xfs_dablk_t             bno,
2748         xfs_daddr_t             mappedbno,
2749         int                     whichfork,
2750         const struct xfs_buf_ops *ops)
2751 {
2752         struct xfs_buf_map      map;
2753         struct xfs_buf_map      *mapp;
2754         int                     nmap;
2755         int                     error;
2756
2757         mapp = &map;
2758         nmap = 1;
2759         error = xfs_dabuf_map(dp, bno, mappedbno, whichfork,
2760                                 &mapp, &nmap);
2761         if (error) {
2762                 /* mapping a hole is not an error, but we don't continue */
2763                 if (error == -1)
2764                         error = 0;
2765                 goto out_free;
2766         }
2767
2768         mappedbno = mapp[0].bm_bn;
2769         xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
2770
2771 out_free:
2772         if (mapp != &map)
2773                 kmem_free(mapp);
2774
2775         return error;
2776 }