blob: 26dfc42a28f966e00102111afa188ce10143ea08 [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"
Dave Chinner70a98832013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110029#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.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 Chinner7ced60c2013-05-20 09:51:13 +1000273 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000274 bp->b_ops = &xfs_attr3_leaf_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100275 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100276 return;
277 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner24df33b2013-04-12 07:30:21 +1000278 case XFS_DIR3_LEAFN_MAGIC:
279 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100280 bp->b_ops->verify_read(bp);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100281 return;
282 default:
283 break;
284 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000285
286 /* corrupt block */
287 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
288 xfs_buf_ioerror(bp, EFSCORRUPTED);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100289}
290
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000291const struct xfs_buf_ops xfs_da3_node_buf_ops = {
292 .verify_read = xfs_da3_node_read_verify,
293 .verify_write = xfs_da3_node_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100294};
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{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100305 int err;
306
307 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000308 which_fork, &xfs_da3_node_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100309 if (!err && tp) {
310 struct xfs_da_blkinfo *info = (*bpp)->b_addr;
311 int type;
312
313 switch (be16_to_cpu(info->magic)) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100314 case XFS_DA_NODE_MAGIC:
Dave Chinnercab09a82013-04-30 21:39:36 +1000315 case XFS_DA3_NODE_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100316 type = XFS_BLFT_DA_NODE_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100317 break;
318 case XFS_ATTR_LEAF_MAGIC:
319 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100320 type = XFS_BLFT_ATTR_LEAF_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100321 break;
322 case XFS_DIR2_LEAFN_MAGIC:
323 case XFS_DIR3_LEAFN_MAGIC:
Dave Chinner61fe1352013-04-03 16:11:30 +1100324 type = XFS_BLFT_DIR_LEAFN_BUF;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100325 break;
326 default:
327 type = 0;
328 ASSERT(0);
329 break;
330 }
331 xfs_trans_buf_set_type(tp, *bpp, type);
332 }
333 return err;
Dave Chinnerd9392a42012-11-12 22:54:17 +1100334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*========================================================================
337 * Routines used for growing the Btree.
338 *========================================================================*/
339
340/*
341 * Create the initial contents of an intermediate node.
342 */
343int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000344xfs_da3_node_create(
345 struct xfs_da_args *args,
346 xfs_dablk_t blkno,
347 int level,
348 struct xfs_buf **bpp,
349 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000351 struct xfs_da_intnode *node;
352 struct xfs_trans *tp = args->trans;
353 struct xfs_mount *mp = tp->t_mountp;
354 struct xfs_da3_icnode_hdr ichdr = {0};
355 struct xfs_buf *bp;
356 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Dave Chinner5a5881c2012-03-22 05:15:13 +0000358 trace_xfs_da_node_create(args);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000359 ASSERT(level <= XFS_DA_NODE_MAXDEPTH);
Dave Chinner5a5881c2012-03-22 05:15:13 +0000360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
362 if (error)
363 return(error);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100364 bp->b_ops = &xfs_da3_node_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100365 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000366 node = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000368 if (xfs_sb_version_hascrc(&mp->m_sb)) {
369 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
370
371 ichdr.magic = XFS_DA3_NODE_MAGIC;
372 hdr3->info.blkno = cpu_to_be64(bp->b_bn);
373 hdr3->info.owner = cpu_to_be64(args->dp->i_ino);
374 uuid_copy(&hdr3->info.uuid, &mp->m_sb.sb_uuid);
375 } else {
376 ichdr.magic = XFS_DA_NODE_MAGIC;
377 }
378 ichdr.level = level;
379
380 xfs_da3_node_hdr_to_disk(node, &ichdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000381 xfs_trans_log_buf(tp, bp,
Dave Chinner4bceb182013-10-29 22:11:51 +1100382 XFS_DA_LOGRANGE(node, &node->hdr,
383 args->dp->d_ops->node_hdr_size()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 *bpp = bp;
386 return(0);
387}
388
389/*
390 * Split a leaf node, rebalance, then possibly split
391 * intermediate nodes, rebalance, etc.
392 */
393int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000394xfs_da3_split(
395 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000397 struct xfs_da_state_blk *oldblk;
398 struct xfs_da_state_blk *newblk;
399 struct xfs_da_state_blk *addblk;
400 struct xfs_da_intnode *node;
401 struct xfs_buf *bp;
402 int max;
Dave Chinner836a94a2013-08-12 20:49:44 +1000403 int action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000404 int error;
405 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Dave Chinner5a5881c2012-03-22 05:15:13 +0000407 trace_xfs_da_split(state->args);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Walk back up the tree splitting/inserting/adjusting as necessary.
411 * If we need to insert and there isn't room, split the node, then
412 * decide which fragment to insert the new block from below into.
413 * Note that we may split the root this way, but we need more fixup.
414 */
415 max = state->path.active - 1;
416 ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
417 ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000418 state->path.blk[max].magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 addblk = &state->path.blk[max]; /* initial dummy value */
421 for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
422 oldblk = &state->path.blk[i];
423 newblk = &state->altpath.blk[i];
424
425 /*
426 * If a leaf node then
427 * Allocate a new leaf node, then rebalance across them.
428 * else if an intermediate node then
429 * We split on the last layer, must we split the node?
430 */
431 switch (oldblk->magic) {
432 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +1000433 error = xfs_attr3_leaf_split(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if ((error != 0) && (error != ENOSPC)) {
435 return(error); /* GROT: attr is inconsistent */
436 }
437 if (!error) {
438 addblk = newblk;
439 break;
440 }
441 /*
442 * Entry wouldn't fit, split the leaf again.
443 */
444 state->extravalid = 1;
445 if (state->inleaf) {
446 state->extraafter = 0; /* before newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000447 trace_xfs_attr_leaf_split_before(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000448 error = xfs_attr3_leaf_split(state, oldblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 &state->extrablk);
450 } else {
451 state->extraafter = 1; /* after newblk */
Dave Chinner5a5881c2012-03-22 05:15:13 +0000452 trace_xfs_attr_leaf_split_after(state->args);
Dave Chinner517c2222013-04-24 18:58:55 +1000453 error = xfs_attr3_leaf_split(state, newblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 &state->extrablk);
455 }
456 if (error)
457 return(error); /* GROT: attr inconsistent */
458 addblk = newblk;
459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 error = xfs_dir2_leafn_split(state, oldblk, newblk);
462 if (error)
463 return error;
464 addblk = newblk;
465 break;
466 case XFS_DA_NODE_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000467 error = xfs_da3_node_split(state, oldblk, newblk, addblk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 max - i, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 addblk->bp = NULL;
470 if (error)
471 return(error); /* GROT: dir is inconsistent */
472 /*
473 * Record the newly split block for the next time thru?
474 */
475 if (action)
476 addblk = newblk;
477 else
478 addblk = NULL;
479 break;
480 }
481
482 /*
483 * Update the btree to show the new hashval for this child.
484 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000485 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 if (!addblk)
488 return(0);
489
490 /*
491 * Split the root node.
492 */
493 ASSERT(state->path.active == 0);
494 oldblk = &state->path.blk[0];
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000495 error = xfs_da3_root_split(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 addblk->bp = NULL;
498 return(error); /* GROT: dir is inconsistent */
499 }
500
501 /*
502 * Update pointers to the node which used to be block 0 and
503 * just got bumped because of the addition of a new root node.
504 * There might be three blocks involved if a double split occurred,
505 * and the original block 0 could be at any position in the list.
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000506 *
507 * Note: the magic numbers and sibling pointers are in the same
508 * physical place for both v2 and v3 headers (by design). Hence it
509 * doesn't matter which version of the xfs_da_intnode structure we use
510 * here as the result will be the same using either structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000512 node = oldblk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (node->hdr.info.forw) {
Nathan Scott89da0542006-03-17 17:28:40 +1100514 if (be32_to_cpu(node->hdr.info.forw) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 bp = addblk->bp;
516 } else {
517 ASSERT(state->extravalid);
518 bp = state->extrablk.bp;
519 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000520 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100521 node->hdr.info.back = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000522 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 XFS_DA_LOGRANGE(node, &node->hdr.info,
524 sizeof(node->hdr.info)));
525 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000526 node = oldblk->bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100527 if (node->hdr.info.back) {
528 if (be32_to_cpu(node->hdr.info.back) == addblk->blkno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 bp = addblk->bp;
530 } else {
531 ASSERT(state->extravalid);
532 bp = state->extrablk.bp;
533 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000534 node = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +1100535 node->hdr.info.forw = cpu_to_be32(oldblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000536 xfs_trans_log_buf(state->args->trans, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 XFS_DA_LOGRANGE(node, &node->hdr.info,
538 sizeof(node->hdr.info)));
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 addblk->bp = NULL;
541 return(0);
542}
543
544/*
545 * Split the root. We have to create a new root and point to the two
546 * parts (the split old root) that we just created. Copy block zero to
547 * the EOF, extending the inode in process.
548 */
549STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000550xfs_da3_root_split(
551 struct xfs_da_state *state,
552 struct xfs_da_state_blk *blk1,
553 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000555 struct xfs_da_intnode *node;
556 struct xfs_da_intnode *oldroot;
557 struct xfs_da_node_entry *btree;
558 struct xfs_da3_icnode_hdr nodehdr;
559 struct xfs_da_args *args;
560 struct xfs_buf *bp;
561 struct xfs_inode *dp;
562 struct xfs_trans *tp;
563 struct xfs_mount *mp;
564 struct xfs_dir2_leaf *leaf;
565 xfs_dablk_t blkno;
566 int level;
567 int error;
568 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Dave Chinner5a5881c2012-03-22 05:15:13 +0000570 trace_xfs_da_root_split(state->args);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
573 * Copy the existing (incorrect) block from the root node position
574 * to a free space somewhere.
575 */
576 args = state->args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 error = xfs_da_grow_inode(args, &blkno);
578 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000579 return error;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 dp = args->dp;
582 tp = args->trans;
583 mp = state->mp;
584 error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
585 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000586 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000587 node = bp->b_addr;
588 oldroot = blk1->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000589 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
590 oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
591 struct xfs_da3_icnode_hdr nodehdr;
592
593 xfs_da3_node_hdr_from_disk(&nodehdr, oldroot);
Dave Chinner4bceb182013-10-29 22:11:51 +1100594 btree = dp->d_ops->node_tree_p(oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000595 size = (int)((char *)&btree[nodehdr.count] - (char *)oldroot);
596 level = nodehdr.level;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100597
598 /*
599 * we are about to copy oldroot to bp, so set up the type
600 * of bp while we know exactly what it will be.
601 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100602 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DA_NODE_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000604 struct xfs_dir3_icleaf_hdr leafhdr;
605 struct xfs_dir2_leaf_entry *ents;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 leaf = (xfs_dir2_leaf_t *)oldroot;
Dave Chinner24df33b2013-04-12 07:30:21 +1000608 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100609 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000610
611 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
612 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
613 size = (int)((char *)&ents[leafhdr.count] - (char *)leaf);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000614 level = 0;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100615
616 /*
617 * we are about to copy oldroot to bp, so set up the type
618 * of bp while we know exactly what it will be.
619 */
Dave Chinner61fe1352013-04-03 16:11:30 +1100620 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000622
623 /*
624 * we can copy most of the information in the node from one block to
625 * another, but for CRC enabled headers we have to make sure that the
626 * block specific identifiers are kept intact. We update the buffer
627 * directly for this.
628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 memcpy(node, oldroot, size);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000630 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
631 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
632 struct xfs_da3_intnode *node3 = (struct xfs_da3_intnode *)node;
633
634 node3->hdr.info.blkno = cpu_to_be64(bp->b_bn);
635 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000636 xfs_trans_log_buf(tp, bp, 0, size - 1);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100637
Dave Chinner1813dd62012-11-14 17:54:40 +1100638 bp->b_ops = blk1->bp->b_ops;
Dave Chinner0a4edc82013-09-02 10:32:01 +1000639 xfs_trans_buf_copy_type(bp, blk1->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 blk1->bp = bp;
641 blk1->blkno = blkno;
642
643 /*
644 * Set up the new root node.
645 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000646 error = xfs_da3_node_create(args,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000647 (args->whichfork == XFS_DATA_FORK) ? mp->m_dirleafblk : 0,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000648 level + 1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000650 return error;
651
Dave Chinner1d9025e2012-06-22 18:50:14 +1000652 node = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000653 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100654 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000655 btree[0].hashval = cpu_to_be32(blk1->hashval);
656 btree[0].before = cpu_to_be32(blk1->blkno);
657 btree[1].hashval = cpu_to_be32(blk2->hashval);
658 btree[1].before = cpu_to_be32(blk2->blkno);
659 nodehdr.count = 2;
660 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +1000663 if (oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
664 oldroot->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ASSERT(blk1->blkno >= mp->m_dirleafblk &&
666 blk1->blkno < mp->m_dirfreeblk);
667 ASSERT(blk2->blkno >= mp->m_dirleafblk &&
668 blk2->blkno < mp->m_dirfreeblk);
669 }
670#endif
671
672 /* Header is already logged by xfs_da_node_create */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 xfs_trans_log_buf(tp, bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000674 XFS_DA_LOGRANGE(node, btree, sizeof(xfs_da_node_entry_t) * 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/*
680 * Split the node, rebalance, then add the new entry.
681 */
682STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000683xfs_da3_node_split(
684 struct xfs_da_state *state,
685 struct xfs_da_state_blk *oldblk,
686 struct xfs_da_state_blk *newblk,
687 struct xfs_da_state_blk *addblk,
688 int treelevel,
689 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000691 struct xfs_da_intnode *node;
692 struct xfs_da3_icnode_hdr nodehdr;
693 xfs_dablk_t blkno;
694 int newcount;
695 int error;
696 int useextra;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dave Chinner5a5881c2012-03-22 05:15:13 +0000698 trace_xfs_da_node_split(state->args);
699
Dave Chinner1d9025e2012-06-22 18:50:14 +1000700 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000701 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /*
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000704 * With V2 dirs the extra block is data or freespace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000706 useextra = state->extravalid && state->args->whichfork == XFS_ATTR_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 newcount = 1 + useextra;
708 /*
709 * Do we have to split the node?
710 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000711 if (nodehdr.count + newcount > state->node_ents) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * Allocate a new node, add to the doubly linked chain of
714 * nodes, then move some of our excess entries into it.
715 */
716 error = xfs_da_grow_inode(state->args, &blkno);
717 if (error)
718 return(error); /* GROT: dir is inconsistent */
719
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000720 error = xfs_da3_node_create(state->args, blkno, treelevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 &newblk->bp, state->args->whichfork);
722 if (error)
723 return(error); /* GROT: dir is inconsistent */
724 newblk->blkno = blkno;
725 newblk->magic = XFS_DA_NODE_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000726 xfs_da3_node_rebalance(state, oldblk, newblk);
727 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (error)
729 return(error);
730 *result = 1;
731 } else {
732 *result = 0;
733 }
734
735 /*
736 * Insert the new entry(s) into the correct block
737 * (updating last hashval in the process).
738 *
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000739 * xfs_da3_node_add() inserts BEFORE the given index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * and as a result of using node_lookup_int() we always
741 * point to a valid entry (not after one), but a split
742 * operation always results in a new block whose hashvals
743 * FOLLOW the current block.
744 *
745 * If we had double-split op below us, then add the extra block too.
746 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000747 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000748 xfs_da3_node_hdr_from_disk(&nodehdr, node);
749 if (oldblk->index <= nodehdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000751 xfs_da3_node_add(state, oldblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (useextra) {
753 if (state->extraafter)
754 oldblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000755 xfs_da3_node_add(state, oldblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 state->extravalid = 0;
757 }
758 } else {
759 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000760 xfs_da3_node_add(state, newblk, addblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (useextra) {
762 if (state->extraafter)
763 newblk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000764 xfs_da3_node_add(state, newblk, &state->extrablk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 state->extravalid = 0;
766 }
767 }
768
769 return(0);
770}
771
772/*
773 * Balance the btree elements between two intermediate nodes,
774 * usually one full and one empty.
775 *
776 * NOTE: if blk2 is empty, then it will get the upper half of blk1.
777 */
778STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000779xfs_da3_node_rebalance(
780 struct xfs_da_state *state,
781 struct xfs_da_state_blk *blk1,
782 struct xfs_da_state_blk *blk2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000784 struct xfs_da_intnode *node1;
785 struct xfs_da_intnode *node2;
786 struct xfs_da_intnode *tmpnode;
787 struct xfs_da_node_entry *btree1;
788 struct xfs_da_node_entry *btree2;
789 struct xfs_da_node_entry *btree_s;
790 struct xfs_da_node_entry *btree_d;
791 struct xfs_da3_icnode_hdr nodehdr1;
792 struct xfs_da3_icnode_hdr nodehdr2;
793 struct xfs_trans *tp;
794 int count;
795 int tmp;
796 int swap = 0;
Dave Chinner4bceb182013-10-29 22:11:51 +1100797 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Dave Chinner5a5881c2012-03-22 05:15:13 +0000799 trace_xfs_da_node_rebalance(state->args);
800
Dave Chinner1d9025e2012-06-22 18:50:14 +1000801 node1 = blk1->bp->b_addr;
802 node2 = blk2->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000803 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
804 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100805 btree1 = dp->d_ops->node_tree_p(node1);
806 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /*
809 * Figure out how many entries need to move, and in which direction.
810 * Swap the nodes around if that makes it simpler.
811 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000812 if (nodehdr1.count > 0 && nodehdr2.count > 0 &&
813 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
814 (be32_to_cpu(btree2[nodehdr2.count - 1].hashval) <
815 be32_to_cpu(btree1[nodehdr1.count - 1].hashval)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 tmpnode = node1;
817 node1 = node2;
818 node2 = tmpnode;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000819 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
820 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100821 btree1 = dp->d_ops->node_tree_p(node1);
822 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000823 swap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000825
826 count = (nodehdr1.count - nodehdr2.count) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (count == 0)
828 return;
829 tp = state->args->trans;
830 /*
831 * Two cases: high-to-low and low-to-high.
832 */
833 if (count > 0) {
834 /*
835 * Move elements in node2 up to make a hole.
836 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000837 tmp = nodehdr2.count;
838 if (tmp > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000840 btree_s = &btree2[0];
841 btree_d = &btree2[count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 memmove(btree_d, btree_s, tmp);
843 }
844
845 /*
846 * Move the req'd B-tree elements from high in node1 to
847 * low in node2.
848 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000849 nodehdr2.count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000851 btree_s = &btree1[nodehdr1.count - count];
852 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000854 nodehdr1.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
856 /*
857 * Move the req'd B-tree elements from low in node2 to
858 * high in node1.
859 */
860 count = -count;
861 tmp = count * (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000862 btree_s = &btree2[0];
863 btree_d = &btree1[nodehdr1.count];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 memcpy(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000865 nodehdr1.count += count;
866
Dave Chinner1d9025e2012-06-22 18:50:14 +1000867 xfs_trans_log_buf(tp, blk1->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 XFS_DA_LOGRANGE(node1, btree_d, tmp));
869
870 /*
871 * Move elements in node2 down to fill the hole.
872 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000873 tmp = nodehdr2.count - count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000875 btree_s = &btree2[count];
876 btree_d = &btree2[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 memmove(btree_d, btree_s, tmp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000878 nodehdr2.count -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 /*
882 * Log header of node 1 and all current bits of node 2.
883 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000884 xfs_da3_node_hdr_to_disk(node1, &nodehdr1);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000885 xfs_trans_log_buf(tp, blk1->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000886 XFS_DA_LOGRANGE(node1, &node1->hdr,
Dave Chinner4bceb182013-10-29 22:11:51 +1100887 dp->d_ops->node_hdr_size()));
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000888
889 xfs_da3_node_hdr_to_disk(node2, &nodehdr2);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000890 xfs_trans_log_buf(tp, blk2->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 XFS_DA_LOGRANGE(node2, &node2->hdr,
Dave Chinner4bceb182013-10-29 22:11:51 +1100892 dp->d_ops->node_hdr_size() +
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000893 (sizeof(btree2[0]) * nodehdr2.count)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /*
896 * Record the last hashval from each block for upward propagation.
897 * (note: don't use the swapped node pointers)
898 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000899 if (swap) {
900 node1 = blk1->bp->b_addr;
901 node2 = blk2->bp->b_addr;
902 xfs_da3_node_hdr_from_disk(&nodehdr1, node1);
903 xfs_da3_node_hdr_from_disk(&nodehdr2, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +1100904 btree1 = dp->d_ops->node_tree_p(node1);
905 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000906 }
907 blk1->hashval = be32_to_cpu(btree1[nodehdr1.count - 1].hashval);
908 blk2->hashval = be32_to_cpu(btree2[nodehdr2.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * Adjust the expected index for insertion.
912 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000913 if (blk1->index >= nodehdr1.count) {
914 blk2->index = blk1->index - nodehdr1.count;
915 blk1->index = nodehdr1.count + 1; /* make it invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917}
918
919/*
920 * Add a new entry to an intermediate node.
921 */
922STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000923xfs_da3_node_add(
924 struct xfs_da_state *state,
925 struct xfs_da_state_blk *oldblk,
926 struct xfs_da_state_blk *newblk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000928 struct xfs_da_intnode *node;
929 struct xfs_da3_icnode_hdr nodehdr;
930 struct xfs_da_node_entry *btree;
931 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +1100932 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Dave Chinner5a5881c2012-03-22 05:15:13 +0000934 trace_xfs_da_node_add(state->args);
935
Dave Chinner1d9025e2012-06-22 18:50:14 +1000936 node = oldblk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000937 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +1100938 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000939
940 ASSERT(oldblk->index >= 0 && oldblk->index <= nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ASSERT(newblk->blkno != 0);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000942 if (state->args->whichfork == XFS_DATA_FORK)
Christoph Hellwig73523a22010-07-20 17:54:45 +1000943 ASSERT(newblk->blkno >= state->mp->m_dirleafblk &&
944 newblk->blkno < state->mp->m_dirfreeblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /*
947 * We may need to make some room before we insert the new node.
948 */
949 tmp = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000950 if (oldblk->index < nodehdr.count) {
951 tmp = (nodehdr.count - oldblk->index) * (uint)sizeof(*btree);
952 memmove(&btree[oldblk->index + 1], &btree[oldblk->index], tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000954 btree[oldblk->index].hashval = cpu_to_be32(newblk->hashval);
955 btree[oldblk->index].before = cpu_to_be32(newblk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000956 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000957 XFS_DA_LOGRANGE(node, &btree[oldblk->index],
958 tmp + sizeof(*btree)));
959
960 nodehdr.count += 1;
961 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000962 xfs_trans_log_buf(state->args->trans, oldblk->bp,
Dave Chinner4bceb182013-10-29 22:11:51 +1100963 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * Copy the last hash value from the oldblk to propagate upwards.
967 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000968 oldblk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*========================================================================
972 * Routines used for shrinking the Btree.
973 *========================================================================*/
974
975/*
976 * Deallocate an empty leaf node, remove it from its parent,
977 * possibly deallocating that block, etc...
978 */
979int
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000980xfs_da3_join(
981 struct xfs_da_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Dave Chinnerf5ea1102013-04-24 18:58:02 +1000983 struct xfs_da_state_blk *drop_blk;
984 struct xfs_da_state_blk *save_blk;
985 int action = 0;
986 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Chinner5a5881c2012-03-22 05:15:13 +0000988 trace_xfs_da_join(state->args);
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 drop_blk = &state->path.blk[ state->path.active-1 ];
991 save_blk = &state->altpath.blk[ state->path.active-1 ];
992 ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
993 ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +1000994 drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
997 * Walk back up the tree joining/deallocating as necessary.
998 * When we stop dropping blocks, break out.
999 */
1000 for ( ; state->path.active >= 2; drop_blk--, save_blk--,
1001 state->path.active--) {
1002 /*
1003 * See if we can combine the block with a neighbor.
1004 * (action == 0) => no options, just leave
1005 * (action == 1) => coalesce, then unlink
1006 * (action == 2) => block empty, unlink it
1007 */
1008 switch (drop_blk->magic) {
1009 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001010 error = xfs_attr3_leaf_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (error)
1012 return(error);
1013 if (action == 0)
1014 return(0);
Dave Chinner517c2222013-04-24 18:58:55 +10001015 xfs_attr3_leaf_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 case XFS_DIR2_LEAFN_MAGIC:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 error = xfs_dir2_leafn_toosmall(state, &action);
1019 if (error)
1020 return error;
1021 if (action == 0)
1022 return 0;
1023 xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
1024 break;
1025 case XFS_DA_NODE_MAGIC:
1026 /*
1027 * Remove the offending node, fixup hashvals,
1028 * check for a toosmall neighbor.
1029 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001030 xfs_da3_node_remove(state, drop_blk);
1031 xfs_da3_fixhashpath(state, &state->path);
1032 error = xfs_da3_node_toosmall(state, &action);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (error)
1034 return(error);
1035 if (action == 0)
1036 return 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001037 xfs_da3_node_unbalance(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001040 xfs_da3_fixhashpath(state, &state->altpath);
1041 error = xfs_da3_blk_unlink(state, drop_blk, save_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 xfs_da_state_kill_altpath(state);
1043 if (error)
1044 return(error);
1045 error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
1046 drop_blk->bp);
1047 drop_blk->bp = NULL;
1048 if (error)
1049 return(error);
1050 }
1051 /*
1052 * We joined all the way to the top. If it turns out that
1053 * we only have one entry in the root, make the child block
1054 * the new root.
1055 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001056 xfs_da3_node_remove(state, drop_blk);
1057 xfs_da3_fixhashpath(state, &state->path);
1058 error = xfs_da3_root_join(state, &state->path.blk[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return(error);
1060}
1061
Alex Elder1c4f3322011-07-18 18:14:09 +00001062#ifdef DEBUG
1063static void
1064xfs_da_blkinfo_onlychild_validate(struct xfs_da_blkinfo *blkinfo, __u16 level)
1065{
1066 __be16 magic = blkinfo->magic;
1067
1068 if (level == 1) {
1069 ASSERT(magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001070 magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001071 magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1072 magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001073 } else {
1074 ASSERT(magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
1075 magic == cpu_to_be16(XFS_DA3_NODE_MAGIC));
1076 }
Alex Elder1c4f3322011-07-18 18:14:09 +00001077 ASSERT(!blkinfo->forw);
1078 ASSERT(!blkinfo->back);
1079}
1080#else /* !DEBUG */
1081#define xfs_da_blkinfo_onlychild_validate(blkinfo, level)
1082#endif /* !DEBUG */
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/*
1085 * We have only one entry in the root. Copy the only remaining child of
1086 * the old root to block 0 as the new root node.
1087 */
1088STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001089xfs_da3_root_join(
1090 struct xfs_da_state *state,
1091 struct xfs_da_state_blk *root_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001093 struct xfs_da_intnode *oldroot;
1094 struct xfs_da_args *args;
1095 xfs_dablk_t child;
1096 struct xfs_buf *bp;
1097 struct xfs_da3_icnode_hdr oldroothdr;
1098 struct xfs_da_node_entry *btree;
1099 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Dave Chinner5a5881c2012-03-22 05:15:13 +00001101 trace_xfs_da_root_join(state->args);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001104
1105 args = state->args;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001106 oldroot = root_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001107 xfs_da3_node_hdr_from_disk(&oldroothdr, oldroot);
1108 ASSERT(oldroothdr.forw == 0);
1109 ASSERT(oldroothdr.back == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /*
1112 * If the root has more than one child, then don't do anything.
1113 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001114 if (oldroothdr.count > 1)
1115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /*
1118 * Read in the (only) child block, then copy those bytes into
1119 * the root block's buffer and free the original child block.
1120 */
Dave Chinner4bceb182013-10-29 22:11:51 +11001121 btree = args->dp->d_ops->node_tree_p(oldroot);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001122 child = be32_to_cpu(btree[0].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ASSERT(child != 0);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001124 error = xfs_da3_node_read(args->trans, args->dp, child, -1, &bp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001125 args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (error)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001127 return error;
1128 xfs_da_blkinfo_onlychild_validate(bp->b_addr, oldroothdr.level);
Alex Elder1c4f3322011-07-18 18:14:09 +00001129
Dave Chinner612cfbf2012-11-14 17:52:32 +11001130 /*
1131 * This could be copying a leaf back into the root block in the case of
1132 * there only being a single leaf block left in the tree. Hence we have
Dave Chinner1813dd62012-11-14 17:54:40 +11001133 * to update the b_ops pointer as well to match the buffer type change
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001134 * that could occur. For dir3 blocks we also need to update the block
1135 * number in the buffer header.
Dave Chinner612cfbf2012-11-14 17:52:32 +11001136 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001137 memcpy(root_blk->bp->b_addr, bp->b_addr, state->blocksize);
Dave Chinner1813dd62012-11-14 17:54:40 +11001138 root_blk->bp->b_ops = bp->b_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001139 xfs_trans_buf_copy_type(root_blk->bp, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001140 if (oldroothdr.magic == XFS_DA3_NODE_MAGIC) {
1141 struct xfs_da3_blkinfo *da3 = root_blk->bp->b_addr;
1142 da3->blkno = cpu_to_be64(root_blk->bp->b_bn);
1143 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001144 xfs_trans_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 error = xfs_da_shrink_inode(args, child, bp);
1146 return(error);
1147}
1148
1149/*
1150 * Check a node block and its neighbors to see if the block should be
1151 * collapsed into one or the other neighbor. Always keep the block
1152 * with the smaller block number.
1153 * If the current block is over 50% full, don't try to join it, return 0.
1154 * If the block is empty, fill in the state structure and return 2.
1155 * If it can be collapsed, fill in the state structure and return 1.
1156 * If nothing can be done, return 0.
1157 */
1158STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001159xfs_da3_node_toosmall(
1160 struct xfs_da_state *state,
1161 int *action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001163 struct xfs_da_intnode *node;
1164 struct xfs_da_state_blk *blk;
1165 struct xfs_da_blkinfo *info;
1166 xfs_dablk_t blkno;
1167 struct xfs_buf *bp;
1168 struct xfs_da3_icnode_hdr nodehdr;
1169 int count;
1170 int forward;
1171 int error;
1172 int retval;
1173 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Dave Chinneree732592012-11-12 22:53:53 +11001175 trace_xfs_da_node_toosmall(state->args);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * Check for the degenerate case of the block being over 50% full.
1179 * If so, it's not worth even looking to see if we might be able
1180 * to coalesce with a sibling.
1181 */
1182 blk = &state->path.blk[ state->path.active-1 ];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001183 info = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 node = (xfs_da_intnode_t *)info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001185 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1186 if (nodehdr.count > (state->node_ents >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 *action = 0; /* blk over 50%, don't try to join */
1188 return(0); /* blk over 50%, don't try to join */
1189 }
1190
1191 /*
1192 * Check for the degenerate case of the block being empty.
1193 * If the block is empty, we'll simply delete it, no need to
Nathan Scottc41564b2006-03-29 08:55:14 +10001194 * coalesce it with a sibling block. We choose (arbitrarily)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * to merge with the forward block unless it is NULL.
1196 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001197 if (nodehdr.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
1199 * Make altpath point to the block we want to keep and
1200 * path point to the block we want to drop (this one).
1201 */
Nathan Scott89da0542006-03-17 17:28:40 +11001202 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001204 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 0, &retval);
1206 if (error)
1207 return(error);
1208 if (retval) {
1209 *action = 0;
1210 } else {
1211 *action = 2;
1212 }
1213 return(0);
1214 }
1215
1216 /*
1217 * Examine each sibling block to see if we can coalesce with
1218 * at least 25% free space to spare. We need to figure out
1219 * whether to merge with the forward or the backward block.
1220 * We prefer coalescing with the lower numbered sibling so as
1221 * to shrink a directory over time.
1222 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001223 count = state->node_ents;
1224 count -= state->node_ents >> 2;
1225 count -= nodehdr.count;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* start with smaller blk num */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001228 forward = nodehdr.forw < nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 for (i = 0; i < 2; forward = !forward, i++) {
Mark Tinguely997def22013-09-23 12:18:58 -05001230 struct xfs_da3_icnode_hdr thdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (forward)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001232 blkno = nodehdr.forw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001234 blkno = nodehdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (blkno == 0)
1236 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001237 error = xfs_da3_node_read(state->args->trans, state->args->dp,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001238 blkno, -1, &bp, state->args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (error)
1240 return(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Dave Chinner1d9025e2012-06-22 18:50:14 +10001242 node = bp->b_addr;
Mark Tinguely997def22013-09-23 12:18:58 -05001243 xfs_da3_node_hdr_from_disk(&thdr, node);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001244 xfs_trans_brelse(state->args->trans, bp);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001245
Mark Tinguely997def22013-09-23 12:18:58 -05001246 if (count - thdr.count >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break; /* fits with at least 25% to spare */
1248 }
1249 if (i >= 2) {
1250 *action = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
1254 /*
1255 * Make altpath point to the block we want to keep (the lower
1256 * numbered block) and path point to the block we want to drop.
1257 */
1258 memcpy(&state->altpath, &state->path, sizeof(state->path));
1259 if (blkno < blk->blkno) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001260 error = xfs_da3_path_shift(state, &state->altpath, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 0, &retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001263 error = xfs_da3_path_shift(state, &state->path, forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 0, &retval);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001265 }
1266 if (error)
1267 return error;
1268 if (retval) {
1269 *action = 0;
1270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 *action = 1;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001273 return 0;
1274}
1275
1276/*
1277 * Pick up the last hashvalue from an intermediate node.
1278 */
1279STATIC uint
1280xfs_da3_node_lasthash(
Dave Chinner4bceb182013-10-29 22:11:51 +11001281 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001282 struct xfs_buf *bp,
1283 int *count)
1284{
1285 struct xfs_da_intnode *node;
1286 struct xfs_da_node_entry *btree;
1287 struct xfs_da3_icnode_hdr nodehdr;
1288
1289 node = bp->b_addr;
1290 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1291 if (count)
1292 *count = nodehdr.count;
1293 if (!nodehdr.count)
1294 return 0;
Dave Chinner4bceb182013-10-29 22:11:51 +11001295 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001296 return be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299/*
1300 * Walk back up the tree adjusting hash values as necessary,
1301 * when we stop making changes, return.
1302 */
1303void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001304xfs_da3_fixhashpath(
1305 struct xfs_da_state *state,
1306 struct xfs_da_state_path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001308 struct xfs_da_state_blk *blk;
1309 struct xfs_da_intnode *node;
1310 struct xfs_da_node_entry *btree;
1311 xfs_dahash_t lasthash=0;
1312 int level;
1313 int count;
Dave Chinner4bceb182013-10-29 22:11:51 +11001314 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Dave Chinneree732592012-11-12 22:53:53 +11001316 trace_xfs_da_fixhashpath(state->args);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 level = path->active-1;
1319 blk = &path->blk[ level ];
1320 switch (blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case XFS_ATTR_LEAF_MAGIC:
1322 lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
1323 if (count == 0)
1324 return;
1325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001327 lasthash = xfs_dir2_leafn_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (count == 0)
1329 return;
1330 break;
1331 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001332 lasthash = xfs_da3_node_lasthash(dp, blk->bp, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (count == 0)
1334 return;
1335 break;
1336 }
1337 for (blk--, level--; level >= 0; blk--, level--) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001338 struct xfs_da3_icnode_hdr nodehdr;
1339
Dave Chinner1d9025e2012-06-22 18:50:14 +10001340 node = blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001341 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001342 btree = dp->d_ops->node_tree_p(node);
Nathan Scott403432d2006-03-17 17:29:46 +11001343 if (be32_to_cpu(btree->hashval) == lasthash)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
1345 blk->hashval = lasthash;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001346 btree[blk->index].hashval = cpu_to_be32(lasthash);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001347 xfs_trans_log_buf(state->args->trans, blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001348 XFS_DA_LOGRANGE(node, &btree[blk->index],
1349 sizeof(*btree)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001351 lasthash = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353}
1354
1355/*
1356 * Remove an entry from an intermediate node.
1357 */
1358STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001359xfs_da3_node_remove(
1360 struct xfs_da_state *state,
1361 struct xfs_da_state_blk *drop_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001363 struct xfs_da_intnode *node;
1364 struct xfs_da3_icnode_hdr nodehdr;
1365 struct xfs_da_node_entry *btree;
1366 int index;
1367 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001368 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Dave Chinner5a5881c2012-03-22 05:15:13 +00001370 trace_xfs_da_node_remove(state->args);
1371
Dave Chinner1d9025e2012-06-22 18:50:14 +10001372 node = drop_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001373 xfs_da3_node_hdr_from_disk(&nodehdr, node);
1374 ASSERT(drop_blk->index < nodehdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 ASSERT(drop_blk->index >= 0);
1376
1377 /*
1378 * Copy over the offending entry, or just zero it out.
1379 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001380 index = drop_blk->index;
Dave Chinner4bceb182013-10-29 22:11:51 +11001381 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001382 if (index < nodehdr.count - 1) {
1383 tmp = nodehdr.count - index - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 tmp *= (uint)sizeof(xfs_da_node_entry_t);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001385 memmove(&btree[index], &btree[index + 1], tmp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001386 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001387 XFS_DA_LOGRANGE(node, &btree[index], tmp));
1388 index = nodehdr.count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001390 memset(&btree[index], 0, sizeof(xfs_da_node_entry_t));
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001392 XFS_DA_LOGRANGE(node, &btree[index], sizeof(btree[index])));
1393 nodehdr.count -= 1;
1394 xfs_da3_node_hdr_to_disk(node, &nodehdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001395 xfs_trans_log_buf(state->args->trans, drop_blk->bp,
Dave Chinner4bceb182013-10-29 22:11:51 +11001396 XFS_DA_LOGRANGE(node, &node->hdr, dp->d_ops->node_hdr_size()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
1399 * Copy the last hash value from the block to propagate upwards.
1400 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001401 drop_blk->hashval = be32_to_cpu(btree[index - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
1404/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001405 * Unbalance the elements between two intermediate nodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * move all Btree elements from one node into another.
1407 */
1408STATIC void
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001409xfs_da3_node_unbalance(
1410 struct xfs_da_state *state,
1411 struct xfs_da_state_blk *drop_blk,
1412 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001414 struct xfs_da_intnode *drop_node;
1415 struct xfs_da_intnode *save_node;
1416 struct xfs_da_node_entry *drop_btree;
1417 struct xfs_da_node_entry *save_btree;
1418 struct xfs_da3_icnode_hdr drop_hdr;
1419 struct xfs_da3_icnode_hdr save_hdr;
1420 struct xfs_trans *tp;
1421 int sindex;
1422 int tmp;
Dave Chinner4bceb182013-10-29 22:11:51 +11001423 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Dave Chinner5a5881c2012-03-22 05:15:13 +00001425 trace_xfs_da_node_unbalance(state->args);
1426
Dave Chinner1d9025e2012-06-22 18:50:14 +10001427 drop_node = drop_blk->bp->b_addr;
1428 save_node = save_blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001429 xfs_da3_node_hdr_from_disk(&drop_hdr, drop_node);
1430 xfs_da3_node_hdr_from_disk(&save_hdr, save_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001431 drop_btree = dp->d_ops->node_tree_p(drop_node);
1432 save_btree = dp->d_ops->node_tree_p(save_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 tp = state->args->trans;
1434
1435 /*
1436 * If the dying block has lower hashvals, then move all the
1437 * elements in the remaining block up to make a hole.
1438 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001439 if ((be32_to_cpu(drop_btree[0].hashval) <
1440 be32_to_cpu(save_btree[0].hashval)) ||
1441 (be32_to_cpu(drop_btree[drop_hdr.count - 1].hashval) <
1442 be32_to_cpu(save_btree[save_hdr.count - 1].hashval))) {
1443 /* XXX: check this - is memmove dst correct? */
1444 tmp = save_hdr.count * sizeof(xfs_da_node_entry_t);
1445 memmove(&save_btree[drop_hdr.count], &save_btree[0], tmp);
1446
1447 sindex = 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001448 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001449 XFS_DA_LOGRANGE(save_node, &save_btree[0],
1450 (save_hdr.count + drop_hdr.count) *
1451 sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001453 sindex = save_hdr.count;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001454 xfs_trans_log_buf(tp, save_blk->bp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001455 XFS_DA_LOGRANGE(save_node, &save_btree[sindex],
1456 drop_hdr.count * sizeof(xfs_da_node_entry_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
1459 /*
1460 * Move all the B-tree elements from drop_blk to save_blk.
1461 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001462 tmp = drop_hdr.count * (uint)sizeof(xfs_da_node_entry_t);
1463 memcpy(&save_btree[sindex], &drop_btree[0], tmp);
1464 save_hdr.count += drop_hdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001466 xfs_da3_node_hdr_to_disk(save_node, &save_hdr);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001467 xfs_trans_log_buf(tp, save_blk->bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 XFS_DA_LOGRANGE(save_node, &save_node->hdr,
Dave Chinner4bceb182013-10-29 22:11:51 +11001469 dp->d_ops->node_hdr_size()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /*
1472 * Save the last hashval in the remaining block for upward propagation.
1473 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001474 save_blk->hashval = be32_to_cpu(save_btree[save_hdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477/*========================================================================
1478 * Routines used for finding things in the Btree.
1479 *========================================================================*/
1480
1481/*
1482 * Walk down the Btree looking for a particular filename, filling
1483 * in the state structure as we go.
1484 *
1485 * We will set the state structure to point to each of the elements
1486 * in each of the nodes where either the hashval is or should be.
1487 *
1488 * We support duplicate hashval's so for each entry in the current
1489 * node that could contain the desired hashval, descend. This is a
1490 * pruned depth-first tree search.
1491 */
1492int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001493xfs_da3_node_lookup_int(
1494 struct xfs_da_state *state,
1495 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001497 struct xfs_da_state_blk *blk;
1498 struct xfs_da_blkinfo *curr;
1499 struct xfs_da_intnode *node;
1500 struct xfs_da_node_entry *btree;
1501 struct xfs_da3_icnode_hdr nodehdr;
1502 struct xfs_da_args *args;
1503 xfs_dablk_t blkno;
1504 xfs_dahash_t hashval;
1505 xfs_dahash_t btreehashval;
1506 int probe;
1507 int span;
1508 int max;
1509 int error;
1510 int retval;
Dave Chinner4bceb182013-10-29 22:11:51 +11001511 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 args = state->args;
1514
1515 /*
1516 * Descend thru the B-tree searching each level for the right
1517 * node to use, until the right hashval is found.
1518 */
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001519 blkno = (args->whichfork == XFS_DATA_FORK)? state->mp->m_dirleafblk : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 for (blk = &state->path.blk[0], state->path.active = 1;
1521 state->path.active <= XFS_DA_NODE_MAXDEPTH;
1522 blk++, state->path.active++) {
1523 /*
1524 * Read the next node down in the tree.
1525 */
1526 blk->blkno = blkno;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001527 error = xfs_da3_node_read(args->trans, args->dp, blkno,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001528 -1, &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (error) {
1530 blk->blkno = 0;
1531 state->path.active--;
1532 return(error);
1533 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001534 curr = blk->bp->b_addr;
Nathan Scottd432c802006-09-28 11:03:44 +10001535 blk->magic = be16_to_cpu(curr->magic);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001536
Dave Chinner517c2222013-04-24 18:58:55 +10001537 if (blk->magic == XFS_ATTR_LEAF_MAGIC ||
1538 blk->magic == XFS_ATTR3_LEAF_MAGIC) {
1539 blk->magic = XFS_ATTR_LEAF_MAGIC;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001540 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
1541 break;
1542 }
1543
1544 if (blk->magic == XFS_DIR2_LEAFN_MAGIC ||
1545 blk->magic == XFS_DIR3_LEAFN_MAGIC) {
1546 blk->magic = XFS_DIR2_LEAFN_MAGIC;
Dave Chinner41419562013-10-29 22:11:50 +11001547 blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
1548 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001549 break;
1550 }
1551
1552 blk->magic = XFS_DA_NODE_MAGIC;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 /*
1556 * Search an intermediate node for a match.
1557 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001558 node = blk->bp->b_addr;
1559 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001560 btree = dp->d_ops->node_tree_p(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001562 max = nodehdr.count;
1563 blk->hashval = be32_to_cpu(btree[max - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001565 /*
1566 * Binary search. (note: small blocks will skip loop)
1567 */
1568 probe = span = max / 2;
1569 hashval = args->hashval;
1570 while (span > 4) {
1571 span /= 2;
1572 btreehashval = be32_to_cpu(btree[probe].hashval);
1573 if (btreehashval < hashval)
1574 probe += span;
1575 else if (btreehashval > hashval)
1576 probe -= span;
1577 else
1578 break;
1579 }
1580 ASSERT((probe >= 0) && (probe < max));
1581 ASSERT((span <= 4) ||
1582 (be32_to_cpu(btree[probe].hashval) == hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001584 /*
1585 * Since we may have duplicate hashval's, find the first
1586 * matching hashval in the node.
1587 */
1588 while (probe > 0 &&
1589 be32_to_cpu(btree[probe].hashval) >= hashval) {
1590 probe--;
1591 }
1592 while (probe < max &&
1593 be32_to_cpu(btree[probe].hashval) < hashval) {
1594 probe++;
1595 }
1596
1597 /*
1598 * Pick the right block to descend on.
1599 */
1600 if (probe == max) {
1601 blk->index = max - 1;
1602 blkno = be32_to_cpu(btree[max - 1].before);
1603 } else {
1604 blk->index = probe;
1605 blkno = be32_to_cpu(btree[probe].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607 }
1608
1609 /*
1610 * A leaf block that ends in the hashval that we are interested in
1611 * (final hashval == search hashval) means that the next block may
1612 * contain more entries with the same hashval, shift upward to the
1613 * next leaf and keep searching.
1614 */
1615 for (;;) {
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001616 if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
1618 &blk->index, state);
Nathan Scottd432c802006-09-28 11:03:44 +10001619 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Dave Chinner517c2222013-04-24 18:58:55 +10001620 retval = xfs_attr3_leaf_lookup_int(blk->bp, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 blk->index = args->index;
1622 args->blkno = blk->blkno;
Nathan Scottd432c802006-09-28 11:03:44 +10001623 } else {
1624 ASSERT(0);
1625 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (((retval == ENOENT) || (retval == ENOATTR)) &&
1628 (blk->hashval == args->hashval)) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001629 error = xfs_da3_path_shift(state, &state->path, 1, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 &retval);
1631 if (error)
1632 return(error);
1633 if (retval == 0) {
1634 continue;
Nathan Scottd432c802006-09-28 11:03:44 +10001635 } else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* path_shift() gives ENOENT */
1637 retval = XFS_ERROR(ENOATTR);
1638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 break;
1641 }
1642 *result = retval;
1643 return(0);
1644}
1645
1646/*========================================================================
1647 * Utility routines.
1648 *========================================================================*/
1649
1650/*
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001651 * Compare two intermediate nodes for "order".
1652 */
1653STATIC int
1654xfs_da3_node_order(
Dave Chinner4bceb182013-10-29 22:11:51 +11001655 struct xfs_inode *dp,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001656 struct xfs_buf *node1_bp,
1657 struct xfs_buf *node2_bp)
1658{
1659 struct xfs_da_intnode *node1;
1660 struct xfs_da_intnode *node2;
1661 struct xfs_da_node_entry *btree1;
1662 struct xfs_da_node_entry *btree2;
1663 struct xfs_da3_icnode_hdr node1hdr;
1664 struct xfs_da3_icnode_hdr node2hdr;
1665
1666 node1 = node1_bp->b_addr;
1667 node2 = node2_bp->b_addr;
1668 xfs_da3_node_hdr_from_disk(&node1hdr, node1);
1669 xfs_da3_node_hdr_from_disk(&node2hdr, node2);
Dave Chinner4bceb182013-10-29 22:11:51 +11001670 btree1 = dp->d_ops->node_tree_p(node1);
1671 btree2 = dp->d_ops->node_tree_p(node2);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001672
1673 if (node1hdr.count > 0 && node2hdr.count > 0 &&
1674 ((be32_to_cpu(btree2[0].hashval) < be32_to_cpu(btree1[0].hashval)) ||
1675 (be32_to_cpu(btree2[node2hdr.count - 1].hashval) <
1676 be32_to_cpu(btree1[node1hdr.count - 1].hashval)))) {
1677 return 1;
1678 }
1679 return 0;
1680}
1681
1682/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 * Link a new block into a doubly linked list of blocks (of whatever type).
1684 */
1685int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001686xfs_da3_blk_link(
1687 struct xfs_da_state *state,
1688 struct xfs_da_state_blk *old_blk,
1689 struct xfs_da_state_blk *new_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001691 struct xfs_da_blkinfo *old_info;
1692 struct xfs_da_blkinfo *new_info;
1693 struct xfs_da_blkinfo *tmp_info;
1694 struct xfs_da_args *args;
1695 struct xfs_buf *bp;
1696 int before = 0;
1697 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001698 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 /*
1701 * Set up environment.
1702 */
1703 args = state->args;
1704 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001705 old_info = old_blk->bp->b_addr;
1706 new_info = new_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001708 old_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 old_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 switch (old_blk->magic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 case XFS_ATTR_LEAF_MAGIC:
1713 before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
1714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 case XFS_DIR2_LEAFN_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001716 before = xfs_dir2_leafn_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 break;
1718 case XFS_DA_NODE_MAGIC:
Dave Chinner4bceb182013-10-29 22:11:51 +11001719 before = xfs_da3_node_order(dp, old_blk->bp, new_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721 }
1722
1723 /*
1724 * Link blocks in appropriate order.
1725 */
1726 if (before) {
1727 /*
1728 * Link new block in before existing block.
1729 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001730 trace_xfs_da_link_before(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001731 new_info->forw = cpu_to_be32(old_blk->blkno);
1732 new_info->back = old_info->back;
1733 if (old_info->back) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001734 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001735 be32_to_cpu(old_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001736 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (error)
1738 return(error);
1739 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001740 tmp_info = bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001741 ASSERT(tmp_info->magic == old_info->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001742 ASSERT(be32_to_cpu(tmp_info->forw) == old_blk->blkno);
1743 tmp_info->forw = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001744 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Nathan Scott89da0542006-03-17 17:28:40 +11001746 old_info->back = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 } else {
1748 /*
1749 * Link new block in after existing block.
1750 */
Dave Chinner5a5881c2012-03-22 05:15:13 +00001751 trace_xfs_da_link_after(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001752 new_info->forw = old_info->forw;
1753 new_info->back = cpu_to_be32(old_blk->blkno);
1754 if (old_info->forw) {
Dave Chinner4bceb182013-10-29 22:11:51 +11001755 error = xfs_da3_node_read(args->trans, dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001756 be32_to_cpu(old_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001757 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (error)
1759 return(error);
1760 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001761 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001762 ASSERT(tmp_info->magic == old_info->magic);
1763 ASSERT(be32_to_cpu(tmp_info->back) == old_blk->blkno);
1764 tmp_info->back = cpu_to_be32(new_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001765 xfs_trans_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
Nathan Scott89da0542006-03-17 17:28:40 +11001767 old_info->forw = cpu_to_be32(new_blk->blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 }
1769
Dave Chinner1d9025e2012-06-22 18:50:14 +10001770 xfs_trans_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
1771 xfs_trans_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return(0);
1773}
1774
1775/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * Unlink a block from a doubly linked list of blocks.
1777 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001778STATIC int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001779xfs_da3_blk_unlink(
1780 struct xfs_da_state *state,
1781 struct xfs_da_state_blk *drop_blk,
1782 struct xfs_da_state_blk *save_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001784 struct xfs_da_blkinfo *drop_info;
1785 struct xfs_da_blkinfo *save_info;
1786 struct xfs_da_blkinfo *tmp_info;
1787 struct xfs_da_args *args;
1788 struct xfs_buf *bp;
1789 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /*
1792 * Set up environment.
1793 */
1794 args = state->args;
1795 ASSERT(args != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001796 save_info = save_blk->bp->b_addr;
1797 drop_info = drop_blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001799 save_blk->magic == XFS_DIR2_LEAFN_MAGIC ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 save_blk->magic == XFS_ATTR_LEAF_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 ASSERT(save_blk->magic == drop_blk->magic);
Nathan Scott89da0542006-03-17 17:28:40 +11001802 ASSERT((be32_to_cpu(save_info->forw) == drop_blk->blkno) ||
1803 (be32_to_cpu(save_info->back) == drop_blk->blkno));
1804 ASSERT((be32_to_cpu(drop_info->forw) == save_blk->blkno) ||
1805 (be32_to_cpu(drop_info->back) == save_blk->blkno));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 /*
1808 * Unlink the leaf block from the doubly linked chain of leaves.
1809 */
Nathan Scott89da0542006-03-17 17:28:40 +11001810 if (be32_to_cpu(save_info->back) == drop_blk->blkno) {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001811 trace_xfs_da_unlink_back(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001812 save_info->back = drop_info->back;
1813 if (drop_info->back) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001814 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001815 be32_to_cpu(drop_info->back),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001816 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (error)
1818 return(error);
1819 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001820 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001821 ASSERT(tmp_info->magic == save_info->magic);
1822 ASSERT(be32_to_cpu(tmp_info->forw) == drop_blk->blkno);
1823 tmp_info->forw = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001824 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 } else {
Dave Chinner5a5881c2012-03-22 05:15:13 +00001828 trace_xfs_da_unlink_forward(args);
Nathan Scott89da0542006-03-17 17:28:40 +11001829 save_info->forw = drop_info->forw;
1830 if (drop_info->forw) {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001831 error = xfs_da3_node_read(args->trans, args->dp,
Nathan Scott89da0542006-03-17 17:28:40 +11001832 be32_to_cpu(drop_info->forw),
Dave Chinnerd9392a42012-11-12 22:54:17 +11001833 -1, &bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (error)
1835 return(error);
1836 ASSERT(bp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001837 tmp_info = bp->b_addr;
Nathan Scott89da0542006-03-17 17:28:40 +11001838 ASSERT(tmp_info->magic == save_info->magic);
1839 ASSERT(be32_to_cpu(tmp_info->back) == drop_blk->blkno);
1840 tmp_info->back = cpu_to_be32(save_blk->blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001841 xfs_trans_log_buf(args->trans, bp, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sizeof(*tmp_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 }
1845
Dave Chinner1d9025e2012-06-22 18:50:14 +10001846 xfs_trans_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return(0);
1848}
1849
1850/*
1851 * Move a path "forward" or "!forward" one block at the current level.
1852 *
1853 * This routine will adjust a "path" to point to the next block
1854 * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
1855 * Btree, including updating pointers to the intermediate nodes between
1856 * the new bottom and the root.
1857 */
1858int /* error */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001859xfs_da3_path_shift(
1860 struct xfs_da_state *state,
1861 struct xfs_da_state_path *path,
1862 int forward,
1863 int release,
1864 int *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001866 struct xfs_da_state_blk *blk;
1867 struct xfs_da_blkinfo *info;
1868 struct xfs_da_intnode *node;
1869 struct xfs_da_args *args;
1870 struct xfs_da_node_entry *btree;
1871 struct xfs_da3_icnode_hdr nodehdr;
1872 xfs_dablk_t blkno = 0;
1873 int level;
1874 int error;
Dave Chinner4bceb182013-10-29 22:11:51 +11001875 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Dave Chinneree732592012-11-12 22:53:53 +11001877 trace_xfs_da_path_shift(state->args);
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /*
1880 * Roll up the Btree looking for the first block where our
1881 * current index is not at the edge of the block. Note that
1882 * we skip the bottom layer because we want the sibling block.
1883 */
1884 args = state->args;
1885 ASSERT(args != NULL);
1886 ASSERT(path != NULL);
1887 ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1888 level = (path->active-1) - 1; /* skip bottom layer in path */
1889 for (blk = &path->blk[level]; level >= 0; blk--, level--) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001890 node = blk->bp->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001891 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001892 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001893
1894 if (forward && (blk->index < nodehdr.count - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 blk->index++;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001896 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
1898 } else if (!forward && (blk->index > 0)) {
1899 blk->index--;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001900 blkno = be32_to_cpu(btree[blk->index].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902 }
1903 }
1904 if (level < 0) {
1905 *result = XFS_ERROR(ENOENT); /* we're out of our tree */
Barry Naujok6a178102008-05-21 16:42:05 +10001906 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return(0);
1908 }
1909
1910 /*
1911 * Roll down the edge of the subtree until we reach the
1912 * same depth we were at originally.
1913 */
1914 for (blk++, level++; level < path->active; blk++, level++) {
1915 /*
1916 * Release the old block.
1917 * (if it's dirty, trans won't actually let go)
1918 */
1919 if (release)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001920 xfs_trans_brelse(args->trans, blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /*
1923 * Read the next child block.
1924 */
1925 blk->blkno = blkno;
Dave Chinner4bceb182013-10-29 22:11:51 +11001926 error = xfs_da3_node_read(args->trans, dp, blkno, -1,
Dave Chinnerd9392a42012-11-12 22:54:17 +11001927 &blk->bp, args->whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (error)
1929 return(error);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001930 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001931 ASSERT(info->magic == cpu_to_be16(XFS_DA_NODE_MAGIC) ||
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001932 info->magic == cpu_to_be16(XFS_DA3_NODE_MAGIC) ||
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001933 info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001934 info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) ||
Dave Chinner517c2222013-04-24 18:58:55 +10001935 info->magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
1936 info->magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001937
1938
1939 /*
1940 * Note: we flatten the magic number to a single type so we
1941 * don't have to compare against crc/non-crc types elsewhere.
1942 */
1943 switch (be16_to_cpu(info->magic)) {
1944 case XFS_DA_NODE_MAGIC:
1945 case XFS_DA3_NODE_MAGIC:
1946 blk->magic = XFS_DA_NODE_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 node = (xfs_da_intnode_t *)info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001948 xfs_da3_node_hdr_from_disk(&nodehdr, node);
Dave Chinner4bceb182013-10-29 22:11:51 +11001949 btree = dp->d_ops->node_tree_p(node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001950 blk->hashval = be32_to_cpu(btree[nodehdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (forward)
1952 blk->index = 0;
1953 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001954 blk->index = nodehdr.count - 1;
1955 blkno = be32_to_cpu(btree[blk->index].before);
1956 break;
1957 case XFS_ATTR_LEAF_MAGIC:
Dave Chinner517c2222013-04-24 18:58:55 +10001958 case XFS_ATTR3_LEAF_MAGIC:
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001959 blk->magic = XFS_ATTR_LEAF_MAGIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 ASSERT(level == path->active-1);
1961 blk->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001962 blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001963 break;
1964 case XFS_DIR2_LEAFN_MAGIC:
1965 case XFS_DIR3_LEAFN_MAGIC:
1966 blk->magic = XFS_DIR2_LEAFN_MAGIC;
1967 ASSERT(level == path->active-1);
1968 blk->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001969 blk->hashval = xfs_dir2_leafn_lasthash(args->dp,
1970 blk->bp, NULL);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001971 break;
1972 default:
1973 ASSERT(0);
1974 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
1976 }
1977 *result = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
1981
1982/*========================================================================
1983 * Utility routines.
1984 *========================================================================*/
1985
1986/*
1987 * Implement a simple hash on a character string.
1988 * Rotate the hash value by 7 bits, then XOR each character in.
1989 * This is implemented with some source-level loop unrolling.
1990 */
1991xfs_dahash_t
Christoph Hellwiga5687782009-02-09 08:37:39 +01001992xfs_da_hashname(const __uint8_t *name, int namelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
1994 xfs_dahash_t hash;
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /*
1997 * Do four characters at a time as long as we can.
1998 */
1999 for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
2000 hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
2001 (name[3] << 0) ^ rol32(hash, 7 * 4);
2002
2003 /*
2004 * Now do the rest of the characters.
2005 */
2006 switch (namelen) {
2007 case 3:
2008 return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
2009 rol32(hash, 7 * 3);
2010 case 2:
2011 return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
2012 case 1:
2013 return (name[0] << 0) ^ rol32(hash, 7 * 1);
Nathan Scott2b3b6d02005-11-02 15:12:28 +11002014 default: /* case 0: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return hash;
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Barry Naujok5163f952008-05-21 16:41:01 +10002019enum xfs_dacmp
2020xfs_da_compname(
2021 struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +11002022 const unsigned char *name,
2023 int len)
Barry Naujok5163f952008-05-21 16:41:01 +10002024{
2025 return (args->namelen == len && memcmp(args->name, name, len) == 0) ?
2026 XFS_CMP_EXACT : XFS_CMP_DIFFERENT;
2027}
2028
2029static xfs_dahash_t
2030xfs_default_hashname(
2031 struct xfs_name *name)
2032{
2033 return xfs_da_hashname(name->name, name->len);
2034}
2035
2036const struct xfs_nameops xfs_default_nameops = {
2037 .hashname = xfs_default_hashname,
2038 .compname = xfs_da_compname
2039};
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041int
Christoph Hellwig77936d02011-07-13 13:43:49 +02002042xfs_da_grow_inode_int(
2043 struct xfs_da_args *args,
2044 xfs_fileoff_t *bno,
2045 int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Christoph Hellwig77936d02011-07-13 13:43:49 +02002047 struct xfs_trans *tp = args->trans;
2048 struct xfs_inode *dp = args->dp;
2049 int w = args->whichfork;
2050 xfs_drfsbno_t nblks = dp->i_d.di_nblocks;
2051 struct xfs_bmbt_irec map, *mapp;
2052 int nmap, error, got, i, mapi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
2055 * Find a spot in the file space to put the new block.
2056 */
Christoph Hellwig77936d02011-07-13 13:43:49 +02002057 error = xfs_bmap_first_unused(tp, dp, count, bno, w);
2058 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
2062 * Try mapping it in one filesystem block.
2063 */
2064 nmap = 1;
2065 ASSERT(args->firstblock != NULL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002066 error = xfs_bmapi_write(tp, dp, *bno, count,
2067 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA|XFS_BMAPI_CONTIG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 args->firstblock, args->total, &map, &nmap,
Christoph Hellwig77936d02011-07-13 13:43:49 +02002069 args->flist);
2070 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return error;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 ASSERT(nmap <= 1);
2074 if (nmap == 1) {
2075 mapp = &map;
2076 mapi = 1;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002077 } else if (nmap == 0 && count > 1) {
2078 xfs_fileoff_t b;
2079 int c;
2080
2081 /*
2082 * If we didn't get it and the block might work if fragmented,
2083 * try without the CONTIG flag. Loop until we get it all.
2084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002086 for (b = *bno, mapi = 0; b < *bno + count; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 nmap = MIN(XFS_BMAP_MAX_NMAP, count);
Christoph Hellwig77936d02011-07-13 13:43:49 +02002088 c = (int)(*bno + count - b);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00002089 error = xfs_bmapi_write(tp, dp, b, c,
2090 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 args->firstblock, args->total,
Christoph Hellwig77936d02011-07-13 13:43:49 +02002092 &mapp[mapi], &nmap, args->flist);
2093 if (error)
2094 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (nmap < 1)
2096 break;
2097 mapi += nmap;
2098 b = mapp[mapi - 1].br_startoff +
2099 mapp[mapi - 1].br_blockcount;
2100 }
2101 } else {
2102 mapi = 0;
2103 mapp = NULL;
2104 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Count the blocks we got, make sure it matches the total.
2108 */
2109 for (i = 0, got = 0; i < mapi; i++)
2110 got += mapp[i].br_blockcount;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002111 if (got != count || mapp[0].br_startoff != *bno ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
Christoph Hellwig77936d02011-07-13 13:43:49 +02002113 *bno + count) {
2114 error = XFS_ERROR(ENOSPC);
2115 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Christoph Hellwig77936d02011-07-13 13:43:49 +02002117
David Chinnera7444052008-10-30 17:38:12 +11002118 /* account for newly allocated blocks in reserved blocks total */
2119 args->total -= dp->i_d.di_nblocks - nblks;
Christoph Hellwig77936d02011-07-13 13:43:49 +02002120
2121out_free_map:
2122 if (mapp != &map)
2123 kmem_free(mapp);
2124 return error;
2125}
2126
2127/*
2128 * Add a block to the btree ahead of the file.
2129 * Return the new block number to the caller.
2130 */
2131int
2132xfs_da_grow_inode(
2133 struct xfs_da_args *args,
2134 xfs_dablk_t *new_blkno)
2135{
2136 xfs_fileoff_t bno;
2137 int count;
2138 int error;
2139
Dave Chinner5a5881c2012-03-22 05:15:13 +00002140 trace_xfs_da_grow_inode(args);
2141
Christoph Hellwig77936d02011-07-13 13:43:49 +02002142 if (args->whichfork == XFS_DATA_FORK) {
2143 bno = args->dp->i_mount->m_dirleafblk;
2144 count = args->dp->i_mount->m_dirblkfsbs;
2145 } else {
2146 bno = 0;
2147 count = 1;
2148 }
2149
2150 error = xfs_da_grow_inode_int(args, &bno, count);
2151 if (!error)
2152 *new_blkno = (xfs_dablk_t)bno;
2153 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
2156/*
2157 * Ick. We need to always be able to remove a btree block, even
2158 * if there's no space reservation because the filesystem is full.
2159 * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
2160 * It swaps the target block with the last block in the file. The
2161 * last block in the file can always be removed since it can't cause
2162 * a bmap btree split to do that.
2163 */
2164STATIC int
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002165xfs_da3_swap_lastblock(
2166 struct xfs_da_args *args,
2167 xfs_dablk_t *dead_blknop,
2168 struct xfs_buf **dead_bufp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002170 struct xfs_da_blkinfo *dead_info;
2171 struct xfs_da_blkinfo *sib_info;
2172 struct xfs_da_intnode *par_node;
2173 struct xfs_da_intnode *dead_node;
2174 struct xfs_dir2_leaf *dead_leaf2;
2175 struct xfs_da_node_entry *btree;
2176 struct xfs_da3_icnode_hdr par_hdr;
Dave Chinner4bceb182013-10-29 22:11:51 +11002177 struct xfs_inode *dp;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002178 struct xfs_trans *tp;
2179 struct xfs_mount *mp;
2180 struct xfs_buf *dead_buf;
2181 struct xfs_buf *last_buf;
2182 struct xfs_buf *sib_buf;
2183 struct xfs_buf *par_buf;
2184 xfs_dahash_t dead_hash;
2185 xfs_fileoff_t lastoff;
2186 xfs_dablk_t dead_blkno;
2187 xfs_dablk_t last_blkno;
2188 xfs_dablk_t sib_blkno;
2189 xfs_dablk_t par_blkno;
2190 int error;
2191 int w;
2192 int entno;
2193 int level;
2194 int dead_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Dave Chinner5a5881c2012-03-22 05:15:13 +00002196 trace_xfs_da_swap_lastblock(args);
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 dead_buf = *dead_bufp;
2199 dead_blkno = *dead_blknop;
2200 tp = args->trans;
Dave Chinner4bceb182013-10-29 22:11:51 +11002201 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 w = args->whichfork;
2203 ASSERT(w == XFS_DATA_FORK);
Dave Chinner4bceb182013-10-29 22:11:51 +11002204 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002205 lastoff = mp->m_dirfreeblk;
Dave Chinner4bceb182013-10-29 22:11:51 +11002206 error = xfs_bmap_last_before(tp, dp, &lastoff, w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (error)
2208 return error;
2209 if (unlikely(lastoff == 0)) {
2210 XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
2211 mp);
2212 return XFS_ERROR(EFSCORRUPTED);
2213 }
2214 /*
2215 * Read the last block in the btree space.
2216 */
2217 last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
Dave Chinner4bceb182013-10-29 22:11:51 +11002218 error = xfs_da3_node_read(tp, dp, last_blkno, -1, &last_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002219 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221 /*
2222 * Copy the last block into the dead buffer and log it.
2223 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002224 memcpy(dead_buf->b_addr, last_buf->b_addr, mp->m_dirblksize);
2225 xfs_trans_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
2226 dead_info = dead_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * Get values from the moved block.
2229 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002230 if (dead_info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
2231 dead_info->magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
2232 struct xfs_dir3_icleaf_hdr leafhdr;
2233 struct xfs_dir2_leaf_entry *ents;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
Dave Chinner24df33b2013-04-12 07:30:21 +10002236 xfs_dir3_leaf_hdr_from_disk(&leafhdr, dead_leaf2);
Dave Chinner4bceb182013-10-29 22:11:51 +11002237 ents = dp->d_ops->leaf_ents_p(dead_leaf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 dead_level = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10002239 dead_hash = be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 } else {
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002241 struct xfs_da3_icnode_hdr deadhdr;
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 dead_node = (xfs_da_intnode_t *)dead_info;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002244 xfs_da3_node_hdr_from_disk(&deadhdr, dead_node);
Dave Chinner4bceb182013-10-29 22:11:51 +11002245 btree = dp->d_ops->node_tree_p(dead_node);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002246 dead_level = deadhdr.level;
2247 dead_hash = be32_to_cpu(btree[deadhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249 sib_buf = par_buf = NULL;
2250 /*
2251 * If the moved block has a left sibling, fix up the pointers.
2252 */
Nathan Scott89da0542006-03-17 17:28:40 +11002253 if ((sib_blkno = be32_to_cpu(dead_info->back))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002254 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002255 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002257 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002259 be32_to_cpu(sib_info->forw) != last_blkno ||
2260 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
2262 XFS_ERRLEVEL_LOW, mp);
2263 error = XFS_ERROR(EFSCORRUPTED);
2264 goto done;
2265 }
Nathan Scott89da0542006-03-17 17:28:40 +11002266 sib_info->forw = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002267 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
2269 sizeof(sib_info->forw)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 sib_buf = NULL;
2271 }
2272 /*
2273 * If the moved block has a right sibling, fix up the pointers.
2274 */
Nathan Scott89da0542006-03-17 17:28:40 +11002275 if ((sib_blkno = be32_to_cpu(dead_info->forw))) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002276 error = xfs_da3_node_read(tp, dp, sib_blkno, -1, &sib_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002277 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002279 sib_info = sib_buf->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (unlikely(
Nathan Scott89da0542006-03-17 17:28:40 +11002281 be32_to_cpu(sib_info->back) != last_blkno ||
2282 sib_info->magic != dead_info->magic)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
2284 XFS_ERRLEVEL_LOW, mp);
2285 error = XFS_ERROR(EFSCORRUPTED);
2286 goto done;
2287 }
Nathan Scott89da0542006-03-17 17:28:40 +11002288 sib_info->back = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002289 xfs_trans_log_buf(tp, sib_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 XFS_DA_LOGRANGE(sib_info, &sib_info->back,
2291 sizeof(sib_info->back)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 sib_buf = NULL;
2293 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002294 par_blkno = mp->m_dirleafblk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 level = -1;
2296 /*
2297 * Walk down the tree looking for the parent of the moved block.
2298 */
2299 for (;;) {
Dave Chinner4bceb182013-10-29 22:11:51 +11002300 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002303 par_node = par_buf->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002304 xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
2305 if (level >= 0 && level != par_hdr.level + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
2307 XFS_ERRLEVEL_LOW, mp);
2308 error = XFS_ERROR(EFSCORRUPTED);
2309 goto done;
2310 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002311 level = par_hdr.level;
Dave Chinner4bceb182013-10-29 22:11:51 +11002312 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 for (entno = 0;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002314 entno < par_hdr.count &&
2315 be32_to_cpu(btree[entno].hashval) < dead_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 entno++)
2317 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002318 if (entno == par_hdr.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
2320 XFS_ERRLEVEL_LOW, mp);
2321 error = XFS_ERROR(EFSCORRUPTED);
2322 goto done;
2323 }
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002324 par_blkno = be32_to_cpu(btree[entno].before);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (level == dead_level + 1)
2326 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002327 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 par_buf = NULL;
2329 }
2330 /*
2331 * We're in the right parent block.
2332 * Look for the right entry.
2333 */
2334 for (;;) {
2335 for (;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002336 entno < par_hdr.count &&
2337 be32_to_cpu(btree[entno].before) != last_blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 entno++)
2339 continue;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002340 if (entno < par_hdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002342 par_blkno = par_hdr.forw;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002343 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 par_buf = NULL;
2345 if (unlikely(par_blkno == 0)) {
2346 XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
2347 XFS_ERRLEVEL_LOW, mp);
2348 error = XFS_ERROR(EFSCORRUPTED);
2349 goto done;
2350 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002351 error = xfs_da3_node_read(tp, dp, par_blkno, -1, &par_buf, w);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002352 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto done;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002354 par_node = par_buf->b_addr;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002355 xfs_da3_node_hdr_from_disk(&par_hdr, par_node);
2356 if (par_hdr.level != level) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
2358 XFS_ERRLEVEL_LOW, mp);
2359 error = XFS_ERROR(EFSCORRUPTED);
2360 goto done;
2361 }
Dave Chinner4bceb182013-10-29 22:11:51 +11002362 btree = dp->d_ops->node_tree_p(par_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 entno = 0;
2364 }
2365 /*
2366 * Update the parent entry pointing to the moved block.
2367 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002368 btree[entno].before = cpu_to_be32(dead_blkno);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002369 xfs_trans_log_buf(tp, par_buf,
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002370 XFS_DA_LOGRANGE(par_node, &btree[entno].before,
2371 sizeof(btree[entno].before)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *dead_blknop = last_blkno;
2373 *dead_bufp = last_buf;
2374 return 0;
2375done:
2376 if (par_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002377 xfs_trans_brelse(tp, par_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (sib_buf)
Dave Chinner1d9025e2012-06-22 18:50:14 +10002379 xfs_trans_brelse(tp, sib_buf);
2380 xfs_trans_brelse(tp, last_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return error;
2382}
2383
2384/*
2385 * Remove a btree block from a directory or attribute.
2386 */
2387int
Dave Chinner1d9025e2012-06-22 18:50:14 +10002388xfs_da_shrink_inode(
2389 xfs_da_args_t *args,
2390 xfs_dablk_t dead_blkno,
2391 struct xfs_buf *dead_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
2393 xfs_inode_t *dp;
2394 int done, error, w, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 xfs_trans_t *tp;
2396 xfs_mount_t *mp;
2397
Dave Chinner5a5881c2012-03-22 05:15:13 +00002398 trace_xfs_da_shrink_inode(args);
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 dp = args->dp;
2401 w = args->whichfork;
2402 tp = args->trans;
2403 mp = dp->i_mount;
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002404 if (w == XFS_DATA_FORK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 count = mp->m_dirblkfsbs;
2406 else
2407 count = 1;
2408 for (;;) {
2409 /*
2410 * Remove extents. If we get ENOSPC for a dir we have to move
2411 * the last block to the place we want to kill.
2412 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002413 error = xfs_bunmapi(tp, dp, dead_blkno, count,
2414 xfs_bmapi_aflag(w)|XFS_BMAPI_METADATA,
2415 0, args->firstblock, args->flist, &done);
2416 if (error == ENOSPC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (w != XFS_DATA_FORK)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002418 break;
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002419 error = xfs_da3_swap_lastblock(args, &dead_blkno,
2420 &dead_buf);
2421 if (error)
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002422 break;
2423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002427 xfs_trans_binval(tp, dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return error;
2429}
2430
2431/*
2432 * See if the mapping(s) for this btree block are valid, i.e.
2433 * don't contain holes, are logically contiguous, and cover the whole range.
2434 */
2435STATIC int
2436xfs_da_map_covers_blocks(
2437 int nmap,
2438 xfs_bmbt_irec_t *mapp,
2439 xfs_dablk_t bno,
2440 int count)
2441{
2442 int i;
2443 xfs_fileoff_t off;
2444
2445 for (i = 0, off = bno; i < nmap; i++) {
2446 if (mapp[i].br_startblock == HOLESTARTBLOCK ||
2447 mapp[i].br_startblock == DELAYSTARTBLOCK) {
2448 return 0;
2449 }
2450 if (off != mapp[i].br_startoff) {
2451 return 0;
2452 }
2453 off += mapp[i].br_blockcount;
2454 }
2455 return off == bno + count;
2456}
2457
2458/*
Dave Chinner36054312012-06-22 18:50:13 +10002459 * Convert a struct xfs_bmbt_irec to a struct xfs_buf_map.
2460 *
2461 * For the single map case, it is assumed that the caller has provided a pointer
2462 * to a valid xfs_buf_map. For the multiple map case, this function will
2463 * allocate the xfs_buf_map to hold all the maps and replace the caller's single
2464 * map pointer with the allocated map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
Dave Chinner36054312012-06-22 18:50:13 +10002466static int
2467xfs_buf_map_from_irec(
2468 struct xfs_mount *mp,
2469 struct xfs_buf_map **mapp,
Dave Chinner836a94a2013-08-12 20:49:44 +10002470 int *nmaps,
Dave Chinner36054312012-06-22 18:50:13 +10002471 struct xfs_bmbt_irec *irecs,
Dave Chinner836a94a2013-08-12 20:49:44 +10002472 int nirecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Dave Chinner36054312012-06-22 18:50:13 +10002474 struct xfs_buf_map *map;
2475 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Dave Chinner36054312012-06-22 18:50:13 +10002477 ASSERT(*nmaps == 1);
2478 ASSERT(nirecs >= 1);
2479
2480 if (nirecs > 1) {
Dave Chinnerb17cb362013-05-20 09:51:12 +10002481 map = kmem_zalloc(nirecs * sizeof(struct xfs_buf_map),
2482 KM_SLEEP | KM_NOFS);
Dave Chinner36054312012-06-22 18:50:13 +10002483 if (!map)
2484 return ENOMEM;
2485 *mapp = map;
2486 }
2487
2488 *nmaps = nirecs;
2489 map = *mapp;
2490 for (i = 0; i < *nmaps; i++) {
2491 ASSERT(irecs[i].br_startblock != DELAYSTARTBLOCK &&
2492 irecs[i].br_startblock != HOLESTARTBLOCK);
2493 map[i].bm_bn = XFS_FSB_TO_DADDR(mp, irecs[i].br_startblock);
2494 map[i].bm_len = XFS_FSB_TO_BB(mp, irecs[i].br_blockcount);
2495 }
2496 return 0;
2497}
2498
2499/*
2500 * Map the block we are given ready for reading. There are three possible return
2501 * values:
2502 * -1 - will be returned if we land in a hole and mappedbno == -2 so the
2503 * caller knows not to execute a subsequent read.
2504 * 0 - if we mapped the block successfully
2505 * >0 - positive error number if there was an error.
2506 */
2507static int
2508xfs_dabuf_map(
2509 struct xfs_trans *trans,
2510 struct xfs_inode *dp,
2511 xfs_dablk_t bno,
2512 xfs_daddr_t mappedbno,
2513 int whichfork,
2514 struct xfs_buf_map **map,
2515 int *nmaps)
2516{
2517 struct xfs_mount *mp = dp->i_mount;
2518 int nfsb;
2519 int error = 0;
2520 struct xfs_bmbt_irec irec;
2521 struct xfs_bmbt_irec *irecs = &irec;
2522 int nirecs;
2523
2524 ASSERT(map && *map);
2525 ASSERT(*nmaps == 1);
2526
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002527 nfsb = (whichfork == XFS_DATA_FORK) ? mp->m_dirblkfsbs : 1;
Dave Chinner36054312012-06-22 18:50:13 +10002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * Caller doesn't have a mapping. -2 means don't complain
2531 * if we land in a hole.
2532 */
2533 if (mappedbno == -1 || mappedbno == -2) {
2534 /*
2535 * Optimize the one-block case.
2536 */
Dave Chinner36054312012-06-22 18:50:13 +10002537 if (nfsb != 1)
Dave Chinnerb17cb362013-05-20 09:51:12 +10002538 irecs = kmem_zalloc(sizeof(irec) * nfsb,
2539 KM_SLEEP | KM_NOFS);
Dave Chinner5b777ad2011-09-18 20:40:46 +00002540
Dave Chinner36054312012-06-22 18:50:13 +10002541 nirecs = nfsb;
2542 error = xfs_bmapi_read(dp, (xfs_fileoff_t)bno, nfsb, irecs,
2543 &nirecs, xfs_bmapi_aflag(whichfork));
Dave Chinner5b777ad2011-09-18 20:40:46 +00002544 if (error)
Dave Chinner36054312012-06-22 18:50:13 +10002545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 } else {
Dave Chinner36054312012-06-22 18:50:13 +10002547 irecs->br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
2548 irecs->br_startoff = (xfs_fileoff_t)bno;
2549 irecs->br_blockcount = nfsb;
2550 irecs->br_state = 0;
2551 nirecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Dave Chinner36054312012-06-22 18:50:13 +10002553
2554 if (!xfs_da_map_covers_blocks(nirecs, irecs, bno, nfsb)) {
2555 error = mappedbno == -2 ? -1 : XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (unlikely(error == EFSCORRUPTED)) {
2557 if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
Dave Chinner36054312012-06-22 18:50:13 +10002558 int i;
Dave Chinner0b932cc2011-03-07 10:08:35 +11002559 xfs_alert(mp, "%s: bno %lld dir: inode %lld",
2560 __func__, (long long)bno,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 (long long)dp->i_ino);
Dave Chinner36054312012-06-22 18:50:13 +10002562 for (i = 0; i < *nmaps; i++) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11002563 xfs_alert(mp,
2564"[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 i,
Dave Chinner36054312012-06-22 18:50:13 +10002566 (long long)irecs[i].br_startoff,
2567 (long long)irecs[i].br_startblock,
2568 (long long)irecs[i].br_blockcount,
2569 irecs[i].br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 }
2572 XFS_ERROR_REPORT("xfs_da_do_buf(1)",
2573 XFS_ERRLEVEL_LOW, mp);
2574 }
Dave Chinner36054312012-06-22 18:50:13 +10002575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
Dave Chinner36054312012-06-22 18:50:13 +10002577 error = xfs_buf_map_from_irec(mp, map, nmaps, irecs, nirecs);
2578out:
2579 if (irecs != &irec)
2580 kmem_free(irecs);
2581 return error;
2582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Dave Chinner36054312012-06-22 18:50:13 +10002584/*
2585 * Get a buffer for the dir/attr block.
2586 */
2587int
2588xfs_da_get_buf(
2589 struct xfs_trans *trans,
2590 struct xfs_inode *dp,
2591 xfs_dablk_t bno,
2592 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002593 struct xfs_buf **bpp,
Dave Chinner36054312012-06-22 18:50:13 +10002594 int whichfork)
2595{
2596 struct xfs_buf *bp;
2597 struct xfs_buf_map map;
2598 struct xfs_buf_map *mapp;
2599 int nmap;
2600 int error;
2601
2602 *bpp = NULL;
2603 mapp = &map;
2604 nmap = 1;
2605 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2606 &mapp, &nmap);
2607 if (error) {
2608 /* mapping a hole is not an error, but we don't continue */
2609 if (error == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 error = 0;
Dave Chinner36054312012-06-22 18:50:13 +10002611 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
Dave Chinner36054312012-06-22 18:50:13 +10002613
2614 bp = xfs_trans_get_buf_map(trans, dp->i_mount->m_ddev_targp,
2615 mapp, nmap, 0);
2616 error = bp ? bp->b_error : XFS_ERROR(EIO);
2617 if (error) {
2618 xfs_trans_brelse(trans, bp);
2619 goto out_free;
2620 }
2621
Dave Chinner1d9025e2012-06-22 18:50:14 +10002622 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002623
2624out_free:
2625 if (mapp != &map)
2626 kmem_free(mapp);
2627
2628 return error;
2629}
2630
2631/*
2632 * Get a buffer for the dir/attr block, fill in the contents.
2633 */
2634int
2635xfs_da_read_buf(
2636 struct xfs_trans *trans,
2637 struct xfs_inode *dp,
2638 xfs_dablk_t bno,
2639 xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +10002640 struct xfs_buf **bpp,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002641 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002642 const struct xfs_buf_ops *ops)
Dave Chinner36054312012-06-22 18:50:13 +10002643{
2644 struct xfs_buf *bp;
2645 struct xfs_buf_map map;
2646 struct xfs_buf_map *mapp;
2647 int nmap;
2648 int error;
2649
2650 *bpp = NULL;
2651 mapp = &map;
2652 nmap = 1;
2653 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
2654 &mapp, &nmap);
2655 if (error) {
2656 /* mapping a hole is not an error, but we don't continue */
2657 if (error == -1)
2658 error = 0;
2659 goto out_free;
2660 }
2661
2662 error = xfs_trans_read_buf_map(dp->i_mount, trans,
2663 dp->i_mount->m_ddev_targp,
Dave Chinner1813dd62012-11-14 17:54:40 +11002664 mapp, nmap, 0, &bp, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002665 if (error)
2666 goto out_free;
2667
2668 if (whichfork == XFS_ATTR_FORK)
2669 xfs_buf_set_ref(bp, XFS_ATTR_BTREE_REF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 else
Dave Chinner36054312012-06-22 18:50:13 +10002671 xfs_buf_set_ref(bp, XFS_DIR_BTREE_REF);
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
Dave Chinner36054312012-06-22 18:50:13 +10002674 * This verification code will be moved to a CRC verification callback
2675 * function so just leave it here unchanged until then.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 */
Dave Chinner36054312012-06-22 18:50:13 +10002677 {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002678 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
2679 xfs_dir2_free_t *free = bp->b_addr;
2680 xfs_da_blkinfo_t *info = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 uint magic, magic1;
Dave Chinner36054312012-06-22 18:50:13 +10002682 struct xfs_mount *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Nathan Scott89da0542006-03-17 17:28:40 +11002684 magic = be16_to_cpu(info->magic);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002685 magic1 = be32_to_cpu(hdr->magic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (unlikely(
2687 XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002688 (magic != XFS_DA3_NODE_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 (magic != XFS_ATTR_LEAF_MAGIC) &&
Dave Chinner517c2222013-04-24 18:58:55 +10002690 (magic != XFS_ATTR3_LEAF_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 (magic != XFS_DIR2_LEAF1_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002692 (magic != XFS_DIR3_LEAF1_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 (magic != XFS_DIR2_LEAFN_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002694 (magic != XFS_DIR3_LEAFN_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002696 (magic1 != XFS_DIR3_BLOCK_MAGIC) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 (magic1 != XFS_DIR2_DATA_MAGIC) &&
Dave Chinner24df33b2013-04-12 07:30:21 +10002698 (magic1 != XFS_DIR3_DATA_MAGIC) &&
2699 (free->hdr.magic !=
2700 cpu_to_be32(XFS_DIR2_FREE_MAGIC)) &&
2701 (free->hdr.magic !=
2702 cpu_to_be32(XFS_DIR3_FREE_MAGIC)),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 mp, XFS_ERRTAG_DA_READ_BUF,
2704 XFS_RANDOM_DA_READ_BUF))) {
Dave Chinner36054312012-06-22 18:50:13 +10002705 trace_xfs_da_btree_corrupt(bp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
2707 XFS_ERRLEVEL_LOW, mp, info);
2708 error = XFS_ERROR(EFSCORRUPTED);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002709 xfs_trans_brelse(trans, bp);
Dave Chinner36054312012-06-22 18:50:13 +10002710 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10002713 *bpp = bp;
Dave Chinner36054312012-06-22 18:50:13 +10002714out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (mapp != &map)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002716 kmem_free(mapp);
Dave Chinner36054312012-06-22 18:50:13 +10002717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return error;
2719}
2720
2721/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 * Readahead the dir/attr block.
2723 */
2724xfs_daddr_t
2725xfs_da_reada_buf(
Dave Chinner36054312012-06-22 18:50:13 +10002726 struct xfs_trans *trans,
2727 struct xfs_inode *dp,
2728 xfs_dablk_t bno,
Dave Chinnerda6958c2012-11-12 22:54:18 +11002729 xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +11002730 int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +11002731 const struct xfs_buf_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Dave Chinner36054312012-06-22 18:50:13 +10002733 struct xfs_buf_map map;
2734 struct xfs_buf_map *mapp;
2735 int nmap;
2736 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Dave Chinner36054312012-06-22 18:50:13 +10002738 mapp = &map;
2739 nmap = 1;
Dave Chinnerda6958c2012-11-12 22:54:18 +11002740 error = xfs_dabuf_map(trans, dp, bno, mappedbno, whichfork,
Dave Chinner36054312012-06-22 18:50:13 +10002741 &mapp, &nmap);
2742 if (error) {
2743 /* mapping a hole is not an error, but we don't continue */
2744 if (error == -1)
2745 error = 0;
2746 goto out_free;
2747 }
2748
2749 mappedbno = mapp[0].bm_bn;
Dave Chinner1813dd62012-11-14 17:54:40 +11002750 xfs_buf_readahead_map(dp->i_mount->m_ddev_targp, mapp, nmap, ops);
Dave Chinner36054312012-06-22 18:50:13 +10002751
2752out_free:
2753 if (mapp != &map)
2754 kmem_free(mapp);
2755
2756 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return -1;
Dave Chinner36054312012-06-22 18:50:13 +10002758 return mappedbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}