blob: 2f23b14e3adf02a88df79c13d6038ae1891495fe [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
Dave Chinnerd9392a42012-11-12 22:54:17 +110094static void
Dave Chinner612cfbf2012-11-14 17:52:32 +110095xfs_da_node_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +110096 struct xfs_buf *bp)
97{
98 struct xfs_mount *mp = bp->b_target->bt_mount;
99 struct xfs_da_node_hdr *hdr = bp->b_addr;
100 int block_ok = 0;
101
102 block_ok = hdr->info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC);
103 block_ok = block_ok &&
104 be16_to_cpu(hdr->level) > 0 &&
105 be16_to_cpu(hdr->count) > 0 ;
106 if (!block_ok) {
107 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
108 xfs_buf_ioerror(bp, EFSCORRUPTED);
109 }
110
Dave Chinnerd9392a42012-11-12 22:54:17 +1100111}
112
113static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100114xfs_da_node_write_verify(
115 struct xfs_buf *bp)
116{
117 xfs_da_node_verify(bp);
118}
119
Dave Chinner1813dd62012-11-14 17:54:40 +1100120/*
121 * leaf/node format detection on trees is sketchy, so a node read can be done on
122 * leaf level blocks when detection identifies the tree as a node format tree
123 * incorrectly. In this case, we need to swap the verifier to match the correct
124 * format of the block being read.
125 */
Dave Chinner612cfbf2012-11-14 17:52:32 +1100126static void
127xfs_da_node_read_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100128 struct xfs_buf *bp)
129{
130 struct xfs_mount *mp = bp->b_target->bt_mount;
131 struct xfs_da_blkinfo *info = bp->b_addr;
132
133 switch (be16_to_cpu(info->magic)) {
134 case XFS_DA_NODE_MAGIC:
Dave Chinner612cfbf2012-11-14 17:52:32 +1100135 xfs_da_node_verify(bp);
136 break;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100137 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner1813dd62012-11-14 17:54:40 +1100138 bp->b_ops = &xfs_attr_leaf_buf_ops;
139 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100140 return;
141 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000142 case XFS_DIR3_LEAFN_MAGIC:
143 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100144 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100145 return;
146 default:
Dave Chinner612cfbf2012-11-14 17:52:32 +1100147 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW,
148 mp, info);
149 xfs_buf_ioerror(bp, EFSCORRUPTED);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100150 break;
151 }
Dave Chinnerd9392a42012-11-12 22:54:17 +1100152}
153
Dave Chinner1813dd62012-11-14 17:54:40 +1100154const struct xfs_buf_ops xfs_da_node_buf_ops = {
155 .verify_read = xfs_da_node_read_verify,
156 .verify_write = xfs_da_node_write_verify,
157};
158
159
Dave Chinnerd9392a42012-11-12 22:54:17 +1100160int
161xfs_da_node_read(
162 struct xfs_trans *tp,
163 struct xfs_inode *dp,
164 xfs_dablk_t bno,
165 xfs_daddr_t mappedbno,
166 struct xfs_buf **bpp,
167 int which_fork)
168{
169 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100170 which_fork, &xfs_da_node_buf_ops);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*========================================================================
174 * Routines used for growing the Btree.
175 *========================================================================*/
176
177/*
178 * Create the initial contents of an intermediate node.
179 */
180int
181xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000182 struct xfs_buf **bpp, int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 xfs_da_intnode_t *node;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000185 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int error;
187 xfs_trans_t *tp;
188
Dave Chinner5a5881c2012-03-22 05:15:13 +0000189 trace_xfs_da_node_create(args);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 tp = args->trans;
192 error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
193 if (error)
194 return(error);
195 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000196 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 node->hdr.info.forw = 0;
198 node->hdr.info.back = 0;
Nathan Scott89da0542006-03-17 17:28:40 +1100199 node->hdr.info.magic = cpu_to_be16(XFS_DA_NODE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 node->hdr.info.pad = 0;
201 node->hdr.count = 0;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100202 node->hdr.level = cpu_to_be16(level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Dave Chinner1d9025e2012-06-22 18:50:14 +1000204 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
206
Dave Chinner1813dd62012-11-14 17:54:40 +1100207 bp->b_ops = &xfs_da_node_buf_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *bpp = bp;
209 return(0);
210}
211
212/*
213 * Split a leaf node, rebalance, then possibly split
214 * intermediate nodes, rebalance, etc.
215 */
216int /* error */
217xfs_da_split(xfs_da_state_t *state)
218{
219 xfs_da_state_blk_t *oldblk, *newblk, *addblk;
220 xfs_da_intnode_t *node;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000221 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int max, action, error, i;
223
Dave Chinner5a5881c2012-03-22 05:15:13 +0000224 trace_xfs_da_split(state->args);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
227 * Walk back up the tree splitting/inserting/adjusting as necessary.
228 * If we need to insert and there isn't room, split the node, then
229 * decide which fragment to insert the new block from below into.
230 * Note that we may split the root this way, but we need more fixup.
231 */
232 max = state->path.active - 1;
233 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
234 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000235 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 addblk = &state->path.blk[max]; /* initial dummy value */
238 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
239 oldblk = &state->path.blk[i];
240 newblk = &state->altpath.blk[i];
241
242 /*
243 * If a leaf node then
244 * Allocate a new leaf node, then rebalance across them.
245 * else if an intermediate node then
246 * We split on the last layer, must we split the node?
247 */
248 switch (oldblk->magic) {
249 case XFS_ATTR_LEAF_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 error = xfs_attr_leaf_split(state, oldblk, newblk);
251 if ((error != 0) && (error != ENOSPC)) {
252 return(error); /* GROT: attr is inconsistent */
253 }
254 if (!error) {
255 addblk = newblk;
256 break;
257 }
258 /*
259 * Entry wouldn't fit, split the leaf again.
260 */
261 state->extravalid = 1;
262 if (state->inleaf) {
263 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000264 trace_xfs_attr_leaf_split_before(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 error = xfs_attr_leaf_split(state, oldblk,
266 &state->extrablk);
267 } else {
268 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000269 trace_xfs_attr_leaf_split_after(state->args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 error = xfs_attr_leaf_split(state, newblk,
271 &state->extrablk);
272 }
273 if (error)
274 return(error); /* GROT: attr inconsistent */
275 addblk = newblk;
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 error = xfs_dir2_leafn_split(state, oldblk, newblk);
279 if (error)
280 return error;
281 addblk = newblk;
282 break;
283 case XFS_DA_NODE_MAGIC:
284 error = xfs_da_node_split(state, oldblk, newblk, addblk,
285 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 addblk->bp = NULL;
287 if (error)
288 return(error); /* GROT: dir is inconsistent */
289 /*
290 * Record the newly split block for the next time thru?
291 */
292 if (action)
293 addblk = newblk;
294 else
295 addblk = NULL;
296 break;
297 }
298
299 /*
300 * Update the btree to show the new hashval for this child.
301 */
302 xfs_da_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304 if (!addblk)
305 return(0);
306
307 /*
308 * Split the root node.
309 */
310 ASSERT(state->path.active == 0);
311 oldblk = &state->path.blk[0];
312 error = xfs_da_root_split(state, oldblk, addblk);
313 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 addblk->bp = NULL;
315 return(error); /* GROT: dir is inconsistent */
316 }
317
318 /*
319 * Update pointers to the node which used to be block 0 and
320 * just got bumped because of the addition of a new root node.
321 * There might be three blocks involved if a double split occurred,
322 * and the original block 0 could be at any position in the list.
323 */
324
Dave Chinner1d9025e2012-06-22 18:50:14 +1000325 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (node->hdr.info.forw) {
Nathan Scott89da0542006-03-17 17:28:40 +1100327 if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 bp = addblk->bp;
329 } else {
330 ASSERT(state->extravalid);
331 bp = state->extrablk.bp;
332 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000333 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100334 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000335 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 XFS_DA_LOGRANGE(node, &node->hdr.info,
337 sizeof(node->hdr.info)));
338 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000339 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100340 if (node->hdr.info.back) {
341 if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 bp = addblk->bp;
343 } else {
344 ASSERT(state->extravalid);
345 bp = state->extrablk.bp;
346 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000347 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100348 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000349 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 XFS_DA_LOGRANGE(node, &node->hdr.info,
351 sizeof(node->hdr.info)));
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 addblk->bp = NULL;
354 return(0);
355}
356
357/*
358 * Split the root. We have to create a new root and point to the two
359 * parts (the split old root) that we just created. Copy block zero to
360 * the EOF, extending the inode in process.
361 */
362STATIC int /* error */
363xfs_da_root_split(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
364 xfs_da_state_blk_t *blk2)
365{
366 xfs_da_intnode_t *node, *oldroot;
367 xfs_da_args_t *args;
368 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000369 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int error, size;
371 xfs_inode_t *dp;
372 xfs_trans_t *tp;
373 xfs_mount_t *mp;
374 xfs_dir2_leaf_t *leaf;
375
Dave Chinner5a5881c2012-03-22 05:15:13 +0000376 trace_xfs_da_root_split(state->args);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Copy the existing (incorrect) block from the root node position
380 * to a free space somewhere.
381 */
382 args = state->args;
383 ASSERT(args != NULL);
384 error = xfs_da_grow_inode(args, &blkno);
385 if (error)
386 return(error);
387 dp = args->dp;
388 tp = args->trans;
389 mp = state->mp;
390 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
391 if (error)
392 return(error);
393 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000394 node = bp->b_addr;
395 oldroot = blk1->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200396 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC)) {
Nathan Scottfac80cc2006-03-17 17:29:56 +1100397 size = (int)((char *)&oldroot->btree[be16_to_cpu(oldroot->hdr.count)] -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 (char *)oldroot);
399 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000400 struct xfs_dir3_icleaf_hdr leafhdr;
401 struct xfs_dir2_leaf_entry *ents;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 leaf = (xfs_dir2_leaf_t *)oldroot;
Dave Chinner24df33b2013-04-12 07:30:21 +1000404 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
405 ents = xfs_dir3_leaf_ents_p(leaf);
406
407 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
408 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
409 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000411 /* XXX: can't just copy CRC headers from one block to another */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 memcpy(node, oldroot, size);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000413 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100414
Dave Chinner1813dd62012-11-14 17:54:40 +1100415 bp->b_ops = blk1->bp->b_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 blk1->bp = bp;
417 blk1->blkno = blkno;
418
419 /*
420 * Set up the new root node.
421 */
422 error = xfs_da_node_create(args,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000423 (args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,
Nathan Scottfac80cc2006-03-17 17:29:56 +1100424 be16_to_cpu(node->hdr.level) + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (error)
426 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000427 node = bp->b_addr;
Nathan Scott403432d2006-03-17 17:29:46 +1100428 node->btree[0].hashval = cpu_to_be32(blk1->hashval);
429 node->btree[0].before = cpu_to_be32(blk1->blkno);
430 node->btree[1].hashval = cpu_to_be32(blk2->hashval);
431 node->btree[1].before = cpu_to_be32(blk2->blkno);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100432 node->hdr.count = cpu_to_be16(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000435 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
436 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ASSERT(blk1->blkno >= mp->m_dirleafblk &&
438 blk1->blkno < mp->m_dirfreeblk);
439 ASSERT(blk2->blkno >= mp->m_dirleafblk &&
440 blk2->blkno < mp->m_dirfreeblk);
441 }
442#endif
443
444 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000445 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 XFS_DA_LOGRANGE(node, node->btree,
447 sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return(0);
450}
451
452/*
453 * Split the node, rebalance, then add the new entry.
454 */
455STATIC int /* error */
456xfs_da_node_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
457 xfs_da_state_blk_t *newblk,
458 xfs_da_state_blk_t *addblk,
459 int treelevel, int *result)
460{
461 xfs_da_intnode_t *node;
462 xfs_dablk_t blkno;
463 int newcount, error;
464 int useextra;
465
Dave Chinner5a5881c2012-03-22 05:15:13 +0000466 trace_xfs_da_node_split(state->args);
467
Dave Chinner1d9025e2012-06-22 18:50:14 +1000468 node = oldblk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200469 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000472 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000474 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 newcount = 1 + useextra;
476 /*
477 * Do we have to split the node?
478 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100479 if ((be16_to_cpu(node->hdr.count) + newcount) > state->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * Allocate a new node, add to the doubly linked chain of
482 * nodes, then move some of our excess entries into it.
483 */
484 error = xfs_da_grow_inode(state->args, &blkno);
485 if (error)
486 return(error); /* GROT: dir is inconsistent */
487
488 error = xfs_da_node_create(state->args, blkno, treelevel,
489 &newblk->bp, state->args->whichfork);
490 if (error)
491 return(error); /* GROT: dir is inconsistent */
492 newblk->blkno = blkno;
493 newblk->magic = XFS_DA_NODE_MAGIC;
494 xfs_da_node_rebalance(state, oldblk, newblk);
495 error = xfs_da_blk_link(state, oldblk, newblk);
496 if (error)
497 return(error);
498 *result = 1;
499 } else {
500 *result = 0;
501 }
502
503 /*
504 * Insert the new entry(s) into the correct block
505 * (updating last hashval in the process).
506 *
507 * xfs_da_node_add() inserts BEFORE the given index,
508 * and as a result of using node_lookup_int() we always
509 * point to a valid entry (not after one), but a split
510 * operation always results in a new block whose hashvals
511 * FOLLOW the current block.
512 *
513 * If we had double-split op below us, then add the extra block too.
514 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000515 node = oldblk->bp->b_addr;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100516 if (oldblk->index <= be16_to_cpu(node->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 oldblk->index++;
518 xfs_da_node_add(state, oldblk, addblk);
519 if (useextra) {
520 if (state->extraafter)
521 oldblk->index++;
522 xfs_da_node_add(state, oldblk, &state->extrablk);
523 state->extravalid = 0;
524 }
525 } else {
526 newblk->index++;
527 xfs_da_node_add(state, newblk, addblk);
528 if (useextra) {
529 if (state->extraafter)
530 newblk->index++;
531 xfs_da_node_add(state, newblk, &state->extrablk);
532 state->extravalid = 0;
533 }
534 }
535
536 return(0);
537}
538
539/*
540 * Balance the btree elements between two intermediate nodes,
541 * usually one full and one empty.
542 *
543 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
544 */
545STATIC void
546xfs_da_node_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
547 xfs_da_state_blk_t *blk2)
548{
549 xfs_da_intnode_t *node1, *node2, *tmpnode;
550 xfs_da_node_entry_t *btree_s, *btree_d;
551 int count, tmp;
552 xfs_trans_t *tp;
553
Dave Chinner5a5881c2012-03-22 05:15:13 +0000554 trace_xfs_da_node_rebalance(state->args);
555
Dave Chinner1d9025e2012-06-22 18:50:14 +1000556 node1 = blk1->bp->b_addr;
557 node2 = blk2->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Figure out how many entries need to move, and in which direction.
560 * Swap the nodes around if that makes it simpler.
561 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100562 if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
Nathan Scott403432d2006-03-17 17:29:46 +1100563 ((be32_to_cpu(node2->btree[0].hashval) < be32_to_cpu(node1->btree[0].hashval)) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +1100564 (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
565 be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 tmpnode = node1;
567 node1 = node2;
568 node2 = tmpnode;
569 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200570 ASSERT(node1->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
571 ASSERT(node2->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +1100572 count = (be16_to_cpu(node1->hdr.count) - be16_to_cpu(node2->hdr.count)) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (count == 0)
574 return;
575 tp = state->args->trans;
576 /*
577 * Two cases: high-to-low and low-to-high.
578 */
579 if (count > 0) {
580 /*
581 * Move elements in node2 up to make a hole.
582 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100583 if ((tmp = be16_to_cpu(node2->hdr.count)) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 tmp *= (uint)sizeof(xfs_da_node_entry_t);
585 btree_s = &node2->btree[0];
586 btree_d = &node2->btree[count];
587 memmove(btree_d, btree_s, tmp);
588 }
589
590 /*
591 * Move the req'd B-tree elements from high in node1 to
592 * low in node2.
593 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800594 be16_add_cpu(&node2->hdr.count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Nathan Scottfac80cc2006-03-17 17:29:56 +1100596 btree_s = &node1->btree[be16_to_cpu(node1->hdr.count) - count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 btree_d = &node2->btree[0];
598 memcpy(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800599 be16_add_cpu(&node1->hdr.count, -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } else {
601 /*
602 * Move the req'd B-tree elements from low in node2 to
603 * high in node1.
604 */
605 count = -count;
606 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
607 btree_s = &node2->btree[0];
Nathan Scottfac80cc2006-03-17 17:29:56 +1100608 btree_d = &node1->btree[be16_to_cpu(node1->hdr.count)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 memcpy(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800610 be16_add_cpu(&node1->hdr.count, count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 XFS_DA_LOGRANGE(node1, btree_d, tmp));
613
614 /*
615 * Move elements in node2 down to fill the hole.
616 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100617 tmp = be16_to_cpu(node2->hdr.count) - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 tmp *= (uint)sizeof(xfs_da_node_entry_t);
619 btree_s = &node2->btree[count];
620 btree_d = &node2->btree[0];
621 memmove(btree_d, btree_s, tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800622 be16_add_cpu(&node2->hdr.count, -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 /*
626 * Log header of node 1 and all current bits of node 2.
627 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000628 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 XFS_DA_LOGRANGE(node1, &node1->hdr, sizeof(node1->hdr)));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000630 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 XFS_DA_LOGRANGE(node2, &node2->hdr,
632 sizeof(node2->hdr) +
Nathan Scottfac80cc2006-03-17 17:29:56 +1100633 sizeof(node2->btree[0]) * be16_to_cpu(node2->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /*
636 * Record the last hashval from each block for upward propagation.
637 * (note: don't use the swapped node pointers)
638 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000639 node1 = blk1->bp->b_addr;
640 node2 = blk2->bp->b_addr;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100641 blk1->hashval = be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval);
642 blk2->hashval = be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /*
645 * Adjust the expected index for insertion.
646 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100647 if (blk1->index >= be16_to_cpu(node1->hdr.count)) {
648 blk2->index = blk1->index - be16_to_cpu(node1->hdr.count);
649 blk1->index = be16_to_cpu(node1->hdr.count) + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651}
652
653/*
654 * Add a new entry to an intermediate node.
655 */
656STATIC void
657xfs_da_node_add(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
658 xfs_da_state_blk_t *newblk)
659{
660 xfs_da_intnode_t *node;
661 xfs_da_node_entry_t *btree;
662 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dave Chinner5a5881c2012-03-22 05:15:13 +0000664 trace_xfs_da_node_add(state->args);
665
Dave Chinner1d9025e2012-06-22 18:50:14 +1000666 node = oldblk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200667 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +1100668 ASSERT((oldblk->index >= 0) && (oldblk->index <= be16_to_cpu(node->hdr.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000670 if (state->args->whichfork == XFS_DATA_FORK)
Christoph Hellwig73523a22010-07-20 17:54:45 +1000671 ASSERT(newblk->blkno >= state->mp->m_dirleafblk &&
672 newblk->blkno < state->mp->m_dirfreeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /*
675 * We may need to make some room before we insert the new node.
676 */
677 tmp = 0;
678 btree = &node->btree[ oldblk->index ];
Nathan Scottfac80cc2006-03-17 17:29:56 +1100679 if (oldblk->index < be16_to_cpu(node->hdr.count)) {
680 tmp = (be16_to_cpu(node->hdr.count) - oldblk->index) * (uint)sizeof(*btree);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 memmove(btree + 1, btree, tmp);
682 }
Nathan Scott403432d2006-03-17 17:29:46 +1100683 btree->hashval = cpu_to_be32(newblk->hashval);
684 btree->before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000685 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 XFS_DA_LOGRANGE(node, btree, tmp + sizeof(*btree)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800687 be16_add_cpu(&node->hdr.count, 1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000688 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
690
691 /*
692 * Copy the last hash value from the oldblk to propagate upwards.
693 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100694 oldblk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1 ].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697/*========================================================================
698 * Routines used for shrinking the Btree.
699 *========================================================================*/
700
701/*
702 * Deallocate an empty leaf node, remove it from its parent,
703 * possibly deallocating that block, etc...
704 */
705int
706xfs_da_join(xfs_da_state_t *state)
707{
708 xfs_da_state_blk_t *drop_blk, *save_blk;
709 int action, error;
710
Dave Chinner5a5881c2012-03-22 05:15:13 +0000711 trace_xfs_da_join(state->args);
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 action = 0;
714 drop_blk = &state->path.blk[ state->path.active-1 ];
715 save_blk = &state->altpath.blk[ state->path.active-1 ];
716 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
717 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000718 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * Walk back up the tree joining/deallocating as necessary.
722 * When we stop dropping blocks, break out.
723 */
724 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
725 state->path.active--) {
726 /*
727 * See if we can combine the block with a neighbor.
728 * (action == 0) => no options, just leave
729 * (action == 1) => coalesce, then unlink
730 * (action == 2) => block empty, unlink it
731 */
732 switch (drop_blk->magic) {
733 case XFS_ATTR_LEAF_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 error = xfs_attr_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (error)
736 return(error);
737 if (action == 0)
738 return(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xfs_attr_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 error = xfs_dir2_leafn_toosmall(state, &action);
743 if (error)
744 return error;
745 if (action == 0)
746 return 0;
747 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
748 break;
749 case XFS_DA_NODE_MAGIC:
750 /*
751 * Remove the offending node, fixup hashvals,
752 * check for a toosmall neighbor.
753 */
754 xfs_da_node_remove(state, drop_blk);
755 xfs_da_fixhashpath(state, &state->path);
756 error = xfs_da_node_toosmall(state, &action);
757 if (error)
758 return(error);
759 if (action == 0)
760 return 0;
761 xfs_da_node_unbalance(state, drop_blk, save_blk);
762 break;
763 }
764 xfs_da_fixhashpath(state, &state->altpath);
765 error = xfs_da_blk_unlink(state, drop_blk, save_blk);
766 xfs_da_state_kill_altpath(state);
767 if (error)
768 return(error);
769 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
770 drop_blk->bp);
771 drop_blk->bp = NULL;
772 if (error)
773 return(error);
774 }
775 /*
776 * We joined all the way to the top. If it turns out that
777 * we only have one entry in the root, make the child block
778 * the new root.
779 */
780 xfs_da_node_remove(state, drop_blk);
781 xfs_da_fixhashpath(state, &state->path);
782 error = xfs_da_root_join(state, &state->path.blk[0]);
783 return(error);
784}
785
Alex Elder1c4f3322011-07-18 18:14:09 +0000786#ifdef DEBUG
787static void
788xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
789{
790 __be16 magic = blkinfo->magic;
791
792 if (level == 1) {
793 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +1000794 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Alex Elder1c4f3322011-07-18 18:14:09 +0000795 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
796 } else
797 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
798 ASSERT(!blkinfo->forw);
799 ASSERT(!blkinfo->back);
800}
801#else /* !DEBUG */
802#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
803#endif /* !DEBUG */
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * We have only one entry in the root. Copy the only remaining child of
807 * the old root to block 0 as the new root node.
808 */
809STATIC int
810xfs_da_root_join(xfs_da_state_t *state, xfs_da_state_blk_t *root_blk)
811{
812 xfs_da_intnode_t *oldroot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xfs_da_args_t *args;
814 xfs_dablk_t child;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000815 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int error;
817
Dave Chinner5a5881c2012-03-22 05:15:13 +0000818 trace_xfs_da_root_join(state->args);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 args = state->args;
821 ASSERT(args != NULL);
822 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000823 oldroot = root_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200824 ASSERT(oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ASSERT(!oldroot->hdr.info.forw);
826 ASSERT(!oldroot->hdr.info.back);
827
828 /*
829 * If the root has more than one child, then don't do anything.
830 */
Nathan Scottfac80cc2006-03-17 17:29:56 +1100831 if (be16_to_cpu(oldroot->hdr.count) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return(0);
833
834 /*
835 * Read in the (only) child block, then copy those bytes into
836 * the root block's buffer and free the original child block.
837 */
Nathan Scott403432d2006-03-17 17:29:46 +1100838 child = be32_to_cpu(oldroot->btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ASSERT(child != 0);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100840 error = xfs_da_node_read(args->trans, args->dp, child, -1, &bp,
841 args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (error)
843 return(error);
844 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000845 xfs_da_blkinfo_onlychild_validate(bp->b_addr,
Alex Elder1c4f3322011-07-18 18:14:09 +0000846 be16_to_cpu(oldroot->hdr.level));
847
Dave Chinner612cfbf2012-11-14 17:52:32 +1100848 /*
849 * This could be copying a leaf back into the root block in the case of
850 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +1100851 * to update the b_ops pointer as well to match the buffer type change
Dave Chinner612cfbf2012-11-14 17:52:32 +1100852 * that could occur.
853 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854 memcpy(root_blk->bp->b_addr, bp->b_addr, state->blocksize);
Dave Chinner1813dd62012-11-14 17:54:40 +1100855 root_blk->bp->b_ops = bp->b_ops;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000856 xfs_trans_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 error = xfs_da_shrink_inode(args, child, bp);
858 return(error);
859}
860
861/*
862 * Check a node block and its neighbors to see if the block should be
863 * collapsed into one or the other neighbor. Always keep the block
864 * with the smaller block number.
865 * If the current block is over 50% full, don't try to join it, return 0.
866 * If the block is empty, fill in the state structure and return 2.
867 * If it can be collapsed, fill in the state structure and return 1.
868 * If nothing can be done, return 0.
869 */
870STATIC int
871xfs_da_node_toosmall(xfs_da_state_t *state, int *action)
872{
873 xfs_da_intnode_t *node;
874 xfs_da_state_blk_t *blk;
875 xfs_da_blkinfo_t *info;
876 int count, forward, error, retval, i;
877 xfs_dablk_t blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000878 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Dave Chinneree732592012-11-12 22:53:53 +1100880 trace_xfs_da_node_toosmall(state->args);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
883 * Check for the degenerate case of the block being over 50% full.
884 * If so, it's not worth even looking to see if we might be able
885 * to coalesce with a sibling.
886 */
887 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +1000888 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200889 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 node = (xfs_da_intnode_t *)info;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100891 count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (count > (state->node_ents >> 1)) {
893 *action = 0; /* blk over 50%, don't try to join */
894 return(0); /* blk over 50%, don't try to join */
895 }
896
897 /*
898 * Check for the degenerate case of the block being empty.
899 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +1000900 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * to merge with the forward block unless it is NULL.
902 */
903 if (count == 0) {
904 /*
905 * Make altpath point to the block we want to keep and
906 * path point to the block we want to drop (this one).
907 */
Nathan Scott89da0542006-03-17 17:28:40 +1100908 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 memcpy(&state->altpath, &state->path, sizeof(state->path));
910 error = xfs_da_path_shift(state, &state->altpath, forward,
911 0, &retval);
912 if (error)
913 return(error);
914 if (retval) {
915 *action = 0;
916 } else {
917 *action = 2;
918 }
919 return(0);
920 }
921
922 /*
923 * Examine each sibling block to see if we can coalesce with
924 * at least 25% free space to spare. We need to figure out
925 * whether to merge with the forward or the backward block.
926 * We prefer coalescing with the lower numbered sibling so as
927 * to shrink a directory over time.
928 */
929 /* start with smaller blk num */
Nathan Scott89da0542006-03-17 17:28:40 +1100930 forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 for (i = 0; i < 2; forward = !forward, i++) {
932 if (forward)
Nathan Scott89da0542006-03-17 17:28:40 +1100933 blkno = be32_to_cpu(info->forw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 else
Nathan Scott89da0542006-03-17 17:28:40 +1100935 blkno = be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (blkno == 0)
937 continue;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100938 error = xfs_da_node_read(state->args->trans, state->args->dp,
939 blkno, -1, &bp, state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (error)
941 return(error);
942 ASSERT(bp != NULL);
943
944 node = (xfs_da_intnode_t *)info;
945 count = state->node_ents;
946 count -= state->node_ents >> 2;
Nathan Scottfac80cc2006-03-17 17:29:56 +1100947 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000948 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200949 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +1100950 count -= be16_to_cpu(node->hdr.count);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000951 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (count >= 0)
953 break; /* fits with at least 25% to spare */
954 }
955 if (i >= 2) {
956 *action = 0;
957 return(0);
958 }
959
960 /*
961 * Make altpath point to the block we want to keep (the lower
962 * numbered block) and path point to the block we want to drop.
963 */
964 memcpy(&state->altpath, &state->path, sizeof(state->path));
965 if (blkno < blk->blkno) {
966 error = xfs_da_path_shift(state, &state->altpath, forward,
967 0, &retval);
968 if (error) {
969 return(error);
970 }
971 if (retval) {
972 *action = 0;
973 return(0);
974 }
975 } else {
976 error = xfs_da_path_shift(state, &state->path, forward,
977 0, &retval);
978 if (error) {
979 return(error);
980 }
981 if (retval) {
982 *action = 0;
983 return(0);
984 }
985 }
986 *action = 1;
987 return(0);
988}
989
990/*
991 * Walk back up the tree adjusting hash values as necessary,
992 * when we stop making changes, return.
993 */
994void
995xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
996{
997 xfs_da_state_blk_t *blk;
998 xfs_da_intnode_t *node;
999 xfs_da_node_entry_t *btree;
1000 xfs_dahash_t lasthash=0;
1001 int level, count;
1002
Dave Chinneree732592012-11-12 22:53:53 +11001003 trace_xfs_da_fixhashpath(state->args);
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 level = path->active-1;
1006 blk = &path->blk[ level ];
1007 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 case XFS_ATTR_LEAF_MAGIC:
1009 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1010 if (count == 0)
1011 return;
1012 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
1015 if (count == 0)
1016 return;
1017 break;
1018 case XFS_DA_NODE_MAGIC:
1019 lasthash = xfs_da_node_lasthash(blk->bp, &count);
1020 if (count == 0)
1021 return;
1022 break;
1023 }
1024 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001025 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001026 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 btree = &node->btree[ blk->index ];
Nathan Scott403432d2006-03-17 17:29:46 +11001028 if (be32_to_cpu(btree->hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 break;
1030 blk->hashval = lasthash;
Nathan Scott403432d2006-03-17 17:29:46 +11001031 btree->hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001032 xfs_trans_log_buf(state->args->trans, blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
1034
Nathan Scottfac80cc2006-03-17 17:29:56 +11001035 lasthash = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037}
1038
1039/*
1040 * Remove an entry from an intermediate node.
1041 */
1042STATIC void
1043xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
1044{
1045 xfs_da_intnode_t *node;
1046 xfs_da_node_entry_t *btree;
1047 int tmp;
1048
Dave Chinner5a5881c2012-03-22 05:15:13 +00001049 trace_xfs_da_node_remove(state->args);
1050
Dave Chinner1d9025e2012-06-22 18:50:14 +10001051 node = drop_blk->bp->b_addr;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001052 ASSERT(drop_blk->index < be16_to_cpu(node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ASSERT(drop_blk->index >= 0);
1054
1055 /*
1056 * Copy over the offending entry, or just zero it out.
1057 */
1058 btree = &node->btree[drop_blk->index];
Nathan Scottfac80cc2006-03-17 17:29:56 +11001059 if (drop_blk->index < (be16_to_cpu(node->hdr.count)-1)) {
1060 tmp = be16_to_cpu(node->hdr.count) - drop_blk->index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 tmp *= (uint)sizeof(xfs_da_node_entry_t);
1062 memmove(btree, btree + 1, tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001063 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 XFS_DA_LOGRANGE(node, btree, tmp));
Nathan Scottfac80cc2006-03-17 17:29:56 +11001065 btree = &node->btree[be16_to_cpu(node->hdr.count)-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001068 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001070 be16_add_cpu(&node->hdr.count, -1);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001071 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
1073
1074 /*
1075 * Copy the last hash value from the block to propagate upwards.
1076 */
1077 btree--;
Nathan Scott403432d2006-03-17 17:29:46 +11001078 drop_blk->hashval = be32_to_cpu(btree->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081/*
1082 * Unbalance the btree elements between two intermediate nodes,
1083 * move all Btree elements from one node into another.
1084 */
1085STATIC void
1086xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1087 xfs_da_state_blk_t *save_blk)
1088{
1089 xfs_da_intnode_t *drop_node, *save_node;
1090 xfs_da_node_entry_t *btree;
1091 int tmp;
1092 xfs_trans_t *tp;
1093
Dave Chinner5a5881c2012-03-22 05:15:13 +00001094 trace_xfs_da_node_unbalance(state->args);
1095
Dave Chinner1d9025e2012-06-22 18:50:14 +10001096 drop_node = drop_blk->bp->b_addr;
1097 save_node = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001098 ASSERT(drop_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
1099 ASSERT(save_node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 tp = state->args->trans;
1101
1102 /*
1103 * If the dying block has lower hashvals, then move all the
1104 * elements in the remaining block up to make a hole.
1105 */
Nathan Scott403432d2006-03-17 17:29:46 +11001106 if ((be32_to_cpu(drop_node->btree[0].hashval) < be32_to_cpu(save_node->btree[ 0 ].hashval)) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001107 (be32_to_cpu(drop_node->btree[be16_to_cpu(drop_node->hdr.count)-1].hashval) <
1108 be32_to_cpu(save_node->btree[be16_to_cpu(save_node->hdr.count)-1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 {
Nathan Scottfac80cc2006-03-17 17:29:56 +11001110 btree = &save_node->btree[be16_to_cpu(drop_node->hdr.count)];
1111 tmp = be16_to_cpu(save_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 memmove(btree, &save_node->btree[0], tmp);
1113 btree = &save_node->btree[0];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001114 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cc2006-03-17 17:29:56 +11001116 (be16_to_cpu(save_node->hdr.count) + be16_to_cpu(drop_node->hdr.count)) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sizeof(xfs_da_node_entry_t)));
1118 } else {
Nathan Scottfac80cc2006-03-17 17:29:56 +11001119 btree = &save_node->btree[be16_to_cpu(save_node->hdr.count)];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001120 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 XFS_DA_LOGRANGE(save_node, btree,
Nathan Scottfac80cc2006-03-17 17:29:56 +11001122 be16_to_cpu(drop_node->hdr.count) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 sizeof(xfs_da_node_entry_t)));
1124 }
1125
1126 /*
1127 * Move all the B-tree elements from drop_blk to save_blk.
1128 */
Nathan Scottfac80cc2006-03-17 17:29:56 +11001129 tmp = be16_to_cpu(drop_node->hdr.count) * (uint)sizeof(xfs_da_node_entry_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 memcpy(btree, &drop_node->btree[0], tmp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001131 be16_add_cpu(&save_node->hdr.count, be16_to_cpu(drop_node->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Dave Chinner1d9025e2012-06-22 18:50:14 +10001133 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
1135 sizeof(save_node->hdr)));
1136
1137 /*
1138 * Save the last hashval in the remaining block for upward propagation.
1139 */
Nathan Scottfac80cc2006-03-17 17:29:56 +11001140 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 -07001141}
1142
1143/*========================================================================
1144 * Routines used for finding things in the Btree.
1145 *========================================================================*/
1146
1147/*
1148 * Walk down the Btree looking for a particular filename, filling
1149 * in the state structure as we go.
1150 *
1151 * We will set the state structure to point to each of the elements
1152 * in each of the nodes where either the hashval is or should be.
1153 *
1154 * We support duplicate hashval's so for each entry in the current
1155 * node that could contain the desired hashval, descend. This is a
1156 * pruned depth-first tree search.
1157 */
1158int /* error */
1159xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
1160{
1161 xfs_da_state_blk_t *blk;
1162 xfs_da_blkinfo_t *curr;
1163 xfs_da_intnode_t *node;
1164 xfs_da_node_entry_t *btree;
1165 xfs_dablk_t blkno;
1166 int probe, span, max, error, retval;
Nathan Scottd432c802006-09-28 11:03:44 +10001167 xfs_dahash_t hashval, btreehashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 xfs_da_args_t *args;
1169
1170 args = state->args;
1171
1172 /*
1173 * Descend thru the B-tree searching each level for the right
1174 * node to use, until the right hashval is found.
1175 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001176 blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 for (blk = &state->path.blk[0], state->path.active = 1;
1178 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1179 blk++, state->path.active++) {
1180 /*
1181 * Read the next node down in the tree.
1182 */
1183 blk->blkno = blkno;
Dave Chinnerd9392a42012-11-12 22:54:17 +11001184 error = xfs_da_node_read(args->trans, args->dp, blkno,
1185 -1, &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (error) {
1187 blk->blkno = 0;
1188 state->path.active--;
1189 return(error);
1190 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001191 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001192 blk->magic = be16_to_cpu(curr->magic);
1193 ASSERT(blk->magic == XFS_DA_NODE_MAGIC ||
1194 blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1195 blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /*
1198 * Search an intermediate node for a match.
1199 */
Nathan Scott89da0542006-03-17 17:28:40 +11001200 if (blk->magic == XFS_DA_NODE_MAGIC) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001201 node = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001202 max = be16_to_cpu(node->hdr.count);
Lachlan McIlroy1c91ad32007-02-10 18:35:27 +11001203 blk->hashval = be32_to_cpu(node->btree[max-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /*
1206 * Binary search. (note: small blocks will skip loop)
1207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 probe = span = max / 2;
1209 hashval = args->hashval;
1210 for (btree = &node->btree[probe]; span > 4;
1211 btree = &node->btree[probe]) {
1212 span /= 2;
Nathan Scottd432c802006-09-28 11:03:44 +10001213 btreehashval = be32_to_cpu(btree->hashval);
1214 if (btreehashval < hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 probe += span;
Nathan Scottd432c802006-09-28 11:03:44 +10001216 else if (btreehashval > hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 probe -= span;
1218 else
1219 break;
1220 }
1221 ASSERT((probe >= 0) && (probe < max));
Nathan Scott403432d2006-03-17 17:29:46 +11001222 ASSERT((span <= 4) || (be32_to_cpu(btree->hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /*
1225 * Since we may have duplicate hashval's, find the first
1226 * matching hashval in the node.
1227 */
Nathan Scott403432d2006-03-17 17:29:46 +11001228 while ((probe > 0) && (be32_to_cpu(btree->hashval) >= hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 btree--;
1230 probe--;
1231 }
Nathan Scott403432d2006-03-17 17:29:46 +11001232 while ((probe < max) && (be32_to_cpu(btree->hashval) < hashval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 btree++;
1234 probe++;
1235 }
1236
1237 /*
1238 * Pick the right block to descend on.
1239 */
1240 if (probe == max) {
1241 blk->index = max-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001242 blkno = be32_to_cpu(node->btree[max-1].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } else {
1244 blk->index = probe;
Nathan Scott403432d2006-03-17 17:29:46 +11001245 blkno = be32_to_cpu(btree->before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Nathan Scottd432c802006-09-28 11:03:44 +10001247 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1249 break;
Nathan Scottd432c802006-09-28 11:03:44 +10001250 } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
1252 break;
1253 }
1254 }
1255
1256 /*
1257 * A leaf block that ends in the hashval that we are interested in
1258 * (final hashval == search hashval) means that the next block may
1259 * contain more entries with the same hashval, shift upward to the
1260 * next leaf and keep searching.
1261 */
1262 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001263 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1265 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001266 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 retval = xfs_attr_leaf_lookup_int(blk->bp, args);
1268 blk->index = args->index;
1269 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001270 } else {
1271 ASSERT(0);
1272 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1275 (blk->hashval == args->hashval)) {
1276 error = xfs_da_path_shift(state, &state->path, 1, 1,
1277 &retval);
1278 if (error)
1279 return(error);
1280 if (retval == 0) {
1281 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001282 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* path_shift() gives ENOENT */
1284 retval = XFS_ERROR(ENOATTR);
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 break;
1288 }
1289 *result = retval;
1290 return(0);
1291}
1292
1293/*========================================================================
1294 * Utility routines.
1295 *========================================================================*/
1296
1297/*
1298 * Link a new block into a doubly linked list of blocks (of whatever type).
1299 */
1300int /* error */
1301xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
1302 xfs_da_state_blk_t *new_blk)
1303{
1304 xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
1305 xfs_da_args_t *args;
1306 int before=0, error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001307 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
1310 * Set up environment.
1311 */
1312 args = state->args;
1313 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001314 old_info = old_blk->bp->b_addr;
1315 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001317 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001319 ASSERT(old_blk->magic == be16_to_cpu(old_info->magic));
1320 ASSERT(new_blk->magic == be16_to_cpu(new_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 ASSERT(old_blk->magic == new_blk->magic);
1322
1323 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 case XFS_ATTR_LEAF_MAGIC:
1325 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
1329 break;
1330 case XFS_DA_NODE_MAGIC:
1331 before = xfs_da_node_order(old_blk->bp, new_blk->bp);
1332 break;
1333 }
1334
1335 /*
1336 * Link blocks in appropriate order.
1337 */
1338 if (before) {
1339 /*
1340 * Link new block in before existing block.
1341 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001342 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001343 new_info->forw = cpu_to_be32(old_blk->blkno);
1344 new_info->back = old_info->back;
1345 if (old_info->back) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001346 error = xfs_da_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001347 be32_to_cpu(old_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001348 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 if (error)
1350 return(error);
1351 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001352 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001353 ASSERT(be16_to_cpu(tmp_info->magic) == be16_to_cpu(old_info->magic));
1354 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1355 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001356 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Nathan Scott89da0542006-03-17 17:28:40 +11001358 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else {
1360 /*
1361 * Link new block in after existing block.
1362 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001363 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001364 new_info->forw = old_info->forw;
1365 new_info->back = cpu_to_be32(old_blk->blkno);
1366 if (old_info->forw) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001367 error = xfs_da_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001368 be32_to_cpu(old_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001369 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (error)
1371 return(error);
1372 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001373 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001374 ASSERT(tmp_info->magic == old_info->magic);
1375 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1376 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001377 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Nathan Scott89da0542006-03-17 17:28:40 +11001379 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Dave Chinner1d9025e2012-06-22 18:50:14 +10001382 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1383 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return(0);
1385}
1386
1387/*
1388 * Compare two intermediate nodes for "order".
1389 */
1390STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391xfs_da_node_order(
1392 struct xfs_buf *node1_bp,
1393 struct xfs_buf *node2_bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 xfs_da_intnode_t *node1, *node2;
1396
Dave Chinner1d9025e2012-06-22 18:50:14 +10001397 node1 = node1_bp->b_addr;
1398 node2 = node2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001399 ASSERT(node1->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) &&
1400 node2->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +11001401 if ((be16_to_cpu(node1->hdr.count) > 0) && (be16_to_cpu(node2->hdr.count) > 0) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001402 ((be32_to_cpu(node2->btree[0].hashval) <
1403 be32_to_cpu(node1->btree[0].hashval)) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001404 (be32_to_cpu(node2->btree[be16_to_cpu(node2->hdr.count)-1].hashval) <
1405 be32_to_cpu(node1->btree[be16_to_cpu(node1->hdr.count)-1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return(1);
1407 }
1408 return(0);
1409}
1410
1411/*
1412 * Pick up the last hashvalue from an intermediate node.
1413 */
1414STATIC uint
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415xfs_da_node_lasthash(
1416 struct xfs_buf *bp,
1417 int *count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 xfs_da_intnode_t *node;
1420
Dave Chinner1d9025e2012-06-22 18:50:14 +10001421 node = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001422 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (count)
Nathan Scottfac80cc2006-03-17 17:29:56 +11001424 *count = be16_to_cpu(node->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (!node->hdr.count)
1426 return(0);
Nathan Scottfac80cc2006-03-17 17:29:56 +11001427 return be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
1430/*
1431 * Unlink a block from a doubly linked list of blocks.
1432 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001433STATIC int /* error */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1435 xfs_da_state_blk_t *save_blk)
1436{
1437 xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
1438 xfs_da_args_t *args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001439 struct xfs_buf *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int error;
1441
1442 /*
1443 * Set up environment.
1444 */
1445 args = state->args;
1446 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001447 save_info = save_blk->bp->b_addr;
1448 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001450 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Nathan Scott89da0542006-03-17 17:28:40 +11001452 ASSERT(save_blk->magic == be16_to_cpu(save_info->magic));
1453 ASSERT(drop_blk->magic == be16_to_cpu(drop_info->magic));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001455 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1456 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1457 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1458 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * Unlink the leaf block from the doubly linked chain of leaves.
1462 */
Nathan Scott89da0542006-03-17 17:28:40 +11001463 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001464 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001465 save_info->back = drop_info->back;
1466 if (drop_info->back) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001467 error = xfs_da_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001468 be32_to_cpu(drop_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001469 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (error)
1471 return(error);
1472 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001473 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001474 ASSERT(tmp_info->magic == save_info->magic);
1475 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1476 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001477 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001481 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001482 save_info->forw = drop_info->forw;
1483 if (drop_info->forw) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001484 error = xfs_da_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001485 be32_to_cpu(drop_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001486 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (error)
1488 return(error);
1489 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001490 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001491 ASSERT(tmp_info->magic == save_info->magic);
1492 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1493 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001494 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
1497 }
1498
Dave Chinner1d9025e2012-06-22 18:50:14 +10001499 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return(0);
1501}
1502
1503/*
1504 * Move a path "forward" or "!forward" one block at the current level.
1505 *
1506 * This routine will adjust a "path" to point to the next block
1507 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1508 * Btree, including updating pointers to the intermediate nodes between
1509 * the new bottom and the root.
1510 */
1511int /* error */
1512xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
1513 int forward, int release, int *result)
1514{
1515 xfs_da_state_blk_t *blk;
1516 xfs_da_blkinfo_t *info;
1517 xfs_da_intnode_t *node;
1518 xfs_da_args_t *args;
1519 xfs_dablk_t blkno=0;
1520 int level, error;
1521
Dave Chinneree732592012-11-12 22:53:53 +11001522 trace_xfs_da_path_shift(state->args);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /*
1525 * Roll up the Btree looking for the first block where our
1526 * current index is not at the edge of the block. Note that
1527 * we skip the bottom layer because we want the sibling block.
1528 */
1529 args = state->args;
1530 ASSERT(args != NULL);
1531 ASSERT(path != NULL);
1532 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1533 level = (path->active-1) - 1; /* skip bottom layer in path */
1534 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
1535 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001536 node = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001537 ASSERT(node->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Nathan Scottfac80cc2006-03-17 17:29:56 +11001538 if (forward && (blk->index < be16_to_cpu(node->hdr.count)-1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 blk->index++;
Nathan Scott403432d2006-03-17 17:29:46 +11001540 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542 } else if (!forward && (blk->index > 0)) {
1543 blk->index--;
Nathan Scott403432d2006-03-17 17:29:46 +11001544 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
1546 }
1547 }
1548 if (level < 0) {
1549 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001550 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return(0);
1552 }
1553
1554 /*
1555 * Roll down the edge of the subtree until we reach the
1556 * same depth we were at originally.
1557 */
1558 for (blk++, level++; level < path->active; blk++, level++) {
1559 /*
1560 * Release the old block.
1561 * (if it's dirty, trans won't actually let go)
1562 */
1563 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001564 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /*
1567 * Read the next child block.
1568 */
1569 blk->blkno = blkno;
Dave Chinnerd9392a42012-11-12 22:54:17 +11001570 error = xfs_da_node_read(args->trans, args->dp, blkno, -1,
1571 &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (error)
1573 return(error);
1574 ASSERT(blk->bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001576 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1577 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001578 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001579 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC));
Nathan Scott89da0542006-03-17 17:28:40 +11001580 blk->magic = be16_to_cpu(info->magic);
1581 if (blk->magic == XFS_DA_NODE_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 node = (xfs_da_intnode_t *)info;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001583 blk->hashval = be32_to_cpu(node->btree[be16_to_cpu(node->hdr.count)-1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (forward)
1585 blk->index = 0;
1586 else
Nathan Scottfac80cc2006-03-17 17:29:56 +11001587 blk->index = be16_to_cpu(node->hdr.count)-1;
Nathan Scott403432d2006-03-17 17:29:46 +11001588 blkno = be32_to_cpu(node->btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 } else {
1590 ASSERT(level == path->active-1);
1591 blk->index = 0;
1592 switch(blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 case XFS_ATTR_LEAF_MAGIC:
1594 blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
1595 NULL);
1596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +10001598 case XFS_DIR3_LEAFN_MAGIC:
1599 blk->magic = XFS_DIR2_LEAFN_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
1601 NULL);
1602 break;
1603 default:
Dave Chinner24df33b2013-04-12 07:30:21 +10001604 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
1606 }
1607 }
1608 }
1609 *result = 0;
1610 return(0);
1611}
1612
1613
1614/*========================================================================
1615 * Utility routines.
1616 *========================================================================*/
1617
1618/*
1619 * Implement a simple hash on a character string.
1620 * Rotate the hash value by 7 bits, then XOR each character in.
1621 * This is implemented with some source-level loop unrolling.
1622 */
1623xfs_dahash_t
Christoph Hellwiga5687782009-02-09 08:37:39 +01001624xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 xfs_dahash_t hash;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Do four characters at a time as long as we can.
1630 */
1631 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1632 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1633 (name[3] << 0) ^ rol32(hash, 7 * 4);
1634
1635 /*
1636 * Now do the rest of the characters.
1637 */
1638 switch (namelen) {
1639 case 3:
1640 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
1641 rol32(hash, 7 * 3);
1642 case 2:
1643 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
1644 case 1:
1645 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11001646 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return hash;
1648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Barry Naujok5163f952008-05-21 16:41:01 +10001651enum xfs_dacmp
1652xfs_da_compname(
1653 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11001654 const unsigned char *name,
1655 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10001656{
1657 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
1658 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
1659}
1660
1661static xfs_dahash_t
1662xfs_default_hashname(
1663 struct xfs_name *name)
1664{
1665 return xfs_da_hashname(name->name, name->len);
1666}
1667
1668const struct xfs_nameops xfs_default_nameops = {
1669 .hashname = xfs_default_hashname,
1670 .compname = xfs_da_compname
1671};
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673int
Christoph Hellwig77936d02011-07-13 13:43:49 +02001674xfs_da_grow_inode_int(
1675 struct xfs_da_args *args,
1676 xfs_fileoff_t *bno,
1677 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Christoph Hellwig77936d02011-07-13 13:43:49 +02001679 struct xfs_trans *tp = args->trans;
1680 struct xfs_inode *dp = args->dp;
1681 int w = args->whichfork;
1682 xfs_drfsbno_t nblks = dp->i_d.di_nblocks;
1683 struct xfs_bmbt_irec map, *mapp;
1684 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Find a spot in the file space to put the new block.
1688 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02001689 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
1690 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
1694 * Try mapping it in one filesystem block.
1695 */
1696 nmap = 1;
1697 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001698 error = xfs_bmapi_write(tp, dp, *bno, count,
1699 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 args->firstblock, args->total, &map, &nmap,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001701 args->flist);
1702 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 ASSERT(nmap <= 1);
1706 if (nmap == 1) {
1707 mapp = &map;
1708 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001709 } else if (nmap == 0 && count > 1) {
1710 xfs_fileoff_t b;
1711 int c;
1712
1713 /*
1714 * If we didn't get it and the block might work if fragmented,
1715 * try without the CONTIG flag. Loop until we get it all.
1716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001718 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02001720 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00001721 error = xfs_bmapi_write(tp, dp, b, c,
1722 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 args->firstblock, args->total,
Christoph Hellwig77936d02011-07-13 13:43:49 +02001724 &mapp[mapi], &nmap, args->flist);
1725 if (error)
1726 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (nmap < 1)
1728 break;
1729 mapi += nmap;
1730 b = mapp[mapi - 1].br_startoff +
1731 mapp[mapi - 1].br_blockcount;
1732 }
1733 } else {
1734 mapi = 0;
1735 mapp = NULL;
1736 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
1739 * Count the blocks we got, make sure it matches the total.
1740 */
1741 for (i = 0, got = 0; i < mapi; i++)
1742 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001743 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02001745 *bno + count) {
1746 error = XFS_ERROR(ENOSPC);
1747 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02001749
David Chinnera7444052008-10-30 17:38:12 +11001750 /* account for newly allocated blocks in reserved blocks total */
1751 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02001752
1753out_free_map:
1754 if (mapp != &map)
1755 kmem_free(mapp);
1756 return error;
1757}
1758
1759/*
1760 * Add a block to the btree ahead of the file.
1761 * Return the new block number to the caller.
1762 */
1763int
1764xfs_da_grow_inode(
1765 struct xfs_da_args *args,
1766 xfs_dablk_t *new_blkno)
1767{
1768 xfs_fileoff_t bno;
1769 int count;
1770 int error;
1771
Dave Chinner5a5881c2012-03-22 05:15:13 +00001772 trace_xfs_da_grow_inode(args);
1773
Christoph Hellwig77936d02011-07-13 13:43:49 +02001774 if (args->whichfork == XFS_DATA_FORK) {
1775 bno = args->dp->i_mount->m_dirleafblk;
1776 count = args->dp->i_mount->m_dirblkfsbs;
1777 } else {
1778 bno = 0;
1779 count = 1;
1780 }
1781
1782 error = xfs_da_grow_inode_int(args, &bno, count);
1783 if (!error)
1784 *new_blkno = (xfs_dablk_t)bno;
1785 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
1788/*
1789 * Ick. We need to always be able to remove a btree block, even
1790 * if there's no space reservation because the filesystem is full.
1791 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
1792 * It swaps the target block with the last block in the file. The
1793 * last block in the file can always be removed since it can't cause
1794 * a bmap btree split to do that.
1795 */
1796STATIC int
Dave Chinner1d9025e2012-06-22 18:50:14 +10001797xfs_da_swap_lastblock(
1798 xfs_da_args_t *args,
1799 xfs_dablk_t *dead_blknop,
1800 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001803 struct xfs_buf *dead_buf, *last_buf, *sib_buf, *par_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 xfs_fileoff_t lastoff;
1805 xfs_inode_t *ip;
1806 xfs_trans_t *tp;
1807 xfs_mount_t *mp;
1808 int error, w, entno, level, dead_level;
1809 xfs_da_blkinfo_t *dead_info, *sib_info;
1810 xfs_da_intnode_t *par_node, *dead_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 xfs_dir2_leaf_t *dead_leaf2;
1812 xfs_dahash_t dead_hash;
1813
Dave Chinner5a5881c2012-03-22 05:15:13 +00001814 trace_xfs_da_swap_lastblock(args);
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 dead_buf = *dead_bufp;
1817 dead_blkno = *dead_blknop;
1818 tp = args->trans;
1819 ip = args->dp;
1820 w = args->whichfork;
1821 ASSERT(w == XFS_DATA_FORK);
1822 mp = ip->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001823 lastoff = mp->m_dirfreeblk;
1824 error = xfs_bmap_last_before(tp, ip, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (error)
1826 return error;
1827 if (unlikely(lastoff == 0)) {
1828 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
1829 mp);
1830 return XFS_ERROR(EFSCORRUPTED);
1831 }
1832 /*
1833 * Read the last block in the btree space.
1834 */
1835 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
Dave Chinnerd9392a42012-11-12 22:54:17 +11001836 error = xfs_da_node_read(tp, ip, last_blkno, -1, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return error;
1839 /*
1840 * Copy the last block into the dead buffer and log it.
1841 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001842 memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
1843 xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
1844 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 /*
1846 * Get values from the moved block.
1847 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001848 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1849 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
1850 struct xfs_dir3_icleaf_hdr leafhdr;
1851 struct xfs_dir2_leaf_entry *ents;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Dave Chinner24df33b2013-04-12 07:30:21 +10001854 xfs_dir3_leaf_hdr_from_disk(&leafhdr, dead_leaf2);
1855 ents = xfs_dir3_leaf_ents_p(dead_leaf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10001857 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 } else {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001859 ASSERT(dead_info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 dead_node = (xfs_da_intnode_t *)dead_info;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001861 dead_level = be16_to_cpu(dead_node->hdr.level);
1862 dead_hash = be32_to_cpu(dead_node->btree[be16_to_cpu(dead_node->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 sib_buf = par_buf = NULL;
1865 /*
1866 * If the moved block has a left sibling, fix up the pointers.
1867 */
Nathan Scott89da0542006-03-17 17:28:40 +11001868 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001869 error = xfs_da_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001872 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001874 be32_to_cpu(sib_info->forw) != last_blkno ||
1875 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
1877 XFS_ERRLEVEL_LOW, mp);
1878 error = XFS_ERROR(EFSCORRUPTED);
1879 goto done;
1880 }
Nathan Scott89da0542006-03-17 17:28:40 +11001881 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001882 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
1884 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 sib_buf = NULL;
1886 }
1887 /*
1888 * If the moved block has a right sibling, fix up the pointers.
1889 */
Nathan Scott89da0542006-03-17 17:28:40 +11001890 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001891 error = xfs_da_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001892 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001894 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11001896 be32_to_cpu(sib_info->back) != last_blkno ||
1897 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
1899 XFS_ERRLEVEL_LOW, mp);
1900 error = XFS_ERROR(EFSCORRUPTED);
1901 goto done;
1902 }
Nathan Scott89da0542006-03-17 17:28:40 +11001903 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001904 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
1906 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 sib_buf = NULL;
1908 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001909 par_blkno = mp->m_dirleafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 level = -1;
1911 /*
1912 * Walk down the tree looking for the parent of the moved block.
1913 */
1914 for (;;) {
Dave Chinnerd9392a42012-11-12 22:54:17 +11001915 error = xfs_da_node_read(tp, ip, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001916 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001918 par_node = par_buf->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001919 if (unlikely(par_node->hdr.info.magic !=
1920 cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Nathan Scottfac80cc2006-03-17 17:29:56 +11001921 (level >= 0 && level != be16_to_cpu(par_node->hdr.level) + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
1923 XFS_ERRLEVEL_LOW, mp);
1924 error = XFS_ERROR(EFSCORRUPTED);
1925 goto done;
1926 }
Nathan Scottfac80cc2006-03-17 17:29:56 +11001927 level = be16_to_cpu(par_node->hdr.level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 for (entno = 0;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001929 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001930 be32_to_cpu(par_node->btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 entno++)
1932 continue;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001933 if (unlikely(entno == be16_to_cpu(par_node->hdr.count))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
1935 XFS_ERRLEVEL_LOW, mp);
1936 error = XFS_ERROR(EFSCORRUPTED);
1937 goto done;
1938 }
Nathan Scott403432d2006-03-17 17:29:46 +11001939 par_blkno = be32_to_cpu(par_node->btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (level == dead_level + 1)
1941 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001942 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 par_buf = NULL;
1944 }
1945 /*
1946 * We're in the right parent block.
1947 * Look for the right entry.
1948 */
1949 for (;;) {
1950 for (;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001951 entno < be16_to_cpu(par_node->hdr.count) &&
Nathan Scott403432d2006-03-17 17:29:46 +11001952 be32_to_cpu(par_node->btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 entno++)
1954 continue;
Nathan Scottfac80cc2006-03-17 17:29:56 +11001955 if (entno < be16_to_cpu(par_node->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
Nathan Scott89da0542006-03-17 17:28:40 +11001957 par_blkno = be32_to_cpu(par_node->hdr.info.forw);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001958 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 par_buf = NULL;
1960 if (unlikely(par_blkno == 0)) {
1961 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
1962 XFS_ERRLEVEL_LOW, mp);
1963 error = XFS_ERROR(EFSCORRUPTED);
1964 goto done;
1965 }
Dave Chinnerd9392a42012-11-12 22:54:17 +11001966 error = xfs_da_node_read(tp, ip, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001969 par_node = par_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (unlikely(
Nathan Scottfac80cc2006-03-17 17:29:56 +11001971 be16_to_cpu(par_node->hdr.level) != level ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001972 par_node->hdr.info.magic != cpu_to_be16(XFS_DA_NODE_MAGIC))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
1974 XFS_ERRLEVEL_LOW, mp);
1975 error = XFS_ERROR(EFSCORRUPTED);
1976 goto done;
1977 }
1978 entno = 0;
1979 }
1980 /*
1981 * Update the parent entry pointing to the moved block.
1982 */
Nathan Scott403432d2006-03-17 17:29:46 +11001983 par_node->btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001984 xfs_trans_log_buf(tp, par_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
1986 sizeof(par_node->btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 *dead_blknop = last_blkno;
1988 *dead_bufp = last_buf;
1989 return 0;
1990done:
1991 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001992 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001994 xfs_trans_brelse(tp, sib_buf);
1995 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return error;
1997}
1998
1999/*
2000 * Remove a btree block from a directory or attribute.
2001 */
2002int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002003xfs_da_shrink_inode(
2004 xfs_da_args_t *args,
2005 xfs_dablk_t dead_blkno,
2006 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
2008 xfs_inode_t *dp;
2009 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 xfs_trans_t *tp;
2011 xfs_mount_t *mp;
2012
Dave Chinner5a5881c2012-03-22 05:15:13 +00002013 trace_xfs_da_shrink_inode(args);
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 dp = args->dp;
2016 w = args->whichfork;
2017 tp = args->trans;
2018 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002019 if (w == XFS_DATA_FORK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 count = mp->m_dirblkfsbs;
2021 else
2022 count = 1;
2023 for (;;) {
2024 /*
2025 * Remove extents. If we get ENOSPC for a dir we have to move
2026 * the last block to the place we want to kill.
2027 */
2028 if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002029 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002030 0, args->firstblock, args->flist,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 &done)) == ENOSPC) {
2032 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
2035 &dead_buf)))
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002036 break;
2037 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002041 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return error;
2043}
2044
2045/*
2046 * See if the mapping(s) for this btree block are valid, i.e.
2047 * don't contain holes, are logically contiguous, and cover the whole range.
2048 */
2049STATIC int
2050xfs_da_map_covers_blocks(
2051 int nmap,
2052 xfs_bmbt_irec_t *mapp,
2053 xfs_dablk_t bno,
2054 int count)
2055{
2056 int i;
2057 xfs_fileoff_t off;
2058
2059 for (i = 0, off = bno; i < nmap; i++) {
2060 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2061 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2062 return 0;
2063 }
2064 if (off != mapp[i].br_startoff) {
2065 return 0;
2066 }
2067 off += mapp[i].br_blockcount;
2068 }
2069 return off == bno + count;
2070}
2071
2072/*
Dave Chinner36054312012-06-22 18:50:13 +10002073 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2074 *
2075 * For the single map case, it is assumed that the caller has provided a pointer
2076 * to a valid xfs_buf_map. For the multiple map case, this function will
2077 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2078 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
Dave Chinner36054312012-06-22 18:50:13 +10002080static int
2081xfs_buf_map_from_irec(
2082 struct xfs_mount *mp,
2083 struct xfs_buf_map **mapp,
2084 unsigned int *nmaps,
2085 struct xfs_bmbt_irec *irecs,
2086 unsigned int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Dave Chinner36054312012-06-22 18:50:13 +10002088 struct xfs_buf_map *map;
2089 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Dave Chinner36054312012-06-22 18:50:13 +10002091 ASSERT(*nmaps == 1);
2092 ASSERT(nirecs >= 1);
2093
2094 if (nirecs > 1) {
2095 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_SLEEP);
2096 if (!map)
2097 return ENOMEM;
2098 *mapp = map;
2099 }
2100
2101 *nmaps = nirecs;
2102 map = *mapp;
2103 for (i = 0; i < *nmaps; i++) {
2104 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2105 irecs[i].br_startblock != HOLESTARTBLOCK);
2106 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2107 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2108 }
2109 return 0;
2110}
2111
2112/*
2113 * Map the block we are given ready for reading. There are three possible return
2114 * values:
2115 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2116 * caller knows not to execute a subsequent read.
2117 * 0 - if we mapped the block successfully
2118 * >0 - positive error number if there was an error.
2119 */
2120static int
2121xfs_dabuf_map(
2122 struct xfs_trans *trans,
2123 struct xfs_inode *dp,
2124 xfs_dablk_t bno,
2125 xfs_daddr_t mappedbno,
2126 int whichfork,
2127 struct xfs_buf_map **map,
2128 int *nmaps)
2129{
2130 struct xfs_mount *mp = dp->i_mount;
2131 int nfsb;
2132 int error = 0;
2133 struct xfs_bmbt_irec irec;
2134 struct xfs_bmbt_irec *irecs = &irec;
2135 int nirecs;
2136
2137 ASSERT(map && *map);
2138 ASSERT(*nmaps == 1);
2139
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002140 nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
Dave Chinner36054312012-06-22 18:50:13 +10002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Caller doesn't have a mapping. -2 means don't complain
2144 * if we land in a hole.
2145 */
2146 if (mappedbno == -1 || mappedbno == -2) {
2147 /*
2148 * Optimize the one-block case.
2149 */
Dave Chinner36054312012-06-22 18:50:13 +10002150 if (nfsb != 1)
2151 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002152
Dave Chinner36054312012-06-22 18:50:13 +10002153 nirecs = nfsb;
2154 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2155 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002156 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002159 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2160 irecs->br_startoff = (xfs_fileoff_t)bno;
2161 irecs->br_blockcount = nfsb;
2162 irecs->br_state = 0;
2163 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Dave Chinner36054312012-06-22 18:50:13 +10002165
2166 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2167 error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (unlikely(error == EFSCORRUPTED)) {
2169 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002170 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002171 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2172 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002174 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002175 xfs_alert(mp,
2176"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 i,
Dave Chinner36054312012-06-22 18:50:13 +10002178 (long long)irecs[i].br_startoff,
2179 (long long)irecs[i].br_startblock,
2180 (long long)irecs[i].br_blockcount,
2181 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183 }
2184 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2185 XFS_ERRLEVEL_LOW, mp);
2186 }
Dave Chinner36054312012-06-22 18:50:13 +10002187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Dave Chinner36054312012-06-22 18:50:13 +10002189 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2190out:
2191 if (irecs != &irec)
2192 kmem_free(irecs);
2193 return error;
2194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Dave Chinner36054312012-06-22 18:50:13 +10002196/*
2197 * Get a buffer for the dir/attr block.
2198 */
2199int
2200xfs_da_get_buf(
2201 struct xfs_trans *trans,
2202 struct xfs_inode *dp,
2203 xfs_dablk_t bno,
2204 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002205 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002206 int whichfork)
2207{
2208 struct xfs_buf *bp;
2209 struct xfs_buf_map map;
2210 struct xfs_buf_map *mapp;
2211 int nmap;
2212 int error;
2213
2214 *bpp = NULL;
2215 mapp = &map;
2216 nmap = 1;
2217 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2218 &mapp, &nmap);
2219 if (error) {
2220 /* mapping a hole is not an error, but we don't continue */
2221 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002223 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Dave Chinner36054312012-06-22 18:50:13 +10002225
2226 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2227 mapp, nmap, 0);
2228 error = bp ? bp->b_error : XFS_ERROR(EIO);
2229 if (error) {
2230 xfs_trans_brelse(trans, bp);
2231 goto out_free;
2232 }
2233
Dave Chinner1d9025e2012-06-22 18:50:14 +10002234 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002235
2236out_free:
2237 if (mapp != &map)
2238 kmem_free(mapp);
2239
2240 return error;
2241}
2242
2243/*
2244 * Get a buffer for the dir/attr block, fill in the contents.
2245 */
2246int
2247xfs_da_read_buf(
2248 struct xfs_trans *trans,
2249 struct xfs_inode *dp,
2250 xfs_dablk_t bno,
2251 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002252 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002253 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002254 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002255{
2256 struct xfs_buf *bp;
2257 struct xfs_buf_map map;
2258 struct xfs_buf_map *mapp;
2259 int nmap;
2260 int error;
2261
2262 *bpp = NULL;
2263 mapp = &map;
2264 nmap = 1;
2265 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2266 &mapp, &nmap);
2267 if (error) {
2268 /* mapping a hole is not an error, but we don't continue */
2269 if (error == -1)
2270 error = 0;
2271 goto out_free;
2272 }
2273
2274 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2275 dp->i_mount->m_ddev_targp,
Dave Chinner1813dd62012-11-14 17:54:40 +11002276 mapp, nmap, 0, &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002277 if (error)
2278 goto out_free;
2279
2280 if (whichfork == XFS_ATTR_FORK)
2281 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 else
Dave Chinner36054312012-06-22 18:50:13 +10002283 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 /*
Dave Chinner36054312012-06-22 18:50:13 +10002286 * This verification code will be moved to a CRC verification callback
2287 * function so just leave it here unchanged until then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 */
Dave Chinner36054312012-06-22 18:50:13 +10002289 {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002290 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
2291 xfs_dir2_free_t *free = bp->b_addr;
2292 xfs_da_blkinfo_t *info = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 uint magic, magic1;
Dave Chinner36054312012-06-22 18:50:13 +10002294 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Nathan Scott89da0542006-03-17 17:28:40 +11002296 magic = be16_to_cpu(info->magic);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002297 magic1 = be32_to_cpu(hdr->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 if (unlikely(
2299 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 (magic != XFS_ATTR_LEAF_MAGIC) &&
2301 (magic != XFS_DIR2_LEAF1_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002302 (magic != XFS_DIR3_LEAF1_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 (magic != XFS_DIR2_LEAFN_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002304 (magic != XFS_DIR3_LEAFN_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002306 (magic1 != XFS_DIR3_BLOCK_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 (magic1 != XFS_DIR2_DATA_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002308 (magic1 != XFS_DIR3_DATA_MAGIC) &&
2309 (free->hdr.magic !=
2310 cpu_to_be32(XFS_DIR2_FREE_MAGIC)) &&
2311 (free->hdr.magic !=
2312 cpu_to_be32(XFS_DIR3_FREE_MAGIC)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 mp, XFS_ERRTAG_DA_READ_BUF,
2314 XFS_RANDOM_DA_READ_BUF))) {
Dave Chinner36054312012-06-22 18:50:13 +10002315 trace_xfs_da_btree_corrupt(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2317 XFS_ERRLEVEL_LOW, mp, info);
2318 error = XFS_ERROR(EFSCORRUPTED);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002319 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002320 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002323 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002324out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002326 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return error;
2329}
2330
2331/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * Readahead the dir/attr block.
2333 */
2334xfs_daddr_t
2335xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002336 struct xfs_trans *trans,
2337 struct xfs_inode *dp,
2338 xfs_dablk_t bno,
Dave Chinnerda6958c2012-11-12 22:54:18 +11002339 xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002340 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002341 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Dave Chinner36054312012-06-22 18:50:13 +10002343 struct xfs_buf_map map;
2344 struct xfs_buf_map *mapp;
2345 int nmap;
2346 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Dave Chinner36054312012-06-22 18:50:13 +10002348 mapp = &map;
2349 nmap = 1;
Dave Chinnerda6958c2012-11-12 22:54:18 +11002350 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002351 &mapp, &nmap);
2352 if (error) {
2353 /* mapping a hole is not an error, but we don't continue */
2354 if (error == -1)
2355 error = 0;
2356 goto out_free;
2357 }
2358
2359 mappedbno = mapp[0].bm_bn;
Dave Chinner1813dd62012-11-14 17:54:40 +11002360 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002361
2362out_free:
2363 if (mapp != &map)
2364 kmem_free(mapp);
2365
2366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return -1;
Dave Chinner36054312012-06-22 18:50:13 +10002368 return mappedbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373/*
2374 * Allocate a dir-state structure.
2375 * We don't put them on the stack since they're large.
2376 */
2377xfs_da_state_t *
2378xfs_da_state_alloc(void)
2379{
Christoph Hellwigf41d7fb2009-07-18 18:14:55 -04002380 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
2382
2383/*
2384 * Kill the altpath contents of a da-state structure.
2385 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10002386STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387xfs_da_state_kill_altpath(xfs_da_state_t *state)
2388{
2389 int i;
2390
Dave Chinner1d9025e2012-06-22 18:50:14 +10002391 for (i = 0; i < state->altpath.active; i++)
2392 state->altpath.blk[i].bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 state->altpath.active = 0;
2394}
2395
2396/*
2397 * Free a da-state structure.
2398 */
2399void
2400xfs_da_state_free(xfs_da_state_t *state)
2401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 xfs_da_state_kill_altpath(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403#ifdef DEBUG
2404 memset((char *)state, 0, sizeof(*state));
2405#endif /* DEBUG */
2406 kmem_zone_free(xfs_da_state_zone, state);
2407}