blob: c62e7e6ff50eb09ba4c68a496ff764a58554b638 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020029#include "xfs_dir2.h"
30#include "xfs_dir2_format.h"
31#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
35#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000040#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * xfs_da_btree.c
44 *
45 * Routines to implement directories as Btrees of hashed names.
46 */
47
48/*========================================================================
49 * Function prototypes for the kernel.
50 *========================================================================*/
51
52/*
53 * Routines used for growing the Btree.
54 */
55STATIC int xfs_da_root_split(xfs_da_state_t *state,
56 xfs_da_state_blk_t *existing_root,
57 xfs_da_state_blk_t *new_child);
58STATIC int xfs_da_node_split(xfs_da_state_t *state,
59 xfs_da_state_blk_t *existing_blk,
60 xfs_da_state_blk_t *split_blk,
61 xfs_da_state_blk_t *blk_to_add,
62 int treelevel,
63 int *result);
64STATIC void xfs_da_node_rebalance(xfs_da_state_t *state,
65 xfs_da_state_blk_t *node_blk_1,
66 xfs_da_state_blk_t *node_blk_2);
67STATIC void xfs_da_node_add(xfs_da_state_t *state,
68 xfs_da_state_blk_t *old_node_blk,
69 xfs_da_state_blk_t *new_node_blk);
70
71/*
72 * Routines used for shrinking the Btree.
73 */
74STATIC int xfs_da_root_join(xfs_da_state_t *state,
75 xfs_da_state_blk_t *root_blk);
76STATIC int xfs_da_node_toosmall(xfs_da_state_t *state, int *retval);
77STATIC void xfs_da_node_remove(xfs_da_state_t *state,
78 xfs_da_state_blk_t *drop_blk);
79STATIC void xfs_da_node_unbalance(xfs_da_state_t *state,
80 xfs_da_state_blk_t *src_node_blk,
81 xfs_da_state_blk_t *dst_node_blk);
82
83/*
84 * Utility routines.
85 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100086STATIC uint xfs_da_node_lasthash(struct xfs_buf *bp, int *count);
87STATIC int xfs_da_node_order(struct xfs_buf *node1_bp,
88 struct xfs_buf *node2_bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100089STATIC int xfs_da_blk_unlink(xfs_da_state_t *state,
90 xfs_da_state_blk_t *drop_blk,
91 xfs_da_state_blk_t *save_blk);
92STATIC void xfs_da_state_kill_altpath(xfs_da_state_t *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*========================================================================
95 * Routines used for growing the Btree.
96 *========================================================================*/
97
98/*
99 * Create the initial contents of an intermediate node.
100 */
101int
102xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000103 struct xfs_buf **bpp, int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 xfs_da_intnode_t *node;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000106 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int error;
108 xfs_trans_t *tp;
109
Dave Chinner5a5881c2012-03-22 05:15:13 +0000110 trace_xfs_da_node_create(args);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 tp = args->trans;
113 error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
114 if (error)
115 return(error);
116 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000117 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 node->hdr.info.forw = 0;
119 node->hdr.info.back = 0;
Nathan Scott89da0542006-03-17 17:28:40 +1100120 node->hdr.info.magic = cpu_to_be16(XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 node->hdr.info.pad = 0;
122 node->hdr.count = 0;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100123 node->hdr.level = cpu_to_be16(level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Dave Chinner1d9025e2012-06-22 18:50:14 +1000125 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
127
128 *bpp = bp;
129 return(0);
130}
131
132/*
133 * Split a leaf node, rebalance, then possibly split
134 * intermediate nodes, rebalance, etc.
135 */
136int /* error */
137xfs_da_split(xfs_da_state_t *state)
138{
139 xfs_da_state_blk_t *oldblk, *newblk, *addblk;
140 xfs_da_intnode_t *node;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000141 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int max, action, error, i;
143
Dave Chinner5a5881c2012-03-22 05:15:13 +0000144 trace_xfs_da_split(state->args);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /*
147 * Walk back up the tree splitting/inserting/adjusting as necessary.
148 * If we need to insert and there isn't room, split the node, then
149 * decide which fragment to insert the new block from below into.
150 * Note that we may split the root this way, but we need more fixup.
151 */
152 max = state->path.active - 1;
153 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
154 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000155 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 addblk = &state->path.blk[max]; /* initial dummy value */
158 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
159 oldblk = &state->path.blk[i];
160 newblk = &state->altpath.blk[i];
161
162 /*
163 * If a leaf node then
164 * Allocate a new leaf node, then rebalance across them.
165 * else if an intermediate node then
166 * We split on the last layer, must we split the node?
167 */
168 switch (oldblk->magic) {
169 case XFS_ATTR_LEAF_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 error = xfs_attr_leaf_split(state, oldblk, newblk);
171 if ((error != 0) && (error != ENOSPC)) {
172 return(error); /* GROT: attr is inconsistent */
173 }
174 if (!error) {
175 addblk = newblk;
176 break;
177 }
178 /*
179 * Entry wouldn't fit, split the leaf again.
180 */
181 state->extravalid = 1;
182 if (state->inleaf) {
183 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000184 trace_xfs_attr_leaf_split_before(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 error = xfs_attr_leaf_split(state, oldblk,
186 &state->extrablk);
187 } else {
188 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000189 trace_xfs_attr_leaf_split_after(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 error = xfs_attr_leaf_split(state, newblk,
191 &state->extrablk);
192 }
193 if (error)
194 return(error); /* GROT: attr inconsistent */
195 addblk = newblk;
196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 error = xfs_dir2_leafn_split(state, oldblk, newblk);
199 if (error)
200 return error;
201 addblk = newblk;
202 break;
203 case XFS_DA_NODE_MAGIC:
204 error = xfs_da_node_split(state, oldblk, newblk, addblk,
205 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 addblk->bp = NULL;
207 if (error)
208 return(error); /* GROT: dir is inconsistent */
209 /*
210 * Record the newly split block for the next time thru?
211 */
212 if (action)
213 addblk = newblk;
214 else
215 addblk = NULL;
216 break;
217 }
218
219 /*
220 * Update the btree to show the new hashval for this child.
221 */
222 xfs_da_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 if (!addblk)
225 return(0);
226
227 /*
228 * Split the root node.
229 */
230 ASSERT(state->path.active == 0);
231 oldblk = &state->path.blk[0];
232 error = xfs_da_root_split(state, oldblk, addblk);
233 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 addblk->bp = NULL;
235 return(error); /* GROT: dir is inconsistent */
236 }
237
238 /*
239 * Update pointers to the node which used to be block 0 and
240 * just got bumped because of the addition of a new root node.
241 * There might be three blocks involved if a double split occurred,
242 * and the original block 0 could be at any position in the list.
243 */
244
Dave Chinner1d9025e2012-06-22 18:50:14 +1000245 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (node->hdr.info.forw) {
Nathan Scott89da0542006-03-17 17:28:40 +1100247 if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 bp = addblk->bp;
249 } else {
250 ASSERT(state->extravalid);
251 bp = state->extrablk.bp;
252 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000253 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100254 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000255 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 XFS_DA_LOGRANGE(node, &node->hdr.info,
257 sizeof(node->hdr.info)));
258 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000259 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100260 if (node->hdr.info.back) {
261 if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 bp = addblk->bp;
263 } else {
264 ASSERT(state->extravalid);
265 bp = state->extrablk.bp;
266 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000267 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100268 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000269 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 XFS_DA_LOGRANGE(node, &node->hdr.info,
271 sizeof(node->hdr.info)));
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 addblk->bp = NULL;
274 return(0);
275}
276
277/*
278 * Split the root. We have to create a new root and point to the two
279 * parts (the split old root) that we just created. Copy block zero to
280 * the EOF, extending the inode in process.
281 */
282STATIC int /* error */
283xfs_da_root_split(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
284 xfs_da_state_blk_t *blk2)
285{
286 xfs_da_intnode_t *node, *oldroot;
287 xfs_da_args_t *args;
288 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000289 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int error, size;
291 xfs_inode_t *dp;
292 xfs_trans_t *tp;
293 xfs_mount_t *mp;
294 xfs_dir2_leaf_t *leaf;
295
Dave Chinner5a5881c2012-03-22 05:15:13 +0000296 trace_xfs_da_root_split(state->args);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /*
299 * Copy the existing (incorrect) block from the root node position
300 * to a free space somewhere.
301 */
302 args = state->args;
303 ASSERT(args != NULL);
304 error = xfs_da_grow_inode(args, &blkno);
305 if (error)
306 return(error);
307 dp = args->dp;
308 tp = args->trans;
309 mp = state->mp;
310 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
311 if (error)
312 return(error);
313 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000314 node = bp->b_addr;
315 oldroot = blk1->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200316 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Nathan Scottfac80cce2006-03-17 17:29:56 +1100317 size = (int)((char *)&oldroot->btree[be16_to_cpu(oldroot->hdr.count)] -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 (char *)oldroot);
319 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200320 ASSERT(oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 leaf = (xfs_dir2_leaf_t *)oldroot;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100322 size = (int)((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (char *)leaf);
324 }
325 memcpy(node, oldroot, size);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000326 xfs_trans_log_buf(tp, bp, 0, size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 blk1->bp = bp;
328 blk1->blkno = blkno;
329
330 /*
331 * Set up the new root node.
332 */
333 error = xfs_da_node_create(args,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000334 (args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,
Nathan Scottfac80cce2006-03-17 17:29:56 +1100335 be16_to_cpu(node->hdr.level) + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (error)
337 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000338 node = bp->b_addr;
Nathan Scott403432d2006-03-17 17:29:46 +1100339 node->btree[0].hashval = cpu_to_be32(blk1->hashval);
340 node->btree[0].before = cpu_to_be32(blk1->blkno);
341 node->btree[1].hashval = cpu_to_be32(blk2->hashval);
342 node->btree[1].before = cpu_to_be32(blk2->blkno);
Nathan Scottfac80cce2006-03-17 17:29:56 +1100343 node->hdr.count = cpu_to_be16(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#ifdef DEBUG
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200346 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 ASSERT(blk1->blkno >= mp->m_dirleafblk &&
348 blk1->blkno < mp->m_dirfreeblk);
349 ASSERT(blk2->blkno >= mp->m_dirleafblk &&
350 blk2->blkno < mp->m_dirfreeblk);
351 }
352#endif
353
354 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000355 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 XFS_DA_LOGRANGE(node, node->btree,
357 sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return(0);
360}
361
362/*
363 * Split the node, rebalance, then add the new entry.
364 */
365STATIC int /* error */
366xfs_da_node_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
367 xfs_da_state_blk_t *newblk,
368 xfs_da_state_blk_t *addblk,
369 int treelevel, int *result)
370{
371 xfs_da_intnode_t *node;
372 xfs_dablk_t blkno;
373 int newcount, error;
374 int useextra;
375
Dave Chinner5a5881c2012-03-22 05:15:13 +0000376 trace_xfs_da_node_split(state->args);
377
Dave Chinner1d9025e2012-06-22 18:50:14 +1000378 node = oldblk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200379 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000382 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000384 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 newcount = 1 + useextra;
386 /*
387 * Do we have to split the node?
388 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100389 if ((be16_to_cpu(node->hdr.count) + newcount) > state->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Allocate a new node, add to the doubly linked chain of
392 * nodes, then move some of our excess entries into it.
393 */
394 error = xfs_da_grow_inode(state->args, &blkno);
395 if (error)
396 return(error); /* GROT: dir is inconsistent */
397
398 error = xfs_da_node_create(state->args, blkno, treelevel,
399 &newblk->bp, state->args->whichfork);
400 if (error)
401 return(error); /* GROT: dir is inconsistent */
402 newblk->blkno = blkno;
403 newblk->magic = XFS_DA_NODE_MAGIC;
404 xfs_da_node_rebalance(state, oldblk, newblk);
405 error = xfs_da_blk_link(state, oldblk, newblk);
406 if (error)
407 return(error);
408 *result = 1;
409 } else {
410 *result = 0;
411 }
412
413 /*
414 * Insert the new entry(s) into the correct block
415 * (updating last hashval in the process).
416 *
417 * xfs_da_node_add() inserts BEFORE the given index,
418 * and as a result of using node_lookup_int() we always
419 * point to a valid entry (not after one), but a split
420 * operation always results in a new block whose hashvals
421 * FOLLOW the current block.
422 *
423 * If we had double-split op below us, then add the extra block too.
424 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000425 node = oldblk->bp->b_addr;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100426 if (oldblk->index <= be16_to_cpu(node->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 oldblk->index++;
428 xfs_da_node_add(state, oldblk, addblk);
429 if (useextra) {
430 if (state->extraafter)
431 oldblk->index++;
432 xfs_da_node_add(state, oldblk, &state->extrablk);
433 state->extravalid = 0;
434 }
435 } else {
436 newblk->index++;
437 xfs_da_node_add(state, newblk, addblk);
438 if (useextra) {
439 if (state->extraafter)
440 newblk->index++;
441 xfs_da_node_add(state, newblk, &state->extrablk);
442 state->extravalid = 0;
443 }
444 }
445
446 return(0);
447}
448
449/*
450 * Balance the btree elements between two intermediate nodes,
451 * usually one full and one empty.
452 *
453 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
454 */
455STATIC void
456xfs_da_node_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
457 xfs_da_state_blk_t *blk2)
458{
459 xfs_da_intnode_t *node1, *node2, *tmpnode;
460 xfs_da_node_entry_t *btree_s, *btree_d;
461 int count, tmp;
462 xfs_trans_t *tp;
463
Dave Chinner5a5881c2012-03-22 05:15:13 +0000464 trace_xfs_da_node_rebalance(state->args);
465
Dave Chinner1d9025e2012-06-22 18:50:14 +1000466 node1 = blk1->bp->b_addr;
467 node2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /*
469 * Figure out how many entries need to move, and in which direction.
470 * Swap the nodes around if that makes it simpler.
471 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100472 if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
Nathan Scott403432d2006-03-17 17:29:46 +1100473 ((be32_to_cpu(node2->btree[0].hashval) < be32_to_cpu(node1->btree[0].hashval)) ||
Nathan Scottfac80cce2006-03-17 17:29:56 +1100474 (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
475 be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 tmpnode = node1;
477 node1 = node2;
478 node2 = tmpnode;
479 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200480 ASSERT(node1->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
481 ASSERT(node2->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cce2006-03-17 17:29:56 +1100482 count = (be16_to_cpu(node1->hdr.count) - be16_to_cpu(node2->hdr.count)) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (count == 0)
484 return;
485 tp = state->args->trans;
486 /*
487 * Two cases: high-to-low and low-to-high.
488 */
489 if (count > 0) {
490 /*
491 * Move elements in node2 up to make a hole.
492 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100493 if ((tmp = be16_to_cpu(node2->hdr.count)) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 tmp *= (uint)sizeof(xfs_da_node_entry_t);
495 btree_s = &node2->btree[0];
496 btree_d = &node2->btree[count];
497 memmove(btree_d, btree_s, tmp);
498 }
499
500 /*
501 * Move the req'd B-tree elements from high in node1 to
502 * low in node2.
503 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800504 be16_add_cpu(&node2->hdr.count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Nathan Scottfac80cce2006-03-17 17:29:56 +1100506 btree_s = &node1->btree[be16_to_cpu(node1->hdr.count) - count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 btree_d = &node2->btree[0];
508 memcpy(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800509 be16_add_cpu(&node1->hdr.count, -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else {
511 /*
512 * Move the req'd B-tree elements from low in node2 to
513 * high in node1.
514 */
515 count = -count;
516 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
517 btree_s = &node2->btree[0];
Nathan Scottfac80cce2006-03-17 17:29:56 +1100518 btree_d = &node1->btree[be16_to_cpu(node1->hdr.count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 memcpy(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800520 be16_add_cpu(&node1->hdr.count, count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000521 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 XFS_DA_LOGRANGE(node1, btree_d, tmp));
523
524 /*
525 * Move elements in node2 down to fill the hole.
526 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100527 tmp = be16_to_cpu(node2->hdr.count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 tmp *= (uint)sizeof(xfs_da_node_entry_t);
529 btree_s = &node2->btree[count];
530 btree_d = &node2->btree[0];
531 memmove(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800532 be16_add_cpu(&node2->hdr.count, -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 /*
536 * Log header of node 1 and all current bits of node 2.
537 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000538 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 XFS_DA_LOGRANGE(node1, &node1->hdr, sizeof(node1->hdr)));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000540 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 XFS_DA_LOGRANGE(node2, &node2->hdr,
542 sizeof(node2->hdr) +
Nathan Scottfac80cce2006-03-17 17:29:56 +1100543 sizeof(node2->btree[0]) * be16_to_cpu(node2->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * Record the last hashval from each block for upward propagation.
547 * (note: don't use the swapped node pointers)
548 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000549 node1 = blk1->bp->b_addr;
550 node2 = blk2->bp->b_addr;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100551 blk1->hashval = be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval);
552 blk2->hashval = be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /*
555 * Adjust the expected index for insertion.
556 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100557 if (blk1->index >= be16_to_cpu(node1->hdr.count)) {
558 blk2->index = blk1->index - be16_to_cpu(node1->hdr.count);
559 blk1->index = be16_to_cpu(node1->hdr.count) + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561}
562
563/*
564 * Add a new entry to an intermediate node.
565 */
566STATIC void
567xfs_da_node_add(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
568 xfs_da_state_blk_t *newblk)
569{
570 xfs_da_intnode_t *node;
571 xfs_da_node_entry_t *btree;
572 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Dave Chinner5a5881c2012-03-22 05:15:13 +0000574 trace_xfs_da_node_add(state->args);
575
Dave Chinner1d9025e2012-06-22 18:50:14 +1000576 node = oldblk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200577 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cce2006-03-17 17:29:56 +1100578 ASSERT((oldblk->index >= 0) && (oldblk->index <= be16_to_cpu(node->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000580 if (state->args->whichfork == XFS_DATA_FORK)
Christoph Hellwig73523a22010-07-20 17:54:45 +1000581 ASSERT(newblk->blkno >= state->mp->m_dirleafblk &&
582 newblk->blkno < state->mp->m_dirfreeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
585 * We may need to make some room before we insert the new node.
586 */
587 tmp = 0;
588 btree = &node->btree[ oldblk->index ];
Nathan Scottfac80cce2006-03-17 17:29:56 +1100589 if (oldblk->index < be16_to_cpu(node->hdr.count)) {
590 tmp = (be16_to_cpu(node->hdr.count) - oldblk->index) * (uint)sizeof(*btree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 memmove(btree + 1, btree, tmp);
592 }
Nathan Scott403432d2006-03-17 17:29:46 +1100593 btree->hashval = cpu_to_be32(newblk->hashval);
594 btree->before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000595 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 XFS_DA_LOGRANGE(node, btree, tmp + sizeof(*btree)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800597 be16_add_cpu(&node->hdr.count, 1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000598 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
600
601 /*
602 * Copy the last hash value from the oldblk to propagate upwards.
603 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100604 oldblk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1 ].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/*========================================================================
608 * Routines used for shrinking the Btree.
609 *========================================================================*/
610
611/*
612 * Deallocate an empty leaf node, remove it from its parent,
613 * possibly deallocating that block, etc...
614 */
615int
616xfs_da_join(xfs_da_state_t *state)
617{
618 xfs_da_state_blk_t *drop_blk, *save_blk;
619 int action, error;
620
Dave Chinner5a5881c2012-03-22 05:15:13 +0000621 trace_xfs_da_join(state->args);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 action = 0;
624 drop_blk = &state->path.blk[ state->path.active-1 ];
625 save_blk = &state->altpath.blk[ state->path.active-1 ];
626 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
627 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000628 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /*
631 * Walk back up the tree joining/deallocating as necessary.
632 * When we stop dropping blocks, break out.
633 */
634 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
635 state->path.active--) {
636 /*
637 * See if we can combine the block with a neighbor.
638 * (action == 0) => no options, just leave
639 * (action == 1) => coalesce, then unlink
640 * (action == 2) => block empty, unlink it
641 */
642 switch (drop_blk->magic) {
643 case XFS_ATTR_LEAF_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 error = xfs_attr_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (error)
646 return(error);
647 if (action == 0)
648 return(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 xfs_attr_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 error = xfs_dir2_leafn_toosmall(state, &action);
653 if (error)
654 return error;
655 if (action == 0)
656 return 0;
657 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
658 break;
659 case XFS_DA_NODE_MAGIC:
660 /*
661 * Remove the offending node, fixup hashvals,
662 * check for a toosmall neighbor.
663 */
664 xfs_da_node_remove(state, drop_blk);
665 xfs_da_fixhashpath(state, &state->path);
666 error = xfs_da_node_toosmall(state, &action);
667 if (error)
668 return(error);
669 if (action == 0)
670 return 0;
671 xfs_da_node_unbalance(state, drop_blk, save_blk);
672 break;
673 }
674 xfs_da_fixhashpath(state, &state->altpath);
675 error = xfs_da_blk_unlink(state, drop_blk, save_blk);
676 xfs_da_state_kill_altpath(state);
677 if (error)
678 return(error);
679 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
680 drop_blk->bp);
681 drop_blk->bp = NULL;
682 if (error)
683 return(error);
684 }
685 /*
686 * We joined all the way to the top. If it turns out that
687 * we only have one entry in the root, make the child block
688 * the new root.
689 */
690 xfs_da_node_remove(state, drop_blk);
691 xfs_da_fixhashpath(state, &state->path);
692 error = xfs_da_root_join(state, &state->path.blk[0]);
693 return(error);
694}
695
Alex Elder1c4f3322011-07-18 18:14:09 +0000696#ifdef DEBUG
697static void
698xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
699{
700 __be16 magic = blkinfo->magic;
701
702 if (level == 1) {
703 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
704 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
705 } else
706 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
707 ASSERT(!blkinfo->forw);
708 ASSERT(!blkinfo->back);
709}
710#else /* !DEBUG */
711#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
712#endif /* !DEBUG */
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/*
715 * We have only one entry in the root. Copy the only remaining child of
716 * the old root to block 0 as the new root node.
717 */
718STATIC int
719xfs_da_root_join(xfs_da_state_t *state, xfs_da_state_blk_t *root_blk)
720{
721 xfs_da_intnode_t *oldroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 xfs_da_args_t *args;
723 xfs_dablk_t child;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000724 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int error;
726
Dave Chinner5a5881c2012-03-22 05:15:13 +0000727 trace_xfs_da_root_join(state->args);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 args = state->args;
730 ASSERT(args != NULL);
731 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 oldroot = root_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200733 ASSERT(oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ASSERT(!oldroot->hdr.info.forw);
735 ASSERT(!oldroot->hdr.info.back);
736
737 /*
738 * If the root has more than one child, then don't do anything.
739 */
Nathan Scottfac80cce2006-03-17 17:29:56 +1100740 if (be16_to_cpu(oldroot->hdr.count) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return(0);
742
743 /*
744 * Read in the (only) child block, then copy those bytes into
745 * the root block's buffer and free the original child block.
746 */
Nathan Scott403432d2006-03-17 17:29:46 +1100747 child = be32_to_cpu(oldroot->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 ASSERT(child != 0);
749 error = xfs_da_read_buf(args->trans, args->dp, child, -1, &bp,
750 args->whichfork);
751 if (error)
752 return(error);
753 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000754 xfs_da_blkinfo_onlychild_validate(bp->b_addr,
Alex Elder1c4f3322011-07-18 18:14:09 +0000755 be16_to_cpu(oldroot->hdr.level));
756
Dave Chinner1d9025e2012-06-22 18:50:14 +1000757 memcpy(root_blk->bp->b_addr, bp->b_addr, state->blocksize);
758 xfs_trans_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 error = xfs_da_shrink_inode(args, child, bp);
760 return(error);
761}
762
763/*
764 * Check a node block and its neighbors to see if the block should be
765 * collapsed into one or the other neighbor. Always keep the block
766 * with the smaller block number.
767 * If the current block is over 50% full, don't try to join it, return 0.
768 * If the block is empty, fill in the state structure and return 2.
769 * If it can be collapsed, fill in the state structure and return 1.
770 * If nothing can be done, return 0.
771 */
772STATIC int
773xfs_da_node_toosmall(xfs_da_state_t *state, int *action)
774{
775 xfs_da_intnode_t *node;
776 xfs_da_state_blk_t *blk;
777 xfs_da_blkinfo_t *info;
778 int count, forward, error, retval, i;
779 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000780 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dave Chinneree732592012-11-12 22:53:53 +1100782 trace_xfs_da_node_toosmall(state->args);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /*
785 * Check for the degenerate case of the block being over 50% full.
786 * If so, it's not worth even looking to see if we might be able
787 * to coalesce with a sibling.
788 */
789 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +1000790 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200791 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 node = (xfs_da_intnode_t *)info;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100793 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (count > (state->node_ents >> 1)) {
795 *action = 0; /* blk over 50%, don't try to join */
796 return(0); /* blk over 50%, don't try to join */
797 }
798
799 /*
800 * Check for the degenerate case of the block being empty.
801 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +1000802 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 * to merge with the forward block unless it is NULL.
804 */
805 if (count == 0) {
806 /*
807 * Make altpath point to the block we want to keep and
808 * path point to the block we want to drop (this one).
809 */
Nathan Scott89da0542006-03-17 17:28:40 +1100810 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 memcpy(&state->altpath, &state->path, sizeof(state->path));
812 error = xfs_da_path_shift(state, &state->altpath, forward,
813 0, &retval);
814 if (error)
815 return(error);
816 if (retval) {
817 *action = 0;
818 } else {
819 *action = 2;
820 }
821 return(0);
822 }
823
824 /*
825 * Examine each sibling block to see if we can coalesce with
826 * at least 25% free space to spare. We need to figure out
827 * whether to merge with the forward or the backward block.
828 * We prefer coalescing with the lower numbered sibling so as
829 * to shrink a directory over time.
830 */
831 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +1100832 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 for (i = 0; i < 2; forward = !forward, i++) {
834 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +1100835 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 else
Nathan Scott89da0542006-03-17 17:28:40 +1100837 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (blkno == 0)
839 continue;
840 error = xfs_da_read_buf(state->args->trans, state->args->dp,
841 blkno, -1, &bp, state->args->whichfork);
842 if (error)
843 return(error);
844 ASSERT(bp != NULL);
845
846 node = (xfs_da_intnode_t *)info;
847 count = state->node_ents;
848 count -= state->node_ents >> 2;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100849 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000850 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200851 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cce2006-03-17 17:29:56 +1100852 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000853 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (count >= 0)
855 break; /* fits with at least 25% to spare */
856 }
857 if (i >= 2) {
858 *action = 0;
859 return(0);
860 }
861
862 /*
863 * Make altpath point to the block we want to keep (the lower
864 * numbered block) and path point to the block we want to drop.
865 */
866 memcpy(&state->altpath, &state->path, sizeof(state->path));
867 if (blkno < blk->blkno) {
868 error = xfs_da_path_shift(state, &state->altpath, forward,
869 0, &retval);
870 if (error) {
871 return(error);
872 }
873 if (retval) {
874 *action = 0;
875 return(0);
876 }
877 } else {
878 error = xfs_da_path_shift(state, &state->path, forward,
879 0, &retval);
880 if (error) {
881 return(error);
882 }
883 if (retval) {
884 *action = 0;
885 return(0);
886 }
887 }
888 *action = 1;
889 return(0);
890}
891
892/*
893 * Walk back up the tree adjusting hash values as necessary,
894 * when we stop making changes, return.
895 */
896void
897xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
898{
899 xfs_da_state_blk_t *blk;
900 xfs_da_intnode_t *node;
901 xfs_da_node_entry_t *btree;
902 xfs_dahash_t lasthash=0;
903 int level, count;
904
Dave Chinneree732592012-11-12 22:53:53 +1100905 trace_xfs_da_fixhashpath(state->args);
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 level = path->active-1;
908 blk = &path->blk[ level ];
909 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 case XFS_ATTR_LEAF_MAGIC:
911 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
912 if (count == 0)
913 return;
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
917 if (count == 0)
918 return;
919 break;
920 case XFS_DA_NODE_MAGIC:
921 lasthash = xfs_da_node_lasthash(blk->bp, &count);
922 if (count == 0)
923 return;
924 break;
925 }
926 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000927 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200928 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 btree = &node->btree[ blk->index ];
Nathan Scott403432d2006-03-17 17:29:46 +1100930 if (be32_to_cpu(btree->hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
932 blk->hashval = lasthash;
Nathan Scott403432d2006-03-17 17:29:46 +1100933 btree->hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000934 xfs_trans_log_buf(state->args->trans, blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
936
Nathan Scottfac80cce2006-03-17 17:29:56 +1100937 lasthash = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939}
940
941/*
942 * Remove an entry from an intermediate node.
943 */
944STATIC void
945xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
946{
947 xfs_da_intnode_t *node;
948 xfs_da_node_entry_t *btree;
949 int tmp;
950
Dave Chinner5a5881c2012-03-22 05:15:13 +0000951 trace_xfs_da_node_remove(state->args);
952
Dave Chinner1d9025e2012-06-22 18:50:14 +1000953 node = drop_blk->bp->b_addr;
Nathan Scottfac80cce2006-03-17 17:29:56 +1100954 ASSERT(drop_blk->index < be16_to_cpu(node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ASSERT(drop_blk->index >= 0);
956
957 /*
958 * Copy over the offending entry, or just zero it out.
959 */
960 btree = &node->btree[drop_blk->index];
Nathan Scottfac80cce2006-03-17 17:29:56 +1100961 if (drop_blk->index < (be16_to_cpu(node->hdr.count)-1)) {
962 tmp = be16_to_cpu(node->hdr.count) - drop_blk->index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 tmp *= (uint)sizeof(xfs_da_node_entry_t);
964 memmove(btree, btree + 1, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000965 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 XFS_DA_LOGRANGE(node, btree, tmp));
Nathan Scottfac80cce2006-03-17 17:29:56 +1100967 btree = &node->btree[be16_to_cpu(node->hdr.count)-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000970 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800972 be16_add_cpu(&node->hdr.count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000973 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
975
976 /*
977 * Copy the last hash value from the block to propagate upwards.
978 */
979 btree--;
Nathan Scott403432d2006-03-17 17:29:46 +1100980 drop_blk->hashval = be32_to_cpu(btree->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Unbalance the btree elements between two intermediate nodes,
985 * move all Btree elements from one node into another.
986 */
987STATIC void
988xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
989 xfs_da_state_blk_t *save_blk)
990{
991 xfs_da_intnode_t *drop_node, *save_node;
992 xfs_da_node_entry_t *btree;
993 int tmp;
994 xfs_trans_t *tp;
995
Dave Chinner5a5881c2012-03-22 05:15:13 +0000996 trace_xfs_da_node_unbalance(state->args);
997
Dave Chinner1d9025e2012-06-22 18:50:14 +1000998 drop_node = drop_blk->bp->b_addr;
999 save_node = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001000 ASSERT(drop_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
1001 ASSERT(save_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 tp = state->args->trans;
1003
1004 /*
1005 * If the dying block has lower hashvals, then move all the
1006 * elements in the remaining block up to make a hole.
1007 */
Nathan Scott403432d2006-03-17 17:29:46 +11001008 if ((be32_to_cpu(drop_node->btree[0].hashval) < be32_to_cpu(save_node->btree[ 0 ].hashval)) ||
Nathan Scottfac80cce2006-03-17 17:29:56 +11001009 (be32_to_cpu(drop_node->btree[be16_to_cpu(drop_node->hdr.count)-1].hashval) <
1010 be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 {
Nathan Scottfac80cce2006-03-17 17:29:56 +11001012 btree = &save_node->btree[be16_to_cpu(drop_node->hdr.count)];
1013 tmp = be16_to_cpu(save_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 memmove(btree, &save_node->btree[0], tmp);
1015 btree = &save_node->btree[0];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001016 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cce2006-03-17 17:29:56 +11001018 (be16_to_cpu(save_node->hdr.count) + be16_to_cpu(drop_node->hdr.count)) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 sizeof(xfs_da_node_entry_t)));
1020 } else {
Nathan Scottfac80cce2006-03-17 17:29:56 +11001021 btree = &save_node->btree[be16_to_cpu(save_node->hdr.count)];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001022 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cce2006-03-17 17:29:56 +11001024 be16_to_cpu(drop_node->hdr.count) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sizeof(xfs_da_node_entry_t)));
1026 }
1027
1028 /*
1029 * Move all the B-tree elements from drop_blk to save_blk.
1030 */
Nathan Scottfac80cce2006-03-17 17:29:56 +11001031 tmp = be16_to_cpu(drop_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 memcpy(btree, &drop_node->btree[0], tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001033 be16_add_cpu(&save_node->hdr.count, be16_to_cpu(drop_node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Dave Chinner1d9025e2012-06-22 18:50:14 +10001035 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1037 sizeof(save_node->hdr)));
1038
1039 /*
1040 * Save the last hashval in the remaining block for upward propagation.
1041 */
Nathan Scottfac80cce2006-03-17 17:29:56 +11001042 save_blk->hashval = be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/*========================================================================
1046 * Routines used for finding things in the Btree.
1047 *========================================================================*/
1048
1049/*
1050 * Walk down the Btree looking for a particular filename, filling
1051 * in the state structure as we go.
1052 *
1053 * We will set the state structure to point to each of the elements
1054 * in each of the nodes where either the hashval is or should be.
1055 *
1056 * We support duplicate hashval's so for each entry in the current
1057 * node that could contain the desired hashval, descend. This is a
1058 * pruned depth-first tree search.
1059 */
1060int /* error */
1061xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
1062{
1063 xfs_da_state_blk_t *blk;
1064 xfs_da_blkinfo_t *curr;
1065 xfs_da_intnode_t *node;
1066 xfs_da_node_entry_t *btree;
1067 xfs_dablk_t blkno;
1068 int probe, span, max, error, retval;
Nathan Scottd432c802006-09-28 11:03:44 +10001069 xfs_dahash_t hashval, btreehashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 xfs_da_args_t *args;
1071
1072 args = state->args;
1073
1074 /*
1075 * Descend thru the B-tree searching each level for the right
1076 * node to use, until the right hashval is found.
1077 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001078 blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 for (blk = &state->path.blk[0], state->path.active = 1;
1080 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1081 blk++, state->path.active++) {
1082 /*
1083 * Read the next node down in the tree.
1084 */
1085 blk->blkno = blkno;
1086 error = xfs_da_read_buf(args->trans, args->dp, blkno,
1087 -1, &blk->bp, args->whichfork);
1088 if (error) {
1089 blk->blkno = 0;
1090 state->path.active--;
1091 return(error);
1092 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001093 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001094 blk->magic = be16_to_cpu(curr->magic);
1095 ASSERT(blk->magic == XFS_DA_NODE_MAGIC ||
1096 blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1097 blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
1100 * Search an intermediate node for a match.
1101 */
Nathan Scott89da0542006-03-17 17:28:40 +11001102 if (blk->magic == XFS_DA_NODE_MAGIC) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001103 node = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001104 max = be16_to_cpu(node->hdr.count);
Lachlan McIlroy1c91ad32007-02-10 18:35:27 +11001105 blk->hashval = be32_to_cpu(node->btree[max-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /*
1108 * Binary search. (note: small blocks will skip loop)
1109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 probe = span = max / 2;
1111 hashval = args->hashval;
1112 for (btree = &node->btree[probe]; span > 4;
1113 btree = &node->btree[probe]) {
1114 span /= 2;
Nathan Scottd432c802006-09-28 11:03:44 +10001115 btreehashval = be32_to_cpu(btree->hashval);
1116 if (btreehashval < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 probe += span;
Nathan Scottd432c802006-09-28 11:03:44 +10001118 else if (btreehashval > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 probe -= span;
1120 else
1121 break;
1122 }
1123 ASSERT((probe >= 0) && (probe < max));
Nathan Scott403432d2006-03-17 17:29:46 +11001124 ASSERT((span <= 4) || (be32_to_cpu(btree->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * Since we may have duplicate hashval's, find the first
1128 * matching hashval in the node.
1129 */
Nathan Scott403432d2006-03-17 17:29:46 +11001130 while ((probe > 0) && (be32_to_cpu(btree->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 btree--;
1132 probe--;
1133 }
Nathan Scott403432d2006-03-17 17:29:46 +11001134 while ((probe < max) && (be32_to_cpu(btree->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 btree++;
1136 probe++;
1137 }
1138
1139 /*
1140 * Pick the right block to descend on.
1141 */
1142 if (probe == max) {
1143 blk->index = max-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001144 blkno = be32_to_cpu(node->btree[max-1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
1146 blk->index = probe;
Nathan Scott403432d2006-03-17 17:29:46 +11001147 blkno = be32_to_cpu(btree->before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Nathan Scottd432c802006-09-28 11:03:44 +10001149 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1151 break;
Nathan Scottd432c802006-09-28 11:03:44 +10001152 } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
1154 break;
1155 }
1156 }
1157
1158 /*
1159 * A leaf block that ends in the hashval that we are interested in
1160 * (final hashval == search hashval) means that the next block may
1161 * contain more entries with the same hashval, shift upward to the
1162 * next leaf and keep searching.
1163 */
1164 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001165 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1167 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001168 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 retval = xfs_attr_leaf_lookup_int(blk->bp, args);
1170 blk->index = args->index;
1171 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001172 } else {
1173 ASSERT(0);
1174 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1177 (blk->hashval == args->hashval)) {
1178 error = xfs_da_path_shift(state, &state->path, 1, 1,
1179 &retval);
1180 if (error)
1181 return(error);
1182 if (retval == 0) {
1183 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001184 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* path_shift() gives ENOENT */
1186 retval = XFS_ERROR(ENOATTR);
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 break;
1190 }
1191 *result = retval;
1192 return(0);
1193}
1194
1195/*========================================================================
1196 * Utility routines.
1197 *========================================================================*/
1198
1199/*
1200 * Link a new block into a doubly linked list of blocks (of whatever type).
1201 */
1202int /* error */
1203xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
1204 xfs_da_state_blk_t *new_blk)
1205{
1206 xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
1207 xfs_da_args_t *args;
1208 int before=0, error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001209 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 /*
1212 * Set up environment.
1213 */
1214 args = state->args;
1215 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001216 old_info = old_blk->bp->b_addr;
1217 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001219 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001221 ASSERT(old_blk->magic == be16_to_cpu(old_info->magic));
1222 ASSERT(new_blk->magic == be16_to_cpu(new_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ASSERT(old_blk->magic == new_blk->magic);
1224
1225 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case XFS_ATTR_LEAF_MAGIC:
1227 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
1231 break;
1232 case XFS_DA_NODE_MAGIC:
1233 before = xfs_da_node_order(old_blk->bp, new_blk->bp);
1234 break;
1235 }
1236
1237 /*
1238 * Link blocks in appropriate order.
1239 */
1240 if (before) {
1241 /*
1242 * Link new block in before existing block.
1243 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001244 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001245 new_info->forw = cpu_to_be32(old_blk->blkno);
1246 new_info->back = old_info->back;
1247 if (old_info->back) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001249 be32_to_cpu(old_info->back),
1250 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (error)
1252 return(error);
1253 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001254 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001255 ASSERT(be16_to_cpu(tmp_info->magic) == be16_to_cpu(old_info->magic));
1256 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1257 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001258 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Nathan Scott89da0542006-03-17 17:28:40 +11001260 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 } else {
1262 /*
1263 * Link new block in after existing block.
1264 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001265 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001266 new_info->forw = old_info->forw;
1267 new_info->back = cpu_to_be32(old_blk->blkno);
1268 if (old_info->forw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001270 be32_to_cpu(old_info->forw),
1271 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (error)
1273 return(error);
1274 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001275 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001276 ASSERT(tmp_info->magic == old_info->magic);
1277 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1278 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001279 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
Nathan Scott89da0542006-03-17 17:28:40 +11001281 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Dave Chinner1d9025e2012-06-22 18:50:14 +10001284 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1285 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return(0);
1287}
1288
1289/*
1290 * Compare two intermediate nodes for "order".
1291 */
1292STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001293xfs_da_node_order(
1294 struct xfs_buf *node1_bp,
1295 struct xfs_buf *node2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 xfs_da_intnode_t *node1, *node2;
1298
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 node1 = node1_bp->b_addr;
1300 node2 = node2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001301 ASSERT(node1->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) &&
1302 node2->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cce2006-03-17 17:29:56 +11001303 if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001304 ((be32_to_cpu(node2->btree[0].hashval) <
1305 be32_to_cpu(node1->btree[0].hashval)) ||
Nathan Scottfac80cce2006-03-17 17:29:56 +11001306 (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
1307 be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return(1);
1309 }
1310 return(0);
1311}
1312
1313/*
1314 * Pick up the last hashvalue from an intermediate node.
1315 */
1316STATIC uint
Dave Chinner1d9025e2012-06-22 18:50:14 +10001317xfs_da_node_lasthash(
1318 struct xfs_buf *bp,
1319 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 xfs_da_intnode_t *node;
1322
Dave Chinner1d9025e2012-06-22 18:50:14 +10001323 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001324 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (count)
Nathan Scottfac80cce2006-03-17 17:29:56 +11001326 *count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (!node->hdr.count)
1328 return(0);
Nathan Scottfac80cce2006-03-17 17:29:56 +11001329 return be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332/*
1333 * Unlink a block from a doubly linked list of blocks.
1334 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001335STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1337 xfs_da_state_blk_t *save_blk)
1338{
1339 xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
1340 xfs_da_args_t *args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001341 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int error;
1343
1344 /*
1345 * Set up environment.
1346 */
1347 args = state->args;
1348 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001349 save_info = save_blk->bp->b_addr;
1350 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001352 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001354 ASSERT(save_blk->magic == be16_to_cpu(save_info->magic));
1355 ASSERT(drop_blk->magic == be16_to_cpu(drop_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001357 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1358 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1359 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1360 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /*
1363 * Unlink the leaf block from the doubly linked chain of leaves.
1364 */
Nathan Scott89da0542006-03-17 17:28:40 +11001365 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001366 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001367 save_info->back = drop_info->back;
1368 if (drop_info->back) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001370 be32_to_cpu(drop_info->back),
1371 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (error)
1373 return(error);
1374 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001375 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001376 ASSERT(tmp_info->magic == save_info->magic);
1377 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1378 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001379 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001383 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001384 save_info->forw = drop_info->forw;
1385 if (drop_info->forw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001387 be32_to_cpu(drop_info->forw),
1388 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (error)
1390 return(error);
1391 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001392 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001393 ASSERT(tmp_info->magic == save_info->magic);
1394 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1395 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001396 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 }
1400
Dave Chinner1d9025e2012-06-22 18:50:14 +10001401 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return(0);
1403}
1404
1405/*
1406 * Move a path "forward" or "!forward" one block at the current level.
1407 *
1408 * This routine will adjust a "path" to point to the next block
1409 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1410 * Btree, including updating pointers to the intermediate nodes between
1411 * the new bottom and the root.
1412 */
1413int /* error */
1414xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
1415 int forward, int release, int *result)
1416{
1417 xfs_da_state_blk_t *blk;
1418 xfs_da_blkinfo_t *info;
1419 xfs_da_intnode_t *node;
1420 xfs_da_args_t *args;
1421 xfs_dablk_t blkno=0;
1422 int level, error;
1423
Dave Chinneree732592012-11-12 22:53:53 +11001424 trace_xfs_da_path_shift(state->args);
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /*
1427 * Roll up the Btree looking for the first block where our
1428 * current index is not at the edge of the block. Note that
1429 * we skip the bottom layer because we want the sibling block.
1430 */
1431 args = state->args;
1432 ASSERT(args != NULL);
1433 ASSERT(path != NULL);
1434 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1435 level = (path->active-1) - 1; /* skip bottom layer in path */
1436 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1437 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001438 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001439 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cce2006-03-17 17:29:56 +11001440 if (forward && (blk->index < be16_to_cpu(node->hdr.count)-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 blk->index++;
Nathan Scott403432d2006-03-17 17:29:46 +11001442 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 } else if (!forward && (blk->index > 0)) {
1445 blk->index--;
Nathan Scott403432d2006-03-17 17:29:46 +11001446 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 }
1449 }
1450 if (level < 0) {
1451 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001452 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return(0);
1454 }
1455
1456 /*
1457 * Roll down the edge of the subtree until we reach the
1458 * same depth we were at originally.
1459 */
1460 for (blk++, level++; level < path->active; blk++, level++) {
1461 /*
1462 * Release the old block.
1463 * (if it's dirty, trans won't actually let go)
1464 */
1465 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001466 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
1469 * Read the next child block.
1470 */
1471 blk->blkno = blkno;
1472 error = xfs_da_read_buf(args->trans, args->dp, blkno, -1,
1473 &blk->bp, args->whichfork);
1474 if (error)
1475 return(error);
1476 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001477 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001478 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1479 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1480 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott89da0542006-03-17 17:28:40 +11001481 blk->magic = be16_to_cpu(info->magic);
1482 if (blk->magic == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 node = (xfs_da_intnode_t *)info;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001484 blk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (forward)
1486 blk->index = 0;
1487 else
Nathan Scottfac80cce2006-03-17 17:29:56 +11001488 blk->index = be16_to_cpu(node->hdr.count)-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001489 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 } else {
1491 ASSERT(level == path->active-1);
1492 blk->index = 0;
1493 switch(blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 case XFS_ATTR_LEAF_MAGIC:
1495 blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
1496 NULL);
1497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
1500 NULL);
1501 break;
1502 default:
1503 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001504 blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506 }
1507 }
1508 }
1509 *result = 0;
1510 return(0);
1511}
1512
1513
1514/*========================================================================
1515 * Utility routines.
1516 *========================================================================*/
1517
1518/*
1519 * Implement a simple hash on a character string.
1520 * Rotate the hash value by 7 bits, then XOR each character in.
1521 * This is implemented with some source-level loop unrolling.
1522 */
1523xfs_dahash_t
Christoph Hellwiga5687782009-02-09 08:37:39 +01001524xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 xfs_dahash_t hash;
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /*
1529 * Do four characters at a time as long as we can.
1530 */
1531 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1532 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1533 (name[3] << 0) ^ rol32(hash, 7 * 4);
1534
1535 /*
1536 * Now do the rest of the characters.
1537 */
1538 switch (namelen) {
1539 case 3:
1540 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
1541 rol32(hash, 7 * 3);
1542 case 2:
1543 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
1544 case 1:
1545 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11001546 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return hash;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Barry Naujok5163f952008-05-21 16:41:01 +10001551enum xfs_dacmp
1552xfs_da_compname(
1553 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11001554 const unsigned char *name,
1555 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10001556{
1557 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
1558 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
1559}
1560
1561static xfs_dahash_t
1562xfs_default_hashname(
1563 struct xfs_name *name)
1564{
1565 return xfs_da_hashname(name->name, name->len);
1566}
1567
1568const struct xfs_nameops xfs_default_nameops = {
1569 .hashname = xfs_default_hashname,
1570 .compname = xfs_da_compname
1571};
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573int
Christoph Hellwig77936d02011-07-13 13:43:49 +02001574xfs_da_grow_inode_int(
1575 struct xfs_da_args *args,
1576 xfs_fileoff_t *bno,
1577 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Christoph Hellwig77936d02011-07-13 13:43:49 +02001579 struct xfs_trans *tp = args->trans;
1580 struct xfs_inode *dp = args->dp;
1581 int w = args->whichfork;
1582 xfs_drfsbno_t nblks = dp->i_d.di_nblocks;
1583 struct xfs_bmbt_irec map, *mapp;
1584 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Find a spot in the file space to put the new block.
1588 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02001589 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
1590 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Try mapping it in one filesystem block.
1595 */
1596 nmap = 1;
1597 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001598 error = xfs_bmapi_write(tp, dp, *bno, count,
1599 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 args->firstblock, args->total, &map, &nmap,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001601 args->flist);
1602 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ASSERT(nmap <= 1);
1606 if (nmap == 1) {
1607 mapp = &map;
1608 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001609 } else if (nmap == 0 && count > 1) {
1610 xfs_fileoff_t b;
1611 int c;
1612
1613 /*
1614 * If we didn't get it and the block might work if fragmented,
1615 * try without the CONTIG flag. Loop until we get it all.
1616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001618 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001620 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001621 error = xfs_bmapi_write(tp, dp, b, c,
1622 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 args->firstblock, args->total,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001624 &mapp[mapi], &nmap, args->flist);
1625 if (error)
1626 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (nmap < 1)
1628 break;
1629 mapi += nmap;
1630 b = mapp[mapi - 1].br_startoff +
1631 mapp[mapi - 1].br_blockcount;
1632 }
1633 } else {
1634 mapi = 0;
1635 mapp = NULL;
1636 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * Count the blocks we got, make sure it matches the total.
1640 */
1641 for (i = 0, got = 0; i < mapi; i++)
1642 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001643 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02001645 *bno + count) {
1646 error = XFS_ERROR(ENOSPC);
1647 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001649
David Chinnera7444052008-10-30 17:38:12 +11001650 /* account for newly allocated blocks in reserved blocks total */
1651 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001652
1653out_free_map:
1654 if (mapp != &map)
1655 kmem_free(mapp);
1656 return error;
1657}
1658
1659/*
1660 * Add a block to the btree ahead of the file.
1661 * Return the new block number to the caller.
1662 */
1663int
1664xfs_da_grow_inode(
1665 struct xfs_da_args *args,
1666 xfs_dablk_t *new_blkno)
1667{
1668 xfs_fileoff_t bno;
1669 int count;
1670 int error;
1671
Dave Chinner5a5881c2012-03-22 05:15:13 +00001672 trace_xfs_da_grow_inode(args);
1673
Christoph Hellwig77936d02011-07-13 13:43:49 +02001674 if (args->whichfork == XFS_DATA_FORK) {
1675 bno = args->dp->i_mount->m_dirleafblk;
1676 count = args->dp->i_mount->m_dirblkfsbs;
1677 } else {
1678 bno = 0;
1679 count = 1;
1680 }
1681
1682 error = xfs_da_grow_inode_int(args, &bno, count);
1683 if (!error)
1684 *new_blkno = (xfs_dablk_t)bno;
1685 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
1689 * Ick. We need to always be able to remove a btree block, even
1690 * if there's no space reservation because the filesystem is full.
1691 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1692 * It swaps the target block with the last block in the file. The
1693 * last block in the file can always be removed since it can't cause
1694 * a bmap btree split to do that.
1695 */
1696STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001697xfs_da_swap_lastblock(
1698 xfs_da_args_t *args,
1699 xfs_dablk_t *dead_blknop,
1700 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001703 struct xfs_buf *dead_buf, *last_buf, *sib_buf, *par_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 xfs_fileoff_t lastoff;
1705 xfs_inode_t *ip;
1706 xfs_trans_t *tp;
1707 xfs_mount_t *mp;
1708 int error, w, entno, level, dead_level;
1709 xfs_da_blkinfo_t *dead_info, *sib_info;
1710 xfs_da_intnode_t *par_node, *dead_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_dir2_leaf_t *dead_leaf2;
1712 xfs_dahash_t dead_hash;
1713
Dave Chinner5a5881c2012-03-22 05:15:13 +00001714 trace_xfs_da_swap_lastblock(args);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 dead_buf = *dead_bufp;
1717 dead_blkno = *dead_blknop;
1718 tp = args->trans;
1719 ip = args->dp;
1720 w = args->whichfork;
1721 ASSERT(w == XFS_DATA_FORK);
1722 mp = ip->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001723 lastoff = mp->m_dirfreeblk;
1724 error = xfs_bmap_last_before(tp, ip, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (error)
1726 return error;
1727 if (unlikely(lastoff == 0)) {
1728 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
1729 mp);
1730 return XFS_ERROR(EFSCORRUPTED);
1731 }
1732 /*
1733 * Read the last block in the btree space.
1734 */
1735 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
1736 if ((error = xfs_da_read_buf(tp, ip, last_blkno, -1, &last_buf, w)))
1737 return error;
1738 /*
1739 * Copy the last block into the dead buffer and log it.
1740 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001741 memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
1742 xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
1743 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /*
1745 * Get values from the moved block.
1746 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001747 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
1749 dead_level = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001750 dead_hash = be32_to_cpu(dead_leaf2->ents[be16_to_cpu(dead_leaf2->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001752 ASSERT(dead_info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 dead_node = (xfs_da_intnode_t *)dead_info;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001754 dead_level = be16_to_cpu(dead_node->hdr.level);
1755 dead_hash = be32_to_cpu(dead_node->btree[be16_to_cpu(dead_node->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 sib_buf = par_buf = NULL;
1758 /*
1759 * If the moved block has a left sibling, fix up the pointers.
1760 */
Nathan Scott89da0542006-03-17 17:28:40 +11001761 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
1763 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001764 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001766 be32_to_cpu(sib_info->forw) != last_blkno ||
1767 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1769 XFS_ERRLEVEL_LOW, mp);
1770 error = XFS_ERROR(EFSCORRUPTED);
1771 goto done;
1772 }
Nathan Scott89da0542006-03-17 17:28:40 +11001773 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001774 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
1776 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 sib_buf = NULL;
1778 }
1779 /*
1780 * If the moved block has a right sibling, fix up the pointers.
1781 */
Nathan Scott89da0542006-03-17 17:28:40 +11001782 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
1784 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001785 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001787 be32_to_cpu(sib_info->back) != last_blkno ||
1788 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1790 XFS_ERRLEVEL_LOW, mp);
1791 error = XFS_ERROR(EFSCORRUPTED);
1792 goto done;
1793 }
Nathan Scott89da0542006-03-17 17:28:40 +11001794 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001795 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
1797 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 sib_buf = NULL;
1799 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001800 par_blkno = mp->m_dirleafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 level = -1;
1802 /*
1803 * Walk down the tree looking for the parent of the moved block.
1804 */
1805 for (;;) {
1806 if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
1807 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001808 par_node = par_buf->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001809 if (unlikely(par_node->hdr.info.magic !=
1810 cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Nathan Scottfac80cce2006-03-17 17:29:56 +11001811 (level >= 0 && level != be16_to_cpu(par_node->hdr.level) + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1813 XFS_ERRLEVEL_LOW, mp);
1814 error = XFS_ERROR(EFSCORRUPTED);
1815 goto done;
1816 }
Nathan Scottfac80cce2006-03-17 17:29:56 +11001817 level = be16_to_cpu(par_node->hdr.level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 for (entno = 0;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001819 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001820 be32_to_cpu(par_node->btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 entno++)
1822 continue;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001823 if (unlikely(entno == be16_to_cpu(par_node->hdr.count))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1825 XFS_ERRLEVEL_LOW, mp);
1826 error = XFS_ERROR(EFSCORRUPTED);
1827 goto done;
1828 }
Nathan Scott403432d2006-03-17 17:29:46 +11001829 par_blkno = be32_to_cpu(par_node->btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (level == dead_level + 1)
1831 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 par_buf = NULL;
1834 }
1835 /*
1836 * We're in the right parent block.
1837 * Look for the right entry.
1838 */
1839 for (;;) {
1840 for (;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001841 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001842 be32_to_cpu(par_node->btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 entno++)
1844 continue;
Nathan Scottfac80cce2006-03-17 17:29:56 +11001845 if (entno < be16_to_cpu(par_node->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
Nathan Scott89da0542006-03-17 17:28:40 +11001847 par_blkno = be32_to_cpu(par_node->hdr.info.forw);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001848 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 par_buf = NULL;
1850 if (unlikely(par_blkno == 0)) {
1851 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1852 XFS_ERRLEVEL_LOW, mp);
1853 error = XFS_ERROR(EFSCORRUPTED);
1854 goto done;
1855 }
1856 if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
1857 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001858 par_node = par_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (unlikely(
Nathan Scottfac80cce2006-03-17 17:29:56 +11001860 be16_to_cpu(par_node->hdr.level) != level ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001861 par_node->hdr.info.magic != cpu_to_be16(XFS_DA_NODE_MAGIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1863 XFS_ERRLEVEL_LOW, mp);
1864 error = XFS_ERROR(EFSCORRUPTED);
1865 goto done;
1866 }
1867 entno = 0;
1868 }
1869 /*
1870 * Update the parent entry pointing to the moved block.
1871 */
Nathan Scott403432d2006-03-17 17:29:46 +11001872 par_node->btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001873 xfs_trans_log_buf(tp, par_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
1875 sizeof(par_node->btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *dead_blknop = last_blkno;
1877 *dead_bufp = last_buf;
1878 return 0;
1879done:
1880 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001881 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001883 xfs_trans_brelse(tp, sib_buf);
1884 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return error;
1886}
1887
1888/*
1889 * Remove a btree block from a directory or attribute.
1890 */
1891int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001892xfs_da_shrink_inode(
1893 xfs_da_args_t *args,
1894 xfs_dablk_t dead_blkno,
1895 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 xfs_inode_t *dp;
1898 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 xfs_trans_t *tp;
1900 xfs_mount_t *mp;
1901
Dave Chinner5a5881c2012-03-22 05:15:13 +00001902 trace_xfs_da_shrink_inode(args);
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 dp = args->dp;
1905 w = args->whichfork;
1906 tp = args->trans;
1907 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001908 if (w == XFS_DATA_FORK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 count = mp->m_dirblkfsbs;
1910 else
1911 count = 1;
1912 for (;;) {
1913 /*
1914 * Remove extents. If we get ENOSPC for a dir we have to move
1915 * the last block to the place we want to kill.
1916 */
1917 if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001918 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001919 0, args->firstblock, args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 &done)) == ENOSPC) {
1921 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001922 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
1924 &dead_buf)))
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001925 break;
1926 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001930 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return error;
1932}
1933
1934/*
1935 * See if the mapping(s) for this btree block are valid, i.e.
1936 * don't contain holes, are logically contiguous, and cover the whole range.
1937 */
1938STATIC int
1939xfs_da_map_covers_blocks(
1940 int nmap,
1941 xfs_bmbt_irec_t *mapp,
1942 xfs_dablk_t bno,
1943 int count)
1944{
1945 int i;
1946 xfs_fileoff_t off;
1947
1948 for (i = 0, off = bno; i < nmap; i++) {
1949 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
1950 mapp[i].br_startblock == DELAYSTARTBLOCK) {
1951 return 0;
1952 }
1953 if (off != mapp[i].br_startoff) {
1954 return 0;
1955 }
1956 off += mapp[i].br_blockcount;
1957 }
1958 return off == bno + count;
1959}
1960
1961/*
Dave Chinner36054312012-06-22 18:50:13 +10001962 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
1963 *
1964 * For the single map case, it is assumed that the caller has provided a pointer
1965 * to a valid xfs_buf_map. For the multiple map case, this function will
1966 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
1967 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 */
Dave Chinner36054312012-06-22 18:50:13 +10001969static int
1970xfs_buf_map_from_irec(
1971 struct xfs_mount *mp,
1972 struct xfs_buf_map **mapp,
1973 unsigned int *nmaps,
1974 struct xfs_bmbt_irec *irecs,
1975 unsigned int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Dave Chinner36054312012-06-22 18:50:13 +10001977 struct xfs_buf_map *map;
1978 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Dave Chinner36054312012-06-22 18:50:13 +10001980 ASSERT(*nmaps == 1);
1981 ASSERT(nirecs >= 1);
1982
1983 if (nirecs > 1) {
1984 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_SLEEP);
1985 if (!map)
1986 return ENOMEM;
1987 *mapp = map;
1988 }
1989
1990 *nmaps = nirecs;
1991 map = *mapp;
1992 for (i = 0; i < *nmaps; i++) {
1993 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
1994 irecs[i].br_startblock != HOLESTARTBLOCK);
1995 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
1996 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
1997 }
1998 return 0;
1999}
2000
2001/*
2002 * Map the block we are given ready for reading. There are three possible return
2003 * values:
2004 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2005 * caller knows not to execute a subsequent read.
2006 * 0 - if we mapped the block successfully
2007 * >0 - positive error number if there was an error.
2008 */
2009static int
2010xfs_dabuf_map(
2011 struct xfs_trans *trans,
2012 struct xfs_inode *dp,
2013 xfs_dablk_t bno,
2014 xfs_daddr_t mappedbno,
2015 int whichfork,
2016 struct xfs_buf_map **map,
2017 int *nmaps)
2018{
2019 struct xfs_mount *mp = dp->i_mount;
2020 int nfsb;
2021 int error = 0;
2022 struct xfs_bmbt_irec irec;
2023 struct xfs_bmbt_irec *irecs = &irec;
2024 int nirecs;
2025
2026 ASSERT(map && *map);
2027 ASSERT(*nmaps == 1);
2028
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002029 nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
Dave Chinner36054312012-06-22 18:50:13 +10002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
2032 * Caller doesn't have a mapping. -2 means don't complain
2033 * if we land in a hole.
2034 */
2035 if (mappedbno == -1 || mappedbno == -2) {
2036 /*
2037 * Optimize the one-block case.
2038 */
Dave Chinner36054312012-06-22 18:50:13 +10002039 if (nfsb != 1)
2040 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002041
Dave Chinner36054312012-06-22 18:50:13 +10002042 nirecs = nfsb;
2043 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2044 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002045 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002046 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002048 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2049 irecs->br_startoff = (xfs_fileoff_t)bno;
2050 irecs->br_blockcount = nfsb;
2051 irecs->br_state = 0;
2052 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Dave Chinner36054312012-06-22 18:50:13 +10002054
2055 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2056 error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (unlikely(error == EFSCORRUPTED)) {
2058 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002059 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002060 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2061 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002063 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002064 xfs_alert(mp,
2065"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 i,
Dave Chinner36054312012-06-22 18:50:13 +10002067 (long long)irecs[i].br_startoff,
2068 (long long)irecs[i].br_startblock,
2069 (long long)irecs[i].br_blockcount,
2070 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 }
2073 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2074 XFS_ERRLEVEL_LOW, mp);
2075 }
Dave Chinner36054312012-06-22 18:50:13 +10002076 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Dave Chinner36054312012-06-22 18:50:13 +10002078 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2079out:
2080 if (irecs != &irec)
2081 kmem_free(irecs);
2082 return error;
2083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Dave Chinner36054312012-06-22 18:50:13 +10002085/*
2086 * Get a buffer for the dir/attr block.
2087 */
2088int
2089xfs_da_get_buf(
2090 struct xfs_trans *trans,
2091 struct xfs_inode *dp,
2092 xfs_dablk_t bno,
2093 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002094 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002095 int whichfork)
2096{
2097 struct xfs_buf *bp;
2098 struct xfs_buf_map map;
2099 struct xfs_buf_map *mapp;
2100 int nmap;
2101 int error;
2102
2103 *bpp = NULL;
2104 mapp = &map;
2105 nmap = 1;
2106 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2107 &mapp, &nmap);
2108 if (error) {
2109 /* mapping a hole is not an error, but we don't continue */
2110 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002112 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Dave Chinner36054312012-06-22 18:50:13 +10002114
2115 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2116 mapp, nmap, 0);
2117 error = bp ? bp->b_error : XFS_ERROR(EIO);
2118 if (error) {
2119 xfs_trans_brelse(trans, bp);
2120 goto out_free;
2121 }
2122
Dave Chinner1d9025e2012-06-22 18:50:14 +10002123 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002124
2125out_free:
2126 if (mapp != &map)
2127 kmem_free(mapp);
2128
2129 return error;
2130}
2131
2132/*
2133 * Get a buffer for the dir/attr block, fill in the contents.
2134 */
2135int
2136xfs_da_read_buf(
2137 struct xfs_trans *trans,
2138 struct xfs_inode *dp,
2139 xfs_dablk_t bno,
2140 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002141 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002142 int whichfork)
2143{
2144 struct xfs_buf *bp;
2145 struct xfs_buf_map map;
2146 struct xfs_buf_map *mapp;
2147 int nmap;
2148 int error;
2149
2150 *bpp = NULL;
2151 mapp = &map;
2152 nmap = 1;
2153 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2154 &mapp, &nmap);
2155 if (error) {
2156 /* mapping a hole is not an error, but we don't continue */
2157 if (error == -1)
2158 error = 0;
2159 goto out_free;
2160 }
2161
2162 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2163 dp->i_mount->m_ddev_targp,
2164 mapp, nmap, 0, &bp);
2165 if (error)
2166 goto out_free;
2167
2168 if (whichfork == XFS_ATTR_FORK)
2169 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 else
Dave Chinner36054312012-06-22 18:50:13 +10002171 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
Dave Chinner36054312012-06-22 18:50:13 +10002174 * This verification code will be moved to a CRC verification callback
2175 * function so just leave it here unchanged until then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
Dave Chinner36054312012-06-22 18:50:13 +10002177 {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002178 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
2179 xfs_dir2_free_t *free = bp->b_addr;
2180 xfs_da_blkinfo_t *info = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 uint magic, magic1;
Dave Chinner36054312012-06-22 18:50:13 +10002182 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Nathan Scott89da0542006-03-17 17:28:40 +11002184 magic = be16_to_cpu(info->magic);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002185 magic1 = be32_to_cpu(hdr->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (unlikely(
2187 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 (magic != XFS_ATTR_LEAF_MAGIC) &&
2189 (magic != XFS_DIR2_LEAF1_MAGIC) &&
2190 (magic != XFS_DIR2_LEAFN_MAGIC) &&
2191 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
2192 (magic1 != XFS_DIR2_DATA_MAGIC) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002193 (free->hdr.magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 mp, XFS_ERRTAG_DA_READ_BUF,
2195 XFS_RANDOM_DA_READ_BUF))) {
Dave Chinner36054312012-06-22 18:50:13 +10002196 trace_xfs_da_btree_corrupt(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2198 XFS_ERRLEVEL_LOW, mp, info);
2199 error = XFS_ERROR(EFSCORRUPTED);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002200 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002201 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002204 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002205out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002207 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return error;
2210}
2211
2212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * Readahead the dir/attr block.
2214 */
2215xfs_daddr_t
2216xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002217 struct xfs_trans *trans,
2218 struct xfs_inode *dp,
2219 xfs_dablk_t bno,
2220 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Dave Chinner36054312012-06-22 18:50:13 +10002222 xfs_daddr_t mappedbno = -1;
2223 struct xfs_buf_map map;
2224 struct xfs_buf_map *mapp;
2225 int nmap;
2226 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Dave Chinner36054312012-06-22 18:50:13 +10002228 mapp = &map;
2229 nmap = 1;
2230 error = xfs_dabuf_map(trans, dp, bno, -1, whichfork,
2231 &mapp, &nmap);
2232 if (error) {
2233 /* mapping a hole is not an error, but we don't continue */
2234 if (error == -1)
2235 error = 0;
2236 goto out_free;
2237 }
2238
2239 mappedbno = mapp[0].bm_bn;
2240 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap);
2241
2242out_free:
2243 if (mapp != &map)
2244 kmem_free(mapp);
2245
2246 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return -1;
Dave Chinner36054312012-06-22 18:50:13 +10002248 return mappedbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253/*
2254 * Allocate a dir-state structure.
2255 * We don't put them on the stack since they're large.
2256 */
2257xfs_da_state_t *
2258xfs_da_state_alloc(void)
2259{
Christoph Hellwigf41d7fb2009-07-18 18:14:55 -04002260 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
2263/*
2264 * Kill the altpath contents of a da-state structure.
2265 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002266STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267xfs_da_state_kill_altpath(xfs_da_state_t *state)
2268{
2269 int i;
2270
Dave Chinner1d9025e2012-06-22 18:50:14 +10002271 for (i = 0; i < state->altpath.active; i++)
2272 state->altpath.blk[i].bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 state->altpath.active = 0;
2274}
2275
2276/*
2277 * Free a da-state structure.
2278 */
2279void
2280xfs_da_state_free(xfs_da_state_t *state)
2281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xfs_da_state_kill_altpath(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283#ifdef DEBUG
2284 memset((char *)state, 0, sizeof(*state));
2285#endif /* DEBUG */
2286 kmem_zone_free(xfs_da_state_zone, state);
2287}