blob: f9e9149de00951149015c62433b50e0d0984a159 [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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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 Scottfac80cc2006-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,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100750 args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 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 Scottfac80cc2006-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,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100841 blkno, -1, &bp, state->args->whichfork,
842 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (error)
844 return(error);
845 ASSERT(bp != NULL);
846
847 node = (xfs_da_intnode_t *)info;
848 count = state->node_ents;
849 count -= state->node_ents >> 2;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100850 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000851 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200852 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +1100853 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (count >= 0)
856 break; /* fits with at least 25% to spare */
857 }
858 if (i >= 2) {
859 *action = 0;
860 return(0);
861 }
862
863 /*
864 * Make altpath point to the block we want to keep (the lower
865 * numbered block) and path point to the block we want to drop.
866 */
867 memcpy(&state->altpath, &state->path, sizeof(state->path));
868 if (blkno < blk->blkno) {
869 error = xfs_da_path_shift(state, &state->altpath, forward,
870 0, &retval);
871 if (error) {
872 return(error);
873 }
874 if (retval) {
875 *action = 0;
876 return(0);
877 }
878 } else {
879 error = xfs_da_path_shift(state, &state->path, forward,
880 0, &retval);
881 if (error) {
882 return(error);
883 }
884 if (retval) {
885 *action = 0;
886 return(0);
887 }
888 }
889 *action = 1;
890 return(0);
891}
892
893/*
894 * Walk back up the tree adjusting hash values as necessary,
895 * when we stop making changes, return.
896 */
897void
898xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
899{
900 xfs_da_state_blk_t *blk;
901 xfs_da_intnode_t *node;
902 xfs_da_node_entry_t *btree;
903 xfs_dahash_t lasthash=0;
904 int level, count;
905
Dave Chinneree732592012-11-12 22:53:53 +1100906 trace_xfs_da_fixhashpath(state->args);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 level = path->active-1;
909 blk = &path->blk[ level ];
910 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 case XFS_ATTR_LEAF_MAGIC:
912 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
913 if (count == 0)
914 return;
915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
918 if (count == 0)
919 return;
920 break;
921 case XFS_DA_NODE_MAGIC:
922 lasthash = xfs_da_node_lasthash(blk->bp, &count);
923 if (count == 0)
924 return;
925 break;
926 }
927 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000928 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200929 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 btree = &node->btree[ blk->index ];
Nathan Scott403432d2006-03-17 17:29:46 +1100931 if (be32_to_cpu(btree->hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 blk->hashval = lasthash;
Nathan Scott403432d2006-03-17 17:29:46 +1100934 btree->hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000935 xfs_trans_log_buf(state->args->trans, blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
937
Nathan Scottfac80cc2006-03-17 17:29:56 +1100938 lasthash = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940}
941
942/*
943 * Remove an entry from an intermediate node.
944 */
945STATIC void
946xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
947{
948 xfs_da_intnode_t *node;
949 xfs_da_node_entry_t *btree;
950 int tmp;
951
Dave Chinner5a5881c2012-03-22 05:15:13 +0000952 trace_xfs_da_node_remove(state->args);
953
Dave Chinner1d9025e2012-06-22 18:50:14 +1000954 node = drop_blk->bp->b_addr;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100955 ASSERT(drop_blk->index < be16_to_cpu(node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ASSERT(drop_blk->index >= 0);
957
958 /*
959 * Copy over the offending entry, or just zero it out.
960 */
961 btree = &node->btree[drop_blk->index];
Nathan Scottfac80cc2006-03-17 17:29:56 +1100962 if (drop_blk->index < (be16_to_cpu(node->hdr.count)-1)) {
963 tmp = be16_to_cpu(node->hdr.count) - drop_blk->index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 tmp *= (uint)sizeof(xfs_da_node_entry_t);
965 memmove(btree, btree + 1, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000966 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 XFS_DA_LOGRANGE(node, btree, tmp));
Nathan Scottfac80cc2006-03-17 17:29:56 +1100968 btree = &node->btree[be16_to_cpu(node->hdr.count)-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000971 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800973 be16_add_cpu(&node->hdr.count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000974 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
976
977 /*
978 * Copy the last hash value from the block to propagate upwards.
979 */
980 btree--;
Nathan Scott403432d2006-03-17 17:29:46 +1100981 drop_blk->hashval = be32_to_cpu(btree->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
985 * Unbalance the btree elements between two intermediate nodes,
986 * move all Btree elements from one node into another.
987 */
988STATIC void
989xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
990 xfs_da_state_blk_t *save_blk)
991{
992 xfs_da_intnode_t *drop_node, *save_node;
993 xfs_da_node_entry_t *btree;
994 int tmp;
995 xfs_trans_t *tp;
996
Dave Chinner5a5881c2012-03-22 05:15:13 +0000997 trace_xfs_da_node_unbalance(state->args);
998
Dave Chinner1d9025e2012-06-22 18:50:14 +1000999 drop_node = drop_blk->bp->b_addr;
1000 save_node = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001001 ASSERT(drop_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
1002 ASSERT(save_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 tp = state->args->trans;
1004
1005 /*
1006 * If the dying block has lower hashvals, then move all the
1007 * elements in the remaining block up to make a hole.
1008 */
Nathan Scott403432d2006-03-17 17:29:46 +11001009 if ((be32_to_cpu(drop_node->btree[0].hashval) < be32_to_cpu(save_node->btree[ 0 ].hashval)) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001010 (be32_to_cpu(drop_node->btree[be16_to_cpu(drop_node->hdr.count)-1].hashval) <
1011 be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 {
Nathan Scottfac80cc2006-03-17 17:29:56 +11001013 btree = &save_node->btree[be16_to_cpu(drop_node->hdr.count)];
1014 tmp = be16_to_cpu(save_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 memmove(btree, &save_node->btree[0], tmp);
1016 btree = &save_node->btree[0];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001017 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cc2006-03-17 17:29:56 +11001019 (be16_to_cpu(save_node->hdr.count) + be16_to_cpu(drop_node->hdr.count)) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sizeof(xfs_da_node_entry_t)));
1021 } else {
Nathan Scottfac80cc2006-03-17 17:29:56 +11001022 btree = &save_node->btree[be16_to_cpu(save_node->hdr.count)];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001023 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cc2006-03-17 17:29:56 +11001025 be16_to_cpu(drop_node->hdr.count) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sizeof(xfs_da_node_entry_t)));
1027 }
1028
1029 /*
1030 * Move all the B-tree elements from drop_blk to save_blk.
1031 */
Nathan Scottfac80cc2006-03-17 17:29:56 +11001032 tmp = be16_to_cpu(drop_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 memcpy(btree, &drop_node->btree[0], tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001034 be16_add_cpu(&save_node->hdr.count, be16_to_cpu(drop_node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Dave Chinner1d9025e2012-06-22 18:50:14 +10001036 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1038 sizeof(save_node->hdr)));
1039
1040 /*
1041 * Save the last hashval in the remaining block for upward propagation.
1042 */
Nathan Scottfac80cc2006-03-17 17:29:56 +11001043 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 -07001044}
1045
1046/*========================================================================
1047 * Routines used for finding things in the Btree.
1048 *========================================================================*/
1049
1050/*
1051 * Walk down the Btree looking for a particular filename, filling
1052 * in the state structure as we go.
1053 *
1054 * We will set the state structure to point to each of the elements
1055 * in each of the nodes where either the hashval is or should be.
1056 *
1057 * We support duplicate hashval's so for each entry in the current
1058 * node that could contain the desired hashval, descend. This is a
1059 * pruned depth-first tree search.
1060 */
1061int /* error */
1062xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
1063{
1064 xfs_da_state_blk_t *blk;
1065 xfs_da_blkinfo_t *curr;
1066 xfs_da_intnode_t *node;
1067 xfs_da_node_entry_t *btree;
1068 xfs_dablk_t blkno;
1069 int probe, span, max, error, retval;
Nathan Scottd432c802006-09-28 11:03:44 +10001070 xfs_dahash_t hashval, btreehashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 xfs_da_args_t *args;
1072
1073 args = state->args;
1074
1075 /*
1076 * Descend thru the B-tree searching each level for the right
1077 * node to use, until the right hashval is found.
1078 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001079 blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 for (blk = &state->path.blk[0], state->path.active = 1;
1081 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1082 blk++, state->path.active++) {
1083 /*
1084 * Read the next node down in the tree.
1085 */
1086 blk->blkno = blkno;
1087 error = xfs_da_read_buf(args->trans, args->dp, blkno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11001088 -1, &blk->bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (error) {
1090 blk->blkno = 0;
1091 state->path.active--;
1092 return(error);
1093 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001094 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001095 blk->magic = be16_to_cpu(curr->magic);
1096 ASSERT(blk->magic == XFS_DA_NODE_MAGIC ||
1097 blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1098 blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /*
1101 * Search an intermediate node for a match.
1102 */
Nathan Scott89da0542006-03-17 17:28:40 +11001103 if (blk->magic == XFS_DA_NODE_MAGIC) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001104 node = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001105 max = be16_to_cpu(node->hdr.count);
Lachlan McIlroy1c91ad32007-02-10 18:35:27 +11001106 blk->hashval = be32_to_cpu(node->btree[max-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /*
1109 * Binary search. (note: small blocks will skip loop)
1110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 probe = span = max / 2;
1112 hashval = args->hashval;
1113 for (btree = &node->btree[probe]; span > 4;
1114 btree = &node->btree[probe]) {
1115 span /= 2;
Nathan Scottd432c802006-09-28 11:03:44 +10001116 btreehashval = be32_to_cpu(btree->hashval);
1117 if (btreehashval < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 probe += span;
Nathan Scottd432c802006-09-28 11:03:44 +10001119 else if (btreehashval > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 probe -= span;
1121 else
1122 break;
1123 }
1124 ASSERT((probe >= 0) && (probe < max));
Nathan Scott403432d2006-03-17 17:29:46 +11001125 ASSERT((span <= 4) || (be32_to_cpu(btree->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Since we may have duplicate hashval's, find the first
1129 * matching hashval in the node.
1130 */
Nathan Scott403432d2006-03-17 17:29:46 +11001131 while ((probe > 0) && (be32_to_cpu(btree->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 btree--;
1133 probe--;
1134 }
Nathan Scott403432d2006-03-17 17:29:46 +11001135 while ((probe < max) && (be32_to_cpu(btree->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 btree++;
1137 probe++;
1138 }
1139
1140 /*
1141 * Pick the right block to descend on.
1142 */
1143 if (probe == max) {
1144 blk->index = max-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001145 blkno = be32_to_cpu(node->btree[max-1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 } else {
1147 blk->index = probe;
Nathan Scott403432d2006-03-17 17:29:46 +11001148 blkno = be32_to_cpu(btree->before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
Nathan Scottd432c802006-09-28 11:03:44 +10001150 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1152 break;
Nathan Scottd432c802006-09-28 11:03:44 +10001153 } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
1155 break;
1156 }
1157 }
1158
1159 /*
1160 * A leaf block that ends in the hashval that we are interested in
1161 * (final hashval == search hashval) means that the next block may
1162 * contain more entries with the same hashval, shift upward to the
1163 * next leaf and keep searching.
1164 */
1165 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001166 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1168 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001169 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 retval = xfs_attr_leaf_lookup_int(blk->bp, args);
1171 blk->index = args->index;
1172 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001173 } else {
1174 ASSERT(0);
1175 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1178 (blk->hashval == args->hashval)) {
1179 error = xfs_da_path_shift(state, &state->path, 1, 1,
1180 &retval);
1181 if (error)
1182 return(error);
1183 if (retval == 0) {
1184 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001185 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* path_shift() gives ENOENT */
1187 retval = XFS_ERROR(ENOATTR);
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 break;
1191 }
1192 *result = retval;
1193 return(0);
1194}
1195
1196/*========================================================================
1197 * Utility routines.
1198 *========================================================================*/
1199
1200/*
1201 * Link a new block into a doubly linked list of blocks (of whatever type).
1202 */
1203int /* error */
1204xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
1205 xfs_da_state_blk_t *new_blk)
1206{
1207 xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
1208 xfs_da_args_t *args;
1209 int before=0, error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001210 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * Set up environment.
1214 */
1215 args = state->args;
1216 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001217 old_info = old_blk->bp->b_addr;
1218 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001220 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001222 ASSERT(old_blk->magic == be16_to_cpu(old_info->magic));
1223 ASSERT(new_blk->magic == be16_to_cpu(new_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 ASSERT(old_blk->magic == new_blk->magic);
1225
1226 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case XFS_ATTR_LEAF_MAGIC:
1228 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
1232 break;
1233 case XFS_DA_NODE_MAGIC:
1234 before = xfs_da_node_order(old_blk->bp, new_blk->bp);
1235 break;
1236 }
1237
1238 /*
1239 * Link blocks in appropriate order.
1240 */
1241 if (before) {
1242 /*
1243 * Link new block in before existing block.
1244 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001245 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001246 new_info->forw = cpu_to_be32(old_blk->blkno);
1247 new_info->back = old_info->back;
1248 if (old_info->back) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001250 be32_to_cpu(old_info->back),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001251 -1, &bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (error)
1253 return(error);
1254 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001255 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001256 ASSERT(be16_to_cpu(tmp_info->magic) == be16_to_cpu(old_info->magic));
1257 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1258 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001259 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Nathan Scott89da0542006-03-17 17:28:40 +11001261 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
1263 /*
1264 * Link new block in after existing block.
1265 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001266 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001267 new_info->forw = old_info->forw;
1268 new_info->back = cpu_to_be32(old_blk->blkno);
1269 if (old_info->forw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001271 be32_to_cpu(old_info->forw),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001272 -1, &bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (error)
1274 return(error);
1275 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001276 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001277 ASSERT(tmp_info->magic == old_info->magic);
1278 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1279 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001280 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Nathan Scott89da0542006-03-17 17:28:40 +11001282 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1286 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return(0);
1288}
1289
1290/*
1291 * Compare two intermediate nodes for "order".
1292 */
1293STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001294xfs_da_node_order(
1295 struct xfs_buf *node1_bp,
1296 struct xfs_buf *node2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 xfs_da_intnode_t *node1, *node2;
1299
Dave Chinner1d9025e2012-06-22 18:50:14 +10001300 node1 = node1_bp->b_addr;
1301 node2 = node2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001302 ASSERT(node1->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) &&
1303 node2->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +11001304 if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001305 ((be32_to_cpu(node2->btree[0].hashval) <
1306 be32_to_cpu(node1->btree[0].hashval)) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001307 (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
1308 be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return(1);
1310 }
1311 return(0);
1312}
1313
1314/*
1315 * Pick up the last hashvalue from an intermediate node.
1316 */
1317STATIC uint
Dave Chinner1d9025e2012-06-22 18:50:14 +10001318xfs_da_node_lasthash(
1319 struct xfs_buf *bp,
1320 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 xfs_da_intnode_t *node;
1323
Dave Chinner1d9025e2012-06-22 18:50:14 +10001324 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001325 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (count)
Nathan Scottfac80cc2006-03-17 17:29:56 +11001327 *count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (!node->hdr.count)
1329 return(0);
Nathan Scottfac80cc2006-03-17 17:29:56 +11001330 return be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333/*
1334 * Unlink a block from a doubly linked list of blocks.
1335 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001336STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1338 xfs_da_state_blk_t *save_blk)
1339{
1340 xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
1341 xfs_da_args_t *args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001342 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int error;
1344
1345 /*
1346 * Set up environment.
1347 */
1348 args = state->args;
1349 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001350 save_info = save_blk->bp->b_addr;
1351 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001353 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001355 ASSERT(save_blk->magic == be16_to_cpu(save_info->magic));
1356 ASSERT(drop_blk->magic == be16_to_cpu(drop_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001358 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1359 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1360 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1361 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
1364 * Unlink the leaf block from the doubly linked chain of leaves.
1365 */
Nathan Scott89da0542006-03-17 17:28:40 +11001366 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001367 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001368 save_info->back = drop_info->back;
1369 if (drop_info->back) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001371 be32_to_cpu(drop_info->back),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001372 -1, &bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (error)
1374 return(error);
1375 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001376 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001377 ASSERT(tmp_info->magic == save_info->magic);
1378 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1379 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001380 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001384 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001385 save_info->forw = drop_info->forw;
1386 if (drop_info->forw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 error = xfs_da_read_buf(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001388 be32_to_cpu(drop_info->forw),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001389 -1, &bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (error)
1391 return(error);
1392 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001393 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001394 ASSERT(tmp_info->magic == save_info->magic);
1395 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1396 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001397 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 }
1401
Dave Chinner1d9025e2012-06-22 18:50:14 +10001402 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return(0);
1404}
1405
1406/*
1407 * Move a path "forward" or "!forward" one block at the current level.
1408 *
1409 * This routine will adjust a "path" to point to the next block
1410 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1411 * Btree, including updating pointers to the intermediate nodes between
1412 * the new bottom and the root.
1413 */
1414int /* error */
1415xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
1416 int forward, int release, int *result)
1417{
1418 xfs_da_state_blk_t *blk;
1419 xfs_da_blkinfo_t *info;
1420 xfs_da_intnode_t *node;
1421 xfs_da_args_t *args;
1422 xfs_dablk_t blkno=0;
1423 int level, error;
1424
Dave Chinneree732592012-11-12 22:53:53 +11001425 trace_xfs_da_path_shift(state->args);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * Roll up the Btree looking for the first block where our
1429 * current index is not at the edge of the block. Note that
1430 * we skip the bottom layer because we want the sibling block.
1431 */
1432 args = state->args;
1433 ASSERT(args != NULL);
1434 ASSERT(path != NULL);
1435 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1436 level = (path->active-1) - 1; /* skip bottom layer in path */
1437 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1438 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001439 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001440 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +11001441 if (forward && (blk->index < be16_to_cpu(node->hdr.count)-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 blk->index++;
Nathan Scott403432d2006-03-17 17:29:46 +11001443 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 break;
1445 } else if (!forward && (blk->index > 0)) {
1446 blk->index--;
Nathan Scott403432d2006-03-17 17:29:46 +11001447 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 }
1450 }
1451 if (level < 0) {
1452 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001453 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return(0);
1455 }
1456
1457 /*
1458 * Roll down the edge of the subtree until we reach the
1459 * same depth we were at originally.
1460 */
1461 for (blk++, level++; level < path->active; blk++, level++) {
1462 /*
1463 * Release the old block.
1464 * (if it's dirty, trans won't actually let go)
1465 */
1466 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001467 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /*
1470 * Read the next child block.
1471 */
1472 blk->blkno = blkno;
1473 error = xfs_da_read_buf(args->trans, args->dp, blkno, -1,
Dave Chinner4bb20a82012-11-12 22:54:10 +11001474 &blk->bp, args->whichfork, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (error)
1476 return(error);
1477 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001478 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001479 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1480 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1481 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott89da0542006-03-17 17:28:40 +11001482 blk->magic = be16_to_cpu(info->magic);
1483 if (blk->magic == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 node = (xfs_da_intnode_t *)info;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001485 blk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (forward)
1487 blk->index = 0;
1488 else
Nathan Scottfac80cc2006-03-17 17:29:56 +11001489 blk->index = be16_to_cpu(node->hdr.count)-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001490 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 } else {
1492 ASSERT(level == path->active-1);
1493 blk->index = 0;
1494 switch(blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 case XFS_ATTR_LEAF_MAGIC:
1496 blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
1497 NULL);
1498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
1501 NULL);
1502 break;
1503 default:
1504 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001505 blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507 }
1508 }
1509 }
1510 *result = 0;
1511 return(0);
1512}
1513
1514
1515/*========================================================================
1516 * Utility routines.
1517 *========================================================================*/
1518
1519/*
1520 * Implement a simple hash on a character string.
1521 * Rotate the hash value by 7 bits, then XOR each character in.
1522 * This is implemented with some source-level loop unrolling.
1523 */
1524xfs_dahash_t
Christoph Hellwiga5687782009-02-09 08:37:39 +01001525xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 xfs_dahash_t hash;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /*
1530 * Do four characters at a time as long as we can.
1531 */
1532 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1533 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1534 (name[3] << 0) ^ rol32(hash, 7 * 4);
1535
1536 /*
1537 * Now do the rest of the characters.
1538 */
1539 switch (namelen) {
1540 case 3:
1541 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
1542 rol32(hash, 7 * 3);
1543 case 2:
1544 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
1545 case 1:
1546 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11001547 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return hash;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
Barry Naujok5163f952008-05-21 16:41:01 +10001552enum xfs_dacmp
1553xfs_da_compname(
1554 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11001555 const unsigned char *name,
1556 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10001557{
1558 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
1559 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
1560}
1561
1562static xfs_dahash_t
1563xfs_default_hashname(
1564 struct xfs_name *name)
1565{
1566 return xfs_da_hashname(name->name, name->len);
1567}
1568
1569const struct xfs_nameops xfs_default_nameops = {
1570 .hashname = xfs_default_hashname,
1571 .compname = xfs_da_compname
1572};
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574int
Christoph Hellwig77936d02011-07-13 13:43:49 +02001575xfs_da_grow_inode_int(
1576 struct xfs_da_args *args,
1577 xfs_fileoff_t *bno,
1578 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Christoph Hellwig77936d02011-07-13 13:43:49 +02001580 struct xfs_trans *tp = args->trans;
1581 struct xfs_inode *dp = args->dp;
1582 int w = args->whichfork;
1583 xfs_drfsbno_t nblks = dp->i_d.di_nblocks;
1584 struct xfs_bmbt_irec map, *mapp;
1585 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Find a spot in the file space to put the new block.
1589 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02001590 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
1591 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * Try mapping it in one filesystem block.
1596 */
1597 nmap = 1;
1598 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001599 error = xfs_bmapi_write(tp, dp, *bno, count,
1600 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 args->firstblock, args->total, &map, &nmap,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001602 args->flist);
1603 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ASSERT(nmap <= 1);
1607 if (nmap == 1) {
1608 mapp = &map;
1609 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001610 } else if (nmap == 0 && count > 1) {
1611 xfs_fileoff_t b;
1612 int c;
1613
1614 /*
1615 * If we didn't get it and the block might work if fragmented,
1616 * try without the CONTIG flag. Loop until we get it all.
1617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001619 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001621 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001622 error = xfs_bmapi_write(tp, dp, b, c,
1623 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 args->firstblock, args->total,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001625 &mapp[mapi], &nmap, args->flist);
1626 if (error)
1627 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (nmap < 1)
1629 break;
1630 mapi += nmap;
1631 b = mapp[mapi - 1].br_startoff +
1632 mapp[mapi - 1].br_blockcount;
1633 }
1634 } else {
1635 mapi = 0;
1636 mapp = NULL;
1637 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Count the blocks we got, make sure it matches the total.
1641 */
1642 for (i = 0, got = 0; i < mapi; i++)
1643 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001644 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02001646 *bno + count) {
1647 error = XFS_ERROR(ENOSPC);
1648 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001650
David Chinnera7444052008-10-30 17:38:12 +11001651 /* account for newly allocated blocks in reserved blocks total */
1652 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001653
1654out_free_map:
1655 if (mapp != &map)
1656 kmem_free(mapp);
1657 return error;
1658}
1659
1660/*
1661 * Add a block to the btree ahead of the file.
1662 * Return the new block number to the caller.
1663 */
1664int
1665xfs_da_grow_inode(
1666 struct xfs_da_args *args,
1667 xfs_dablk_t *new_blkno)
1668{
1669 xfs_fileoff_t bno;
1670 int count;
1671 int error;
1672
Dave Chinner5a5881c2012-03-22 05:15:13 +00001673 trace_xfs_da_grow_inode(args);
1674
Christoph Hellwig77936d02011-07-13 13:43:49 +02001675 if (args->whichfork == XFS_DATA_FORK) {
1676 bno = args->dp->i_mount->m_dirleafblk;
1677 count = args->dp->i_mount->m_dirblkfsbs;
1678 } else {
1679 bno = 0;
1680 count = 1;
1681 }
1682
1683 error = xfs_da_grow_inode_int(args, &bno, count);
1684 if (!error)
1685 *new_blkno = (xfs_dablk_t)bno;
1686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/*
1690 * Ick. We need to always be able to remove a btree block, even
1691 * if there's no space reservation because the filesystem is full.
1692 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1693 * It swaps the target block with the last block in the file. The
1694 * last block in the file can always be removed since it can't cause
1695 * a bmap btree split to do that.
1696 */
1697STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001698xfs_da_swap_lastblock(
1699 xfs_da_args_t *args,
1700 xfs_dablk_t *dead_blknop,
1701 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001704 struct xfs_buf *dead_buf, *last_buf, *sib_buf, *par_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 xfs_fileoff_t lastoff;
1706 xfs_inode_t *ip;
1707 xfs_trans_t *tp;
1708 xfs_mount_t *mp;
1709 int error, w, entno, level, dead_level;
1710 xfs_da_blkinfo_t *dead_info, *sib_info;
1711 xfs_da_intnode_t *par_node, *dead_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 xfs_dir2_leaf_t *dead_leaf2;
1713 xfs_dahash_t dead_hash;
1714
Dave Chinner5a5881c2012-03-22 05:15:13 +00001715 trace_xfs_da_swap_lastblock(args);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 dead_buf = *dead_bufp;
1718 dead_blkno = *dead_blknop;
1719 tp = args->trans;
1720 ip = args->dp;
1721 w = args->whichfork;
1722 ASSERT(w == XFS_DATA_FORK);
1723 mp = ip->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001724 lastoff = mp->m_dirfreeblk;
1725 error = xfs_bmap_last_before(tp, ip, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (error)
1727 return error;
1728 if (unlikely(lastoff == 0)) {
1729 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
1730 mp);
1731 return XFS_ERROR(EFSCORRUPTED);
1732 }
1733 /*
1734 * Read the last block in the btree space.
1735 */
1736 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001737 error = xfs_da_read_buf(tp, ip, last_blkno, -1, &last_buf, w, NULL);
1738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return error;
1740 /*
1741 * Copy the last block into the dead buffer and log it.
1742 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001743 memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
1744 xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
1745 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * Get values from the moved block.
1748 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001749 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
1751 dead_level = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001752 dead_hash = be32_to_cpu(dead_leaf2->ents[be16_to_cpu(dead_leaf2->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001754 ASSERT(dead_info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 dead_node = (xfs_da_intnode_t *)dead_info;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001756 dead_level = be16_to_cpu(dead_node->hdr.level);
1757 dead_hash = be32_to_cpu(dead_node->btree[be16_to_cpu(dead_node->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 sib_buf = par_buf = NULL;
1760 /*
1761 * If the moved block has a left sibling, fix up the pointers.
1762 */
Nathan Scott89da0542006-03-17 17:28:40 +11001763 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinner4bb20a82012-11-12 22:54:10 +11001764 error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w,
1765 NULL);
1766 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001768 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001770 be32_to_cpu(sib_info->forw) != last_blkno ||
1771 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1773 XFS_ERRLEVEL_LOW, mp);
1774 error = XFS_ERROR(EFSCORRUPTED);
1775 goto done;
1776 }
Nathan Scott89da0542006-03-17 17:28:40 +11001777 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001778 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
1780 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 sib_buf = NULL;
1782 }
1783 /*
1784 * If the moved block has a right sibling, fix up the pointers.
1785 */
Nathan Scott89da0542006-03-17 17:28:40 +11001786 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinner4bb20a82012-11-12 22:54:10 +11001787 error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w,
1788 NULL);
1789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001791 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001793 be32_to_cpu(sib_info->back) != last_blkno ||
1794 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1796 XFS_ERRLEVEL_LOW, mp);
1797 error = XFS_ERROR(EFSCORRUPTED);
1798 goto done;
1799 }
Nathan Scott89da0542006-03-17 17:28:40 +11001800 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001801 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
1803 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 sib_buf = NULL;
1805 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001806 par_blkno = mp->m_dirleafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 level = -1;
1808 /*
1809 * Walk down the tree looking for the parent of the moved block.
1810 */
1811 for (;;) {
Dave Chinner4bb20a82012-11-12 22:54:10 +11001812 error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w,
1813 NULL);
1814 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001816 par_node = par_buf->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001817 if (unlikely(par_node->hdr.info.magic !=
1818 cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001819 (level >= 0 && level != be16_to_cpu(par_node->hdr.level) + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1821 XFS_ERRLEVEL_LOW, mp);
1822 error = XFS_ERROR(EFSCORRUPTED);
1823 goto done;
1824 }
Nathan Scottfac80cc2006-03-17 17:29:56 +11001825 level = be16_to_cpu(par_node->hdr.level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 for (entno = 0;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001827 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001828 be32_to_cpu(par_node->btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 entno++)
1830 continue;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001831 if (unlikely(entno == be16_to_cpu(par_node->hdr.count))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1833 XFS_ERRLEVEL_LOW, mp);
1834 error = XFS_ERROR(EFSCORRUPTED);
1835 goto done;
1836 }
Nathan Scott403432d2006-03-17 17:29:46 +11001837 par_blkno = be32_to_cpu(par_node->btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (level == dead_level + 1)
1839 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001840 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 par_buf = NULL;
1842 }
1843 /*
1844 * We're in the right parent block.
1845 * Look for the right entry.
1846 */
1847 for (;;) {
1848 for (;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001849 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001850 be32_to_cpu(par_node->btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 entno++)
1852 continue;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001853 if (entno < be16_to_cpu(par_node->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
Nathan Scott89da0542006-03-17 17:28:40 +11001855 par_blkno = be32_to_cpu(par_node->hdr.info.forw);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001856 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 par_buf = NULL;
1858 if (unlikely(par_blkno == 0)) {
1859 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1860 XFS_ERRLEVEL_LOW, mp);
1861 error = XFS_ERROR(EFSCORRUPTED);
1862 goto done;
1863 }
Dave Chinner4bb20a82012-11-12 22:54:10 +11001864 error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w,
1865 NULL);
1866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001868 par_node = par_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (unlikely(
Nathan Scottfac80cc2006-03-17 17:29:56 +11001870 be16_to_cpu(par_node->hdr.level) != level ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001871 par_node->hdr.info.magic != cpu_to_be16(XFS_DA_NODE_MAGIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1873 XFS_ERRLEVEL_LOW, mp);
1874 error = XFS_ERROR(EFSCORRUPTED);
1875 goto done;
1876 }
1877 entno = 0;
1878 }
1879 /*
1880 * Update the parent entry pointing to the moved block.
1881 */
Nathan Scott403432d2006-03-17 17:29:46 +11001882 par_node->btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001883 xfs_trans_log_buf(tp, par_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
1885 sizeof(par_node->btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *dead_blknop = last_blkno;
1887 *dead_bufp = last_buf;
1888 return 0;
1889done:
1890 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001891 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001893 xfs_trans_brelse(tp, sib_buf);
1894 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return error;
1896}
1897
1898/*
1899 * Remove a btree block from a directory or attribute.
1900 */
1901int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001902xfs_da_shrink_inode(
1903 xfs_da_args_t *args,
1904 xfs_dablk_t dead_blkno,
1905 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 xfs_inode_t *dp;
1908 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 xfs_trans_t *tp;
1910 xfs_mount_t *mp;
1911
Dave Chinner5a5881c2012-03-22 05:15:13 +00001912 trace_xfs_da_shrink_inode(args);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dp = args->dp;
1915 w = args->whichfork;
1916 tp = args->trans;
1917 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001918 if (w == XFS_DATA_FORK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 count = mp->m_dirblkfsbs;
1920 else
1921 count = 1;
1922 for (;;) {
1923 /*
1924 * Remove extents. If we get ENOSPC for a dir we have to move
1925 * the last block to the place we want to kill.
1926 */
1927 if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
Eric Sandeen9d87c312009-01-14 23:22:07 -06001928 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10001929 0, args->firstblock, args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 &done)) == ENOSPC) {
1931 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
1934 &dead_buf)))
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001935 break;
1936 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001940 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return error;
1942}
1943
1944/*
1945 * See if the mapping(s) for this btree block are valid, i.e.
1946 * don't contain holes, are logically contiguous, and cover the whole range.
1947 */
1948STATIC int
1949xfs_da_map_covers_blocks(
1950 int nmap,
1951 xfs_bmbt_irec_t *mapp,
1952 xfs_dablk_t bno,
1953 int count)
1954{
1955 int i;
1956 xfs_fileoff_t off;
1957
1958 for (i = 0, off = bno; i < nmap; i++) {
1959 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
1960 mapp[i].br_startblock == DELAYSTARTBLOCK) {
1961 return 0;
1962 }
1963 if (off != mapp[i].br_startoff) {
1964 return 0;
1965 }
1966 off += mapp[i].br_blockcount;
1967 }
1968 return off == bno + count;
1969}
1970
1971/*
Dave Chinner36054312012-06-22 18:50:13 +10001972 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
1973 *
1974 * For the single map case, it is assumed that the caller has provided a pointer
1975 * to a valid xfs_buf_map. For the multiple map case, this function will
1976 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
1977 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 */
Dave Chinner36054312012-06-22 18:50:13 +10001979static int
1980xfs_buf_map_from_irec(
1981 struct xfs_mount *mp,
1982 struct xfs_buf_map **mapp,
1983 unsigned int *nmaps,
1984 struct xfs_bmbt_irec *irecs,
1985 unsigned int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Dave Chinner36054312012-06-22 18:50:13 +10001987 struct xfs_buf_map *map;
1988 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Dave Chinner36054312012-06-22 18:50:13 +10001990 ASSERT(*nmaps == 1);
1991 ASSERT(nirecs >= 1);
1992
1993 if (nirecs > 1) {
1994 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_SLEEP);
1995 if (!map)
1996 return ENOMEM;
1997 *mapp = map;
1998 }
1999
2000 *nmaps = nirecs;
2001 map = *mapp;
2002 for (i = 0; i < *nmaps; i++) {
2003 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2004 irecs[i].br_startblock != HOLESTARTBLOCK);
2005 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2006 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2007 }
2008 return 0;
2009}
2010
2011/*
2012 * Map the block we are given ready for reading. There are three possible return
2013 * values:
2014 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2015 * caller knows not to execute a subsequent read.
2016 * 0 - if we mapped the block successfully
2017 * >0 - positive error number if there was an error.
2018 */
2019static int
2020xfs_dabuf_map(
2021 struct xfs_trans *trans,
2022 struct xfs_inode *dp,
2023 xfs_dablk_t bno,
2024 xfs_daddr_t mappedbno,
2025 int whichfork,
2026 struct xfs_buf_map **map,
2027 int *nmaps)
2028{
2029 struct xfs_mount *mp = dp->i_mount;
2030 int nfsb;
2031 int error = 0;
2032 struct xfs_bmbt_irec irec;
2033 struct xfs_bmbt_irec *irecs = &irec;
2034 int nirecs;
2035
2036 ASSERT(map && *map);
2037 ASSERT(*nmaps == 1);
2038
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002039 nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
Dave Chinner36054312012-06-22 18:50:13 +10002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Caller doesn't have a mapping. -2 means don't complain
2043 * if we land in a hole.
2044 */
2045 if (mappedbno == -1 || mappedbno == -2) {
2046 /*
2047 * Optimize the one-block case.
2048 */
Dave Chinner36054312012-06-22 18:50:13 +10002049 if (nfsb != 1)
2050 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002051
Dave Chinner36054312012-06-22 18:50:13 +10002052 nirecs = nfsb;
2053 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2054 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002055 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002058 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2059 irecs->br_startoff = (xfs_fileoff_t)bno;
2060 irecs->br_blockcount = nfsb;
2061 irecs->br_state = 0;
2062 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Dave Chinner36054312012-06-22 18:50:13 +10002064
2065 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2066 error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (unlikely(error == EFSCORRUPTED)) {
2068 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002069 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002070 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2071 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002073 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002074 xfs_alert(mp,
2075"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 i,
Dave Chinner36054312012-06-22 18:50:13 +10002077 (long long)irecs[i].br_startoff,
2078 (long long)irecs[i].br_startblock,
2079 (long long)irecs[i].br_blockcount,
2080 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082 }
2083 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2084 XFS_ERRLEVEL_LOW, mp);
2085 }
Dave Chinner36054312012-06-22 18:50:13 +10002086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Dave Chinner36054312012-06-22 18:50:13 +10002088 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2089out:
2090 if (irecs != &irec)
2091 kmem_free(irecs);
2092 return error;
2093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Dave Chinner36054312012-06-22 18:50:13 +10002095/*
2096 * Get a buffer for the dir/attr block.
2097 */
2098int
2099xfs_da_get_buf(
2100 struct xfs_trans *trans,
2101 struct xfs_inode *dp,
2102 xfs_dablk_t bno,
2103 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002104 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002105 int whichfork)
2106{
2107 struct xfs_buf *bp;
2108 struct xfs_buf_map map;
2109 struct xfs_buf_map *mapp;
2110 int nmap;
2111 int error;
2112
2113 *bpp = NULL;
2114 mapp = &map;
2115 nmap = 1;
2116 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2117 &mapp, &nmap);
2118 if (error) {
2119 /* mapping a hole is not an error, but we don't continue */
2120 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002122 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Dave Chinner36054312012-06-22 18:50:13 +10002124
2125 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2126 mapp, nmap, 0);
2127 error = bp ? bp->b_error : XFS_ERROR(EIO);
2128 if (error) {
2129 xfs_trans_brelse(trans, bp);
2130 goto out_free;
2131 }
2132
Dave Chinner1d9025e2012-06-22 18:50:14 +10002133 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002134
2135out_free:
2136 if (mapp != &map)
2137 kmem_free(mapp);
2138
2139 return error;
2140}
2141
2142/*
2143 * Get a buffer for the dir/attr block, fill in the contents.
2144 */
2145int
2146xfs_da_read_buf(
2147 struct xfs_trans *trans,
2148 struct xfs_inode *dp,
2149 xfs_dablk_t bno,
2150 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002151 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002152 int whichfork,
2153 xfs_buf_iodone_t verifier)
Dave Chinner36054312012-06-22 18:50:13 +10002154{
2155 struct xfs_buf *bp;
2156 struct xfs_buf_map map;
2157 struct xfs_buf_map *mapp;
2158 int nmap;
2159 int error;
2160
2161 *bpp = NULL;
2162 mapp = &map;
2163 nmap = 1;
2164 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2165 &mapp, &nmap);
2166 if (error) {
2167 /* mapping a hole is not an error, but we don't continue */
2168 if (error == -1)
2169 error = 0;
2170 goto out_free;
2171 }
2172
2173 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2174 dp->i_mount->m_ddev_targp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002175 mapp, nmap, 0, &bp, verifier);
Dave Chinner36054312012-06-22 18:50:13 +10002176 if (error)
2177 goto out_free;
2178
2179 if (whichfork == XFS_ATTR_FORK)
2180 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 else
Dave Chinner36054312012-06-22 18:50:13 +10002182 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
Dave Chinner36054312012-06-22 18:50:13 +10002185 * This verification code will be moved to a CRC verification callback
2186 * function so just leave it here unchanged until then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
Dave Chinner36054312012-06-22 18:50:13 +10002188 {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002189 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
2190 xfs_dir2_free_t *free = bp->b_addr;
2191 xfs_da_blkinfo_t *info = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 uint magic, magic1;
Dave Chinner36054312012-06-22 18:50:13 +10002193 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Nathan Scott89da0542006-03-17 17:28:40 +11002195 magic = be16_to_cpu(info->magic);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002196 magic1 = be32_to_cpu(hdr->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (unlikely(
2198 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 (magic != XFS_ATTR_LEAF_MAGIC) &&
2200 (magic != XFS_DIR2_LEAF1_MAGIC) &&
2201 (magic != XFS_DIR2_LEAFN_MAGIC) &&
2202 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
2203 (magic1 != XFS_DIR2_DATA_MAGIC) &&
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002204 (free->hdr.magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 mp, XFS_ERRTAG_DA_READ_BUF,
2206 XFS_RANDOM_DA_READ_BUF))) {
Dave Chinner36054312012-06-22 18:50:13 +10002207 trace_xfs_da_btree_corrupt(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2209 XFS_ERRLEVEL_LOW, mp, info);
2210 error = XFS_ERROR(EFSCORRUPTED);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002211 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002212 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002215 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002216out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002218 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221}
2222
2223/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 * Readahead the dir/attr block.
2225 */
2226xfs_daddr_t
2227xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002228 struct xfs_trans *trans,
2229 struct xfs_inode *dp,
2230 xfs_dablk_t bno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002231 int whichfork,
2232 xfs_buf_iodone_t verifier)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Dave Chinner36054312012-06-22 18:50:13 +10002234 xfs_daddr_t mappedbno = -1;
2235 struct xfs_buf_map map;
2236 struct xfs_buf_map *mapp;
2237 int nmap;
2238 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Dave Chinner36054312012-06-22 18:50:13 +10002240 mapp = &map;
2241 nmap = 1;
2242 error = xfs_dabuf_map(trans, dp, bno, -1, whichfork,
2243 &mapp, &nmap);
2244 if (error) {
2245 /* mapping a hole is not an error, but we don't continue */
2246 if (error == -1)
2247 error = 0;
2248 goto out_free;
2249 }
2250
2251 mappedbno = mapp[0].bm_bn;
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002252 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, NULL);
Dave Chinner36054312012-06-22 18:50:13 +10002253
2254out_free:
2255 if (mapp != &map)
2256 kmem_free(mapp);
2257
2258 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return -1;
Dave Chinner36054312012-06-22 18:50:13 +10002260 return mappedbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265/*
2266 * Allocate a dir-state structure.
2267 * We don't put them on the stack since they're large.
2268 */
2269xfs_da_state_t *
2270xfs_da_state_alloc(void)
2271{
Christoph Hellwigf41d7fb2009-07-18 18:14:55 -04002272 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
2275/*
2276 * Kill the altpath contents of a da-state structure.
2277 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002278STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279xfs_da_state_kill_altpath(xfs_da_state_t *state)
2280{
2281 int i;
2282
Dave Chinner1d9025e2012-06-22 18:50:14 +10002283 for (i = 0; i < state->altpath.active; i++)
2284 state->altpath.blk[i].bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 state->altpath.active = 0;
2286}
2287
2288/*
2289 * Free a da-state structure.
2290 */
2291void
2292xfs_da_state_free(xfs_da_state_t *state)
2293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 xfs_da_state_kill_altpath(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295#ifdef DEBUG
2296 memset((char *)state, 0, sizeof(*state));
2297#endif /* DEBUG */
2298 kmem_zone_free(xfs_da_state_zone, state);
2299}