blob: c71cff85950c7e78776e999291ece7a405acf85e [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"
40#include "xfs_dir2_trace.h"
41#include "xfs_error.h"
42
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 */
68void
69xfs_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
126 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
127 dp = args->dp;
128 mp = dp->i_mount;
129 tp = args->trans;
130 /*
131 * Add a freespace block to the directory.
132 */
133 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
134 return error;
135 }
136 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
137 /*
138 * Get the buffer for the new freespace block.
139 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000140 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 XFS_DATA_FORK))) {
142 return error;
143 }
144 ASSERT(fbp != NULL);
145 free = fbp->data;
146 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000147 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /*
149 * Initialize the freespace block header.
150 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100151 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100153 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100154 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /*
156 * Copy freespace entries from the leaf block to the new block.
157 * Count active entries.
158 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000159 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100160 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100161 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100163 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100165 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100166 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /*
168 * Log everything.
169 */
170 xfs_dir2_leaf_log_header(tp, lbp);
171 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100172 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_da_buf_done(fbp);
174 xfs_dir2_leafn_check(dp, lbp);
175 return 0;
176}
177
178/*
179 * Add a leaf entry to a leaf block in a node-form directory.
180 * The other work necessary is done from the caller.
181 */
182static int /* error */
183xfs_dir2_leafn_add(
184 xfs_dabuf_t *bp, /* leaf buffer */
185 xfs_da_args_t *args, /* operation arguments */
186 int index) /* insertion pt for new entry */
187{
188 int compact; /* compacting stale leaves */
189 xfs_inode_t *dp; /* incore directory inode */
190 int highstale; /* next stale entry */
191 xfs_dir2_leaf_t *leaf; /* leaf structure */
192 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
193 int lfloghigh; /* high leaf entry logging */
194 int lfloglow; /* low leaf entry logging */
195 int lowstale; /* previous stale entry */
196 xfs_mount_t *mp; /* filesystem mount point */
197 xfs_trans_t *tp; /* transaction pointer */
198
199 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
200 dp = args->dp;
201 mp = dp->i_mount;
202 tp = args->trans;
203 leaf = bp->data;
204
205 /*
206 * Quick check just to make sure we are not going to index
207 * into other peoples memory
208 */
209 if (index < 0)
210 return XFS_ERROR(EFSCORRUPTED);
211
212 /*
213 * If there are already the maximum number of leaf entries in
214 * the block, if there are no stale entries it won't fit.
215 * Caller will do a split. If there are stale entries we'll do
216 * a compact.
217 */
218
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else
224 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Barry Naujok6a178102008-05-21 16:42:05 +1000229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231
232 /*
233 * Compact out all but one stale leaf entry. Leaves behind
234 * the entry closest to index.
235 */
236 if (compact) {
237 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
238 &lfloglow, &lfloghigh);
239 }
240 /*
241 * Set impossible logging indices for this case.
242 */
243 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100244 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 lfloghigh = -1;
246 }
247 /*
248 * No stale entries, just insert a space for the new entry.
249 */
250 if (!leaf->hdr.stale) {
251 lep = &leaf->ents[index];
Nathan Scotta818e5d2006-03-17 17:28:07 +1100252 if (index < be16_to_cpu(leaf->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 memmove(lep + 1, lep,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100254 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 lfloglow = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100256 lfloghigh = be16_to_cpu(leaf->hdr.count);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800257 be16_add_cpu(&leaf->hdr.count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 /*
260 * There are stale entries. We'll use one for the new entry.
261 */
262 else {
263 /*
264 * If we didn't do a compact then we need to figure out
265 * which stale entry will be used.
266 */
267 if (compact == 0) {
268 /*
269 * Find first stale entry before our insertion point.
270 */
271 for (lowstale = index - 1;
272 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100273 be32_to_cpu(leaf->ents[lowstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 XFS_DIR2_NULL_DATAPTR;
275 lowstale--)
276 continue;
277 /*
278 * Find next stale entry after insertion point.
279 * Stop looking if the answer would be worse than
280 * lowstale already found.
281 */
282 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100283 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100284 be32_to_cpu(leaf->ents[highstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 XFS_DIR2_NULL_DATAPTR &&
286 (lowstale < 0 ||
287 index - lowstale - 1 >= highstale - index);
288 highstale++)
289 continue;
290 }
291 /*
292 * Using the low stale entry.
293 * Shift entries up toward the stale slot.
294 */
295 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100296 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 index - lowstale - 1 < highstale - index)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100298 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 XFS_DIR2_NULL_DATAPTR);
300 ASSERT(index - lowstale - 1 >= 0);
301 if (index - lowstale - 1 > 0)
302 memmove(&leaf->ents[lowstale],
303 &leaf->ents[lowstale + 1],
304 (index - lowstale - 1) * sizeof(*lep));
305 lep = &leaf->ents[index - 1];
306 lfloglow = MIN(lowstale, lfloglow);
307 lfloghigh = MAX(index - 1, lfloghigh);
308 }
309 /*
310 * Using the high stale entry.
311 * Shift entries down toward the stale slot.
312 */
313 else {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100314 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_DIR2_NULL_DATAPTR);
316 ASSERT(highstale - index >= 0);
317 if (highstale - index > 0)
318 memmove(&leaf->ents[index + 1],
319 &leaf->ents[index],
320 (highstale - index) * sizeof(*lep));
321 lep = &leaf->ents[index];
322 lfloglow = MIN(index, lfloglow);
323 lfloghigh = MAX(highstale, lfloghigh);
324 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800325 be16_add_cpu(&leaf->hdr.stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 /*
328 * Insert the new entry, log everything.
329 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100330 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000331 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100332 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_dir2_leaf_log_header(tp, bp);
334 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
335 xfs_dir2_leafn_check(dp, bp);
336 return 0;
337}
338
339#ifdef DEBUG
340/*
341 * Check internal consistency of a leafn block.
342 */
343void
344xfs_dir2_leafn_check(
345 xfs_inode_t *dp, /* incore directory inode */
346 xfs_dabuf_t *bp) /* leaf buffer */
347{
348 int i; /* leaf index */
349 xfs_dir2_leaf_t *leaf; /* leaf structure */
350 xfs_mount_t *mp; /* filesystem mount point */
351 int stale; /* count of stale leaves */
352
353 leaf = bp->data;
354 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100355 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000356 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100357 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
358 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100359 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
360 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100362 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 stale++;
364 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100365 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367#endif /* DEBUG */
368
369/*
370 * Return the last hash value in the leaf.
371 * Stale entries are ok.
372 */
373xfs_dahash_t /* hash value */
374xfs_dir2_leafn_lasthash(
375 xfs_dabuf_t *bp, /* leaf buffer */
376 int *count) /* count of entries in leaf */
377{
378 xfs_dir2_leaf_t *leaf; /* leaf structure */
379
380 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100381 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100383 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!leaf->hdr.count)
385 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100386 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000390 * Look up a leaf entry for space to add a name in a node-format leaf block.
391 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000393STATIC int
394xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_dabuf_t *bp, /* leaf buffer */
396 xfs_da_args_t *args, /* operation arguments */
397 int *indexp, /* out: leaf entry index */
398 xfs_da_state_t *state) /* state to fill in */
399{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000400 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
401 xfs_dir2_db_t curdb = -1; /* current data block number */
402 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_inode_t *dp; /* incore directory inode */
404 int error; /* error return value */
405 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int index; /* leaf entry index */
408 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000409 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
411 xfs_mount_t *mp; /* filesystem mount point */
412 xfs_dir2_db_t newdb; /* new data block number */
413 xfs_dir2_db_t newfdb; /* new free block number */
414 xfs_trans_t *tp; /* transaction pointer */
415
416 dp = args->dp;
417 tp = args->trans;
418 mp = dp->i_mount;
419 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100420 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100422 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif
424 xfs_dir2_leafn_check(dp, bp);
425 /*
426 * Look up the hash value in the leaf entries.
427 */
428 index = xfs_dir2_leaf_search_hash(args, bp);
429 /*
430 * Do we have a buffer coming in?
431 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000432 if (state->extravalid) {
433 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000435 curfdb = state->extrablk.blkno;
436 free = curbp->data;
437 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000439 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Loop over leaf entries with the right hash value.
442 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000443 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
444 be32_to_cpu(lep->hashval) == args->hashval;
445 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Skip stale leaf entries.
448 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100449 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 continue;
451 /*
452 * Pull the data block number from the entry.
453 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000454 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * For addname, we're looking for a place to put the new entry.
457 * We want to use a data block with an entry of equal
458 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000459 *
460 * If this block isn't the data block we already have
461 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000463 if (newdb != curdb) {
464 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000466 * Convert the data block to the free block
467 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000469 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000471 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000473 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000475 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
477 if (curbp)
478 xfs_da_brelse(tp, curbp);
479 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000480 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000482 error = xfs_da_read_buf(tp, dp,
483 xfs_dir2_db_to_da(mp, newfdb),
484 -1, &curbp, XFS_DATA_FORK);
485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000487 free = curbp->data;
488 ASSERT(be32_to_cpu(free->hdr.magic) ==
489 XFS_DIR2_FREE_MAGIC);
490 ASSERT((be32_to_cpu(free->hdr.firstdb) %
491 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
492 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
493 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
494 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000497 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000499 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000501 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000503 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
504 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
505 XFS_ERRLEVEL_LOW, mp);
506 if (curfdb != newfdb)
507 xfs_da_brelse(tp, curbp);
508 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000510 curfdb = newfdb;
511 if (be16_to_cpu(free->bests[fi]) >= length)
512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000515 /* Didn't find any space */
516 fi = -1;
517out:
Barry Naujok6a178102008-05-21 16:42:05 +1000518 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000519 if (curbp) {
520 /* Giving back a free block. */
521 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000523 state->extrablk.index = fi;
524 state->extrablk.blkno = curfdb;
525 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
526 } else {
527 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000530 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return XFS_ERROR(ENOENT);
534}
535
536/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000537 * Look up a leaf entry in a node-format leaf block.
538 * The extrablk in state a data block.
539 */
540STATIC int
541xfs_dir2_leafn_lookup_for_entry(
542 xfs_dabuf_t *bp, /* leaf buffer */
543 xfs_da_args_t *args, /* operation arguments */
544 int *indexp, /* out: leaf entry index */
545 xfs_da_state_t *state) /* state to fill in */
546{
547 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
548 xfs_dir2_db_t curdb = -1; /* current data block number */
549 xfs_dir2_data_entry_t *dep; /* data block entry */
550 xfs_inode_t *dp; /* incore directory inode */
551 int error; /* error return value */
552 int di; /* data entry index */
553 int index; /* leaf entry index */
554 xfs_dir2_leaf_t *leaf; /* leaf structure */
555 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
556 xfs_mount_t *mp; /* filesystem mount point */
557 xfs_dir2_db_t newdb; /* new data block number */
558 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000559 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000560
561 dp = args->dp;
562 tp = args->trans;
563 mp = dp->i_mount;
564 leaf = bp->data;
565 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
566#ifdef __KERNEL__
567 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
568#endif
569 xfs_dir2_leafn_check(dp, bp);
570 /*
571 * Look up the hash value in the leaf entries.
572 */
573 index = xfs_dir2_leaf_search_hash(args, bp);
574 /*
575 * Do we have a buffer coming in?
576 */
577 if (state->extravalid) {
578 curbp = state->extrablk.bp;
579 curdb = state->extrablk.blkno;
580 }
581 /*
582 * Loop over leaf entries with the right hash value.
583 */
584 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
585 be32_to_cpu(lep->hashval) == args->hashval;
586 lep++, index++) {
587 /*
588 * Skip stale leaf entries.
589 */
590 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
591 continue;
592 /*
593 * Pull the data block number from the entry.
594 */
595 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
596 /*
597 * Not adding a new entry, so we really want to find
598 * the name given to us.
599 *
600 * If it's a different data block, go get it.
601 */
602 if (newdb != curdb) {
603 /*
604 * If we had a block before, drop it.
605 */
606 if (curbp)
607 xfs_da_brelse(tp, curbp);
608 /*
609 * Read the data block.
610 */
611 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp,
612 newdb), -1, &curbp, XFS_DATA_FORK);
613 if (error)
614 return error;
615 xfs_dir2_data_check(dp, curbp);
616 curdb = newdb;
617 }
618 /*
619 * Point to the data entry.
620 */
621 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
622 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
623 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000624 * Compare the entry and if it's an exact match, return
625 * EEXIST immediately. If it's the first case-insensitive
626 * match, store the inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 */
Barry Naujok5163f952008-05-21 16:41:01 +1000628 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
629 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
630 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 args->inumber = be64_to_cpu(dep->inumber);
632 di = (int)((char *)dep - (char *)curbp->data);
633 error = EEXIST;
Barry Naujok5163f952008-05-21 16:41:01 +1000634 if (cmp == XFS_CMP_EXACT)
635 goto out;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000636 }
637 }
Barry Naujok5163f952008-05-21 16:41:01 +1000638 /* Didn't find an exact match. */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 error = ENOENT;
640 di = -1;
Barry Naujok6a178102008-05-21 16:42:05 +1000641 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
642 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643out:
644 if (curbp) {
645 /* Giving back a data block. */
646 state->extravalid = 1;
647 state->extrablk.bp = curbp;
648 state->extrablk.index = di;
649 state->extrablk.blkno = curdb;
650 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
651 } else {
652 state->extravalid = 0;
653 }
654 /*
655 * Return the index, that will be the insertion point.
656 */
657 *indexp = index;
658 return XFS_ERROR(error);
659}
660
661/*
662 * Look up a leaf entry in a node-format leaf block.
663 * If this is an addname then the extrablk in state is a freespace block,
664 * otherwise it's a data block.
665 */
666int
667xfs_dir2_leafn_lookup_int(
668 xfs_dabuf_t *bp, /* leaf buffer */
669 xfs_da_args_t *args, /* operation arguments */
670 int *indexp, /* out: leaf entry index */
671 xfs_da_state_t *state) /* state to fill in */
672{
Barry Naujok6a178102008-05-21 16:42:05 +1000673 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
675 state);
676 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
677}
678
679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * Move count leaf entries from source to destination leaf.
681 * Log entries and headers. Stale entries are preserved.
682 */
683static void
684xfs_dir2_leafn_moveents(
685 xfs_da_args_t *args, /* operation arguments */
686 xfs_dabuf_t *bp_s, /* source leaf buffer */
687 int start_s, /* source leaf index */
688 xfs_dabuf_t *bp_d, /* destination leaf buffer */
689 int start_d, /* destination leaf index */
690 int count) /* count of leaves to copy */
691{
692 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
693 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
694 int stale; /* count stale leaves copied */
695 xfs_trans_t *tp; /* transaction pointer */
696
697 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
698 start_d, count);
699 /*
700 * Silently return if nothing to do.
701 */
702 if (count == 0) {
703 return;
704 }
705 tp = args->trans;
706 leaf_s = bp_s->data;
707 leaf_d = bp_d->data;
708 /*
709 * If the destination index is not the end of the current
710 * destination leaf entries, open up a hole in the destination
711 * to hold the new entries.
712 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100713 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100715 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 sizeof(xfs_dir2_leaf_entry_t));
717 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100718 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 /*
721 * If the source has stale leaves, count the ones in the copy range
722 * so we can update the header correctly.
723 */
724 if (leaf_s->hdr.stale) {
725 int i; /* temp leaf index */
726
727 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100728 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 stale++;
730 }
731 } else
732 stale = 0;
733 /*
734 * Copy the leaf entries from source to destination.
735 */
736 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
737 count * sizeof(xfs_dir2_leaf_entry_t));
738 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
739 /*
740 * If there are source entries after the ones we copied,
741 * delete the ones we copied by sliding the next ones down.
742 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100743 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
745 count * sizeof(xfs_dir2_leaf_entry_t));
746 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
747 }
748 /*
749 * Update the headers and log them.
750 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800751 be16_add_cpu(&leaf_s->hdr.count, -(count));
752 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
753 be16_add_cpu(&leaf_d->hdr.count, count);
754 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xfs_dir2_leaf_log_header(tp, bp_s);
756 xfs_dir2_leaf_log_header(tp, bp_d);
757 xfs_dir2_leafn_check(args->dp, bp_s);
758 xfs_dir2_leafn_check(args->dp, bp_d);
759}
760
761/*
762 * Determine the sort order of two leaf blocks.
763 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
764 */
765int /* sort order */
766xfs_dir2_leafn_order(
767 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
768 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
769{
770 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
771 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
772
773 leaf1 = leaf1_bp->data;
774 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100775 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
776 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100777 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
778 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100779 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
780 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
781 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 1;
783 return 0;
784}
785
786/*
787 * Rebalance leaf entries between two leaf blocks.
788 * This is actually only called when the second block is new,
789 * though the code deals with the general case.
790 * A new entry will be inserted in one of the blocks, and that
791 * entry is taken into account when balancing.
792 */
793static void
794xfs_dir2_leafn_rebalance(
795 xfs_da_state_t *state, /* btree cursor */
796 xfs_da_state_blk_t *blk1, /* first btree block */
797 xfs_da_state_blk_t *blk2) /* second btree block */
798{
799 xfs_da_args_t *args; /* operation arguments */
800 int count; /* count (& direction) leaves */
801 int isleft; /* new goes in left leaf */
802 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
803 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
804 int mid; /* midpoint leaf index */
805#ifdef DEBUG
806 int oldstale; /* old count of stale leaves */
807#endif
808 int oldsum; /* old total leaf count */
809 int swap; /* swapped leaf blocks */
810
811 args = state->args;
812 /*
813 * If the block order is wrong, swap the arguments.
814 */
815 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
816 xfs_da_state_blk_t *tmp; /* temp for block swap */
817
818 tmp = blk1;
819 blk1 = blk2;
820 blk2 = tmp;
821 }
822 leaf1 = blk1->bp->data;
823 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100824 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100826 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827#endif
828 mid = oldsum >> 1;
829 /*
830 * If the old leaf count was odd then the new one will be even,
831 * so we need to divide the new count evenly.
832 */
833 if (oldsum & 1) {
834 xfs_dahash_t midhash; /* middle entry hash value */
835
Nathan Scotta818e5d2006-03-17 17:28:07 +1100836 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100837 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100839 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 isleft = args->hashval <= midhash;
841 }
842 /*
843 * If the old count is even then the new count is odd, so there's
844 * no preferred side for the new entry.
845 * Pick the left one.
846 */
847 else
848 isleft = 1;
849 /*
850 * Calculate moved entry count. Positive means left-to-right,
851 * negative means right-to-left. Then move the entries.
852 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100853 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (count > 0)
855 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100856 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else if (count < 0)
858 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100859 be16_to_cpu(leaf1->hdr.count), count);
860 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
861 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * Mark whether we're inserting into the old or new leaf.
864 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100865 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100867 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 state->inleaf = !swap;
869 else
870 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100871 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Adjust the expected index for insertion.
874 */
875 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100876 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000877
878 /*
879 * Finally sanity check just to make sure we are not returning a
880 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
882 if(blk2->index < 0) {
883 state->inleaf = 1;
884 blk2->index = 0;
885 cmn_err(CE_ALERT,
Nathan Scottc41564b2006-03-29 08:55:14 +1000886 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 "blk1->index %d\n",
888 blk1->index);
889 }
890}
891
892/*
893 * Remove an entry from a node directory.
894 * This removes the leaf entry and the data entry,
895 * and updates the free block if necessary.
896 */
897static int /* error */
898xfs_dir2_leafn_remove(
899 xfs_da_args_t *args, /* operation arguments */
900 xfs_dabuf_t *bp, /* leaf buffer */
901 int index, /* leaf entry index */
902 xfs_da_state_blk_t *dblk, /* data block */
903 int *rval) /* resulting block needs join */
904{
905 xfs_dir2_data_t *data; /* data block structure */
906 xfs_dir2_db_t db; /* data block number */
907 xfs_dabuf_t *dbp; /* data block buffer */
908 xfs_dir2_data_entry_t *dep; /* data block entry */
909 xfs_inode_t *dp; /* incore directory inode */
910 xfs_dir2_leaf_t *leaf; /* leaf structure */
911 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
912 int longest; /* longest data free entry */
913 int off; /* data block entry offset */
914 xfs_mount_t *mp; /* filesystem mount point */
915 int needlog; /* need to log data header */
916 int needscan; /* need to rescan data frees */
917 xfs_trans_t *tp; /* transaction pointer */
918
919 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
920 dp = args->dp;
921 tp = args->trans;
922 mp = dp->i_mount;
923 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100924 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 /*
926 * Point to the entry we're removing.
927 */
928 lep = &leaf->ents[index];
929 /*
930 * Extract the data block and offset from the entry.
931 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000932 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000934 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ASSERT(dblk->index == off);
936 /*
937 * Kill the leaf entry by marking it stale.
938 * Log the leaf block changes.
939 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800940 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100942 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 xfs_dir2_leaf_log_ents(tp, bp, index, index);
944 /*
945 * Make the data entry free. Keep track of the longest freespace
946 * in the data block in case it changes.
947 */
948 dbp = dblk->bp;
949 data = dbp->data;
950 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100951 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 needlog = needscan = 0;
953 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000954 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * Rescan the data block freespaces for bestfree.
957 * Log the data block header if needed.
958 */
959 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000960 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (needlog)
962 xfs_dir2_data_log_header(tp, dbp);
963 xfs_dir2_data_check(dp, dbp);
964 /*
965 * If the longest data block freespace changes, need to update
966 * the corresponding freeblock entry.
967 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100968 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int error; /* error return value */
970 xfs_dabuf_t *fbp; /* freeblock buffer */
971 xfs_dir2_db_t fdb; /* freeblock block number */
972 int findex; /* index in freeblock entries */
973 xfs_dir2_free_t *free; /* freeblock structure */
974 int logfree; /* need to log free entry */
975
976 /*
977 * Convert the data block number to a free block,
978 * read in the free block.
979 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000980 fdb = xfs_dir2_db_to_fdb(mp, db);
981 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 -1, &fbp, XFS_DATA_FORK))) {
983 return error;
984 }
985 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100986 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
987 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 XFS_DIR2_MAX_FREE_BESTS(mp) *
989 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
990 /*
991 * Calculate which entry we need to fix.
992 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000993 findex = xfs_dir2_db_to_fdindex(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +1100994 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * If the data block is now empty we can get rid of it
997 * (usually).
998 */
999 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1000 /*
1001 * Try to punch out the data block.
1002 */
1003 error = xfs_dir2_shrink_inode(args, db, dbp);
1004 if (error == 0) {
1005 dblk->bp = NULL;
1006 data = NULL;
1007 }
1008 /*
1009 * We can get ENOSPC if there's no space reservation.
1010 * In this case just drop the buffer and some one else
1011 * will eventually get rid of the empty block.
1012 */
1013 else if (error == ENOSPC && args->total == 0)
1014 xfs_da_buf_done(dbp);
1015 else
1016 return error;
1017 }
1018 /*
1019 * If we got rid of the data block, we can eliminate that entry
1020 * in the free block.
1021 */
1022 if (data == NULL) {
1023 /*
1024 * One less used entry in the free table.
1025 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001026 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 xfs_dir2_free_log_header(tp, fbp);
1028 /*
1029 * If this was the last entry in the table, we can
1030 * trim the table size back. There might be other
1031 * entries at the end referring to non-existent
1032 * data blocks, get those too.
1033 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001034 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 int i; /* free entry index */
1036
1037 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001038 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 i--)
1040 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001041 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 logfree = 0;
1043 }
1044 /*
1045 * Not the last entry, just punch it out.
1046 */
1047 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001048 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 logfree = 1;
1050 }
1051 /*
1052 * If there are no useful entries left in the block,
1053 * get rid of the block if we can.
1054 */
1055 if (!free->hdr.nused) {
1056 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1057 if (error == 0) {
1058 fbp = NULL;
1059 logfree = 0;
1060 } else if (error != ENOSPC || args->total != 0)
1061 return error;
1062 /*
1063 * It's possible to get ENOSPC if there is no
1064 * space reservation. In this case some one
1065 * else will eventually get rid of this block.
1066 */
1067 }
1068 }
1069 /*
1070 * Data block is not empty, just set the free entry to
1071 * the new value.
1072 */
1073 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001074 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 logfree = 1;
1076 }
1077 /*
1078 * Log the free entry that changed, unless we got rid of it.
1079 */
1080 if (logfree)
1081 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1082 /*
1083 * Drop the buffer if we still have it.
1084 */
1085 if (fbp)
1086 xfs_da_buf_done(fbp);
1087 }
1088 xfs_dir2_leafn_check(dp, bp);
1089 /*
1090 * Return indication of whether this leaf block is emtpy enough
1091 * to justify trying to join it with a neighbor.
1092 */
1093 *rval =
1094 ((uint)sizeof(leaf->hdr) +
1095 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001096 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 mp->m_dir_magicpct;
1098 return 0;
1099}
1100
1101/*
1102 * Split the leaf entries in the old block into old and new blocks.
1103 */
1104int /* error */
1105xfs_dir2_leafn_split(
1106 xfs_da_state_t *state, /* btree cursor */
1107 xfs_da_state_blk_t *oldblk, /* original block */
1108 xfs_da_state_blk_t *newblk) /* newly created block */
1109{
1110 xfs_da_args_t *args; /* operation arguments */
1111 xfs_dablk_t blkno; /* new leaf block number */
1112 int error; /* error return value */
1113 xfs_mount_t *mp; /* filesystem mount point */
1114
1115 /*
1116 * Allocate space for a new leaf node.
1117 */
1118 args = state->args;
1119 mp = args->dp->i_mount;
1120 ASSERT(args != NULL);
1121 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1122 error = xfs_da_grow_inode(args, &blkno);
1123 if (error) {
1124 return error;
1125 }
1126 /*
1127 * Initialize the new leaf block.
1128 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001129 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1131 if (error) {
1132 return error;
1133 }
1134 newblk->blkno = blkno;
1135 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1136 /*
1137 * Rebalance the entries across the two leaves, link the new
1138 * block into the leaves.
1139 */
1140 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1141 error = xfs_da_blk_link(state, oldblk, newblk);
1142 if (error) {
1143 return error;
1144 }
1145 /*
1146 * Insert the new entry in the correct block.
1147 */
1148 if (state->inleaf)
1149 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1150 else
1151 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1152 /*
1153 * Update last hashval in each block since we added the name.
1154 */
1155 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1156 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1157 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1158 xfs_dir2_leafn_check(args->dp, newblk->bp);
1159 return error;
1160}
1161
1162/*
1163 * Check a leaf block and its neighbors to see if the block should be
1164 * collapsed into one or the other neighbor. Always keep the block
1165 * with the smaller block number.
1166 * If the current block is over 50% full, don't try to join it, return 0.
1167 * If the block is empty, fill in the state structure and return 2.
1168 * If it can be collapsed, fill in the state structure and return 1.
1169 * If nothing can be done, return 0.
1170 */
1171int /* error */
1172xfs_dir2_leafn_toosmall(
1173 xfs_da_state_t *state, /* btree cursor */
1174 int *action) /* resulting action to take */
1175{
1176 xfs_da_state_blk_t *blk; /* leaf block */
1177 xfs_dablk_t blkno; /* leaf block number */
1178 xfs_dabuf_t *bp; /* leaf buffer */
1179 int bytes; /* bytes in use */
1180 int count; /* leaf live entry count */
1181 int error; /* error return value */
1182 int forward; /* sibling block direction */
1183 int i; /* sibling counter */
1184 xfs_da_blkinfo_t *info; /* leaf block header */
1185 xfs_dir2_leaf_t *leaf; /* leaf structure */
1186 int rval; /* result from path_shift */
1187
1188 /*
1189 * Check for the degenerate case of the block being over 50% full.
1190 * If so, it's not worth even looking to see if we might be able
1191 * to coalesce with a sibling.
1192 */
1193 blk = &state->path.blk[state->path.active - 1];
1194 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001195 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001197 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1199 if (bytes > (state->blocksize >> 1)) {
1200 /*
1201 * Blk over 50%, don't try to join.
1202 */
1203 *action = 0;
1204 return 0;
1205 }
1206 /*
1207 * Check for the degenerate case of the block being empty.
1208 * If the block is empty, we'll simply delete it, no need to
1209 * coalesce it with a sibling block. We choose (arbitrarily)
1210 * to merge with the forward block unless it is NULL.
1211 */
1212 if (count == 0) {
1213 /*
1214 * Make altpath point to the block we want to keep and
1215 * path point to the block we want to drop (this one).
1216 */
Nathan Scott89da0542006-03-17 17:28:40 +11001217 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 memcpy(&state->altpath, &state->path, sizeof(state->path));
1219 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1220 &rval);
1221 if (error)
1222 return error;
1223 *action = rval ? 2 : 0;
1224 return 0;
1225 }
1226 /*
1227 * Examine each sibling block to see if we can coalesce with
1228 * at least 25% free space to spare. We need to figure out
1229 * whether to merge with the forward or the backward block.
1230 * We prefer coalescing with the lower numbered sibling so as
1231 * to shrink a directory over time.
1232 */
Nathan Scott89da0542006-03-17 17:28:40 +11001233 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001235 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (blkno == 0)
1237 continue;
1238 /*
1239 * Read the sibling leaf block.
1240 */
1241 if ((error =
1242 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1243 -1, &bp, XFS_DATA_FORK))) {
1244 return error;
1245 }
1246 ASSERT(bp != NULL);
1247 /*
1248 * Count bytes in the two blocks combined.
1249 */
1250 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001251 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 bytes = state->blocksize - (state->blocksize >> 2);
1253 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001254 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001255 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 bytes -= count * (uint)sizeof(leaf->ents[0]);
1257 /*
1258 * Fits with at least 25% to spare.
1259 */
1260 if (bytes >= 0)
1261 break;
1262 xfs_da_brelse(state->args->trans, bp);
1263 }
1264 /*
1265 * Didn't like either block, give up.
1266 */
1267 if (i >= 2) {
1268 *action = 0;
1269 return 0;
1270 }
1271 /*
1272 * Done with the sibling leaf block here, drop the dabuf
1273 * so path_shift can get it.
1274 */
1275 xfs_da_buf_done(bp);
1276 /*
1277 * Make altpath point to the block we want to keep (the lower
1278 * numbered block) and path point to the block we want to drop.
1279 */
1280 memcpy(&state->altpath, &state->path, sizeof(state->path));
1281 if (blkno < blk->blkno)
1282 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1283 &rval);
1284 else
1285 error = xfs_da_path_shift(state, &state->path, forward, 0,
1286 &rval);
1287 if (error) {
1288 return error;
1289 }
1290 *action = rval ? 0 : 1;
1291 return 0;
1292}
1293
1294/*
1295 * Move all the leaf entries from drop_blk to save_blk.
1296 * This is done as part of a join operation.
1297 */
1298void
1299xfs_dir2_leafn_unbalance(
1300 xfs_da_state_t *state, /* cursor */
1301 xfs_da_state_blk_t *drop_blk, /* dead block */
1302 xfs_da_state_blk_t *save_blk) /* surviving block */
1303{
1304 xfs_da_args_t *args; /* operation arguments */
1305 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1306 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1307
1308 args = state->args;
1309 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1310 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1311 drop_leaf = drop_blk->bp->data;
1312 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001313 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1314 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * If there are any stale leaf entries, take this opportunity
1317 * to purge them.
1318 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001319 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001321 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 xfs_dir2_leaf_compact(args, save_blk->bp);
1323 /*
1324 * Move the entries from drop to the appropriate end of save.
1325 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001326 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 -07001327 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1328 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001329 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 else
1331 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001332 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001333 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 -07001334 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1335}
1336
1337/*
1338 * Top-level node form directory addname routine.
1339 */
1340int /* error */
1341xfs_dir2_node_addname(
1342 xfs_da_args_t *args) /* operation arguments */
1343{
1344 xfs_da_state_blk_t *blk; /* leaf block for insert */
1345 int error; /* error return value */
1346 int rval; /* sub-return value */
1347 xfs_da_state_t *state; /* btree cursor */
1348
1349 xfs_dir2_trace_args("node_addname", args);
1350 /*
1351 * Allocate and initialize the state (btree cursor).
1352 */
1353 state = xfs_da_state_alloc();
1354 state->args = args;
1355 state->mp = args->dp->i_mount;
1356 state->blocksize = state->mp->m_dirblksize;
1357 state->node_ents = state->mp->m_dir_node_ents;
1358 /*
1359 * Look up the name. We're not supposed to find it, but
1360 * this gives us the insertion point.
1361 */
1362 error = xfs_da_node_lookup_int(state, &rval);
1363 if (error)
1364 rval = error;
1365 if (rval != ENOENT) {
1366 goto done;
1367 }
1368 /*
1369 * Add the data entry to a data block.
1370 * Extravalid is set to a freeblock found by lookup.
1371 */
1372 rval = xfs_dir2_node_addname_int(args,
1373 state->extravalid ? &state->extrablk : NULL);
1374 if (rval) {
1375 goto done;
1376 }
1377 blk = &state->path.blk[state->path.active - 1];
1378 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1379 /*
1380 * Add the new leaf entry.
1381 */
1382 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1383 if (rval == 0) {
1384 /*
1385 * It worked, fix the hash values up the btree.
1386 */
Barry Naujok6a178102008-05-21 16:42:05 +10001387 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 xfs_da_fixhashpath(state, &state->path);
1389 } else {
1390 /*
1391 * It didn't work, we need to split the leaf block.
1392 */
1393 if (args->total == 0) {
1394 ASSERT(rval == ENOSPC);
1395 goto done;
1396 }
1397 /*
1398 * Split the leaf block and insert the new entry.
1399 */
1400 rval = xfs_da_split(state);
1401 }
1402done:
1403 xfs_da_state_free(state);
1404 return rval;
1405}
1406
1407/*
1408 * Add the data entry for a node-format directory name addition.
1409 * The leaf entry is added in xfs_dir2_leafn_add.
1410 * We may enter with a freespace block that the lookup found.
1411 */
1412static int /* error */
1413xfs_dir2_node_addname_int(
1414 xfs_da_args_t *args, /* operation arguments */
1415 xfs_da_state_blk_t *fblk) /* optional freespace block */
1416{
1417 xfs_dir2_data_t *data; /* data block structure */
1418 xfs_dir2_db_t dbno; /* data block number */
1419 xfs_dabuf_t *dbp; /* data block buffer */
1420 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1421 xfs_inode_t *dp; /* incore directory inode */
1422 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1423 int error; /* error return value */
1424 xfs_dir2_db_t fbno; /* freespace block number */
1425 xfs_dabuf_t *fbp; /* freespace buffer */
1426 int findex; /* freespace entry index */
1427 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1428 xfs_dir2_db_t ifbno; /* initial freespace block no */
1429 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1430 int length; /* length of the new entry */
1431 int logfree; /* need to log free entry */
1432 xfs_mount_t *mp; /* filesystem mount point */
1433 int needlog; /* need to log data header */
1434 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001435 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 xfs_trans_t *tp; /* transaction pointer */
1437
1438 dp = args->dp;
1439 mp = dp->i_mount;
1440 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001441 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * If we came in with a freespace block that means that lookup
1444 * found an entry with our hash value. This is the freespace
1445 * block for that data entry.
1446 */
1447 if (fblk) {
1448 fbp = fblk->bp;
1449 /*
1450 * Remember initial freespace block number.
1451 */
1452 ifbno = fblk->blkno;
1453 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001454 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 findex = fblk->index;
1456 /*
1457 * This means the free entry showed that the data block had
1458 * space for our entry, so we remembered it.
1459 * Use that data block.
1460 */
1461 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001462 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1463 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1464 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1465 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 /*
1468 * The data block looked at didn't have enough room.
1469 * We'll start at the beginning of the freespace entries.
1470 */
1471 else {
1472 dbno = -1;
1473 findex = 0;
1474 }
1475 }
1476 /*
1477 * Didn't come in with a freespace block, so don't have a data block.
1478 */
1479 else {
1480 ifbno = dbno = -1;
1481 fbp = NULL;
1482 findex = 0;
1483 }
1484 /*
1485 * If we don't have a data block yet, we're going to scan the
1486 * freespace blocks looking for one. Figure out what the
1487 * highest freespace block number is.
1488 */
1489 if (dbno == -1) {
1490 xfs_fileoff_t fo; /* freespace block number */
1491
1492 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1493 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001494 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 fbno = ifbno;
1496 }
1497 /*
1498 * While we haven't identified a data block, search the freeblock
1499 * data for a good data block. If we find a null freeblock entry,
1500 * indicating a hole in the data blocks, remember that.
1501 */
1502 while (dbno == -1) {
1503 /*
1504 * If we don't have a freeblock in hand, get the next one.
1505 */
1506 if (fbp == NULL) {
1507 /*
1508 * Happens the first time through unless lookup gave
1509 * us a freespace block to start with.
1510 */
1511 if (++fbno == 0)
1512 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1513 /*
1514 * If it's ifbno we already looked at it.
1515 */
1516 if (fbno == ifbno)
1517 fbno++;
1518 /*
1519 * If it's off the end we're done.
1520 */
1521 if (fbno >= lastfbno)
1522 break;
1523 /*
1524 * Read the block. There can be holes in the
1525 * freespace blocks, so this might not succeed.
1526 * This should be really rare, so there's no reason
1527 * to avoid it.
1528 */
1529 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001530 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 XFS_DATA_FORK))) {
1532 return error;
1533 }
1534 if (unlikely(fbp == NULL)) {
1535 continue;
1536 }
1537 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001538 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 findex = 0;
1540 }
1541 /*
1542 * Look at the current free entry. Is it good enough?
1543 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001544 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1545 be16_to_cpu(free->bests[findex]) >= length)
1546 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 else {
1548 /*
1549 * Are we done with the freeblock?
1550 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001551 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /*
1553 * Drop the block.
1554 */
1555 xfs_da_brelse(tp, fbp);
1556 fbp = NULL;
1557 if (fblk && fblk->bp)
1558 fblk->bp = NULL;
1559 }
1560 }
1561 }
1562 /*
1563 * If we don't have a data block, we need to allocate one and make
1564 * the freespace entries refer to it.
1565 */
1566 if (unlikely(dbno == -1)) {
1567 /*
1568 * Not allowed to allocate, return failure.
1569 */
Barry Naujok6a178102008-05-21 16:42:05 +10001570 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1571 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /*
1573 * Drop the freespace buffer unless it came from our
1574 * caller.
1575 */
1576 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1577 xfs_da_buf_done(fbp);
1578 return XFS_ERROR(ENOSPC);
1579 }
1580 /*
1581 * Allocate and initialize the new data block.
1582 */
1583 if (unlikely((error = xfs_dir2_grow_inode(args,
1584 XFS_DIR2_DATA_SPACE,
1585 &dbno)) ||
1586 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1587 /*
1588 * Drop the freespace buffer unless it came from our
1589 * caller.
1590 */
1591 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1592 xfs_da_buf_done(fbp);
1593 return error;
1594 }
1595 /*
1596 * If (somehow) we have a freespace block, get rid of it.
1597 */
1598 if (fbp)
1599 xfs_da_brelse(tp, fbp);
1600 if (fblk && fblk->bp)
1601 fblk->bp = NULL;
1602
1603 /*
1604 * Get the freespace block corresponding to the data block
1605 * that was just allocated.
1606 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001607 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001609 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 XFS_DATA_FORK))) {
1611 xfs_da_buf_done(dbp);
1612 return error;
1613 }
1614 /*
1615 * If there wasn't a freespace block, the read will
1616 * return a NULL fbp. Allocate and initialize a new one.
1617 */
1618 if( fbp == NULL ) {
1619 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1620 &fbno))) {
1621 return error;
1622 }
1623
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001624 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 cmn_err(CE_ALERT,
1626 "xfs_dir2_node_addname_int: dir ino "
1627 "%llu needed freesp block %lld for\n"
1628 " data block %lld, got %lld\n"
1629 " ifbno %llu lastfbno %d\n",
1630 (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001631 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 (long long)dbno, (long long)fbno,
1633 (unsigned long long)ifbno, lastfbno);
1634 if (fblk) {
1635 cmn_err(CE_ALERT,
1636 " fblk 0x%p blkno %llu "
1637 "index %d magic 0x%x\n",
1638 fblk,
1639 (unsigned long long)fblk->blkno,
1640 fblk->index,
1641 fblk->magic);
1642 } else {
1643 cmn_err(CE_ALERT,
1644 " ... fblk is NULL\n");
1645 }
1646 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1647 XFS_ERRLEVEL_LOW, mp);
1648 return XFS_ERROR(EFSCORRUPTED);
1649 }
1650
1651 /*
1652 * Get a buffer for the new block.
1653 */
1654 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001655 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 -1, &fbp, XFS_DATA_FORK))) {
1657 return error;
1658 }
1659 ASSERT(fbp != NULL);
1660
1661 /*
1662 * Initialize the new block to be empty, and remember
1663 * its first slot as our empty slot.
1664 */
1665 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001666 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1667 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1669 XFS_DIR2_MAX_FREE_BESTS(mp));
1670 free->hdr.nvalid = 0;
1671 free->hdr.nused = 0;
1672 } else {
1673 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001674 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 /*
1678 * Set the freespace block index from the data block number.
1679 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001680 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * If it's after the end of the current entries in the
1683 * freespace block, extend that table.
1684 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001685 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001687 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Tag new entry so nused will go up.
1690 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001691 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 /*
1694 * If this entry was for an empty data block
1695 * (this should always be true) then update the header.
1696 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001697 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001698 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 xfs_dir2_free_log_header(tp, fbp);
1700 }
1701 /*
1702 * Update the real value in the table.
1703 * We haven't allocated the data entry yet so this will
1704 * change again.
1705 */
1706 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001707 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 logfree = 1;
1709 }
1710 /*
1711 * We had a data block so we don't have to make a new one.
1712 */
1713 else {
1714 /*
1715 * If just checking, we succeeded.
1716 */
Barry Naujok6a178102008-05-21 16:42:05 +10001717 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1719 xfs_da_buf_done(fbp);
1720 return 0;
1721 }
1722 /*
1723 * Read the data block in.
1724 */
1725 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001726 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 -1, &dbp, XFS_DATA_FORK))) {
1728 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1729 xfs_da_buf_done(fbp);
1730 return error;
1731 }
1732 data = dbp->data;
1733 logfree = 0;
1734 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001735 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * Point to the existing unused space.
1738 */
1739 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001740 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 needscan = needlog = 0;
1742 /*
1743 * Mark the first part of the unused space, inuse for us.
1744 */
1745 xfs_dir2_data_use_free(tp, dbp, dup,
1746 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1747 &needlog, &needscan);
1748 /*
1749 * Fill in the new entry and log it.
1750 */
1751 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001752 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 dep->namelen = args->namelen;
1754 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001755 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001756 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 xfs_dir2_data_log_entry(tp, dbp, dep);
1758 /*
1759 * Rescan the block for bestfree if needed.
1760 */
1761 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001762 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /*
1764 * Log the data block header if needed.
1765 */
1766 if (needlog)
1767 xfs_dir2_data_log_header(tp, dbp);
1768 /*
1769 * If the freespace entry is now wrong, update it.
1770 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001771 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001772 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 logfree = 1;
1774 }
1775 /*
1776 * Log the freespace entry if needed.
1777 */
1778 if (logfree)
1779 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1780 /*
1781 * If the caller didn't hand us the freespace block, drop it.
1782 */
1783 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1784 xfs_da_buf_done(fbp);
1785 /*
1786 * Return the data block and offset in args, then drop the data block.
1787 */
1788 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001789 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 xfs_da_buf_done(dbp);
1791 return 0;
1792}
1793
1794/*
1795 * Lookup an entry in a node-format directory.
1796 * All the real work happens in xfs_da_node_lookup_int.
1797 * The only real output is the inode number of the entry.
1798 */
1799int /* error */
1800xfs_dir2_node_lookup(
1801 xfs_da_args_t *args) /* operation arguments */
1802{
1803 int error; /* error return value */
1804 int i; /* btree level */
1805 int rval; /* operation return value */
1806 xfs_da_state_t *state; /* btree cursor */
1807
1808 xfs_dir2_trace_args("node_lookup", args);
1809 /*
1810 * Allocate and initialize the btree cursor.
1811 */
1812 state = xfs_da_state_alloc();
1813 state->args = args;
1814 state->mp = args->dp->i_mount;
1815 state->blocksize = state->mp->m_dirblksize;
1816 state->node_ents = state->mp->m_dir_node_ents;
1817 /*
1818 * Fill in the path to the entry in the cursor.
1819 */
1820 error = xfs_da_node_lookup_int(state, &rval);
1821 if (error)
1822 rval = error;
Barry Naujok5163f952008-05-21 16:41:01 +10001823 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE)
1824 rval = EEXIST; /* a case-insensitive match was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Release the btree blocks and leaf block.
1827 */
1828 for (i = 0; i < state->path.active; i++) {
1829 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1830 state->path.blk[i].bp = NULL;
1831 }
1832 /*
1833 * Release the data block if we have it.
1834 */
1835 if (state->extravalid && state->extrablk.bp) {
1836 xfs_da_brelse(args->trans, state->extrablk.bp);
1837 state->extrablk.bp = NULL;
1838 }
1839 xfs_da_state_free(state);
1840 return rval;
1841}
1842
1843/*
1844 * Remove an entry from a node-format directory.
1845 */
1846int /* error */
1847xfs_dir2_node_removename(
1848 xfs_da_args_t *args) /* operation arguments */
1849{
1850 xfs_da_state_blk_t *blk; /* leaf block */
1851 int error; /* error return value */
1852 int rval; /* operation return value */
1853 xfs_da_state_t *state; /* btree cursor */
1854
1855 xfs_dir2_trace_args("node_removename", args);
1856 /*
1857 * Allocate and initialize the btree cursor.
1858 */
1859 state = xfs_da_state_alloc();
1860 state->args = args;
1861 state->mp = args->dp->i_mount;
1862 state->blocksize = state->mp->m_dirblksize;
1863 state->node_ents = state->mp->m_dir_node_ents;
1864 /*
1865 * Look up the entry we're deleting, set up the cursor.
1866 */
1867 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001868 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Didn't find it, upper layer screwed up.
1872 */
1873 if (rval != EEXIST) {
1874 xfs_da_state_free(state);
1875 return rval;
1876 }
1877 blk = &state->path.blk[state->path.active - 1];
1878 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1879 ASSERT(state->extravalid);
1880 /*
1881 * Remove the leaf and data entries.
1882 * Extrablk refers to the data block.
1883 */
1884 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1885 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001886 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Fix the hash values up the btree.
1890 */
1891 xfs_da_fixhashpath(state, &state->path);
1892 /*
1893 * If we need to join leaf blocks, do it.
1894 */
1895 if (rval && state->path.active > 1)
1896 error = xfs_da_join(state);
1897 /*
1898 * If no errors so far, try conversion to leaf format.
1899 */
1900 if (!error)
1901 error = xfs_dir2_node_to_leaf(state);
1902 xfs_da_state_free(state);
1903 return error;
1904}
1905
1906/*
1907 * Replace an entry's inode number in a node-format directory.
1908 */
1909int /* error */
1910xfs_dir2_node_replace(
1911 xfs_da_args_t *args) /* operation arguments */
1912{
1913 xfs_da_state_blk_t *blk; /* leaf block */
1914 xfs_dir2_data_t *data; /* data block structure */
1915 xfs_dir2_data_entry_t *dep; /* data entry changed */
1916 int error; /* error return value */
1917 int i; /* btree level */
1918 xfs_ino_t inum; /* new inode number */
1919 xfs_dir2_leaf_t *leaf; /* leaf structure */
1920 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1921 int rval; /* internal return value */
1922 xfs_da_state_t *state; /* btree cursor */
1923
1924 xfs_dir2_trace_args("node_replace", args);
1925 /*
1926 * Allocate and initialize the btree cursor.
1927 */
1928 state = xfs_da_state_alloc();
1929 state->args = args;
1930 state->mp = args->dp->i_mount;
1931 state->blocksize = state->mp->m_dirblksize;
1932 state->node_ents = state->mp->m_dir_node_ents;
1933 inum = args->inumber;
1934 /*
1935 * Lookup the entry to change in the btree.
1936 */
1937 error = xfs_da_node_lookup_int(state, &rval);
1938 if (error) {
1939 rval = error;
1940 }
1941 /*
1942 * It should be found, since the vnodeops layer has looked it up
1943 * and locked it. But paranoia is good.
1944 */
1945 if (rval == EEXIST) {
1946 /*
1947 * Find the leaf entry.
1948 */
1949 blk = &state->path.blk[state->path.active - 1];
1950 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1951 leaf = blk->bp->data;
1952 lep = &leaf->ents[blk->index];
1953 ASSERT(state->extravalid);
1954 /*
1955 * Point to the data entry.
1956 */
1957 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001958 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 dep = (xfs_dir2_data_entry_t *)
1960 ((char *)data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001961 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001962 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * Fill in the new inode number and log the entry.
1965 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001966 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1968 rval = 0;
1969 }
1970 /*
1971 * Didn't find it, and we're holding a data block. Drop it.
1972 */
1973 else if (state->extravalid) {
1974 xfs_da_brelse(args->trans, state->extrablk.bp);
1975 state->extrablk.bp = NULL;
1976 }
1977 /*
1978 * Release all the buffers in the cursor.
1979 */
1980 for (i = 0; i < state->path.active; i++) {
1981 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1982 state->path.blk[i].bp = NULL;
1983 }
1984 xfs_da_state_free(state);
1985 return rval;
1986}
1987
1988/*
1989 * Trim off a trailing empty freespace block.
1990 * Return (in rvalp) 1 if we did it, 0 if not.
1991 */
1992int /* error */
1993xfs_dir2_node_trim_free(
1994 xfs_da_args_t *args, /* operation arguments */
1995 xfs_fileoff_t fo, /* free block number */
1996 int *rvalp) /* out: did something */
1997{
1998 xfs_dabuf_t *bp; /* freespace buffer */
1999 xfs_inode_t *dp; /* incore directory inode */
2000 int error; /* error return code */
2001 xfs_dir2_free_t *free; /* freespace structure */
2002 xfs_mount_t *mp; /* filesystem mount point */
2003 xfs_trans_t *tp; /* transaction pointer */
2004
2005 dp = args->dp;
2006 mp = dp->i_mount;
2007 tp = args->trans;
2008 /*
2009 * Read the freespace block.
2010 */
2011 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2012 XFS_DATA_FORK))) {
2013 return error;
2014 }
2015
2016 /*
2017 * There can be holes in freespace. If fo is a hole, there's
2018 * nothing to do.
2019 */
2020 if (bp == NULL) {
2021 return 0;
2022 }
2023 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11002024 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /*
2026 * If there are used entries, there's nothing to do.
2027 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11002028 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 xfs_da_brelse(tp, bp);
2030 *rvalp = 0;
2031 return 0;
2032 }
2033 /*
2034 * Blow the block away.
2035 */
2036 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002037 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 bp))) {
2039 /*
2040 * Can't fail with ENOSPC since that only happens with no
2041 * space reservation, when breaking up an extent into two
2042 * pieces. This is the last block of an extent.
2043 */
2044 ASSERT(error != ENOSPC);
2045 xfs_da_brelse(tp, bp);
2046 return error;
2047 }
2048 /*
2049 * Return that we succeeded.
2050 */
2051 *rvalp = 1;
2052 return 0;
2053}