blob: 78fc4d9ae7562d02ea2068132ac44a81e9dfa776 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
39#include "xfs_dir2_node.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Function declarations.
45 */
46static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
47static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
48#ifdef DEBUG
49static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
50#else
51#define xfs_dir2_leafn_check(dp, bp)
52#endif
53static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
54 int start_s, xfs_dabuf_t *bp_d, int start_d,
55 int count);
56static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
57 xfs_da_state_blk_t *blk1,
58 xfs_da_state_blk_t *blk2);
59static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
60 int index, xfs_da_state_blk_t *dblk,
61 int *rval);
62static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
63 xfs_da_state_blk_t *fblk);
64
65/*
66 * Log entries from a freespace block.
67 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000068STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070069xfs_dir2_free_log_bests(
70 xfs_trans_t *tp, /* transaction pointer */
71 xfs_dabuf_t *bp, /* freespace buffer */
72 int first, /* first entry to log */
73 int last) /* last entry to log */
74{
75 xfs_dir2_free_t *free; /* freespace structure */
76
77 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110078 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 xfs_da_log_buf(tp, bp,
80 (uint)((char *)&free->bests[first] - (char *)free),
81 (uint)((char *)&free->bests[last] - (char *)free +
82 sizeof(free->bests[0]) - 1));
83}
84
85/*
86 * Log header from a freespace block.
87 */
88static void
89xfs_dir2_free_log_header(
90 xfs_trans_t *tp, /* transaction pointer */
91 xfs_dabuf_t *bp) /* freespace buffer */
92{
93 xfs_dir2_free_t *free; /* freespace structure */
94
95 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110096 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
98 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
99}
100
101/*
102 * Convert a leaf-format directory to a node-format directory.
103 * We need to change the magic number of the leaf block, and copy
104 * the freespace table out of the leaf block into its own block.
105 */
106int /* error */
107xfs_dir2_leaf_to_node(
108 xfs_da_args_t *args, /* operation arguments */
109 xfs_dabuf_t *lbp) /* leaf buffer */
110{
111 xfs_inode_t *dp; /* incore directory inode */
112 int error; /* error return value */
113 xfs_dabuf_t *fbp; /* freespace buffer */
114 xfs_dir2_db_t fdb; /* freespace block number */
115 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100116 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int i; /* leaf freespace index */
118 xfs_dir2_leaf_t *leaf; /* leaf structure */
119 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
120 xfs_mount_t *mp; /* filesystem mount point */
121 int n; /* count of live freespc ents */
122 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100123 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 xfs_trans_t *tp; /* transaction pointer */
125
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000126 trace_xfs_dir2_leaf_to_node(args);
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 dp = args->dp;
129 mp = dp->i_mount;
130 tp = args->trans;
131 /*
132 * Add a freespace block to the directory.
133 */
134 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
135 return error;
136 }
137 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
138 /*
139 * Get the buffer for the new freespace block.
140 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 XFS_DATA_FORK))) {
143 return error;
144 }
145 ASSERT(fbp != NULL);
146 free = fbp->data;
147 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000148 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Initialize the freespace block header.
151 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100152 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100154 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100155 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Copy freespace entries from the leaf block to the new block.
158 * Count active entries.
159 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000160 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100161 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100162 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100164 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100166 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100167 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
169 * Log everything.
170 */
171 xfs_dir2_leaf_log_header(tp, lbp);
172 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100173 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 xfs_da_buf_done(fbp);
175 xfs_dir2_leafn_check(dp, lbp);
176 return 0;
177}
178
179/*
180 * Add a leaf entry to a leaf block in a node-form directory.
181 * The other work necessary is done from the caller.
182 */
183static int /* error */
184xfs_dir2_leafn_add(
185 xfs_dabuf_t *bp, /* leaf buffer */
186 xfs_da_args_t *args, /* operation arguments */
187 int index) /* insertion pt for new entry */
188{
189 int compact; /* compacting stale leaves */
190 xfs_inode_t *dp; /* incore directory inode */
191 int highstale; /* next stale entry */
192 xfs_dir2_leaf_t *leaf; /* leaf structure */
193 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
194 int lfloghigh; /* high leaf entry logging */
195 int lfloglow; /* low leaf entry logging */
196 int lowstale; /* previous stale entry */
197 xfs_mount_t *mp; /* filesystem mount point */
198 xfs_trans_t *tp; /* transaction pointer */
199
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000200 trace_xfs_dir2_leafn_add(args, index);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dp = args->dp;
203 mp = dp->i_mount;
204 tp = args->trans;
205 leaf = bp->data;
206
207 /*
208 * Quick check just to make sure we are not going to index
209 * into other peoples memory
210 */
211 if (index < 0)
212 return XFS_ERROR(EFSCORRUPTED);
213
214 /*
215 * If there are already the maximum number of leaf entries in
216 * the block, if there are no stale entries it won't fit.
217 * Caller will do a split. If there are stale entries we'll do
218 * a compact.
219 */
220
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000221 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!leaf->hdr.stale)
223 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100224 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 } else
226 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100227 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100228 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100229 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Barry Naujok6a178102008-05-21 16:42:05 +1000231 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233
234 /*
235 * Compact out all but one stale leaf entry. Leaves behind
236 * the entry closest to index.
237 */
238 if (compact) {
239 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
240 &lfloglow, &lfloghigh);
241 }
242 /*
243 * Set impossible logging indices for this case.
244 */
245 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100246 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 lfloghigh = -1;
248 }
249 /*
250 * No stale entries, just insert a space for the new entry.
251 */
252 if (!leaf->hdr.stale) {
253 lep = &leaf->ents[index];
Nathan Scotta818e5d2006-03-17 17:28:07 +1100254 if (index < be16_to_cpu(leaf->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 memmove(lep + 1, lep,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100256 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 lfloglow = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100258 lfloghigh = be16_to_cpu(leaf->hdr.count);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800259 be16_add_cpu(&leaf->hdr.count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 /*
262 * There are stale entries. We'll use one for the new entry.
263 */
264 else {
265 /*
266 * If we didn't do a compact then we need to figure out
267 * which stale entry will be used.
268 */
269 if (compact == 0) {
270 /*
271 * Find first stale entry before our insertion point.
272 */
273 for (lowstale = index - 1;
274 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100275 be32_to_cpu(leaf->ents[lowstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 XFS_DIR2_NULL_DATAPTR;
277 lowstale--)
278 continue;
279 /*
280 * Find next stale entry after insertion point.
281 * Stop looking if the answer would be worse than
282 * lowstale already found.
283 */
284 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100285 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100286 be32_to_cpu(leaf->ents[highstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 XFS_DIR2_NULL_DATAPTR &&
288 (lowstale < 0 ||
289 index - lowstale - 1 >= highstale - index);
290 highstale++)
291 continue;
292 }
293 /*
294 * Using the low stale entry.
295 * Shift entries up toward the stale slot.
296 */
297 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100298 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 index - lowstale - 1 < highstale - index)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100300 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 XFS_DIR2_NULL_DATAPTR);
302 ASSERT(index - lowstale - 1 >= 0);
303 if (index - lowstale - 1 > 0)
304 memmove(&leaf->ents[lowstale],
305 &leaf->ents[lowstale + 1],
306 (index - lowstale - 1) * sizeof(*lep));
307 lep = &leaf->ents[index - 1];
308 lfloglow = MIN(lowstale, lfloglow);
309 lfloghigh = MAX(index - 1, lfloghigh);
310 }
311 /*
312 * Using the high stale entry.
313 * Shift entries down toward the stale slot.
314 */
315 else {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100316 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 XFS_DIR2_NULL_DATAPTR);
318 ASSERT(highstale - index >= 0);
319 if (highstale - index > 0)
320 memmove(&leaf->ents[index + 1],
321 &leaf->ents[index],
322 (highstale - index) * sizeof(*lep));
323 lep = &leaf->ents[index];
324 lfloglow = MIN(index, lfloglow);
325 lfloghigh = MAX(highstale, lfloghigh);
326 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800327 be16_add_cpu(&leaf->hdr.stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 /*
330 * Insert the new entry, log everything.
331 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100332 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000333 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100334 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 xfs_dir2_leaf_log_header(tp, bp);
336 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
337 xfs_dir2_leafn_check(dp, bp);
338 return 0;
339}
340
341#ifdef DEBUG
342/*
343 * Check internal consistency of a leafn block.
344 */
345void
346xfs_dir2_leafn_check(
347 xfs_inode_t *dp, /* incore directory inode */
348 xfs_dabuf_t *bp) /* leaf buffer */
349{
350 int i; /* leaf index */
351 xfs_dir2_leaf_t *leaf; /* leaf structure */
352 xfs_mount_t *mp; /* filesystem mount point */
353 int stale; /* count of stale leaves */
354
355 leaf = bp->data;
356 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100357 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000358 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100359 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
360 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100361 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
362 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100364 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 stale++;
366 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100367 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369#endif /* DEBUG */
370
371/*
372 * Return the last hash value in the leaf.
373 * Stale entries are ok.
374 */
375xfs_dahash_t /* hash value */
376xfs_dir2_leafn_lasthash(
377 xfs_dabuf_t *bp, /* leaf buffer */
378 int *count) /* count of entries in leaf */
379{
380 xfs_dir2_leaf_t *leaf; /* leaf structure */
381
382 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100383 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100385 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!leaf->hdr.count)
387 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100388 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000392 * Look up a leaf entry for space to add a name in a node-format leaf block.
393 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000395STATIC int
396xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_dabuf_t *bp, /* leaf buffer */
398 xfs_da_args_t *args, /* operation arguments */
399 int *indexp, /* out: leaf entry index */
400 xfs_da_state_t *state) /* state to fill in */
401{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000402 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
403 xfs_dir2_db_t curdb = -1; /* current data block number */
404 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 xfs_inode_t *dp; /* incore directory inode */
406 int error; /* error return value */
407 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000408 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int index; /* leaf entry index */
410 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000411 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
413 xfs_mount_t *mp; /* filesystem mount point */
414 xfs_dir2_db_t newdb; /* new data block number */
415 xfs_dir2_db_t newfdb; /* new free block number */
416 xfs_trans_t *tp; /* transaction pointer */
417
418 dp = args->dp;
419 tp = args->trans;
420 mp = dp->i_mount;
421 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100422 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100424 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#endif
426 xfs_dir2_leafn_check(dp, bp);
427 /*
428 * Look up the hash value in the leaf entries.
429 */
430 index = xfs_dir2_leaf_search_hash(args, bp);
431 /*
432 * Do we have a buffer coming in?
433 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000434 if (state->extravalid) {
435 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000437 curfdb = state->extrablk.blkno;
438 free = curbp->data;
439 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000441 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * Loop over leaf entries with the right hash value.
444 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000445 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
446 be32_to_cpu(lep->hashval) == args->hashval;
447 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Skip stale leaf entries.
450 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100451 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 continue;
453 /*
454 * Pull the data block number from the entry.
455 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000456 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /*
458 * For addname, we're looking for a place to put the new entry.
459 * We want to use a data block with an entry of equal
460 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000461 *
462 * If this block isn't the data block we already have
463 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000465 if (newdb != curdb) {
466 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000468 * Convert the data block to the free block
469 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000471 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000473 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000475 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000477 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479 if (curbp)
480 xfs_da_brelse(tp, curbp);
481 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000482 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000484 error = xfs_da_read_buf(tp, dp,
485 xfs_dir2_db_to_da(mp, newfdb),
486 -1, &curbp, XFS_DATA_FORK);
487 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000489 free = curbp->data;
490 ASSERT(be32_to_cpu(free->hdr.magic) ==
491 XFS_DIR2_FREE_MAGIC);
492 ASSERT((be32_to_cpu(free->hdr.firstdb) %
493 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
494 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
495 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
496 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000499 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000501 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000503 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000505 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
506 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
507 XFS_ERRLEVEL_LOW, mp);
508 if (curfdb != newfdb)
509 xfs_da_brelse(tp, curbp);
510 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000512 curfdb = newfdb;
513 if (be16_to_cpu(free->bests[fi]) >= length)
514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000517 /* Didn't find any space */
518 fi = -1;
519out:
Barry Naujok6a178102008-05-21 16:42:05 +1000520 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000521 if (curbp) {
522 /* Giving back a free block. */
523 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000525 state->extrablk.index = fi;
526 state->extrablk.blkno = curfdb;
527 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
528 } else {
529 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000532 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
534 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return XFS_ERROR(ENOENT);
536}
537
538/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000539 * Look up a leaf entry in a node-format leaf block.
540 * The extrablk in state a data block.
541 */
542STATIC int
543xfs_dir2_leafn_lookup_for_entry(
544 xfs_dabuf_t *bp, /* leaf buffer */
545 xfs_da_args_t *args, /* operation arguments */
546 int *indexp, /* out: leaf entry index */
547 xfs_da_state_t *state) /* state to fill in */
548{
549 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
550 xfs_dir2_db_t curdb = -1; /* current data block number */
551 xfs_dir2_data_entry_t *dep; /* data block entry */
552 xfs_inode_t *dp; /* incore directory inode */
553 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000554 int index; /* leaf entry index */
555 xfs_dir2_leaf_t *leaf; /* leaf structure */
556 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
557 xfs_mount_t *mp; /* filesystem mount point */
558 xfs_dir2_db_t newdb; /* new data block number */
559 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000560 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000561
562 dp = args->dp;
563 tp = args->trans;
564 mp = dp->i_mount;
565 leaf = bp->data;
566 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
567#ifdef __KERNEL__
568 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
569#endif
570 xfs_dir2_leafn_check(dp, bp);
571 /*
572 * Look up the hash value in the leaf entries.
573 */
574 index = xfs_dir2_leaf_search_hash(args, bp);
575 /*
576 * Do we have a buffer coming in?
577 */
578 if (state->extravalid) {
579 curbp = state->extrablk.bp;
580 curdb = state->extrablk.blkno;
581 }
582 /*
583 * Loop over leaf entries with the right hash value.
584 */
585 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
586 be32_to_cpu(lep->hashval) == args->hashval;
587 lep++, index++) {
588 /*
589 * Skip stale leaf entries.
590 */
591 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
592 continue;
593 /*
594 * Pull the data block number from the entry.
595 */
596 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
597 /*
598 * Not adding a new entry, so we really want to find
599 * the name given to us.
600 *
601 * If it's a different data block, go get it.
602 */
603 if (newdb != curdb) {
604 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000605 * If we had a block before that we aren't saving
606 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000607 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000608 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
609 curdb != state->extrablk.blkno))
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000610 xfs_da_brelse(tp, curbp);
611 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000612 * If needing the block that is saved with a CI match,
613 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000614 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000615 if (args->cmpresult != XFS_CMP_DIFFERENT &&
616 newdb == state->extrablk.blkno) {
617 ASSERT(state->extravalid);
618 curbp = state->extrablk.bp;
619 } else {
620 error = xfs_da_read_buf(tp, dp,
621 xfs_dir2_db_to_da(mp, newdb),
622 -1, &curbp, XFS_DATA_FORK);
623 if (error)
624 return error;
625 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000626 xfs_dir2_data_check(dp, curbp);
627 curdb = newdb;
628 }
629 /*
630 * Point to the data entry.
631 */
632 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
633 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
634 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000635 * Compare the entry and if it's an exact match, return
636 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000637 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000638 */
Barry Naujok5163f952008-05-21 16:41:01 +1000639 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
640 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000641 /* If there is a CI match block, drop it */
642 if (args->cmpresult != XFS_CMP_DIFFERENT &&
643 curdb != state->extrablk.blkno)
644 xfs_da_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000645 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000647 *indexp = index;
648 state->extravalid = 1;
649 state->extrablk.bp = curbp;
650 state->extrablk.blkno = curdb;
651 state->extrablk.index = (int)((char *)dep -
652 (char *)curbp->data);
653 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000654 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000655 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000656 }
657 }
Barry Naujok6a178102008-05-21 16:42:05 +1000658 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
659 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000661 if (args->cmpresult == XFS_CMP_DIFFERENT) {
662 /* Giving back last used data block. */
663 state->extravalid = 1;
664 state->extrablk.bp = curbp;
665 state->extrablk.index = -1;
666 state->extrablk.blkno = curdb;
667 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
668 } else {
669 /* If the curbp is not the CI match block, drop it */
670 if (state->extrablk.bp != curbp)
671 xfs_da_brelse(tp, curbp);
672 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000673 } else {
674 state->extravalid = 0;
675 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000677 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678}
679
680/*
681 * Look up a leaf entry in a node-format leaf block.
682 * If this is an addname then the extrablk in state is a freespace block,
683 * otherwise it's a data block.
684 */
685int
686xfs_dir2_leafn_lookup_int(
687 xfs_dabuf_t *bp, /* leaf buffer */
688 xfs_da_args_t *args, /* operation arguments */
689 int *indexp, /* out: leaf entry index */
690 xfs_da_state_t *state) /* state to fill in */
691{
Barry Naujok6a178102008-05-21 16:42:05 +1000692 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000693 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
694 state);
695 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
696}
697
698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 * Move count leaf entries from source to destination leaf.
700 * Log entries and headers. Stale entries are preserved.
701 */
702static void
703xfs_dir2_leafn_moveents(
704 xfs_da_args_t *args, /* operation arguments */
705 xfs_dabuf_t *bp_s, /* source leaf buffer */
706 int start_s, /* source leaf index */
707 xfs_dabuf_t *bp_d, /* destination leaf buffer */
708 int start_d, /* destination leaf index */
709 int count) /* count of leaves to copy */
710{
711 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
712 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
713 int stale; /* count stale leaves copied */
714 xfs_trans_t *tp; /* transaction pointer */
715
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000716 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /*
719 * Silently return if nothing to do.
720 */
721 if (count == 0) {
722 return;
723 }
724 tp = args->trans;
725 leaf_s = bp_s->data;
726 leaf_d = bp_d->data;
727 /*
728 * If the destination index is not the end of the current
729 * destination leaf entries, open up a hole in the destination
730 * to hold the new entries.
731 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100732 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100734 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sizeof(xfs_dir2_leaf_entry_t));
736 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100737 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739 /*
740 * If the source has stale leaves, count the ones in the copy range
741 * so we can update the header correctly.
742 */
743 if (leaf_s->hdr.stale) {
744 int i; /* temp leaf index */
745
746 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100747 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 stale++;
749 }
750 } else
751 stale = 0;
752 /*
753 * Copy the leaf entries from source to destination.
754 */
755 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
756 count * sizeof(xfs_dir2_leaf_entry_t));
757 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
758 /*
759 * If there are source entries after the ones we copied,
760 * delete the ones we copied by sliding the next ones down.
761 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100762 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
764 count * sizeof(xfs_dir2_leaf_entry_t));
765 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
766 }
767 /*
768 * Update the headers and log them.
769 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800770 be16_add_cpu(&leaf_s->hdr.count, -(count));
771 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
772 be16_add_cpu(&leaf_d->hdr.count, count);
773 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 xfs_dir2_leaf_log_header(tp, bp_s);
775 xfs_dir2_leaf_log_header(tp, bp_d);
776 xfs_dir2_leafn_check(args->dp, bp_s);
777 xfs_dir2_leafn_check(args->dp, bp_d);
778}
779
780/*
781 * Determine the sort order of two leaf blocks.
782 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
783 */
784int /* sort order */
785xfs_dir2_leafn_order(
786 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
787 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
788{
789 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
790 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
791
792 leaf1 = leaf1_bp->data;
793 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100794 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
795 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100796 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
797 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100798 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
799 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
800 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 1;
802 return 0;
803}
804
805/*
806 * Rebalance leaf entries between two leaf blocks.
807 * This is actually only called when the second block is new,
808 * though the code deals with the general case.
809 * A new entry will be inserted in one of the blocks, and that
810 * entry is taken into account when balancing.
811 */
812static void
813xfs_dir2_leafn_rebalance(
814 xfs_da_state_t *state, /* btree cursor */
815 xfs_da_state_blk_t *blk1, /* first btree block */
816 xfs_da_state_blk_t *blk2) /* second btree block */
817{
818 xfs_da_args_t *args; /* operation arguments */
819 int count; /* count (& direction) leaves */
820 int isleft; /* new goes in left leaf */
821 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
822 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
823 int mid; /* midpoint leaf index */
824#ifdef DEBUG
825 int oldstale; /* old count of stale leaves */
826#endif
827 int oldsum; /* old total leaf count */
828 int swap; /* swapped leaf blocks */
829
830 args = state->args;
831 /*
832 * If the block order is wrong, swap the arguments.
833 */
834 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
835 xfs_da_state_blk_t *tmp; /* temp for block swap */
836
837 tmp = blk1;
838 blk1 = blk2;
839 blk2 = tmp;
840 }
841 leaf1 = blk1->bp->data;
842 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100843 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100845 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846#endif
847 mid = oldsum >> 1;
848 /*
849 * If the old leaf count was odd then the new one will be even,
850 * so we need to divide the new count evenly.
851 */
852 if (oldsum & 1) {
853 xfs_dahash_t midhash; /* middle entry hash value */
854
Nathan Scotta818e5d2006-03-17 17:28:07 +1100855 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100856 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100858 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 isleft = args->hashval <= midhash;
860 }
861 /*
862 * If the old count is even then the new count is odd, so there's
863 * no preferred side for the new entry.
864 * Pick the left one.
865 */
866 else
867 isleft = 1;
868 /*
869 * Calculate moved entry count. Positive means left-to-right,
870 * negative means right-to-left. Then move the entries.
871 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100872 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (count > 0)
874 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100875 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 else if (count < 0)
877 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100878 be16_to_cpu(leaf1->hdr.count), count);
879 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
880 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /*
882 * Mark whether we're inserting into the old or new leaf.
883 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100884 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100886 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 state->inleaf = !swap;
888 else
889 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100890 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Adjust the expected index for insertion.
893 */
894 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100895 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000896
897 /*
898 * Finally sanity check just to make sure we are not returning a
899 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
901 if(blk2->index < 0) {
902 state->inleaf = 1;
903 blk2->index = 0;
904 cmn_err(CE_ALERT,
Nathan Scottc41564b2006-03-29 08:55:14 +1000905 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 "blk1->index %d\n",
907 blk1->index);
908 }
909}
910
911/*
912 * Remove an entry from a node directory.
913 * This removes the leaf entry and the data entry,
914 * and updates the free block if necessary.
915 */
916static int /* error */
917xfs_dir2_leafn_remove(
918 xfs_da_args_t *args, /* operation arguments */
919 xfs_dabuf_t *bp, /* leaf buffer */
920 int index, /* leaf entry index */
921 xfs_da_state_blk_t *dblk, /* data block */
922 int *rval) /* resulting block needs join */
923{
924 xfs_dir2_data_t *data; /* data block structure */
925 xfs_dir2_db_t db; /* data block number */
926 xfs_dabuf_t *dbp; /* data block buffer */
927 xfs_dir2_data_entry_t *dep; /* data block entry */
928 xfs_inode_t *dp; /* incore directory inode */
929 xfs_dir2_leaf_t *leaf; /* leaf structure */
930 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
931 int longest; /* longest data free entry */
932 int off; /* data block entry offset */
933 xfs_mount_t *mp; /* filesystem mount point */
934 int needlog; /* need to log data header */
935 int needscan; /* need to rescan data frees */
936 xfs_trans_t *tp; /* transaction pointer */
937
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000938 trace_xfs_dir2_leafn_remove(args, index);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dp = args->dp;
941 tp = args->trans;
942 mp = dp->i_mount;
943 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100944 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /*
946 * Point to the entry we're removing.
947 */
948 lep = &leaf->ents[index];
949 /*
950 * Extract the data block and offset from the entry.
951 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000952 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000954 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 ASSERT(dblk->index == off);
956 /*
957 * Kill the leaf entry by marking it stale.
958 * Log the leaf block changes.
959 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800960 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100962 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 xfs_dir2_leaf_log_ents(tp, bp, index, index);
964 /*
965 * Make the data entry free. Keep track of the longest freespace
966 * in the data block in case it changes.
967 */
968 dbp = dblk->bp;
969 data = dbp->data;
970 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100971 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 needlog = needscan = 0;
973 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000974 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * Rescan the data block freespaces for bestfree.
977 * Log the data block header if needed.
978 */
979 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000980 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (needlog)
982 xfs_dir2_data_log_header(tp, dbp);
983 xfs_dir2_data_check(dp, dbp);
984 /*
985 * If the longest data block freespace changes, need to update
986 * the corresponding freeblock entry.
987 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100988 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int error; /* error return value */
990 xfs_dabuf_t *fbp; /* freeblock buffer */
991 xfs_dir2_db_t fdb; /* freeblock block number */
992 int findex; /* index in freeblock entries */
993 xfs_dir2_free_t *free; /* freeblock structure */
994 int logfree; /* need to log free entry */
995
996 /*
997 * Convert the data block number to a free block,
998 * read in the free block.
999 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001000 fdb = xfs_dir2_db_to_fdb(mp, db);
1001 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 -1, &fbp, XFS_DATA_FORK))) {
1003 return error;
1004 }
1005 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001006 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1007 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 XFS_DIR2_MAX_FREE_BESTS(mp) *
1009 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1010 /*
1011 * Calculate which entry we need to fix.
1012 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001013 findex = xfs_dir2_db_to_fdindex(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +11001014 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /*
1016 * If the data block is now empty we can get rid of it
1017 * (usually).
1018 */
1019 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1020 /*
1021 * Try to punch out the data block.
1022 */
1023 error = xfs_dir2_shrink_inode(args, db, dbp);
1024 if (error == 0) {
1025 dblk->bp = NULL;
1026 data = NULL;
1027 }
1028 /*
1029 * We can get ENOSPC if there's no space reservation.
1030 * In this case just drop the buffer and some one else
1031 * will eventually get rid of the empty block.
1032 */
1033 else if (error == ENOSPC && args->total == 0)
1034 xfs_da_buf_done(dbp);
1035 else
1036 return error;
1037 }
1038 /*
1039 * If we got rid of the data block, we can eliminate that entry
1040 * in the free block.
1041 */
1042 if (data == NULL) {
1043 /*
1044 * One less used entry in the free table.
1045 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001046 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 xfs_dir2_free_log_header(tp, fbp);
1048 /*
1049 * If this was the last entry in the table, we can
1050 * trim the table size back. There might be other
1051 * entries at the end referring to non-existent
1052 * data blocks, get those too.
1053 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001054 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int i; /* free entry index */
1056
1057 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001058 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 i--)
1060 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001061 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 logfree = 0;
1063 }
1064 /*
1065 * Not the last entry, just punch it out.
1066 */
1067 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001068 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 logfree = 1;
1070 }
1071 /*
1072 * If there are no useful entries left in the block,
1073 * get rid of the block if we can.
1074 */
1075 if (!free->hdr.nused) {
1076 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1077 if (error == 0) {
1078 fbp = NULL;
1079 logfree = 0;
1080 } else if (error != ENOSPC || args->total != 0)
1081 return error;
1082 /*
1083 * It's possible to get ENOSPC if there is no
1084 * space reservation. In this case some one
1085 * else will eventually get rid of this block.
1086 */
1087 }
1088 }
1089 /*
1090 * Data block is not empty, just set the free entry to
1091 * the new value.
1092 */
1093 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001094 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 logfree = 1;
1096 }
1097 /*
1098 * Log the free entry that changed, unless we got rid of it.
1099 */
1100 if (logfree)
1101 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1102 /*
1103 * Drop the buffer if we still have it.
1104 */
1105 if (fbp)
1106 xfs_da_buf_done(fbp);
1107 }
1108 xfs_dir2_leafn_check(dp, bp);
1109 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001110 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * to justify trying to join it with a neighbor.
1112 */
1113 *rval =
1114 ((uint)sizeof(leaf->hdr) +
1115 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001116 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 mp->m_dir_magicpct;
1118 return 0;
1119}
1120
1121/*
1122 * Split the leaf entries in the old block into old and new blocks.
1123 */
1124int /* error */
1125xfs_dir2_leafn_split(
1126 xfs_da_state_t *state, /* btree cursor */
1127 xfs_da_state_blk_t *oldblk, /* original block */
1128 xfs_da_state_blk_t *newblk) /* newly created block */
1129{
1130 xfs_da_args_t *args; /* operation arguments */
1131 xfs_dablk_t blkno; /* new leaf block number */
1132 int error; /* error return value */
1133 xfs_mount_t *mp; /* filesystem mount point */
1134
1135 /*
1136 * Allocate space for a new leaf node.
1137 */
1138 args = state->args;
1139 mp = args->dp->i_mount;
1140 ASSERT(args != NULL);
1141 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1142 error = xfs_da_grow_inode(args, &blkno);
1143 if (error) {
1144 return error;
1145 }
1146 /*
1147 * Initialize the new leaf block.
1148 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001149 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1151 if (error) {
1152 return error;
1153 }
1154 newblk->blkno = blkno;
1155 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1156 /*
1157 * Rebalance the entries across the two leaves, link the new
1158 * block into the leaves.
1159 */
1160 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1161 error = xfs_da_blk_link(state, oldblk, newblk);
1162 if (error) {
1163 return error;
1164 }
1165 /*
1166 * Insert the new entry in the correct block.
1167 */
1168 if (state->inleaf)
1169 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1170 else
1171 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1172 /*
1173 * Update last hashval in each block since we added the name.
1174 */
1175 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1176 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1177 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1178 xfs_dir2_leafn_check(args->dp, newblk->bp);
1179 return error;
1180}
1181
1182/*
1183 * Check a leaf block and its neighbors to see if the block should be
1184 * collapsed into one or the other neighbor. Always keep the block
1185 * with the smaller block number.
1186 * If the current block is over 50% full, don't try to join it, return 0.
1187 * If the block is empty, fill in the state structure and return 2.
1188 * If it can be collapsed, fill in the state structure and return 1.
1189 * If nothing can be done, return 0.
1190 */
1191int /* error */
1192xfs_dir2_leafn_toosmall(
1193 xfs_da_state_t *state, /* btree cursor */
1194 int *action) /* resulting action to take */
1195{
1196 xfs_da_state_blk_t *blk; /* leaf block */
1197 xfs_dablk_t blkno; /* leaf block number */
1198 xfs_dabuf_t *bp; /* leaf buffer */
1199 int bytes; /* bytes in use */
1200 int count; /* leaf live entry count */
1201 int error; /* error return value */
1202 int forward; /* sibling block direction */
1203 int i; /* sibling counter */
1204 xfs_da_blkinfo_t *info; /* leaf block header */
1205 xfs_dir2_leaf_t *leaf; /* leaf structure */
1206 int rval; /* result from path_shift */
1207
1208 /*
1209 * Check for the degenerate case of the block being over 50% full.
1210 * If so, it's not worth even looking to see if we might be able
1211 * to coalesce with a sibling.
1212 */
1213 blk = &state->path.blk[state->path.active - 1];
1214 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001215 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001217 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1219 if (bytes > (state->blocksize >> 1)) {
1220 /*
1221 * Blk over 50%, don't try to join.
1222 */
1223 *action = 0;
1224 return 0;
1225 }
1226 /*
1227 * Check for the degenerate case of the block being empty.
1228 * If the block is empty, we'll simply delete it, no need to
1229 * coalesce it with a sibling block. We choose (arbitrarily)
1230 * to merge with the forward block unless it is NULL.
1231 */
1232 if (count == 0) {
1233 /*
1234 * Make altpath point to the block we want to keep and
1235 * path point to the block we want to drop (this one).
1236 */
Nathan Scott89da0542006-03-17 17:28:40 +11001237 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 memcpy(&state->altpath, &state->path, sizeof(state->path));
1239 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1240 &rval);
1241 if (error)
1242 return error;
1243 *action = rval ? 2 : 0;
1244 return 0;
1245 }
1246 /*
1247 * Examine each sibling block to see if we can coalesce with
1248 * at least 25% free space to spare. We need to figure out
1249 * whether to merge with the forward or the backward block.
1250 * We prefer coalescing with the lower numbered sibling so as
1251 * to shrink a directory over time.
1252 */
Nathan Scott89da0542006-03-17 17:28:40 +11001253 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001255 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (blkno == 0)
1257 continue;
1258 /*
1259 * Read the sibling leaf block.
1260 */
1261 if ((error =
1262 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1263 -1, &bp, XFS_DATA_FORK))) {
1264 return error;
1265 }
1266 ASSERT(bp != NULL);
1267 /*
1268 * Count bytes in the two blocks combined.
1269 */
1270 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001271 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 bytes = state->blocksize - (state->blocksize >> 2);
1273 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001274 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001275 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 bytes -= count * (uint)sizeof(leaf->ents[0]);
1277 /*
1278 * Fits with at least 25% to spare.
1279 */
1280 if (bytes >= 0)
1281 break;
1282 xfs_da_brelse(state->args->trans, bp);
1283 }
1284 /*
1285 * Didn't like either block, give up.
1286 */
1287 if (i >= 2) {
1288 *action = 0;
1289 return 0;
1290 }
1291 /*
1292 * Done with the sibling leaf block here, drop the dabuf
1293 * so path_shift can get it.
1294 */
1295 xfs_da_buf_done(bp);
1296 /*
1297 * Make altpath point to the block we want to keep (the lower
1298 * numbered block) and path point to the block we want to drop.
1299 */
1300 memcpy(&state->altpath, &state->path, sizeof(state->path));
1301 if (blkno < blk->blkno)
1302 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1303 &rval);
1304 else
1305 error = xfs_da_path_shift(state, &state->path, forward, 0,
1306 &rval);
1307 if (error) {
1308 return error;
1309 }
1310 *action = rval ? 0 : 1;
1311 return 0;
1312}
1313
1314/*
1315 * Move all the leaf entries from drop_blk to save_blk.
1316 * This is done as part of a join operation.
1317 */
1318void
1319xfs_dir2_leafn_unbalance(
1320 xfs_da_state_t *state, /* cursor */
1321 xfs_da_state_blk_t *drop_blk, /* dead block */
1322 xfs_da_state_blk_t *save_blk) /* surviving block */
1323{
1324 xfs_da_args_t *args; /* operation arguments */
1325 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1326 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1327
1328 args = state->args;
1329 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1330 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1331 drop_leaf = drop_blk->bp->data;
1332 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001333 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1334 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * If there are any stale leaf entries, take this opportunity
1337 * to purge them.
1338 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001339 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001341 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 xfs_dir2_leaf_compact(args, save_blk->bp);
1343 /*
1344 * Move the entries from drop to the appropriate end of save.
1345 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001346 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1348 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001349 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 else
1351 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001352 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001353 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1355}
1356
1357/*
1358 * Top-level node form directory addname routine.
1359 */
1360int /* error */
1361xfs_dir2_node_addname(
1362 xfs_da_args_t *args) /* operation arguments */
1363{
1364 xfs_da_state_blk_t *blk; /* leaf block for insert */
1365 int error; /* error return value */
1366 int rval; /* sub-return value */
1367 xfs_da_state_t *state; /* btree cursor */
1368
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001369 trace_xfs_dir2_node_addname(args);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Allocate and initialize the state (btree cursor).
1373 */
1374 state = xfs_da_state_alloc();
1375 state->args = args;
1376 state->mp = args->dp->i_mount;
1377 state->blocksize = state->mp->m_dirblksize;
1378 state->node_ents = state->mp->m_dir_node_ents;
1379 /*
1380 * Look up the name. We're not supposed to find it, but
1381 * this gives us the insertion point.
1382 */
1383 error = xfs_da_node_lookup_int(state, &rval);
1384 if (error)
1385 rval = error;
1386 if (rval != ENOENT) {
1387 goto done;
1388 }
1389 /*
1390 * Add the data entry to a data block.
1391 * Extravalid is set to a freeblock found by lookup.
1392 */
1393 rval = xfs_dir2_node_addname_int(args,
1394 state->extravalid ? &state->extrablk : NULL);
1395 if (rval) {
1396 goto done;
1397 }
1398 blk = &state->path.blk[state->path.active - 1];
1399 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1400 /*
1401 * Add the new leaf entry.
1402 */
1403 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1404 if (rval == 0) {
1405 /*
1406 * It worked, fix the hash values up the btree.
1407 */
Barry Naujok6a178102008-05-21 16:42:05 +10001408 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 xfs_da_fixhashpath(state, &state->path);
1410 } else {
1411 /*
1412 * It didn't work, we need to split the leaf block.
1413 */
1414 if (args->total == 0) {
1415 ASSERT(rval == ENOSPC);
1416 goto done;
1417 }
1418 /*
1419 * Split the leaf block and insert the new entry.
1420 */
1421 rval = xfs_da_split(state);
1422 }
1423done:
1424 xfs_da_state_free(state);
1425 return rval;
1426}
1427
1428/*
1429 * Add the data entry for a node-format directory name addition.
1430 * The leaf entry is added in xfs_dir2_leafn_add.
1431 * We may enter with a freespace block that the lookup found.
1432 */
1433static int /* error */
1434xfs_dir2_node_addname_int(
1435 xfs_da_args_t *args, /* operation arguments */
1436 xfs_da_state_blk_t *fblk) /* optional freespace block */
1437{
1438 xfs_dir2_data_t *data; /* data block structure */
1439 xfs_dir2_db_t dbno; /* data block number */
1440 xfs_dabuf_t *dbp; /* data block buffer */
1441 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1442 xfs_inode_t *dp; /* incore directory inode */
1443 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1444 int error; /* error return value */
1445 xfs_dir2_db_t fbno; /* freespace block number */
1446 xfs_dabuf_t *fbp; /* freespace buffer */
1447 int findex; /* freespace entry index */
1448 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1449 xfs_dir2_db_t ifbno; /* initial freespace block no */
1450 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1451 int length; /* length of the new entry */
1452 int logfree; /* need to log free entry */
1453 xfs_mount_t *mp; /* filesystem mount point */
1454 int needlog; /* need to log data header */
1455 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001456 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 xfs_trans_t *tp; /* transaction pointer */
1458
1459 dp = args->dp;
1460 mp = dp->i_mount;
1461 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001462 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
1464 * If we came in with a freespace block that means that lookup
1465 * found an entry with our hash value. This is the freespace
1466 * block for that data entry.
1467 */
1468 if (fblk) {
1469 fbp = fblk->bp;
1470 /*
1471 * Remember initial freespace block number.
1472 */
1473 ifbno = fblk->blkno;
1474 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001475 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 findex = fblk->index;
1477 /*
1478 * This means the free entry showed that the data block had
1479 * space for our entry, so we remembered it.
1480 * Use that data block.
1481 */
1482 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001483 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1484 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1485 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1486 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 /*
1489 * The data block looked at didn't have enough room.
1490 * We'll start at the beginning of the freespace entries.
1491 */
1492 else {
1493 dbno = -1;
1494 findex = 0;
1495 }
1496 }
1497 /*
1498 * Didn't come in with a freespace block, so don't have a data block.
1499 */
1500 else {
1501 ifbno = dbno = -1;
1502 fbp = NULL;
1503 findex = 0;
1504 }
1505 /*
1506 * If we don't have a data block yet, we're going to scan the
1507 * freespace blocks looking for one. Figure out what the
1508 * highest freespace block number is.
1509 */
1510 if (dbno == -1) {
1511 xfs_fileoff_t fo; /* freespace block number */
1512
1513 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1514 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001515 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 fbno = ifbno;
1517 }
1518 /*
1519 * While we haven't identified a data block, search the freeblock
1520 * data for a good data block. If we find a null freeblock entry,
1521 * indicating a hole in the data blocks, remember that.
1522 */
1523 while (dbno == -1) {
1524 /*
1525 * If we don't have a freeblock in hand, get the next one.
1526 */
1527 if (fbp == NULL) {
1528 /*
1529 * Happens the first time through unless lookup gave
1530 * us a freespace block to start with.
1531 */
1532 if (++fbno == 0)
1533 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1534 /*
1535 * If it's ifbno we already looked at it.
1536 */
1537 if (fbno == ifbno)
1538 fbno++;
1539 /*
1540 * If it's off the end we're done.
1541 */
1542 if (fbno >= lastfbno)
1543 break;
1544 /*
1545 * Read the block. There can be holes in the
1546 * freespace blocks, so this might not succeed.
1547 * This should be really rare, so there's no reason
1548 * to avoid it.
1549 */
1550 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001551 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 XFS_DATA_FORK))) {
1553 return error;
1554 }
1555 if (unlikely(fbp == NULL)) {
1556 continue;
1557 }
1558 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001559 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 findex = 0;
1561 }
1562 /*
1563 * Look at the current free entry. Is it good enough?
1564 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001565 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1566 be16_to_cpu(free->bests[findex]) >= length)
1567 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 else {
1569 /*
1570 * Are we done with the freeblock?
1571 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001572 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /*
1574 * Drop the block.
1575 */
1576 xfs_da_brelse(tp, fbp);
1577 fbp = NULL;
1578 if (fblk && fblk->bp)
1579 fblk->bp = NULL;
1580 }
1581 }
1582 }
1583 /*
1584 * If we don't have a data block, we need to allocate one and make
1585 * the freespace entries refer to it.
1586 */
1587 if (unlikely(dbno == -1)) {
1588 /*
1589 * Not allowed to allocate, return failure.
1590 */
Barry Naujok6a178102008-05-21 16:42:05 +10001591 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1592 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Drop the freespace buffer unless it came from our
1595 * caller.
1596 */
1597 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1598 xfs_da_buf_done(fbp);
1599 return XFS_ERROR(ENOSPC);
1600 }
1601 /*
1602 * Allocate and initialize the new data block.
1603 */
1604 if (unlikely((error = xfs_dir2_grow_inode(args,
1605 XFS_DIR2_DATA_SPACE,
1606 &dbno)) ||
1607 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1608 /*
1609 * Drop the freespace buffer unless it came from our
1610 * caller.
1611 */
1612 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1613 xfs_da_buf_done(fbp);
1614 return error;
1615 }
1616 /*
1617 * If (somehow) we have a freespace block, get rid of it.
1618 */
1619 if (fbp)
1620 xfs_da_brelse(tp, fbp);
1621 if (fblk && fblk->bp)
1622 fblk->bp = NULL;
1623
1624 /*
1625 * Get the freespace block corresponding to the data block
1626 * that was just allocated.
1627 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001628 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001630 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 XFS_DATA_FORK))) {
1632 xfs_da_buf_done(dbp);
1633 return error;
1634 }
1635 /*
1636 * If there wasn't a freespace block, the read will
1637 * return a NULL fbp. Allocate and initialize a new one.
1638 */
1639 if( fbp == NULL ) {
1640 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1641 &fbno))) {
1642 return error;
1643 }
1644
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001645 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 cmn_err(CE_ALERT,
1647 "xfs_dir2_node_addname_int: dir ino "
1648 "%llu needed freesp block %lld for\n"
1649 " data block %lld, got %lld\n"
1650 " ifbno %llu lastfbno %d\n",
1651 (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001652 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 (long long)dbno, (long long)fbno,
1654 (unsigned long long)ifbno, lastfbno);
1655 if (fblk) {
1656 cmn_err(CE_ALERT,
1657 " fblk 0x%p blkno %llu "
1658 "index %d magic 0x%x\n",
1659 fblk,
1660 (unsigned long long)fblk->blkno,
1661 fblk->index,
1662 fblk->magic);
1663 } else {
1664 cmn_err(CE_ALERT,
1665 " ... fblk is NULL\n");
1666 }
1667 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1668 XFS_ERRLEVEL_LOW, mp);
1669 return XFS_ERROR(EFSCORRUPTED);
1670 }
1671
1672 /*
1673 * Get a buffer for the new block.
1674 */
1675 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001676 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 -1, &fbp, XFS_DATA_FORK))) {
1678 return error;
1679 }
1680 ASSERT(fbp != NULL);
1681
1682 /*
1683 * Initialize the new block to be empty, and remember
1684 * its first slot as our empty slot.
1685 */
1686 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001687 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1688 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1690 XFS_DIR2_MAX_FREE_BESTS(mp));
1691 free->hdr.nvalid = 0;
1692 free->hdr.nused = 0;
1693 } else {
1694 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001695 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
1697
1698 /*
1699 * Set the freespace block index from the data block number.
1700 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001701 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * If it's after the end of the current entries in the
1704 * freespace block, extend that table.
1705 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001706 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001708 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /*
1710 * Tag new entry so nused will go up.
1711 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001712 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 /*
1715 * If this entry was for an empty data block
1716 * (this should always be true) then update the header.
1717 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001718 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001719 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 xfs_dir2_free_log_header(tp, fbp);
1721 }
1722 /*
1723 * Update the real value in the table.
1724 * We haven't allocated the data entry yet so this will
1725 * change again.
1726 */
1727 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001728 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 logfree = 1;
1730 }
1731 /*
1732 * We had a data block so we don't have to make a new one.
1733 */
1734 else {
1735 /*
1736 * If just checking, we succeeded.
1737 */
Barry Naujok6a178102008-05-21 16:42:05 +10001738 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1740 xfs_da_buf_done(fbp);
1741 return 0;
1742 }
1743 /*
1744 * Read the data block in.
1745 */
1746 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001747 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 -1, &dbp, XFS_DATA_FORK))) {
1749 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1750 xfs_da_buf_done(fbp);
1751 return error;
1752 }
1753 data = dbp->data;
1754 logfree = 0;
1755 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001756 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * Point to the existing unused space.
1759 */
1760 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001761 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 needscan = needlog = 0;
1763 /*
1764 * Mark the first part of the unused space, inuse for us.
1765 */
1766 xfs_dir2_data_use_free(tp, dbp, dup,
1767 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1768 &needlog, &needscan);
1769 /*
1770 * Fill in the new entry and log it.
1771 */
1772 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001773 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 dep->namelen = args->namelen;
1775 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001776 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001777 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 xfs_dir2_data_log_entry(tp, dbp, dep);
1779 /*
1780 * Rescan the block for bestfree if needed.
1781 */
1782 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001783 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * Log the data block header if needed.
1786 */
1787 if (needlog)
1788 xfs_dir2_data_log_header(tp, dbp);
1789 /*
1790 * If the freespace entry is now wrong, update it.
1791 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001792 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001793 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 logfree = 1;
1795 }
1796 /*
1797 * Log the freespace entry if needed.
1798 */
1799 if (logfree)
1800 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1801 /*
1802 * If the caller didn't hand us the freespace block, drop it.
1803 */
1804 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1805 xfs_da_buf_done(fbp);
1806 /*
1807 * Return the data block and offset in args, then drop the data block.
1808 */
1809 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001810 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 xfs_da_buf_done(dbp);
1812 return 0;
1813}
1814
1815/*
1816 * Lookup an entry in a node-format directory.
1817 * All the real work happens in xfs_da_node_lookup_int.
1818 * The only real output is the inode number of the entry.
1819 */
1820int /* error */
1821xfs_dir2_node_lookup(
1822 xfs_da_args_t *args) /* operation arguments */
1823{
1824 int error; /* error return value */
1825 int i; /* btree level */
1826 int rval; /* operation return value */
1827 xfs_da_state_t *state; /* btree cursor */
1828
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001829 trace_xfs_dir2_node_lookup(args);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Allocate and initialize the btree cursor.
1833 */
1834 state = xfs_da_state_alloc();
1835 state->args = args;
1836 state->mp = args->dp->i_mount;
1837 state->blocksize = state->mp->m_dirblksize;
1838 state->node_ents = state->mp->m_dir_node_ents;
1839 /*
1840 * Fill in the path to the entry in the cursor.
1841 */
1842 error = xfs_da_node_lookup_int(state, &rval);
1843 if (error)
1844 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001845 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1846 /* If a CI match, dup the actual name and return EEXIST */
1847 xfs_dir2_data_entry_t *dep;
1848
1849 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1850 data + state->extrablk.index);
1851 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
1854 * Release the btree blocks and leaf block.
1855 */
1856 for (i = 0; i < state->path.active; i++) {
1857 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1858 state->path.blk[i].bp = NULL;
1859 }
1860 /*
1861 * Release the data block if we have it.
1862 */
1863 if (state->extravalid && state->extrablk.bp) {
1864 xfs_da_brelse(args->trans, state->extrablk.bp);
1865 state->extrablk.bp = NULL;
1866 }
1867 xfs_da_state_free(state);
1868 return rval;
1869}
1870
1871/*
1872 * Remove an entry from a node-format directory.
1873 */
1874int /* error */
1875xfs_dir2_node_removename(
1876 xfs_da_args_t *args) /* operation arguments */
1877{
1878 xfs_da_state_blk_t *blk; /* leaf block */
1879 int error; /* error return value */
1880 int rval; /* operation return value */
1881 xfs_da_state_t *state; /* btree cursor */
1882
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001883 trace_xfs_dir2_node_removename(args);
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * Allocate and initialize the btree cursor.
1887 */
1888 state = xfs_da_state_alloc();
1889 state->args = args;
1890 state->mp = args->dp->i_mount;
1891 state->blocksize = state->mp->m_dirblksize;
1892 state->node_ents = state->mp->m_dir_node_ents;
1893 /*
1894 * Look up the entry we're deleting, set up the cursor.
1895 */
1896 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001897 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * Didn't find it, upper layer screwed up.
1901 */
1902 if (rval != EEXIST) {
1903 xfs_da_state_free(state);
1904 return rval;
1905 }
1906 blk = &state->path.blk[state->path.active - 1];
1907 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1908 ASSERT(state->extravalid);
1909 /*
1910 * Remove the leaf and data entries.
1911 * Extrablk refers to the data block.
1912 */
1913 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1914 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001915 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * Fix the hash values up the btree.
1919 */
1920 xfs_da_fixhashpath(state, &state->path);
1921 /*
1922 * If we need to join leaf blocks, do it.
1923 */
1924 if (rval && state->path.active > 1)
1925 error = xfs_da_join(state);
1926 /*
1927 * If no errors so far, try conversion to leaf format.
1928 */
1929 if (!error)
1930 error = xfs_dir2_node_to_leaf(state);
1931 xfs_da_state_free(state);
1932 return error;
1933}
1934
1935/*
1936 * Replace an entry's inode number in a node-format directory.
1937 */
1938int /* error */
1939xfs_dir2_node_replace(
1940 xfs_da_args_t *args) /* operation arguments */
1941{
1942 xfs_da_state_blk_t *blk; /* leaf block */
1943 xfs_dir2_data_t *data; /* data block structure */
1944 xfs_dir2_data_entry_t *dep; /* data entry changed */
1945 int error; /* error return value */
1946 int i; /* btree level */
1947 xfs_ino_t inum; /* new inode number */
1948 xfs_dir2_leaf_t *leaf; /* leaf structure */
1949 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1950 int rval; /* internal return value */
1951 xfs_da_state_t *state; /* btree cursor */
1952
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001953 trace_xfs_dir2_node_replace(args);
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * Allocate and initialize the btree cursor.
1957 */
1958 state = xfs_da_state_alloc();
1959 state->args = args;
1960 state->mp = args->dp->i_mount;
1961 state->blocksize = state->mp->m_dirblksize;
1962 state->node_ents = state->mp->m_dir_node_ents;
1963 inum = args->inumber;
1964 /*
1965 * Lookup the entry to change in the btree.
1966 */
1967 error = xfs_da_node_lookup_int(state, &rval);
1968 if (error) {
1969 rval = error;
1970 }
1971 /*
1972 * It should be found, since the vnodeops layer has looked it up
1973 * and locked it. But paranoia is good.
1974 */
1975 if (rval == EEXIST) {
1976 /*
1977 * Find the leaf entry.
1978 */
1979 blk = &state->path.blk[state->path.active - 1];
1980 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1981 leaf = blk->bp->data;
1982 lep = &leaf->ents[blk->index];
1983 ASSERT(state->extravalid);
1984 /*
1985 * Point to the data entry.
1986 */
1987 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001988 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 dep = (xfs_dir2_data_entry_t *)
1990 ((char *)data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001991 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001992 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
1994 * Fill in the new inode number and log the entry.
1995 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001996 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1998 rval = 0;
1999 }
2000 /*
2001 * Didn't find it, and we're holding a data block. Drop it.
2002 */
2003 else if (state->extravalid) {
2004 xfs_da_brelse(args->trans, state->extrablk.bp);
2005 state->extrablk.bp = NULL;
2006 }
2007 /*
2008 * Release all the buffers in the cursor.
2009 */
2010 for (i = 0; i < state->path.active; i++) {
2011 xfs_da_brelse(args->trans, state->path.blk[i].bp);
2012 state->path.blk[i].bp = NULL;
2013 }
2014 xfs_da_state_free(state);
2015 return rval;
2016}
2017
2018/*
2019 * Trim off a trailing empty freespace block.
2020 * Return (in rvalp) 1 if we did it, 0 if not.
2021 */
2022int /* error */
2023xfs_dir2_node_trim_free(
2024 xfs_da_args_t *args, /* operation arguments */
2025 xfs_fileoff_t fo, /* free block number */
2026 int *rvalp) /* out: did something */
2027{
2028 xfs_dabuf_t *bp; /* freespace buffer */
2029 xfs_inode_t *dp; /* incore directory inode */
2030 int error; /* error return code */
2031 xfs_dir2_free_t *free; /* freespace structure */
2032 xfs_mount_t *mp; /* filesystem mount point */
2033 xfs_trans_t *tp; /* transaction pointer */
2034
2035 dp = args->dp;
2036 mp = dp->i_mount;
2037 tp = args->trans;
2038 /*
2039 * Read the freespace block.
2040 */
2041 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2042 XFS_DATA_FORK))) {
2043 return error;
2044 }
2045
2046 /*
2047 * There can be holes in freespace. If fo is a hole, there's
2048 * nothing to do.
2049 */
2050 if (bp == NULL) {
2051 return 0;
2052 }
2053 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11002054 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
2056 * If there are used entries, there's nothing to do.
2057 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11002058 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 xfs_da_brelse(tp, bp);
2060 *rvalp = 0;
2061 return 0;
2062 }
2063 /*
2064 * Blow the block away.
2065 */
2066 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002067 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 bp))) {
2069 /*
2070 * Can't fail with ENOSPC since that only happens with no
2071 * space reservation, when breaking up an extent into two
2072 * pieces. This is the last block of an extent.
2073 */
2074 ASSERT(error != ENOSPC);
2075 xfs_da_brelse(tp, bp);
2076 return error;
2077 }
2078 /*
2079 * Return that we succeeded.
2080 */
2081 *rvalp = 1;
2082 return 0;
2083}