blob: 779ecdcdbe02f28a567f4127225e1fd142bd2fab [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.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2.h"
31#include "xfs_dir2_format.h"
32#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_inode_item.h"
36#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_attr.h"
39#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100042#include "xfs_cksum.h"
43#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * xfs_da_btree.c
47 *
48 * Routines to implement directories as Btrees of hashed names.
49 */
50
51/*========================================================================
52 * Function prototypes for the kernel.
53 *========================================================================*/
54
55/*
56 * Routines used for growing the Btree.
57 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100058STATIC int xfs_da3_root_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 xfs_da_state_blk_t *existing_root,
60 xfs_da_state_blk_t *new_child);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100061STATIC int xfs_da3_node_split(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 xfs_da_state_blk_t *existing_blk,
63 xfs_da_state_blk_t *split_blk,
64 xfs_da_state_blk_t *blk_to_add,
65 int treelevel,
66 int *result);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100067STATIC void xfs_da3_node_rebalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 xfs_da_state_blk_t *node_blk_1,
69 xfs_da_state_blk_t *node_blk_2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100070STATIC void xfs_da3_node_add(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 xfs_da_state_blk_t *old_node_blk,
72 xfs_da_state_blk_t *new_node_blk);
73
74/*
75 * Routines used for shrinking the Btree.
76 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100077STATIC int xfs_da3_root_join(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 xfs_da_state_blk_t *root_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100079STATIC int xfs_da3_node_toosmall(xfs_da_state_t *state, int *retval);
80STATIC void xfs_da3_node_remove(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 xfs_da_state_blk_t *drop_blk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +100082STATIC void xfs_da3_node_unbalance(xfs_da_state_t *state,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 xfs_da_state_blk_t *src_node_blk,
84 xfs_da_state_blk_t *dst_node_blk);
85
86/*
87 * Utility routines.
88 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +100089STATIC int xfs_da3_blk_unlink(xfs_da_state_t *state,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100090 xfs_da_state_blk_t *drop_blk,
91 xfs_da_state_blk_t *save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Dave Chinnerf5ea1102013-04-24 18:58:02 +100093
94kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
95
96/*
97 * Allocate a dir-state structure.
98 * We don't put them on the stack since they're large.
99 */
100xfs_da_state_t *
101xfs_da_state_alloc(void)
102{
103 return kmem_zone_zalloc(xfs_da_state_zone, KM_NOFS);
104}
105
106/*
107 * Kill the altpath contents of a da-state structure.
108 */
109STATIC void
110xfs_da_state_kill_altpath(xfs_da_state_t *state)
111{
112 int i;
113
114 for (i = 0; i < state->altpath.active; i++)
115 state->altpath.blk[i].bp = NULL;
116 state->altpath.active = 0;
117}
118
119/*
120 * Free a da-state structure.
121 */
122void
123xfs_da_state_free(xfs_da_state_t *state)
124{
125 xfs_da_state_kill_altpath(state);
126#ifdef DEBUG
127 memset((char *)state, 0, sizeof(*state));
128#endif /* DEBUG */
129 kmem_zone_free(xfs_da_state_zone, state);
130}
131
132void
133xfs_da3_node_hdr_from_disk(
134 struct xfs_da3_icnode_hdr *to,
135 struct xfs_da_intnode *from)
136{
137 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
138 from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
139
140 if (from->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
141 struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)from;
142
143 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
144 to->back = be32_to_cpu(hdr3->info.hdr.back);
145 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
Dave Chinner517c2222013-04-24 18:58:55 +1000146 to->count = be16_to_cpu(hdr3->__count);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000147 to->level = be16_to_cpu(hdr3->__level);
148 return;
149 }
150 to->forw = be32_to_cpu(from->hdr.info.forw);
151 to->back = be32_to_cpu(from->hdr.info.back);
152 to->magic = be16_to_cpu(from->hdr.info.magic);
Dave Chinner517c2222013-04-24 18:58:55 +1000153 to->count = be16_to_cpu(from->hdr.__count);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000154 to->level = be16_to_cpu(from->hdr.__level);
155}
156
157void
158xfs_da3_node_hdr_to_disk(
159 struct xfs_da_intnode *to,
160 struct xfs_da3_icnode_hdr *from)
161{
162 ASSERT(from->magic == XFS_DA_NODE_MAGIC ||
163 from->magic == XFS_DA3_NODE_MAGIC);
164
165 if (from->magic == XFS_DA3_NODE_MAGIC) {
166 struct xfs_da3_node_hdr *hdr3 = (struct xfs_da3_node_hdr *)to;
167
168 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
169 hdr3->info.hdr.back = cpu_to_be32(from->back);
170 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
Dave Chinner517c2222013-04-24 18:58:55 +1000171 hdr3->__count = cpu_to_be16(from->count);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000172 hdr3->__level = cpu_to_be16(from->level);
173 return;
174 }
175 to->hdr.info.forw = cpu_to_be32(from->forw);
176 to->hdr.info.back = cpu_to_be32(from->back);
177 to->hdr.info.magic = cpu_to_be16(from->magic);
Dave Chinner517c2222013-04-24 18:58:55 +1000178 to->hdr.__count = cpu_to_be16(from->count);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000179 to->hdr.__level = cpu_to_be16(from->level);
180}
181
182static bool
183xfs_da3_node_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100184 struct xfs_buf *bp)
185{
186 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000187 struct xfs_da_intnode *hdr = bp->b_addr;
188 struct xfs_da3_icnode_hdr ichdr;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100189
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000190 xfs_da3_node_hdr_from_disk(&ichdr, hdr);
191
192 if (xfs_sb_version_hascrc(&mp->m_sb)) {
193 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
194
195 if (ichdr.magic != XFS_DA3_NODE_MAGIC)
196 return false;
197
198 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_uuid))
199 return false;
200 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
201 return false;
202 } else {
203 if (ichdr.magic != XFS_DA_NODE_MAGIC)
204 return false;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100205 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000206 if (ichdr.level == 0)
207 return false;
208 if (ichdr.level > XFS_DA_NODE_MAXDEPTH)
209 return false;
210 if (ichdr.count == 0)
211 return false;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100212
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000213 /*
214 * we don't know if the node is for and attribute or directory tree,
215 * so only fail if the count is outside both bounds
216 */
217 if (ichdr.count > mp->m_dir_node_ents &&
218 ichdr.count > mp->m_attr_node_ents)
219 return false;
220
221 /* XXX: hash order check? */
222
223 return true;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100224}
225
226static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000227xfs_da3_node_write_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100228 struct xfs_buf *bp)
229{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000230 struct xfs_mount *mp = bp->b_target->bt_mount;
231 struct xfs_buf_log_item *bip = bp->b_fspriv;
232 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
233
234 if (!xfs_da3_node_verify(bp)) {
235 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
236 xfs_buf_ioerror(bp, EFSCORRUPTED);
237 return;
238 }
239
240 if (!xfs_sb_version_hascrc(&mp->m_sb))
241 return;
242
243 if (bip)
244 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
245
246 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DA3_NODE_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100247}
248
Dave Chinner1813dd62012-11-14 17:54:40 +1100249/*
250 * leaf/node format detection on trees is sketchy, so a node read can be done on
251 * leaf level blocks when detection identifies the tree as a node format tree
252 * incorrectly. In this case, we need to swap the verifier to match the correct
253 * format of the block being read.
254 */
Dave Chinner612cfbf2012-11-14 17:52:32 +1100255static void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000256xfs_da3_node_read_verify(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100257 struct xfs_buf *bp)
258{
259 struct xfs_mount *mp = bp->b_target->bt_mount;
260 struct xfs_da_blkinfo *info = bp->b_addr;
261
262 switch (be16_to_cpu(info->magic)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000263 case XFS_DA3_NODE_MAGIC:
264 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
265 XFS_DA3_NODE_CRC_OFF))
266 break;
267 /* fall through */
Dave Chinnerd9392a42012-11-12 22:54:17 +1100268 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000269 if (!xfs_da3_node_verify(bp))
270 break;
271 return;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100272 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000273 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100274 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100275 return;
276 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000277 case XFS_DIR3_LEAFN_MAGIC:
278 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100279 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100280 return;
281 default:
282 break;
283 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000284
285 /* corrupt block */
286 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
287 xfs_buf_ioerror(bp, EFSCORRUPTED);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100288}
289
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000290const struct xfs_buf_ops xfs_da3_node_buf_ops = {
291 .verify_read = xfs_da3_node_read_verify,
292 .verify_write = xfs_da3_node_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100293};
294
295
Dave Chinnerd9392a42012-11-12 22:54:17 +1100296int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000297xfs_da3_node_read(
Dave Chinnerd9392a42012-11-12 22:54:17 +1100298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
300 xfs_dablk_t bno,
301 xfs_daddr_t mappedbno,
302 struct xfs_buf **bpp,
303 int which_fork)
304{
305 return xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000306 which_fork, &xfs_da3_node_buf_ops);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*========================================================================
310 * Routines used for growing the Btree.
311 *========================================================================*/
312
313/*
314 * Create the initial contents of an intermediate node.
315 */
316int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000317xfs_da3_node_create(
318 struct xfs_da_args *args,
319 xfs_dablk_t blkno,
320 int level,
321 struct xfs_buf **bpp,
322 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000324 struct xfs_da_intnode *node;
325 struct xfs_trans *tp = args->trans;
326 struct xfs_mount *mp = tp->t_mountp;
327 struct xfs_da3_icnode_hdr ichdr = {0};
328 struct xfs_buf *bp;
329 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dave Chinner5a5881c2012-03-22 05:15:13 +0000331 trace_xfs_da_node_create(args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000332 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
Dave Chinner5a5881c2012-03-22 05:15:13 +0000333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
335 if (error)
336 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000337 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000339 if (xfs_sb_version_hascrc(&mp->m_sb)) {
340 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
341
342 ichdr.magic = XFS_DA3_NODE_MAGIC;
343 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
344 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
345 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_uuid);
346 } else {
347 ichdr.magic = XFS_DA_NODE_MAGIC;
348 }
349 ichdr.level = level;
350
351 xfs_da3_node_hdr_to_disk(node, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000352 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000353 XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000355 bp->b_ops = &xfs_da3_node_buf_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *bpp = bp;
357 return(0);
358}
359
360/*
361 * Split a leaf node, rebalance, then possibly split
362 * intermediate nodes, rebalance, etc.
363 */
364int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000365xfs_da3_split(
366 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000368 struct xfs_da_state_blk *oldblk;
369 struct xfs_da_state_blk *newblk;
370 struct xfs_da_state_blk *addblk;
371 struct xfs_da_intnode *node;
372 struct xfs_buf *bp;
373 int max;
374 int action;
375 int error;
376 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dave Chinner5a5881c2012-03-22 05:15:13 +0000378 trace_xfs_da_split(state->args);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * Walk back up the tree splitting/inserting/adjusting as necessary.
382 * If we need to insert and there isn't room, split the node, then
383 * decide which fragment to insert the new block from below into.
384 * Note that we may split the root this way, but we need more fixup.
385 */
386 max = state->path.active - 1;
387 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
388 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000389 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 addblk = &state->path.blk[max]; /* initial dummy value */
392 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
393 oldblk = &state->path.blk[i];
394 newblk = &state->altpath.blk[i];
395
396 /*
397 * If a leaf node then
398 * Allocate a new leaf node, then rebalance across them.
399 * else if an intermediate node then
400 * We split on the last layer, must we split the node?
401 */
402 switch (oldblk->magic) {
403 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000404 error = xfs_attr3_leaf_split(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if ((error != 0) && (error != ENOSPC)) {
406 return(error); /* GROT: attr is inconsistent */
407 }
408 if (!error) {
409 addblk = newblk;
410 break;
411 }
412 /*
413 * Entry wouldn't fit, split the leaf again.
414 */
415 state->extravalid = 1;
416 if (state->inleaf) {
417 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000418 trace_xfs_attr_leaf_split_before(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000419 error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 &state->extrablk);
421 } else {
422 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000423 trace_xfs_attr_leaf_split_after(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000424 error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 &state->extrablk);
426 }
427 if (error)
428 return(error); /* GROT: attr inconsistent */
429 addblk = newblk;
430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 error = xfs_dir2_leafn_split(state, oldblk, newblk);
433 if (error)
434 return error;
435 addblk = newblk;
436 break;
437 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000438 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 addblk->bp = NULL;
441 if (error)
442 return(error); /* GROT: dir is inconsistent */
443 /*
444 * Record the newly split block for the next time thru?
445 */
446 if (action)
447 addblk = newblk;
448 else
449 addblk = NULL;
450 break;
451 }
452
453 /*
454 * Update the btree to show the new hashval for this child.
455 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000456 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 if (!addblk)
459 return(0);
460
461 /*
462 * Split the root node.
463 */
464 ASSERT(state->path.active == 0);
465 oldblk = &state->path.blk[0];
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000466 error = xfs_da3_root_split(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 addblk->bp = NULL;
469 return(error); /* GROT: dir is inconsistent */
470 }
471
472 /*
473 * Update pointers to the node which used to be block 0 and
474 * just got bumped because of the addition of a new root node.
475 * There might be three blocks involved if a double split occurred,
476 * and the original block 0 could be at any position in the list.
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000477 *
478 * Note: the magic numbers and sibling pointers are in the same
479 * physical place for both v2 and v3 headers (by design). Hence it
480 * doesn't matter which version of the xfs_da_intnode structure we use
481 * here as the result will be the same using either structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000483 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (node->hdr.info.forw) {
Nathan Scott89da0542006-03-17 17:28:40 +1100485 if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 bp = addblk->bp;
487 } else {
488 ASSERT(state->extravalid);
489 bp = state->extrablk.bp;
490 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000491 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100492 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000493 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 XFS_DA_LOGRANGE(node, &node->hdr.info,
495 sizeof(node->hdr.info)));
496 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000497 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100498 if (node->hdr.info.back) {
499 if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 bp = addblk->bp;
501 } else {
502 ASSERT(state->extravalid);
503 bp = state->extrablk.bp;
504 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000505 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100506 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000507 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 XFS_DA_LOGRANGE(node, &node->hdr.info,
509 sizeof(node->hdr.info)));
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 addblk->bp = NULL;
512 return(0);
513}
514
515/*
516 * Split the root. We have to create a new root and point to the two
517 * parts (the split old root) that we just created. Copy block zero to
518 * the EOF, extending the inode in process.
519 */
520STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000521xfs_da3_root_split(
522 struct xfs_da_state *state,
523 struct xfs_da_state_blk *blk1,
524 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000526 struct xfs_da_intnode *node;
527 struct xfs_da_intnode *oldroot;
528 struct xfs_da_node_entry *btree;
529 struct xfs_da3_icnode_hdr nodehdr;
530 struct xfs_da_args *args;
531 struct xfs_buf *bp;
532 struct xfs_inode *dp;
533 struct xfs_trans *tp;
534 struct xfs_mount *mp;
535 struct xfs_dir2_leaf *leaf;
536 xfs_dablk_t blkno;
537 int level;
538 int error;
539 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Dave Chinner5a5881c2012-03-22 05:15:13 +0000541 trace_xfs_da_root_split(state->args);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Copy the existing (incorrect) block from the root node position
545 * to a free space somewhere.
546 */
547 args = state->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 error = xfs_da_grow_inode(args, &blkno);
549 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000550 return error;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dp = args->dp;
553 tp = args->trans;
554 mp = state->mp;
555 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
556 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000557 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000558 node = bp->b_addr;
559 oldroot = blk1->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000560 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
561 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
562 struct xfs_da3_icnode_hdr nodehdr;
563
564 xfs_da3_node_hdr_from_disk(&nodehdr, oldroot);
565 btree = xfs_da3_node_tree_p(oldroot);
566 size = (int)((char *)&btree[nodehdr.count] - (char *)oldroot);
567 level = nodehdr.level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000569 struct xfs_dir3_icleaf_hdr leafhdr;
570 struct xfs_dir2_leaf_entry *ents;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 leaf = (xfs_dir2_leaf_t *)oldroot;
Dave Chinner24df33b2013-04-12 07:30:21 +1000573 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
574 ents = xfs_dir3_leaf_ents_p(leaf);
575
576 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
577 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
578 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000579 level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000581
582 /*
583 * we can copy most of the information in the node from one block to
584 * another, but for CRC enabled headers we have to make sure that the
585 * block specific identifiers are kept intact. We update the buffer
586 * directly for this.
587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 memcpy(node, oldroot, size);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000589 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
590 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
591 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
592
593 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
594 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000595 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100596
Dave Chinner1813dd62012-11-14 17:54:40 +1100597 bp->b_ops = blk1->bp->b_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 blk1->bp = bp;
599 blk1->blkno = blkno;
600
601 /*
602 * Set up the new root node.
603 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000604 error = xfs_da3_node_create(args,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000605 (args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000606 level + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000608 return error;
609
Dave Chinner1d9025e2012-06-22 18:50:14 +1000610 node = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000611 xfs_da3_node_hdr_from_disk(&nodehdr, node);
612 btree = xfs_da3_node_tree_p(node);
613 btree[0].hashval = cpu_to_be32(blk1->hashval);
614 btree[0].before = cpu_to_be32(blk1->blkno);
615 btree[1].hashval = cpu_to_be32(blk2->hashval);
616 btree[1].before = cpu_to_be32(blk2->blkno);
617 nodehdr.count = 2;
618 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000621 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
622 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ASSERT(blk1->blkno >= mp->m_dirleafblk &&
624 blk1->blkno < mp->m_dirfreeblk);
625 ASSERT(blk2->blkno >= mp->m_dirleafblk &&
626 blk2->blkno < mp->m_dirfreeblk);
627 }
628#endif
629
630 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000631 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000632 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637/*
638 * Split the node, rebalance, then add the new entry.
639 */
640STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000641xfs_da3_node_split(
642 struct xfs_da_state *state,
643 struct xfs_da_state_blk *oldblk,
644 struct xfs_da_state_blk *newblk,
645 struct xfs_da_state_blk *addblk,
646 int treelevel,
647 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000649 struct xfs_da_intnode *node;
650 struct xfs_da3_icnode_hdr nodehdr;
651 xfs_dablk_t blkno;
652 int newcount;
653 int error;
654 int useextra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Chinner5a5881c2012-03-22 05:15:13 +0000656 trace_xfs_da_node_split(state->args);
657
Dave Chinner1d9025e2012-06-22 18:50:14 +1000658 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000659 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000662 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000664 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 newcount = 1 + useextra;
666 /*
667 * Do we have to split the node?
668 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000669 if (nodehdr.count + newcount > state->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Allocate a new node, add to the doubly linked chain of
672 * nodes, then move some of our excess entries into it.
673 */
674 error = xfs_da_grow_inode(state->args, &blkno);
675 if (error)
676 return(error); /* GROT: dir is inconsistent */
677
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000678 error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 &newblk->bp, state->args->whichfork);
680 if (error)
681 return(error); /* GROT: dir is inconsistent */
682 newblk->blkno = blkno;
683 newblk->magic = XFS_DA_NODE_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000684 xfs_da3_node_rebalance(state, oldblk, newblk);
685 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (error)
687 return(error);
688 *result = 1;
689 } else {
690 *result = 0;
691 }
692
693 /*
694 * Insert the new entry(s) into the correct block
695 * (updating last hashval in the process).
696 *
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000697 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 * and as a result of using node_lookup_int() we always
699 * point to a valid entry (not after one), but a split
700 * operation always results in a new block whose hashvals
701 * FOLLOW the current block.
702 *
703 * If we had double-split op below us, then add the extra block too.
704 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000705 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000706 xfs_da3_node_hdr_from_disk(&nodehdr, node);
707 if (oldblk->index <= nodehdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000709 xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (useextra) {
711 if (state->extraafter)
712 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000713 xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 state->extravalid = 0;
715 }
716 } else {
717 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000718 xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (useextra) {
720 if (state->extraafter)
721 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000722 xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 state->extravalid = 0;
724 }
725 }
726
727 return(0);
728}
729
730/*
731 * Balance the btree elements between two intermediate nodes,
732 * usually one full and one empty.
733 *
734 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
735 */
736STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000737xfs_da3_node_rebalance(
738 struct xfs_da_state *state,
739 struct xfs_da_state_blk *blk1,
740 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000742 struct xfs_da_intnode *node1;
743 struct xfs_da_intnode *node2;
744 struct xfs_da_intnode *tmpnode;
745 struct xfs_da_node_entry *btree1;
746 struct xfs_da_node_entry *btree2;
747 struct xfs_da_node_entry *btree_s;
748 struct xfs_da_node_entry *btree_d;
749 struct xfs_da3_icnode_hdr nodehdr1;
750 struct xfs_da3_icnode_hdr nodehdr2;
751 struct xfs_trans *tp;
752 int count;
753 int tmp;
754 int swap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Chinner5a5881c2012-03-22 05:15:13 +0000756 trace_xfs_da_node_rebalance(state->args);
757
Dave Chinner1d9025e2012-06-22 18:50:14 +1000758 node1 = blk1->bp->b_addr;
759 node2 = blk2->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000760 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
761 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
762 btree1 = xfs_da3_node_tree_p(node1);
763 btree2 = xfs_da3_node_tree_p(node2);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * Figure out how many entries need to move, and in which direction.
767 * Swap the nodes around if that makes it simpler.
768 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000769 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
770 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
771 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
772 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 tmpnode = node1;
774 node1 = node2;
775 node2 = tmpnode;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000776 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
777 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
778 btree1 = xfs_da3_node_tree_p(node1);
779 btree2 = xfs_da3_node_tree_p(node2);
780 swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000782
783 count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (count == 0)
785 return;
786 tp = state->args->trans;
787 /*
788 * Two cases: high-to-low and low-to-high.
789 */
790 if (count > 0) {
791 /*
792 * Move elements in node2 up to make a hole.
793 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000794 tmp = nodehdr2.count;
795 if (tmp > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000797 btree_s = &btree2[0];
798 btree_d = &btree2[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 memmove(btree_d, btree_s, tmp);
800 }
801
802 /*
803 * Move the req'd B-tree elements from high in node1 to
804 * low in node2.
805 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000806 nodehdr2.count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000808 btree_s = &btree1[nodehdr1.count - count];
809 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000811 nodehdr1.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 } else {
813 /*
814 * Move the req'd B-tree elements from low in node2 to
815 * high in node1.
816 */
817 count = -count;
818 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000819 btree_s = &btree2[0];
820 btree_d = &btree1[nodehdr1.count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000822 nodehdr1.count += count;
823
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 XFS_DA_LOGRANGE(node1, btree_d, tmp));
826
827 /*
828 * Move elements in node2 down to fill the hole.
829 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000830 tmp = nodehdr2.count - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000832 btree_s = &btree2[count];
833 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memmove(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000835 nodehdr2.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
838 /*
839 * Log header of node 1 and all current bits of node 2.
840 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000841 xfs_da3_node_hdr_to_disk(node1, &nodehdr1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000842 xfs_trans_log_buf(tp, blk1->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000843 XFS_DA_LOGRANGE(node1, &node1->hdr,
844 xfs_da3_node_hdr_size(node1)));
845
846 xfs_da3_node_hdr_to_disk(node2, &nodehdr2);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 XFS_DA_LOGRANGE(node2, &node2->hdr,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000849 xfs_da3_node_hdr_size(node2) +
850 (sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Record the last hashval from each block for upward propagation.
854 * (note: don't use the swapped node pointers)
855 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000856 if (swap) {
857 node1 = blk1->bp->b_addr;
858 node2 = blk2->bp->b_addr;
859 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
860 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
861 btree1 = xfs_da3_node_tree_p(node1);
862 btree2 = xfs_da3_node_tree_p(node2);
863 }
864 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
865 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * Adjust the expected index for insertion.
869 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000870 if (blk1->index >= nodehdr1.count) {
871 blk2->index = blk1->index - nodehdr1.count;
872 blk1->index = nodehdr1.count + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874}
875
876/*
877 * Add a new entry to an intermediate node.
878 */
879STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000880xfs_da3_node_add(
881 struct xfs_da_state *state,
882 struct xfs_da_state_blk *oldblk,
883 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000885 struct xfs_da_intnode *node;
886 struct xfs_da3_icnode_hdr nodehdr;
887 struct xfs_da_node_entry *btree;
888 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dave Chinner5a5881c2012-03-22 05:15:13 +0000890 trace_xfs_da_node_add(state->args);
891
Dave Chinner1d9025e2012-06-22 18:50:14 +1000892 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000893 xfs_da3_node_hdr_from_disk(&nodehdr, node);
894 btree = xfs_da3_node_tree_p(node);
895
896 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000898 if (state->args->whichfork == XFS_DATA_FORK)
Christoph Hellwig73523a22010-07-20 17:54:45 +1000899 ASSERT(newblk->blkno >= state->mp->m_dirleafblk &&
900 newblk->blkno < state->mp->m_dirfreeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /*
903 * We may need to make some room before we insert the new node.
904 */
905 tmp = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000906 if (oldblk->index < nodehdr.count) {
907 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
908 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000910 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
911 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000912 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000913 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
914 tmp + sizeof(*btree)));
915
916 nodehdr.count += 1;
917 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000918 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000919 XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /*
922 * Copy the last hash value from the oldblk to propagate upwards.
923 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000924 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/*========================================================================
928 * Routines used for shrinking the Btree.
929 *========================================================================*/
930
931/*
932 * Deallocate an empty leaf node, remove it from its parent,
933 * possibly deallocating that block, etc...
934 */
935int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000936xfs_da3_join(
937 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000939 struct xfs_da_state_blk *drop_blk;
940 struct xfs_da_state_blk *save_blk;
941 int action = 0;
942 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dave Chinner5a5881c2012-03-22 05:15:13 +0000944 trace_xfs_da_join(state->args);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 drop_blk = &state->path.blk[ state->path.active-1 ];
947 save_blk = &state->altpath.blk[ state->path.active-1 ];
948 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
949 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000950 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /*
953 * Walk back up the tree joining/deallocating as necessary.
954 * When we stop dropping blocks, break out.
955 */
956 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
957 state->path.active--) {
958 /*
959 * See if we can combine the block with a neighbor.
960 * (action == 0) => no options, just leave
961 * (action == 1) => coalesce, then unlink
962 * (action == 2) => block empty, unlink it
963 */
964 switch (drop_blk->magic) {
965 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000966 error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (error)
968 return(error);
969 if (action == 0)
970 return(0);
Dave Chinner517c2222013-04-24 18:58:55 +1000971 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 error = xfs_dir2_leafn_toosmall(state, &action);
975 if (error)
976 return error;
977 if (action == 0)
978 return 0;
979 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
980 break;
981 case XFS_DA_NODE_MAGIC:
982 /*
983 * Remove the offending node, fixup hashvals,
984 * check for a toosmall neighbor.
985 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000986 xfs_da3_node_remove(state, drop_blk);
987 xfs_da3_fixhashpath(state, &state->path);
988 error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (error)
990 return(error);
991 if (action == 0)
992 return 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000993 xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000996 xfs_da3_fixhashpath(state, &state->altpath);
997 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 xfs_da_state_kill_altpath(state);
999 if (error)
1000 return(error);
1001 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1002 drop_blk->bp);
1003 drop_blk->bp = NULL;
1004 if (error)
1005 return(error);
1006 }
1007 /*
1008 * We joined all the way to the top. If it turns out that
1009 * we only have one entry in the root, make the child block
1010 * the new root.
1011 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001012 xfs_da3_node_remove(state, drop_blk);
1013 xfs_da3_fixhashpath(state, &state->path);
1014 error = xfs_da3_root_join(state, &state->path.blk[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return(error);
1016}
1017
Alex Elder1c4f3322011-07-18 18:14:09 +00001018#ifdef DEBUG
1019static void
1020xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1021{
1022 __be16 magic = blkinfo->magic;
1023
1024 if (level == 1) {
1025 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001026 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001027 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1028 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001029 } else {
1030 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1031 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1032 }
Alex Elder1c4f3322011-07-18 18:14:09 +00001033 ASSERT(!blkinfo->forw);
1034 ASSERT(!blkinfo->back);
1035}
1036#else /* !DEBUG */
1037#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1038#endif /* !DEBUG */
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
1041 * We have only one entry in the root. Copy the only remaining child of
1042 * the old root to block 0 as the new root node.
1043 */
1044STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001045xfs_da3_root_join(
1046 struct xfs_da_state *state,
1047 struct xfs_da_state_blk *root_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001049 struct xfs_da_intnode *oldroot;
1050 struct xfs_da_args *args;
1051 xfs_dablk_t child;
1052 struct xfs_buf *bp;
1053 struct xfs_da3_icnode_hdr oldroothdr;
1054 struct xfs_da_node_entry *btree;
1055 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Chinner5a5881c2012-03-22 05:15:13 +00001057 trace_xfs_da_root_join(state->args);
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001060
1061 args = state->args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001062 oldroot = root_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001063 xfs_da3_node_hdr_from_disk(&oldroothdr, oldroot);
1064 ASSERT(oldroothdr.forw == 0);
1065 ASSERT(oldroothdr.back == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /*
1068 * If the root has more than one child, then don't do anything.
1069 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001070 if (oldroothdr.count > 1)
1071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
1074 * Read in the (only) child block, then copy those bytes into
1075 * the root block's buffer and free the original child block.
1076 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001077 btree = xfs_da3_node_tree_p(oldroot);
1078 child = be32_to_cpu(btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ASSERT(child != 0);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001080 error = xfs_da3_node_read(args->trans, args->dp, child, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001081 args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001083 return error;
1084 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
Alex Elder1c4f3322011-07-18 18:14:09 +00001085
Dave Chinner612cfbf2012-11-14 17:52:32 +11001086 /*
1087 * This could be copying a leaf back into the root block in the case of
1088 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +11001089 * to update the b_ops pointer as well to match the buffer type change
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001090 * that could occur. For dir3 blocks we also need to update the block
1091 * number in the buffer header.
Dave Chinner612cfbf2012-11-14 17:52:32 +11001092 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001093 memcpy(root_blk->bp->b_addr, bp->b_addr, state->blocksize);
Dave Chinner1813dd62012-11-14 17:54:40 +11001094 root_blk->bp->b_ops = bp->b_ops;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001095 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1096 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1097 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1098 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001099 xfs_trans_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 error = xfs_da_shrink_inode(args, child, bp);
1101 return(error);
1102}
1103
1104/*
1105 * Check a node block and its neighbors to see if the block should be
1106 * collapsed into one or the other neighbor. Always keep the block
1107 * with the smaller block number.
1108 * If the current block is over 50% full, don't try to join it, return 0.
1109 * If the block is empty, fill in the state structure and return 2.
1110 * If it can be collapsed, fill in the state structure and return 1.
1111 * If nothing can be done, return 0.
1112 */
1113STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001114xfs_da3_node_toosmall(
1115 struct xfs_da_state *state,
1116 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001118 struct xfs_da_intnode *node;
1119 struct xfs_da_state_blk *blk;
1120 struct xfs_da_blkinfo *info;
1121 xfs_dablk_t blkno;
1122 struct xfs_buf *bp;
1123 struct xfs_da3_icnode_hdr nodehdr;
1124 int count;
1125 int forward;
1126 int error;
1127 int retval;
1128 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Dave Chinneree732592012-11-12 22:53:53 +11001130 trace_xfs_da_node_toosmall(state->args);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Check for the degenerate case of the block being over 50% full.
1134 * If so, it's not worth even looking to see if we might be able
1135 * to coalesce with a sibling.
1136 */
1137 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001138 info = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 node = (xfs_da_intnode_t *)info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001140 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1141 if (nodehdr.count > (state->node_ents >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *action = 0; /* blk over 50%, don't try to join */
1143 return(0); /* blk over 50%, don't try to join */
1144 }
1145
1146 /*
1147 * Check for the degenerate case of the block being empty.
1148 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001149 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * to merge with the forward block unless it is NULL.
1151 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001152 if (nodehdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Make altpath point to the block we want to keep and
1155 * path point to the block we want to drop (this one).
1156 */
Nathan Scott89da0542006-03-17 17:28:40 +11001157 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001159 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 0, &retval);
1161 if (error)
1162 return(error);
1163 if (retval) {
1164 *action = 0;
1165 } else {
1166 *action = 2;
1167 }
1168 return(0);
1169 }
1170
1171 /*
1172 * Examine each sibling block to see if we can coalesce with
1173 * at least 25% free space to spare. We need to figure out
1174 * whether to merge with the forward or the backward block.
1175 * We prefer coalescing with the lower numbered sibling so as
1176 * to shrink a directory over time.
1177 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001178 count = state->node_ents;
1179 count -= state->node_ents >> 2;
1180 count -= nodehdr.count;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 /* start with smaller blk num */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001183 forward = nodehdr.forw < nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 for (i = 0; i < 2; forward = !forward, i++) {
1185 if (forward)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001186 blkno = nodehdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001188 blkno = nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (blkno == 0)
1190 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001191 error = xfs_da3_node_read(state->args->trans, state->args->dp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001192 blkno, -1, &bp, state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (error)
1194 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Dave Chinner1d9025e2012-06-22 18:50:14 +10001196 node = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001197 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001198 xfs_trans_brelse(state->args->trans, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001199
1200 if (count - nodehdr.count >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break; /* fits with at least 25% to spare */
1202 }
1203 if (i >= 2) {
1204 *action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 /*
1209 * Make altpath point to the block we want to keep (the lower
1210 * numbered block) and path point to the block we want to drop.
1211 */
1212 memcpy(&state->altpath, &state->path, sizeof(state->path));
1213 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001214 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001217 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 0, &retval);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001219 }
1220 if (error)
1221 return error;
1222 if (retval) {
1223 *action = 0;
1224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 *action = 1;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001227 return 0;
1228}
1229
1230/*
1231 * Pick up the last hashvalue from an intermediate node.
1232 */
1233STATIC uint
1234xfs_da3_node_lasthash(
1235 struct xfs_buf *bp,
1236 int *count)
1237{
1238 struct xfs_da_intnode *node;
1239 struct xfs_da_node_entry *btree;
1240 struct xfs_da3_icnode_hdr nodehdr;
1241
1242 node = bp->b_addr;
1243 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1244 if (count)
1245 *count = nodehdr.count;
1246 if (!nodehdr.count)
1247 return 0;
1248 btree = xfs_da3_node_tree_p(node);
1249 return be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252/*
1253 * Walk back up the tree adjusting hash values as necessary,
1254 * when we stop making changes, return.
1255 */
1256void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001257xfs_da3_fixhashpath(
1258 struct xfs_da_state *state,
1259 struct xfs_da_state_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001261 struct xfs_da_state_blk *blk;
1262 struct xfs_da_intnode *node;
1263 struct xfs_da_node_entry *btree;
1264 xfs_dahash_t lasthash=0;
1265 int level;
1266 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Dave Chinneree732592012-11-12 22:53:53 +11001268 trace_xfs_da_fixhashpath(state->args);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 level = path->active-1;
1271 blk = &path->blk[ level ];
1272 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 case XFS_ATTR_LEAF_MAGIC:
1274 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1275 if (count == 0)
1276 return;
1277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
1280 if (count == 0)
1281 return;
1282 break;
1283 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001284 lasthash = xfs_da3_node_lasthash(blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (count == 0)
1286 return;
1287 break;
1288 }
1289 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001290 struct xfs_da3_icnode_hdr nodehdr;
1291
Dave Chinner1d9025e2012-06-22 18:50:14 +10001292 node = blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001293 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1294 btree = xfs_da3_node_tree_p(node);
Nathan Scott403432d2006-03-17 17:29:46 +11001295 if (be32_to_cpu(btree->hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 break;
1297 blk->hashval = lasthash;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001298 btree[blk->index].hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 xfs_trans_log_buf(state->args->trans, blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001300 XFS_DA_LOGRANGE(node, &btree[blk->index],
1301 sizeof(*btree)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001303 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305}
1306
1307/*
1308 * Remove an entry from an intermediate node.
1309 */
1310STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001311xfs_da3_node_remove(
1312 struct xfs_da_state *state,
1313 struct xfs_da_state_blk *drop_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001315 struct xfs_da_intnode *node;
1316 struct xfs_da3_icnode_hdr nodehdr;
1317 struct xfs_da_node_entry *btree;
1318 int index;
1319 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Dave Chinner5a5881c2012-03-22 05:15:13 +00001321 trace_xfs_da_node_remove(state->args);
1322
Dave Chinner1d9025e2012-06-22 18:50:14 +10001323 node = drop_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001324 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1325 ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ASSERT(drop_blk->index >= 0);
1327
1328 /*
1329 * Copy over the offending entry, or just zero it out.
1330 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001331 index = drop_blk->index;
1332 btree = xfs_da3_node_tree_p(node);
1333 if (index < nodehdr.count - 1) {
1334 tmp = nodehdr.count - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001336 memmove(&btree[index], &btree[index + 1], tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001337 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001338 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1339 index = nodehdr.count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001341 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001342 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001343 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1344 nodehdr.count -= 1;
1345 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001346 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001347 XFS_DA_LOGRANGE(node, &node->hdr, xfs_da3_node_hdr_size(node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /*
1350 * Copy the last hash value from the block to propagate upwards.
1351 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001352 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001356 * Unbalance the elements between two intermediate nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * move all Btree elements from one node into another.
1358 */
1359STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001360xfs_da3_node_unbalance(
1361 struct xfs_da_state *state,
1362 struct xfs_da_state_blk *drop_blk,
1363 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001365 struct xfs_da_intnode *drop_node;
1366 struct xfs_da_intnode *save_node;
1367 struct xfs_da_node_entry *drop_btree;
1368 struct xfs_da_node_entry *save_btree;
1369 struct xfs_da3_icnode_hdr drop_hdr;
1370 struct xfs_da3_icnode_hdr save_hdr;
1371 struct xfs_trans *tp;
1372 int sindex;
1373 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Dave Chinner5a5881c2012-03-22 05:15:13 +00001375 trace_xfs_da_node_unbalance(state->args);
1376
Dave Chinner1d9025e2012-06-22 18:50:14 +10001377 drop_node = drop_blk->bp->b_addr;
1378 save_node = save_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001379 xfs_da3_node_hdr_from_disk(&drop_hdr, drop_node);
1380 xfs_da3_node_hdr_from_disk(&save_hdr, save_node);
1381 drop_btree = xfs_da3_node_tree_p(drop_node);
1382 save_btree = xfs_da3_node_tree_p(save_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 tp = state->args->trans;
1384
1385 /*
1386 * If the dying block has lower hashvals, then move all the
1387 * elements in the remaining block up to make a hole.
1388 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001389 if ((be32_to_cpu(drop_btree[0].hashval) <
1390 be32_to_cpu(save_btree[0].hashval)) ||
1391 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1392 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1393 /* XXX: check this - is memmove dst correct? */
1394 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1395 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1396
1397 sindex = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001398 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001399 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1400 (save_hdr.count + drop_hdr.count) *
1401 sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001403 sindex = save_hdr.count;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001404 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001405 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1406 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
1409 /*
1410 * Move all the B-tree elements from drop_blk to save_blk.
1411 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001412 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1413 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1414 save_hdr.count += drop_hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001416 xfs_da3_node_hdr_to_disk(save_node, &save_hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001417 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001419 xfs_da3_node_hdr_size(save_node)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * Save the last hashval in the remaining block for upward propagation.
1423 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001424 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
1427/*========================================================================
1428 * Routines used for finding things in the Btree.
1429 *========================================================================*/
1430
1431/*
1432 * Walk down the Btree looking for a particular filename, filling
1433 * in the state structure as we go.
1434 *
1435 * We will set the state structure to point to each of the elements
1436 * in each of the nodes where either the hashval is or should be.
1437 *
1438 * We support duplicate hashval's so for each entry in the current
1439 * node that could contain the desired hashval, descend. This is a
1440 * pruned depth-first tree search.
1441 */
1442int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001443xfs_da3_node_lookup_int(
1444 struct xfs_da_state *state,
1445 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001447 struct xfs_da_state_blk *blk;
1448 struct xfs_da_blkinfo *curr;
1449 struct xfs_da_intnode *node;
1450 struct xfs_da_node_entry *btree;
1451 struct xfs_da3_icnode_hdr nodehdr;
1452 struct xfs_da_args *args;
1453 xfs_dablk_t blkno;
1454 xfs_dahash_t hashval;
1455 xfs_dahash_t btreehashval;
1456 int probe;
1457 int span;
1458 int max;
1459 int error;
1460 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 args = state->args;
1463
1464 /*
1465 * Descend thru the B-tree searching each level for the right
1466 * node to use, until the right hashval is found.
1467 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001468 blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 for (blk = &state->path.blk[0], state->path.active = 1;
1470 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1471 blk++, state->path.active++) {
1472 /*
1473 * Read the next node down in the tree.
1474 */
1475 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001476 error = xfs_da3_node_read(args->trans, args->dp, blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001477 -1, &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (error) {
1479 blk->blkno = 0;
1480 state->path.active--;
1481 return(error);
1482 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001483 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001484 blk->magic = be16_to_cpu(curr->magic);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001485
Dave Chinner517c2222013-04-24 18:58:55 +10001486 if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
1487 blk->magic == XFS_ATTR3_LEAF_MAGIC) {
1488 blk->magic = XFS_ATTR_LEAF_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001489 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1490 break;
1491 }
1492
1493 if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1494 blk->magic == XFS_DIR3_LEAFN_MAGIC) {
1495 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1496 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
1497 break;
1498 }
1499
1500 blk->magic = XFS_DA_NODE_MAGIC;
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
1504 * Search an intermediate node for a match.
1505 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001506 node = blk->bp->b_addr;
1507 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1508 btree = xfs_da3_node_tree_p(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001510 max = nodehdr.count;
1511 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001513 /*
1514 * Binary search. (note: small blocks will skip loop)
1515 */
1516 probe = span = max / 2;
1517 hashval = args->hashval;
1518 while (span > 4) {
1519 span /= 2;
1520 btreehashval = be32_to_cpu(btree[probe].hashval);
1521 if (btreehashval < hashval)
1522 probe += span;
1523 else if (btreehashval > hashval)
1524 probe -= span;
1525 else
1526 break;
1527 }
1528 ASSERT((probe >= 0) && (probe < max));
1529 ASSERT((span <= 4) ||
1530 (be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001532 /*
1533 * Since we may have duplicate hashval's, find the first
1534 * matching hashval in the node.
1535 */
1536 while (probe > 0 &&
1537 be32_to_cpu(btree[probe].hashval) >= hashval) {
1538 probe--;
1539 }
1540 while (probe < max &&
1541 be32_to_cpu(btree[probe].hashval) < hashval) {
1542 probe++;
1543 }
1544
1545 /*
1546 * Pick the right block to descend on.
1547 */
1548 if (probe == max) {
1549 blk->index = max - 1;
1550 blkno = be32_to_cpu(btree[max - 1].before);
1551 } else {
1552 blk->index = probe;
1553 blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555 }
1556
1557 /*
1558 * A leaf block that ends in the hashval that we are interested in
1559 * (final hashval == search hashval) means that the next block may
1560 * contain more entries with the same hashval, shift upward to the
1561 * next leaf and keep searching.
1562 */
1563 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001564 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1566 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001567 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001568 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 blk->index = args->index;
1570 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001571 } else {
1572 ASSERT(0);
1573 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1576 (blk->hashval == args->hashval)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001577 error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 &retval);
1579 if (error)
1580 return(error);
1581 if (retval == 0) {
1582 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001583 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* path_shift() gives ENOENT */
1585 retval = XFS_ERROR(ENOATTR);
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 break;
1589 }
1590 *result = retval;
1591 return(0);
1592}
1593
1594/*========================================================================
1595 * Utility routines.
1596 *========================================================================*/
1597
1598/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001599 * Compare two intermediate nodes for "order".
1600 */
1601STATIC int
1602xfs_da3_node_order(
1603 struct xfs_buf *node1_bp,
1604 struct xfs_buf *node2_bp)
1605{
1606 struct xfs_da_intnode *node1;
1607 struct xfs_da_intnode *node2;
1608 struct xfs_da_node_entry *btree1;
1609 struct xfs_da_node_entry *btree2;
1610 struct xfs_da3_icnode_hdr node1hdr;
1611 struct xfs_da3_icnode_hdr node2hdr;
1612
1613 node1 = node1_bp->b_addr;
1614 node2 = node2_bp->b_addr;
1615 xfs_da3_node_hdr_from_disk(&node1hdr, node1);
1616 xfs_da3_node_hdr_from_disk(&node2hdr, node2);
1617 btree1 = xfs_da3_node_tree_p(node1);
1618 btree2 = xfs_da3_node_tree_p(node2);
1619
1620 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1621 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1622 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1623 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1624 return 1;
1625 }
1626 return 0;
1627}
1628
1629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * Link a new block into a doubly linked list of blocks (of whatever type).
1631 */
1632int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001633xfs_da3_blk_link(
1634 struct xfs_da_state *state,
1635 struct xfs_da_state_blk *old_blk,
1636 struct xfs_da_state_blk *new_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001638 struct xfs_da_blkinfo *old_info;
1639 struct xfs_da_blkinfo *new_info;
1640 struct xfs_da_blkinfo *tmp_info;
1641 struct xfs_da_args *args;
1642 struct xfs_buf *bp;
1643 int before = 0;
1644 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /*
1647 * Set up environment.
1648 */
1649 args = state->args;
1650 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001651 old_info = old_blk->bp->b_addr;
1652 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001654 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 case XFS_ATTR_LEAF_MAGIC:
1659 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
1663 break;
1664 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001665 before = xfs_da3_node_order(old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 }
1668
1669 /*
1670 * Link blocks in appropriate order.
1671 */
1672 if (before) {
1673 /*
1674 * Link new block in before existing block.
1675 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001676 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001677 new_info->forw = cpu_to_be32(old_blk->blkno);
1678 new_info->back = old_info->back;
1679 if (old_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001680 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001681 be32_to_cpu(old_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001682 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (error)
1684 return(error);
1685 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001686 tmp_info = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001687 ASSERT(tmp_info->magic == old_info->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001688 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1689 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001690 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Nathan Scott89da0542006-03-17 17:28:40 +11001692 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 } else {
1694 /*
1695 * Link new block in after existing block.
1696 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001697 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001698 new_info->forw = old_info->forw;
1699 new_info->back = cpu_to_be32(old_blk->blkno);
1700 if (old_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001701 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001702 be32_to_cpu(old_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001703 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (error)
1705 return(error);
1706 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001707 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001708 ASSERT(tmp_info->magic == old_info->magic);
1709 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1710 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001711 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Nathan Scott89da0542006-03-17 17:28:40 +11001713 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
Dave Chinner1d9025e2012-06-22 18:50:14 +10001716 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1717 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return(0);
1719}
1720
1721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * Unlink a block from a doubly linked list of blocks.
1723 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001724STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001725xfs_da3_blk_unlink(
1726 struct xfs_da_state *state,
1727 struct xfs_da_state_blk *drop_blk,
1728 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001730 struct xfs_da_blkinfo *drop_info;
1731 struct xfs_da_blkinfo *save_info;
1732 struct xfs_da_blkinfo *tmp_info;
1733 struct xfs_da_args *args;
1734 struct xfs_buf *bp;
1735 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /*
1738 * Set up environment.
1739 */
1740 args = state->args;
1741 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001742 save_info = save_blk->bp->b_addr;
1743 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001745 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001748 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1749 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1750 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1751 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /*
1754 * Unlink the leaf block from the doubly linked chain of leaves.
1755 */
Nathan Scott89da0542006-03-17 17:28:40 +11001756 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001757 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001758 save_info->back = drop_info->back;
1759 if (drop_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001760 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001761 be32_to_cpu(drop_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001762 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (error)
1764 return(error);
1765 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001766 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001767 ASSERT(tmp_info->magic == save_info->magic);
1768 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1769 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001770 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001774 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001775 save_info->forw = drop_info->forw;
1776 if (drop_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001777 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001778 be32_to_cpu(drop_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001779 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (error)
1781 return(error);
1782 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001783 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001784 ASSERT(tmp_info->magic == save_info->magic);
1785 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1786 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001787 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790 }
1791
Dave Chinner1d9025e2012-06-22 18:50:14 +10001792 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return(0);
1794}
1795
1796/*
1797 * Move a path "forward" or "!forward" one block at the current level.
1798 *
1799 * This routine will adjust a "path" to point to the next block
1800 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1801 * Btree, including updating pointers to the intermediate nodes between
1802 * the new bottom and the root.
1803 */
1804int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001805xfs_da3_path_shift(
1806 struct xfs_da_state *state,
1807 struct xfs_da_state_path *path,
1808 int forward,
1809 int release,
1810 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001812 struct xfs_da_state_blk *blk;
1813 struct xfs_da_blkinfo *info;
1814 struct xfs_da_intnode *node;
1815 struct xfs_da_args *args;
1816 struct xfs_da_node_entry *btree;
1817 struct xfs_da3_icnode_hdr nodehdr;
1818 xfs_dablk_t blkno = 0;
1819 int level;
1820 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Dave Chinneree732592012-11-12 22:53:53 +11001822 trace_xfs_da_path_shift(state->args);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Roll up the Btree looking for the first block where our
1826 * current index is not at the edge of the block. Note that
1827 * we skip the bottom layer because we want the sibling block.
1828 */
1829 args = state->args;
1830 ASSERT(args != NULL);
1831 ASSERT(path != NULL);
1832 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1833 level = (path->active-1) - 1; /* skip bottom layer in path */
1834 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001835 node = blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001836 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1837 btree = xfs_da3_node_tree_p(node);
1838
1839 if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 blk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001841 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 break;
1843 } else if (!forward && (blk->index > 0)) {
1844 blk->index--;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001845 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
1847 }
1848 }
1849 if (level < 0) {
1850 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001851 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return(0);
1853 }
1854
1855 /*
1856 * Roll down the edge of the subtree until we reach the
1857 * same depth we were at originally.
1858 */
1859 for (blk++, level++; level < path->active; blk++, level++) {
1860 /*
1861 * Release the old block.
1862 * (if it's dirty, trans won't actually let go)
1863 */
1864 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001865 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /*
1868 * Read the next child block.
1869 */
1870 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001871 error = xfs_da3_node_read(args->trans, args->dp, blkno, -1,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001872 &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (error)
1874 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001875 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001876 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001877 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001878 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001879 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001880 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1881 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001882
1883
1884 /*
1885 * Note: we flatten the magic number to a single type so we
1886 * don't have to compare against crc/non-crc types elsewhere.
1887 */
1888 switch (be16_to_cpu(info->magic)) {
1889 case XFS_DA_NODE_MAGIC:
1890 case XFS_DA3_NODE_MAGIC:
1891 blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 node = (xfs_da_intnode_t *)info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001893 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1894 btree = xfs_da3_node_tree_p(node);
1895 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (forward)
1897 blk->index = 0;
1898 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001899 blk->index = nodehdr.count - 1;
1900 blkno = be32_to_cpu(btree[blk->index].before);
1901 break;
1902 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001903 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001904 blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 ASSERT(level == path->active-1);
1906 blk->index = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001907 blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
1908 NULL);
1909 break;
1910 case XFS_DIR2_LEAFN_MAGIC:
1911 case XFS_DIR3_LEAFN_MAGIC:
1912 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1913 ASSERT(level == path->active-1);
1914 blk->index = 0;
1915 blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
1916 NULL);
1917 break;
1918 default:
1919 ASSERT(0);
1920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 }
1923 *result = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
1927
1928/*========================================================================
1929 * Utility routines.
1930 *========================================================================*/
1931
1932/*
1933 * Implement a simple hash on a character string.
1934 * Rotate the hash value by 7 bits, then XOR each character in.
1935 * This is implemented with some source-level loop unrolling.
1936 */
1937xfs_dahash_t
Christoph Hellwiga5687782009-02-09 08:37:39 +01001938xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 xfs_dahash_t hash;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /*
1943 * Do four characters at a time as long as we can.
1944 */
1945 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
1946 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
1947 (name[3] << 0) ^ rol32(hash, 7 * 4);
1948
1949 /*
1950 * Now do the rest of the characters.
1951 */
1952 switch (namelen) {
1953 case 3:
1954 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
1955 rol32(hash, 7 * 3);
1956 case 2:
1957 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
1958 case 1:
1959 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11001960 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return hash;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Barry Naujok5163f952008-05-21 16:41:01 +10001965enum xfs_dacmp
1966xfs_da_compname(
1967 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11001968 const unsigned char *name,
1969 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10001970{
1971 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
1972 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
1973}
1974
1975static xfs_dahash_t
1976xfs_default_hashname(
1977 struct xfs_name *name)
1978{
1979 return xfs_da_hashname(name->name, name->len);
1980}
1981
1982const struct xfs_nameops xfs_default_nameops = {
1983 .hashname = xfs_default_hashname,
1984 .compname = xfs_da_compname
1985};
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987int
Christoph Hellwig77936d02011-07-13 13:43:49 +02001988xfs_da_grow_inode_int(
1989 struct xfs_da_args *args,
1990 xfs_fileoff_t *bno,
1991 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Christoph Hellwig77936d02011-07-13 13:43:49 +02001993 struct xfs_trans *tp = args->trans;
1994 struct xfs_inode *dp = args->dp;
1995 int w = args->whichfork;
1996 xfs_drfsbno_t nblks = dp->i_d.di_nblocks;
1997 struct xfs_bmbt_irec map, *mapp;
1998 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /*
2001 * Find a spot in the file space to put the new block.
2002 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02002003 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2004 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Try mapping it in one filesystem block.
2009 */
2010 nmap = 1;
2011 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002012 error = xfs_bmapi_write(tp, dp, *bno, count,
2013 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 args->firstblock, args->total, &map, &nmap,
Christoph Hellwig77936d02011-07-13 13:43:49 +02002015 args->flist);
2016 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 ASSERT(nmap <= 1);
2020 if (nmap == 1) {
2021 mapp = &map;
2022 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002023 } else if (nmap == 0 && count > 1) {
2024 xfs_fileoff_t b;
2025 int c;
2026
2027 /*
2028 * If we didn't get it and the block might work if fragmented,
2029 * try without the CONTIG flag. Loop until we get it all.
2030 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002032 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002034 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002035 error = xfs_bmapi_write(tp, dp, b, c,
2036 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 args->firstblock, args->total,
Christoph Hellwig77936d02011-07-13 13:43:49 +02002038 &mapp[mapi], &nmap, args->flist);
2039 if (error)
2040 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (nmap < 1)
2042 break;
2043 mapi += nmap;
2044 b = mapp[mapi - 1].br_startoff +
2045 mapp[mapi - 1].br_blockcount;
2046 }
2047 } else {
2048 mapi = 0;
2049 mapp = NULL;
2050 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /*
2053 * Count the blocks we got, make sure it matches the total.
2054 */
2055 for (i = 0, got = 0; i < mapi; i++)
2056 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002057 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02002059 *bno + count) {
2060 error = XFS_ERROR(ENOSPC);
2061 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002063
David Chinnera7444052008-10-30 17:38:12 +11002064 /* account for newly allocated blocks in reserved blocks total */
2065 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002066
2067out_free_map:
2068 if (mapp != &map)
2069 kmem_free(mapp);
2070 return error;
2071}
2072
2073/*
2074 * Add a block to the btree ahead of the file.
2075 * Return the new block number to the caller.
2076 */
2077int
2078xfs_da_grow_inode(
2079 struct xfs_da_args *args,
2080 xfs_dablk_t *new_blkno)
2081{
2082 xfs_fileoff_t bno;
2083 int count;
2084 int error;
2085
Dave Chinner5a5881c2012-03-22 05:15:13 +00002086 trace_xfs_da_grow_inode(args);
2087
Christoph Hellwig77936d02011-07-13 13:43:49 +02002088 if (args->whichfork == XFS_DATA_FORK) {
2089 bno = args->dp->i_mount->m_dirleafblk;
2090 count = args->dp->i_mount->m_dirblkfsbs;
2091 } else {
2092 bno = 0;
2093 count = 1;
2094 }
2095
2096 error = xfs_da_grow_inode_int(args, &bno, count);
2097 if (!error)
2098 *new_blkno = (xfs_dablk_t)bno;
2099 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102/*
2103 * Ick. We need to always be able to remove a btree block, even
2104 * if there's no space reservation because the filesystem is full.
2105 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2106 * It swaps the target block with the last block in the file. The
2107 * last block in the file can always be removed since it can't cause
2108 * a bmap btree split to do that.
2109 */
2110STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002111xfs_da3_swap_lastblock(
2112 struct xfs_da_args *args,
2113 xfs_dablk_t *dead_blknop,
2114 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002116 struct xfs_da_blkinfo *dead_info;
2117 struct xfs_da_blkinfo *sib_info;
2118 struct xfs_da_intnode *par_node;
2119 struct xfs_da_intnode *dead_node;
2120 struct xfs_dir2_leaf *dead_leaf2;
2121 struct xfs_da_node_entry *btree;
2122 struct xfs_da3_icnode_hdr par_hdr;
2123 struct xfs_inode *ip;
2124 struct xfs_trans *tp;
2125 struct xfs_mount *mp;
2126 struct xfs_buf *dead_buf;
2127 struct xfs_buf *last_buf;
2128 struct xfs_buf *sib_buf;
2129 struct xfs_buf *par_buf;
2130 xfs_dahash_t dead_hash;
2131 xfs_fileoff_t lastoff;
2132 xfs_dablk_t dead_blkno;
2133 xfs_dablk_t last_blkno;
2134 xfs_dablk_t sib_blkno;
2135 xfs_dablk_t par_blkno;
2136 int error;
2137 int w;
2138 int entno;
2139 int level;
2140 int dead_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Dave Chinner5a5881c2012-03-22 05:15:13 +00002142 trace_xfs_da_swap_lastblock(args);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 dead_buf = *dead_bufp;
2145 dead_blkno = *dead_blknop;
2146 tp = args->trans;
2147 ip = args->dp;
2148 w = args->whichfork;
2149 ASSERT(w == XFS_DATA_FORK);
2150 mp = ip->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002151 lastoff = mp->m_dirfreeblk;
2152 error = xfs_bmap_last_before(tp, ip, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (error)
2154 return error;
2155 if (unlikely(lastoff == 0)) {
2156 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2157 mp);
2158 return XFS_ERROR(EFSCORRUPTED);
2159 }
2160 /*
2161 * Read the last block in the btree space.
2162 */
2163 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002164 error = xfs_da3_node_read(tp, ip, last_blkno, -1, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002165 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return error;
2167 /*
2168 * Copy the last block into the dead buffer and log it.
2169 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002170 memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
2171 xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
2172 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
2174 * Get values from the moved block.
2175 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002176 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2177 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2178 struct xfs_dir3_icleaf_hdr leafhdr;
2179 struct xfs_dir2_leaf_entry *ents;
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Dave Chinner24df33b2013-04-12 07:30:21 +10002182 xfs_dir3_leaf_hdr_from_disk(&leafhdr, dead_leaf2);
2183 ents = xfs_dir3_leaf_ents_p(dead_leaf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10002185 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002187 struct xfs_da3_icnode_hdr deadhdr;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 dead_node = (xfs_da_intnode_t *)dead_info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002190 xfs_da3_node_hdr_from_disk(&deadhdr, dead_node);
2191 btree = xfs_da3_node_tree_p(dead_node);
2192 dead_level = deadhdr.level;
2193 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 sib_buf = par_buf = NULL;
2196 /*
2197 * If the moved block has a left sibling, fix up the pointers.
2198 */
Nathan Scott89da0542006-03-17 17:28:40 +11002199 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002200 error = xfs_da3_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002201 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002203 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002205 be32_to_cpu(sib_info->forw) != last_blkno ||
2206 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2208 XFS_ERRLEVEL_LOW, mp);
2209 error = XFS_ERROR(EFSCORRUPTED);
2210 goto done;
2211 }
Nathan Scott89da0542006-03-17 17:28:40 +11002212 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002213 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2215 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 sib_buf = NULL;
2217 }
2218 /*
2219 * If the moved block has a right sibling, fix up the pointers.
2220 */
Nathan Scott89da0542006-03-17 17:28:40 +11002221 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002222 error = xfs_da3_node_read(tp, ip, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002223 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002225 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002227 be32_to_cpu(sib_info->back) != last_blkno ||
2228 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2230 XFS_ERRLEVEL_LOW, mp);
2231 error = XFS_ERROR(EFSCORRUPTED);
2232 goto done;
2233 }
Nathan Scott89da0542006-03-17 17:28:40 +11002234 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002235 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2237 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 sib_buf = NULL;
2239 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002240 par_blkno = mp->m_dirleafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 level = -1;
2242 /*
2243 * Walk down the tree looking for the parent of the moved block.
2244 */
2245 for (;;) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002246 error = xfs_da3_node_read(tp, ip, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002247 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002249 par_node = par_buf->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002250 xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
2251 if (level >= 0 && level != par_hdr.level + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2253 XFS_ERRLEVEL_LOW, mp);
2254 error = XFS_ERROR(EFSCORRUPTED);
2255 goto done;
2256 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002257 level = par_hdr.level;
2258 btree = xfs_da3_node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 for (entno = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002260 entno < par_hdr.count &&
2261 be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 entno++)
2263 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002264 if (entno == par_hdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2266 XFS_ERRLEVEL_LOW, mp);
2267 error = XFS_ERROR(EFSCORRUPTED);
2268 goto done;
2269 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002270 par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (level == dead_level + 1)
2272 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002273 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 par_buf = NULL;
2275 }
2276 /*
2277 * We're in the right parent block.
2278 * Look for the right entry.
2279 */
2280 for (;;) {
2281 for (;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002282 entno < par_hdr.count &&
2283 be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 entno++)
2285 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002286 if (entno < par_hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002288 par_blkno = par_hdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002289 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 par_buf = NULL;
2291 if (unlikely(par_blkno == 0)) {
2292 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2293 XFS_ERRLEVEL_LOW, mp);
2294 error = XFS_ERROR(EFSCORRUPTED);
2295 goto done;
2296 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002297 error = xfs_da3_node_read(tp, ip, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002300 par_node = par_buf->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002301 xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
2302 if (par_hdr.level != level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2304 XFS_ERRLEVEL_LOW, mp);
2305 error = XFS_ERROR(EFSCORRUPTED);
2306 goto done;
2307 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002308 btree = xfs_da3_node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 entno = 0;
2310 }
2311 /*
2312 * Update the parent entry pointing to the moved block.
2313 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002314 btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002315 xfs_trans_log_buf(tp, par_buf,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002316 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2317 sizeof(btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 *dead_blknop = last_blkno;
2319 *dead_bufp = last_buf;
2320 return 0;
2321done:
2322 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002323 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002325 xfs_trans_brelse(tp, sib_buf);
2326 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return error;
2328}
2329
2330/*
2331 * Remove a btree block from a directory or attribute.
2332 */
2333int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002334xfs_da_shrink_inode(
2335 xfs_da_args_t *args,
2336 xfs_dablk_t dead_blkno,
2337 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
2339 xfs_inode_t *dp;
2340 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 xfs_trans_t *tp;
2342 xfs_mount_t *mp;
2343
Dave Chinner5a5881c2012-03-22 05:15:13 +00002344 trace_xfs_da_shrink_inode(args);
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 dp = args->dp;
2347 w = args->whichfork;
2348 tp = args->trans;
2349 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002350 if (w == XFS_DATA_FORK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 count = mp->m_dirblkfsbs;
2352 else
2353 count = 1;
2354 for (;;) {
2355 /*
2356 * Remove extents. If we get ENOSPC for a dir we have to move
2357 * the last block to the place we want to kill.
2358 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002359 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2360 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2361 0, args->firstblock, args->flist, &done);
2362 if (error == ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002364 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002365 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2366 &dead_buf);
2367 if (error)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002368 break;
2369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002373 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return error;
2375}
2376
2377/*
2378 * See if the mapping(s) for this btree block are valid, i.e.
2379 * don't contain holes, are logically contiguous, and cover the whole range.
2380 */
2381STATIC int
2382xfs_da_map_covers_blocks(
2383 int nmap,
2384 xfs_bmbt_irec_t *mapp,
2385 xfs_dablk_t bno,
2386 int count)
2387{
2388 int i;
2389 xfs_fileoff_t off;
2390
2391 for (i = 0, off = bno; i < nmap; i++) {
2392 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2393 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2394 return 0;
2395 }
2396 if (off != mapp[i].br_startoff) {
2397 return 0;
2398 }
2399 off += mapp[i].br_blockcount;
2400 }
2401 return off == bno + count;
2402}
2403
2404/*
Dave Chinner36054312012-06-22 18:50:13 +10002405 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2406 *
2407 * For the single map case, it is assumed that the caller has provided a pointer
2408 * to a valid xfs_buf_map. For the multiple map case, this function will
2409 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2410 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
Dave Chinner36054312012-06-22 18:50:13 +10002412static int
2413xfs_buf_map_from_irec(
2414 struct xfs_mount *mp,
2415 struct xfs_buf_map **mapp,
2416 unsigned int *nmaps,
2417 struct xfs_bmbt_irec *irecs,
2418 unsigned int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Dave Chinner36054312012-06-22 18:50:13 +10002420 struct xfs_buf_map *map;
2421 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Dave Chinner36054312012-06-22 18:50:13 +10002423 ASSERT(*nmaps == 1);
2424 ASSERT(nirecs >= 1);
2425
2426 if (nirecs > 1) {
2427 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map), KM_SLEEP);
2428 if (!map)
2429 return ENOMEM;
2430 *mapp = map;
2431 }
2432
2433 *nmaps = nirecs;
2434 map = *mapp;
2435 for (i = 0; i < *nmaps; i++) {
2436 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2437 irecs[i].br_startblock != HOLESTARTBLOCK);
2438 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2439 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2440 }
2441 return 0;
2442}
2443
2444/*
2445 * Map the block we are given ready for reading. There are three possible return
2446 * values:
2447 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2448 * caller knows not to execute a subsequent read.
2449 * 0 - if we mapped the block successfully
2450 * >0 - positive error number if there was an error.
2451 */
2452static int
2453xfs_dabuf_map(
2454 struct xfs_trans *trans,
2455 struct xfs_inode *dp,
2456 xfs_dablk_t bno,
2457 xfs_daddr_t mappedbno,
2458 int whichfork,
2459 struct xfs_buf_map **map,
2460 int *nmaps)
2461{
2462 struct xfs_mount *mp = dp->i_mount;
2463 int nfsb;
2464 int error = 0;
2465 struct xfs_bmbt_irec irec;
2466 struct xfs_bmbt_irec *irecs = &irec;
2467 int nirecs;
2468
2469 ASSERT(map && *map);
2470 ASSERT(*nmaps == 1);
2471
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002472 nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
Dave Chinner36054312012-06-22 18:50:13 +10002473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 /*
2475 * Caller doesn't have a mapping. -2 means don't complain
2476 * if we land in a hole.
2477 */
2478 if (mappedbno == -1 || mappedbno == -2) {
2479 /*
2480 * Optimize the one-block case.
2481 */
Dave Chinner36054312012-06-22 18:50:13 +10002482 if (nfsb != 1)
2483 irecs = kmem_zalloc(sizeof(irec) * nfsb, KM_SLEEP);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002484
Dave Chinner36054312012-06-22 18:50:13 +10002485 nirecs = nfsb;
2486 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2487 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002488 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002491 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2492 irecs->br_startoff = (xfs_fileoff_t)bno;
2493 irecs->br_blockcount = nfsb;
2494 irecs->br_state = 0;
2495 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
Dave Chinner36054312012-06-22 18:50:13 +10002497
2498 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2499 error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (unlikely(error == EFSCORRUPTED)) {
2501 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002502 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002503 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2504 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002506 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002507 xfs_alert(mp,
2508"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 i,
Dave Chinner36054312012-06-22 18:50:13 +10002510 (long long)irecs[i].br_startoff,
2511 (long long)irecs[i].br_startblock,
2512 (long long)irecs[i].br_blockcount,
2513 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515 }
2516 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2517 XFS_ERRLEVEL_LOW, mp);
2518 }
Dave Chinner36054312012-06-22 18:50:13 +10002519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Dave Chinner36054312012-06-22 18:50:13 +10002521 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2522out:
2523 if (irecs != &irec)
2524 kmem_free(irecs);
2525 return error;
2526}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Dave Chinner36054312012-06-22 18:50:13 +10002528/*
2529 * Get a buffer for the dir/attr block.
2530 */
2531int
2532xfs_da_get_buf(
2533 struct xfs_trans *trans,
2534 struct xfs_inode *dp,
2535 xfs_dablk_t bno,
2536 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002537 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002538 int whichfork)
2539{
2540 struct xfs_buf *bp;
2541 struct xfs_buf_map map;
2542 struct xfs_buf_map *mapp;
2543 int nmap;
2544 int error;
2545
2546 *bpp = NULL;
2547 mapp = &map;
2548 nmap = 1;
2549 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2550 &mapp, &nmap);
2551 if (error) {
2552 /* mapping a hole is not an error, but we don't continue */
2553 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002555 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
Dave Chinner36054312012-06-22 18:50:13 +10002557
2558 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2559 mapp, nmap, 0);
2560 error = bp ? bp->b_error : XFS_ERROR(EIO);
2561 if (error) {
2562 xfs_trans_brelse(trans, bp);
2563 goto out_free;
2564 }
2565
Dave Chinner1d9025e2012-06-22 18:50:14 +10002566 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002567
2568out_free:
2569 if (mapp != &map)
2570 kmem_free(mapp);
2571
2572 return error;
2573}
2574
2575/*
2576 * Get a buffer for the dir/attr block, fill in the contents.
2577 */
2578int
2579xfs_da_read_buf(
2580 struct xfs_trans *trans,
2581 struct xfs_inode *dp,
2582 xfs_dablk_t bno,
2583 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002584 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002585 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002586 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002587{
2588 struct xfs_buf *bp;
2589 struct xfs_buf_map map;
2590 struct xfs_buf_map *mapp;
2591 int nmap;
2592 int error;
2593
2594 *bpp = NULL;
2595 mapp = &map;
2596 nmap = 1;
2597 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2598 &mapp, &nmap);
2599 if (error) {
2600 /* mapping a hole is not an error, but we don't continue */
2601 if (error == -1)
2602 error = 0;
2603 goto out_free;
2604 }
2605
2606 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2607 dp->i_mount->m_ddev_targp,
Dave Chinner1813dd62012-11-14 17:54:40 +11002608 mapp, nmap, 0, &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002609 if (error)
2610 goto out_free;
2611
2612 if (whichfork == XFS_ATTR_FORK)
2613 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 else
Dave Chinner36054312012-06-22 18:50:13 +10002615 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /*
Dave Chinner36054312012-06-22 18:50:13 +10002618 * This verification code will be moved to a CRC verification callback
2619 * function so just leave it here unchanged until then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 */
Dave Chinner36054312012-06-22 18:50:13 +10002621 {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002622 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
2623 xfs_dir2_free_t *free = bp->b_addr;
2624 xfs_da_blkinfo_t *info = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 uint magic, magic1;
Dave Chinner36054312012-06-22 18:50:13 +10002626 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Nathan Scott89da0542006-03-17 17:28:40 +11002628 magic = be16_to_cpu(info->magic);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002629 magic1 = be32_to_cpu(hdr->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (unlikely(
2631 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002632 (magic != XFS_DA3_NODE_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 (magic != XFS_ATTR_LEAF_MAGIC) &&
Dave Chinner517c2222013-04-24 18:58:55 +10002634 (magic != XFS_ATTR3_LEAF_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 (magic != XFS_DIR2_LEAF1_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002636 (magic != XFS_DIR3_LEAF1_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 (magic != XFS_DIR2_LEAFN_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002638 (magic != XFS_DIR3_LEAFN_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002640 (magic1 != XFS_DIR3_BLOCK_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 (magic1 != XFS_DIR2_DATA_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002642 (magic1 != XFS_DIR3_DATA_MAGIC) &&
2643 (free->hdr.magic !=
2644 cpu_to_be32(XFS_DIR2_FREE_MAGIC)) &&
2645 (free->hdr.magic !=
2646 cpu_to_be32(XFS_DIR3_FREE_MAGIC)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 mp, XFS_ERRTAG_DA_READ_BUF,
2648 XFS_RANDOM_DA_READ_BUF))) {
Dave Chinner36054312012-06-22 18:50:13 +10002649 trace_xfs_da_btree_corrupt(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2651 XFS_ERRLEVEL_LOW, mp, info);
2652 error = XFS_ERROR(EFSCORRUPTED);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002653 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002654 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002657 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002658out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002660 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 return error;
2663}
2664
2665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 * Readahead the dir/attr block.
2667 */
2668xfs_daddr_t
2669xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002670 struct xfs_trans *trans,
2671 struct xfs_inode *dp,
2672 xfs_dablk_t bno,
Dave Chinnerda6958c2012-11-12 22:54:18 +11002673 xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002674 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002675 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Dave Chinner36054312012-06-22 18:50:13 +10002677 struct xfs_buf_map map;
2678 struct xfs_buf_map *mapp;
2679 int nmap;
2680 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Dave Chinner36054312012-06-22 18:50:13 +10002682 mapp = &map;
2683 nmap = 1;
Dave Chinnerda6958c2012-11-12 22:54:18 +11002684 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002685 &mapp, &nmap);
2686 if (error) {
2687 /* mapping a hole is not an error, but we don't continue */
2688 if (error == -1)
2689 error = 0;
2690 goto out_free;
2691 }
2692
2693 mappedbno = mapp[0].bm_bn;
Dave Chinner1813dd62012-11-14 17:54:40 +11002694 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002695
2696out_free:
2697 if (mapp != &map)
2698 kmem_free(mapp);
2699
2700 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return -1;
Dave Chinner36054312012-06-22 18:50:13 +10002702 return mappedbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}