]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/xfs_dir2_node.c
Merge branch 'fix/asoc' into for-linus
[net-next-2.6.git] / fs / xfs / xfs_dir2_node.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
da353b0d 25#include "xfs_ag.h"
1da177e4 26#include "xfs_dir2.h"
1da177e4 27#include "xfs_mount.h"
a844f451 28#include "xfs_da_btree.h"
1da177e4 29#include "xfs_bmap_btree.h"
1da177e4
LT
30#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
1da177e4
LT
34#include "xfs_dir2_data.h"
35#include "xfs_dir2_leaf.h"
36#include "xfs_dir2_block.h"
37#include "xfs_dir2_node.h"
1da177e4 38#include "xfs_error.h"
0b1b213f 39#include "xfs_trace.h"
1da177e4
LT
40
41/*
42 * Function declarations.
43 */
44static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
45static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
46#ifdef DEBUG
47static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
48#else
49#define xfs_dir2_leafn_check(dp, bp)
50#endif
51static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
52 int start_s, xfs_dabuf_t *bp_d, int start_d,
53 int count);
54static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
55 xfs_da_state_blk_t *blk1,
56 xfs_da_state_blk_t *blk2);
57static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
58 int index, xfs_da_state_blk_t *dblk,
59 int *rval);
60static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
61 xfs_da_state_blk_t *fblk);
62
63/*
64 * Log entries from a freespace block.
65 */
5d77c0dc 66STATIC void
1da177e4
LT
67xfs_dir2_free_log_bests(
68 xfs_trans_t *tp, /* transaction pointer */
69 xfs_dabuf_t *bp, /* freespace buffer */
70 int first, /* first entry to log */
71 int last) /* last entry to log */
72{
73 xfs_dir2_free_t *free; /* freespace structure */
74
75 free = bp->data;
0ba962ef 76 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
77 xfs_da_log_buf(tp, bp,
78 (uint)((char *)&free->bests[first] - (char *)free),
79 (uint)((char *)&free->bests[last] - (char *)free +
80 sizeof(free->bests[0]) - 1));
81}
82
83/*
84 * Log header from a freespace block.
85 */
86static void
87xfs_dir2_free_log_header(
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_dabuf_t *bp) /* freespace buffer */
90{
91 xfs_dir2_free_t *free; /* freespace structure */
92
93 free = bp->data;
0ba962ef 94 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
95 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
96 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
97}
98
99/*
100 * Convert a leaf-format directory to a node-format directory.
101 * We need to change the magic number of the leaf block, and copy
102 * the freespace table out of the leaf block into its own block.
103 */
104int /* error */
105xfs_dir2_leaf_to_node(
106 xfs_da_args_t *args, /* operation arguments */
107 xfs_dabuf_t *lbp) /* leaf buffer */
108{
109 xfs_inode_t *dp; /* incore directory inode */
110 int error; /* error return value */
111 xfs_dabuf_t *fbp; /* freespace buffer */
112 xfs_dir2_db_t fdb; /* freespace block number */
113 xfs_dir2_free_t *free; /* freespace structure */
68b3a102 114 __be16 *from; /* pointer to freespace entry */
1da177e4
LT
115 int i; /* leaf freespace index */
116 xfs_dir2_leaf_t *leaf; /* leaf structure */
117 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
118 xfs_mount_t *mp; /* filesystem mount point */
119 int n; /* count of live freespc ents */
120 xfs_dir2_data_off_t off; /* freespace entry value */
0ba962ef 121 __be16 *to; /* pointer to freespace entry */
1da177e4
LT
122 xfs_trans_t *tp; /* transaction pointer */
123
0b1b213f
CH
124 trace_xfs_dir2_leaf_to_node(args);
125
1da177e4
LT
126 dp = args->dp;
127 mp = dp->i_mount;
128 tp = args->trans;
129 /*
130 * Add a freespace block to the directory.
131 */
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
133 return error;
134 }
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
136 /*
137 * Get the buffer for the new freespace block.
138 */
bbaaf538 139 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
1da177e4
LT
140 XFS_DATA_FORK))) {
141 return error;
142 }
143 ASSERT(fbp != NULL);
144 free = fbp->data;
145 leaf = lbp->data;
bbaaf538 146 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
147 /*
148 * Initialize the freespace block header.
149 */
0ba962ef 150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1da177e4 151 free->hdr.firstdb = 0;
afbcb3f9 152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
0ba962ef 153 free->hdr.nvalid = ltp->bestcount;
1da177e4
LT
154 /*
155 * Copy freespace entries from the leaf block to the new block.
156 * Count active entries.
157 */
bbaaf538 158 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
afbcb3f9 159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
68b3a102 160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
1da177e4 161 n++;
0ba962ef 162 *to = cpu_to_be16(off);
1da177e4 163 }
0ba962ef 164 free->hdr.nused = cpu_to_be32(n);
89da0544 165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
166 /*
167 * Log everything.
168 */
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
0ba962ef 171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
1da177e4
LT
172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
174 return 0;
175}
176
177/*
178 * Add a leaf entry to a leaf block in a node-form directory.
179 * The other work necessary is done from the caller.
180 */
181static int /* error */
182xfs_dir2_leafn_add(
183 xfs_dabuf_t *bp, /* leaf buffer */
184 xfs_da_args_t *args, /* operation arguments */
185 int index) /* insertion pt for new entry */
186{
187 int compact; /* compacting stale leaves */
188 xfs_inode_t *dp; /* incore directory inode */
189 int highstale; /* next stale entry */
190 xfs_dir2_leaf_t *leaf; /* leaf structure */
191 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
192 int lfloghigh; /* high leaf entry logging */
193 int lfloglow; /* low leaf entry logging */
194 int lowstale; /* previous stale entry */
195 xfs_mount_t *mp; /* filesystem mount point */
196 xfs_trans_t *tp; /* transaction pointer */
197
0b1b213f
CH
198 trace_xfs_dir2_leafn_add(args, index);
199
1da177e4
LT
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
bbaaf538 219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
1da177e4
LT
220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
a818e5de 222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
1da177e4
LT
223 } else
224 compact = 0;
3c1f9c15 225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
a818e5de 226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
3c1f9c15 227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
1da177e4 228
6a178100 229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
1da177e4
LT
230 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) {
a818e5de 244 lfloglow = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
245 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];
a818e5de 252 if (index < be16_to_cpu(leaf->hdr.count))
1da177e4 253 memmove(lep + 1, lep,
a818e5de 254 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
1da177e4 255 lfloglow = index;
a818e5de 256 lfloghigh = be16_to_cpu(leaf->hdr.count);
413d57c9 257 be16_add_cpu(&leaf->hdr.count, 1);
1da177e4
LT
258 }
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 &&
3c1f9c15 273 be32_to_cpu(leaf->ents[lowstale].address) !=
1da177e4
LT
274 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;
a818e5de 283 highstale < be16_to_cpu(leaf->hdr.count) &&
3c1f9c15 284 be32_to_cpu(leaf->ents[highstale].address) !=
1da177e4
LT
285 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 &&
a818e5de 296 (highstale == be16_to_cpu(leaf->hdr.count) ||
1da177e4 297 index - lowstale - 1 < highstale - index)) {
3c1f9c15 298 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
1da177e4
LT
299 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 {
3c1f9c15 314 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
1da177e4
LT
315 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 }
413d57c9 325 be16_add_cpu(&leaf->hdr.stale, -1);
1da177e4
LT
326 }
327 /*
328 * Insert the new entry, log everything.
329 */
3c1f9c15 330 lep->hashval = cpu_to_be32(args->hashval);
bbaaf538 331 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
3c1f9c15 332 args->blkno, args->index));
1da177e4
LT
333 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;
89da0544 355 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
bbaaf538 356 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
a818e5de
NS
357 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
358 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
3c1f9c15
NS
359 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
360 be32_to_cpu(leaf->ents[i + 1].hashval));
1da177e4 361 }
3c1f9c15 362 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
363 stale++;
364 }
a818e5de 365 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
1da177e4
LT
366}
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;
89da0544 381 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 382 if (count)
a818e5de 383 *count = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
384 if (!leaf->hdr.count)
385 return 0;
3c1f9c15 386 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
1da177e4
LT
387}
388
389/*
f9f6dce0
BN
390 * 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.
1da177e4 392 */
f9f6dce0
BN
393STATIC int
394xfs_dir2_leafn_lookup_for_addname(
1da177e4
LT
395 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{
f9f6dce0
BN
400 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 */
1da177e4
LT
403 xfs_inode_t *dp; /* incore directory inode */
404 int error; /* error return value */
405 int fi; /* free entry index */
f9f6dce0 406 xfs_dir2_free_t *free = NULL; /* free block structure */
1da177e4
LT
407 int index; /* leaf entry index */
408 xfs_dir2_leaf_t *leaf; /* leaf structure */
f9f6dce0 409 int length; /* length of new data entry */
1da177e4
LT
410 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;
89da0544 420 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 421#ifdef __KERNEL__
a818e5de 422 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
1da177e4
LT
423#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 */
f9f6dce0
BN
432 if (state->extravalid) {
433 /* If so, it's a free block buffer, get the block number. */
1da177e4 434 curbp = state->extrablk.bp;
f9f6dce0
BN
435 curfdb = state->extrablk.blkno;
436 free = curbp->data;
437 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4 438 }
f9f6dce0 439 length = xfs_dir2_data_entsize(args->namelen);
1da177e4
LT
440 /*
441 * Loop over leaf entries with the right hash value.
442 */
f9f6dce0
BN
443 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
444 be32_to_cpu(lep->hashval) == args->hashval;
445 lep++, index++) {
1da177e4
LT
446 /*
447 * Skip stale leaf entries.
448 */
3c1f9c15 449 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
450 continue;
451 /*
452 * Pull the data block number from the entry.
453 */
bbaaf538 454 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4
LT
455 /*
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.
f9f6dce0
BN
459 *
460 * If this block isn't the data block we already have
461 * in hand, take a look at it.
1da177e4 462 */
f9f6dce0
BN
463 if (newdb != curdb) {
464 curdb = newdb;
1da177e4 465 /*
f9f6dce0
BN
466 * Convert the data block to the free block
467 * holding its freespace information.
1da177e4 468 */
f9f6dce0 469 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
1da177e4 470 /*
f9f6dce0 471 * If it's not the one we have in hand, read it in.
1da177e4 472 */
f9f6dce0 473 if (newfdb != curfdb) {
1da177e4 474 /*
f9f6dce0 475 * If we had one before, drop it.
1da177e4
LT
476 */
477 if (curbp)
478 xfs_da_brelse(tp, curbp);
479 /*
f9f6dce0 480 * Read the free block.
1da177e4 481 */
f9f6dce0
BN
482 error = xfs_da_read_buf(tp, dp,
483 xfs_dir2_db_to_da(mp, newfdb),
484 -1, &curbp, XFS_DATA_FORK);
485 if (error)
1da177e4 486 return error;
f9f6dce0
BN
487 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));
1da177e4
LT
495 }
496 /*
f9f6dce0 497 * Get the index for our entry.
1da177e4 498 */
f9f6dce0 499 fi = xfs_dir2_db_to_fdindex(mp, curdb);
1da177e4 500 /*
f9f6dce0 501 * If it has room, return it.
1da177e4 502 */
f9f6dce0
BN
503 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);
1da177e4 509 }
f9f6dce0
BN
510 curfdb = newfdb;
511 if (be16_to_cpu(free->bests[fi]) >= length)
512 goto out;
1da177e4
LT
513 }
514 }
f9f6dce0
BN
515 /* Didn't find any space */
516 fi = -1;
517out:
6a178100 518 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
f9f6dce0
BN
519 if (curbp) {
520 /* Giving back a free block. */
521 state->extravalid = 1;
522 state->extrablk.bp = curbp;
523 state->extrablk.index = fi;
524 state->extrablk.blkno = curfdb;
525 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
526 } else {
527 state->extravalid = 0;
528 }
1da177e4 529 /*
f9f6dce0 530 * Return the index, that will be the insertion point.
1da177e4 531 */
f9f6dce0
BN
532 *indexp = index;
533 return XFS_ERROR(ENOENT);
534}
535
536/*
537 * 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 */
f9f6dce0
BN
552 int index; /* leaf entry index */
553 xfs_dir2_leaf_t *leaf; /* leaf structure */
554 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
555 xfs_mount_t *mp; /* filesystem mount point */
556 xfs_dir2_db_t newdb; /* new data block number */
557 xfs_trans_t *tp; /* transaction pointer */
5163f95a 558 enum xfs_dacmp cmp; /* comparison result */
f9f6dce0
BN
559
560 dp = args->dp;
561 tp = args->trans;
562 mp = dp->i_mount;
563 leaf = bp->data;
564 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
565#ifdef __KERNEL__
566 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
567#endif
568 xfs_dir2_leafn_check(dp, bp);
569 /*
570 * Look up the hash value in the leaf entries.
571 */
572 index = xfs_dir2_leaf_search_hash(args, bp);
573 /*
574 * Do we have a buffer coming in?
575 */
576 if (state->extravalid) {
577 curbp = state->extrablk.bp;
578 curdb = state->extrablk.blkno;
579 }
580 /*
581 * Loop over leaf entries with the right hash value.
582 */
583 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
584 be32_to_cpu(lep->hashval) == args->hashval;
585 lep++, index++) {
586 /*
587 * Skip stale leaf entries.
588 */
589 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
590 continue;
1da177e4 591 /*
f9f6dce0
BN
592 * Pull the data block number from the entry.
593 */
594 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
595 /*
596 * Not adding a new entry, so we really want to find
597 * the name given to us.
598 *
599 * If it's a different data block, go get it.
1da177e4 600 */
f9f6dce0
BN
601 if (newdb != curdb) {
602 /*
90bb7ab0
BN
603 * If we had a block before that we aren't saving
604 * for a CI name, drop it
f9f6dce0 605 */
90bb7ab0
BN
606 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
607 curdb != state->extrablk.blkno))
f9f6dce0
BN
608 xfs_da_brelse(tp, curbp);
609 /*
90bb7ab0
BN
610 * If needing the block that is saved with a CI match,
611 * use it otherwise read in the new data block.
f9f6dce0 612 */
90bb7ab0
BN
613 if (args->cmpresult != XFS_CMP_DIFFERENT &&
614 newdb == state->extrablk.blkno) {
615 ASSERT(state->extravalid);
616 curbp = state->extrablk.bp;
617 } else {
618 error = xfs_da_read_buf(tp, dp,
619 xfs_dir2_db_to_da(mp, newdb),
620 -1, &curbp, XFS_DATA_FORK);
621 if (error)
622 return error;
623 }
f9f6dce0
BN
624 xfs_dir2_data_check(dp, curbp);
625 curdb = newdb;
1da177e4
LT
626 }
627 /*
f9f6dce0 628 * Point to the data entry.
1da177e4 629 */
f9f6dce0
BN
630 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
631 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
632 /*
5163f95a
BN
633 * Compare the entry and if it's an exact match, return
634 * EEXIST immediately. If it's the first case-insensitive
90bb7ab0 635 * match, store the block & inode number and continue looking.
f9f6dce0 636 */
5163f95a
BN
637 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
638 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
90bb7ab0
BN
639 /* If there is a CI match block, drop it */
640 if (args->cmpresult != XFS_CMP_DIFFERENT &&
641 curdb != state->extrablk.blkno)
642 xfs_da_brelse(tp, state->extrablk.bp);
5163f95a 643 args->cmpresult = cmp;
f9f6dce0 644 args->inumber = be64_to_cpu(dep->inumber);
90bb7ab0
BN
645 *indexp = index;
646 state->extravalid = 1;
647 state->extrablk.bp = curbp;
648 state->extrablk.blkno = curdb;
649 state->extrablk.index = (int)((char *)dep -
650 (char *)curbp->data);
651 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
5163f95a 652 if (cmp == XFS_CMP_EXACT)
90bb7ab0 653 return XFS_ERROR(EEXIST);
1da177e4
LT
654 }
655 }
6a178100
BN
656 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
657 (args->op_flags & XFS_DA_OP_OKNOENT));
f9f6dce0 658 if (curbp) {
90bb7ab0
BN
659 if (args->cmpresult == XFS_CMP_DIFFERENT) {
660 /* Giving back last used data block. */
661 state->extravalid = 1;
662 state->extrablk.bp = curbp;
663 state->extrablk.index = -1;
664 state->extrablk.blkno = curdb;
665 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
666 } else {
667 /* If the curbp is not the CI match block, drop it */
668 if (state->extrablk.bp != curbp)
669 xfs_da_brelse(tp, curbp);
670 }
f9f6dce0
BN
671 } else {
672 state->extravalid = 0;
673 }
1da177e4 674 *indexp = index;
90bb7ab0 675 return XFS_ERROR(ENOENT);
f9f6dce0
BN
676}
677
678/*
679 * Look up a leaf entry in a node-format leaf block.
680 * If this is an addname then the extrablk in state is a freespace block,
681 * otherwise it's a data block.
682 */
683int
684xfs_dir2_leafn_lookup_int(
685 xfs_dabuf_t *bp, /* leaf buffer */
686 xfs_da_args_t *args, /* operation arguments */
687 int *indexp, /* out: leaf entry index */
688 xfs_da_state_t *state) /* state to fill in */
689{
6a178100 690 if (args->op_flags & XFS_DA_OP_ADDNAME)
f9f6dce0
BN
691 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
692 state);
693 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
1da177e4
LT
694}
695
696/*
697 * Move count leaf entries from source to destination leaf.
698 * Log entries and headers. Stale entries are preserved.
699 */
700static void
701xfs_dir2_leafn_moveents(
702 xfs_da_args_t *args, /* operation arguments */
703 xfs_dabuf_t *bp_s, /* source leaf buffer */
704 int start_s, /* source leaf index */
705 xfs_dabuf_t *bp_d, /* destination leaf buffer */
706 int start_d, /* destination leaf index */
707 int count) /* count of leaves to copy */
708{
709 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
710 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
711 int stale; /* count stale leaves copied */
712 xfs_trans_t *tp; /* transaction pointer */
713
0b1b213f
CH
714 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
715
1da177e4
LT
716 /*
717 * Silently return if nothing to do.
718 */
719 if (count == 0) {
720 return;
721 }
722 tp = args->trans;
723 leaf_s = bp_s->data;
724 leaf_d = bp_d->data;
725 /*
726 * If the destination index is not the end of the current
727 * destination leaf entries, open up a hole in the destination
728 * to hold the new entries.
729 */
a818e5de 730 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
1da177e4 731 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
a818e5de 732 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
1da177e4
LT
733 sizeof(xfs_dir2_leaf_entry_t));
734 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
a818e5de 735 count + be16_to_cpu(leaf_d->hdr.count) - 1);
1da177e4
LT
736 }
737 /*
738 * If the source has stale leaves, count the ones in the copy range
739 * so we can update the header correctly.
740 */
741 if (leaf_s->hdr.stale) {
742 int i; /* temp leaf index */
743
744 for (i = start_s, stale = 0; i < start_s + count; i++) {
3c1f9c15 745 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
746 stale++;
747 }
748 } else
749 stale = 0;
750 /*
751 * Copy the leaf entries from source to destination.
752 */
753 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
754 count * sizeof(xfs_dir2_leaf_entry_t));
755 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
756 /*
757 * If there are source entries after the ones we copied,
758 * delete the ones we copied by sliding the next ones down.
759 */
a818e5de 760 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
1da177e4
LT
761 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
762 count * sizeof(xfs_dir2_leaf_entry_t));
763 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
764 }
765 /*
766 * Update the headers and log them.
767 */
413d57c9
MS
768 be16_add_cpu(&leaf_s->hdr.count, -(count));
769 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
770 be16_add_cpu(&leaf_d->hdr.count, count);
771 be16_add_cpu(&leaf_d->hdr.stale, stale);
1da177e4
LT
772 xfs_dir2_leaf_log_header(tp, bp_s);
773 xfs_dir2_leaf_log_header(tp, bp_d);
774 xfs_dir2_leafn_check(args->dp, bp_s);
775 xfs_dir2_leafn_check(args->dp, bp_d);
776}
777
778/*
779 * Determine the sort order of two leaf blocks.
780 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
781 */
782int /* sort order */
783xfs_dir2_leafn_order(
784 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
785 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
786{
787 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
788 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
789
790 leaf1 = leaf1_bp->data;
791 leaf2 = leaf2_bp->data;
89da0544
NS
792 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
793 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
a818e5de
NS
794 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
795 be16_to_cpu(leaf2->hdr.count) > 0 &&
3c1f9c15
NS
796 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
797 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
798 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
1da177e4
LT
799 return 1;
800 return 0;
801}
802
803/*
804 * Rebalance leaf entries between two leaf blocks.
805 * This is actually only called when the second block is new,
806 * though the code deals with the general case.
807 * A new entry will be inserted in one of the blocks, and that
808 * entry is taken into account when balancing.
809 */
810static void
811xfs_dir2_leafn_rebalance(
812 xfs_da_state_t *state, /* btree cursor */
813 xfs_da_state_blk_t *blk1, /* first btree block */
814 xfs_da_state_blk_t *blk2) /* second btree block */
815{
816 xfs_da_args_t *args; /* operation arguments */
817 int count; /* count (& direction) leaves */
818 int isleft; /* new goes in left leaf */
819 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
820 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
821 int mid; /* midpoint leaf index */
822#ifdef DEBUG
823 int oldstale; /* old count of stale leaves */
824#endif
825 int oldsum; /* old total leaf count */
826 int swap; /* swapped leaf blocks */
827
828 args = state->args;
829 /*
830 * If the block order is wrong, swap the arguments.
831 */
832 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
833 xfs_da_state_blk_t *tmp; /* temp for block swap */
834
835 tmp = blk1;
836 blk1 = blk2;
837 blk2 = tmp;
838 }
839 leaf1 = blk1->bp->data;
840 leaf2 = blk2->bp->data;
a818e5de 841 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
1da177e4 842#ifdef DEBUG
a818e5de 843 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
1da177e4
LT
844#endif
845 mid = oldsum >> 1;
846 /*
847 * If the old leaf count was odd then the new one will be even,
848 * so we need to divide the new count evenly.
849 */
850 if (oldsum & 1) {
851 xfs_dahash_t midhash; /* middle entry hash value */
852
a818e5de 853 if (mid >= be16_to_cpu(leaf1->hdr.count))
3c1f9c15 854 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
1da177e4 855 else
3c1f9c15 856 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
1da177e4
LT
857 isleft = args->hashval <= midhash;
858 }
859 /*
860 * If the old count is even then the new count is odd, so there's
861 * no preferred side for the new entry.
862 * Pick the left one.
863 */
864 else
865 isleft = 1;
866 /*
867 * Calculate moved entry count. Positive means left-to-right,
868 * negative means right-to-left. Then move the entries.
869 */
a818e5de 870 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
1da177e4
LT
871 if (count > 0)
872 xfs_dir2_leafn_moveents(args, blk1->bp,
a818e5de 873 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
1da177e4
LT
874 else if (count < 0)
875 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
a818e5de
NS
876 be16_to_cpu(leaf1->hdr.count), count);
877 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
878 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
1da177e4
LT
879 /*
880 * Mark whether we're inserting into the old or new leaf.
881 */
a818e5de 882 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
1da177e4 883 state->inleaf = swap;
a818e5de 884 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
1da177e4
LT
885 state->inleaf = !swap;
886 else
887 state->inleaf =
a818e5de 888 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
1da177e4
LT
889 /*
890 * Adjust the expected index for insertion.
891 */
892 if (!state->inleaf)
a818e5de 893 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
f9f6dce0
BN
894
895 /*
896 * Finally sanity check just to make sure we are not returning a
897 * negative index
1da177e4
LT
898 */
899 if(blk2->index < 0) {
900 state->inleaf = 1;
901 blk2->index = 0;
902 cmn_err(CE_ALERT,
c41564b5 903 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
1da177e4
LT
904 "blk1->index %d\n",
905 blk1->index);
906 }
907}
908
909/*
910 * Remove an entry from a node directory.
911 * This removes the leaf entry and the data entry,
912 * and updates the free block if necessary.
913 */
914static int /* error */
915xfs_dir2_leafn_remove(
916 xfs_da_args_t *args, /* operation arguments */
917 xfs_dabuf_t *bp, /* leaf buffer */
918 int index, /* leaf entry index */
919 xfs_da_state_blk_t *dblk, /* data block */
920 int *rval) /* resulting block needs join */
921{
922 xfs_dir2_data_t *data; /* data block structure */
923 xfs_dir2_db_t db; /* data block number */
924 xfs_dabuf_t *dbp; /* data block buffer */
925 xfs_dir2_data_entry_t *dep; /* data block entry */
926 xfs_inode_t *dp; /* incore directory inode */
927 xfs_dir2_leaf_t *leaf; /* leaf structure */
928 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
929 int longest; /* longest data free entry */
930 int off; /* data block entry offset */
931 xfs_mount_t *mp; /* filesystem mount point */
932 int needlog; /* need to log data header */
933 int needscan; /* need to rescan data frees */
934 xfs_trans_t *tp; /* transaction pointer */
935
0b1b213f
CH
936 trace_xfs_dir2_leafn_remove(args, index);
937
1da177e4
LT
938 dp = args->dp;
939 tp = args->trans;
940 mp = dp->i_mount;
941 leaf = bp->data;
89da0544 942 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
943 /*
944 * Point to the entry we're removing.
945 */
946 lep = &leaf->ents[index];
947 /*
948 * Extract the data block and offset from the entry.
949 */
bbaaf538 950 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4 951 ASSERT(dblk->blkno == db);
bbaaf538 952 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
1da177e4
LT
953 ASSERT(dblk->index == off);
954 /*
955 * Kill the leaf entry by marking it stale.
956 * Log the leaf block changes.
957 */
413d57c9 958 be16_add_cpu(&leaf->hdr.stale, 1);
1da177e4 959 xfs_dir2_leaf_log_header(tp, bp);
3c1f9c15 960 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
961 xfs_dir2_leaf_log_ents(tp, bp, index, index);
962 /*
963 * Make the data entry free. Keep track of the longest freespace
964 * in the data block in case it changes.
965 */
966 dbp = dblk->bp;
967 data = dbp->data;
968 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
70e73f59 969 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1da177e4
LT
970 needlog = needscan = 0;
971 xfs_dir2_data_make_free(tp, dbp, off,
bbaaf538 972 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1da177e4
LT
973 /*
974 * Rescan the data block freespaces for bestfree.
975 * Log the data block header if needed.
976 */
977 if (needscan)
ef497f8a 978 xfs_dir2_data_freescan(mp, data, &needlog);
1da177e4
LT
979 if (needlog)
980 xfs_dir2_data_log_header(tp, dbp);
981 xfs_dir2_data_check(dp, dbp);
982 /*
983 * If the longest data block freespace changes, need to update
984 * the corresponding freeblock entry.
985 */
70e73f59 986 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
1da177e4
LT
987 int error; /* error return value */
988 xfs_dabuf_t *fbp; /* freeblock buffer */
989 xfs_dir2_db_t fdb; /* freeblock block number */
990 int findex; /* index in freeblock entries */
991 xfs_dir2_free_t *free; /* freeblock structure */
992 int logfree; /* need to log free entry */
993
994 /*
995 * Convert the data block number to a free block,
996 * read in the free block.
997 */
bbaaf538
CH
998 fdb = xfs_dir2_db_to_fdb(mp, db);
999 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1da177e4
LT
1000 -1, &fbp, XFS_DATA_FORK))) {
1001 return error;
1002 }
1003 free = fbp->data;
0ba962ef
NS
1004 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1005 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
1da177e4
LT
1006 XFS_DIR2_MAX_FREE_BESTS(mp) *
1007 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1008 /*
1009 * Calculate which entry we need to fix.
1010 */
bbaaf538 1011 findex = xfs_dir2_db_to_fdindex(mp, db);
70e73f59 1012 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1da177e4
LT
1013 /*
1014 * If the data block is now empty we can get rid of it
1015 * (usually).
1016 */
1017 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1018 /*
1019 * Try to punch out the data block.
1020 */
1021 error = xfs_dir2_shrink_inode(args, db, dbp);
1022 if (error == 0) {
1023 dblk->bp = NULL;
1024 data = NULL;
1025 }
1026 /*
1027 * We can get ENOSPC if there's no space reservation.
1028 * In this case just drop the buffer and some one else
1029 * will eventually get rid of the empty block.
1030 */
1031 else if (error == ENOSPC && args->total == 0)
1032 xfs_da_buf_done(dbp);
1033 else
1034 return error;
1035 }
1036 /*
1037 * If we got rid of the data block, we can eliminate that entry
1038 * in the free block.
1039 */
1040 if (data == NULL) {
1041 /*
1042 * One less used entry in the free table.
1043 */
413d57c9 1044 be32_add_cpu(&free->hdr.nused, -1);
1da177e4
LT
1045 xfs_dir2_free_log_header(tp, fbp);
1046 /*
1047 * If this was the last entry in the table, we can
1048 * trim the table size back. There might be other
1049 * entries at the end referring to non-existent
1050 * data blocks, get those too.
1051 */
0ba962ef 1052 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
1da177e4
LT
1053 int i; /* free entry index */
1054
1055 for (i = findex - 1;
0ba962ef 1056 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
1da177e4
LT
1057 i--)
1058 continue;
0ba962ef 1059 free->hdr.nvalid = cpu_to_be32(i + 1);
1da177e4
LT
1060 logfree = 0;
1061 }
1062 /*
1063 * Not the last entry, just punch it out.
1064 */
1065 else {
0ba962ef 1066 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
1067 logfree = 1;
1068 }
1069 /*
1070 * If there are no useful entries left in the block,
1071 * get rid of the block if we can.
1072 */
1073 if (!free->hdr.nused) {
1074 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1075 if (error == 0) {
1076 fbp = NULL;
1077 logfree = 0;
1078 } else if (error != ENOSPC || args->total != 0)
1079 return error;
1080 /*
1081 * It's possible to get ENOSPC if there is no
1082 * space reservation. In this case some one
1083 * else will eventually get rid of this block.
1084 */
1085 }
1086 }
1087 /*
1088 * Data block is not empty, just set the free entry to
1089 * the new value.
1090 */
1091 else {
0ba962ef 1092 free->bests[findex] = cpu_to_be16(longest);
1da177e4
LT
1093 logfree = 1;
1094 }
1095 /*
1096 * Log the free entry that changed, unless we got rid of it.
1097 */
1098 if (logfree)
1099 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1100 /*
1101 * Drop the buffer if we still have it.
1102 */
1103 if (fbp)
1104 xfs_da_buf_done(fbp);
1105 }
1106 xfs_dir2_leafn_check(dp, bp);
1107 /*
9da096fd 1108 * Return indication of whether this leaf block is empty enough
1da177e4
LT
1109 * to justify trying to join it with a neighbor.
1110 */
1111 *rval =
1112 ((uint)sizeof(leaf->hdr) +
1113 (uint)sizeof(leaf->ents[0]) *
a818e5de 1114 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
1da177e4
LT
1115 mp->m_dir_magicpct;
1116 return 0;
1117}
1118
1119/*
1120 * Split the leaf entries in the old block into old and new blocks.
1121 */
1122int /* error */
1123xfs_dir2_leafn_split(
1124 xfs_da_state_t *state, /* btree cursor */
1125 xfs_da_state_blk_t *oldblk, /* original block */
1126 xfs_da_state_blk_t *newblk) /* newly created block */
1127{
1128 xfs_da_args_t *args; /* operation arguments */
1129 xfs_dablk_t blkno; /* new leaf block number */
1130 int error; /* error return value */
1131 xfs_mount_t *mp; /* filesystem mount point */
1132
1133 /*
1134 * Allocate space for a new leaf node.
1135 */
1136 args = state->args;
1137 mp = args->dp->i_mount;
1138 ASSERT(args != NULL);
1139 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1140 error = xfs_da_grow_inode(args, &blkno);
1141 if (error) {
1142 return error;
1143 }
1144 /*
1145 * Initialize the new leaf block.
1146 */
bbaaf538 1147 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
1da177e4
LT
1148 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1149 if (error) {
1150 return error;
1151 }
1152 newblk->blkno = blkno;
1153 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1154 /*
1155 * Rebalance the entries across the two leaves, link the new
1156 * block into the leaves.
1157 */
1158 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1159 error = xfs_da_blk_link(state, oldblk, newblk);
1160 if (error) {
1161 return error;
1162 }
1163 /*
1164 * Insert the new entry in the correct block.
1165 */
1166 if (state->inleaf)
1167 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1168 else
1169 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1170 /*
1171 * Update last hashval in each block since we added the name.
1172 */
1173 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1174 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1175 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1176 xfs_dir2_leafn_check(args->dp, newblk->bp);
1177 return error;
1178}
1179
1180/*
1181 * Check a leaf block and its neighbors to see if the block should be
1182 * collapsed into one or the other neighbor. Always keep the block
1183 * with the smaller block number.
1184 * If the current block is over 50% full, don't try to join it, return 0.
1185 * If the block is empty, fill in the state structure and return 2.
1186 * If it can be collapsed, fill in the state structure and return 1.
1187 * If nothing can be done, return 0.
1188 */
1189int /* error */
1190xfs_dir2_leafn_toosmall(
1191 xfs_da_state_t *state, /* btree cursor */
1192 int *action) /* resulting action to take */
1193{
1194 xfs_da_state_blk_t *blk; /* leaf block */
1195 xfs_dablk_t blkno; /* leaf block number */
1196 xfs_dabuf_t *bp; /* leaf buffer */
1197 int bytes; /* bytes in use */
1198 int count; /* leaf live entry count */
1199 int error; /* error return value */
1200 int forward; /* sibling block direction */
1201 int i; /* sibling counter */
1202 xfs_da_blkinfo_t *info; /* leaf block header */
1203 xfs_dir2_leaf_t *leaf; /* leaf structure */
1204 int rval; /* result from path_shift */
1205
1206 /*
1207 * Check for the degenerate case of the block being over 50% full.
1208 * If so, it's not worth even looking to see if we might be able
1209 * to coalesce with a sibling.
1210 */
1211 blk = &state->path.blk[state->path.active - 1];
1212 info = blk->bp->data;
89da0544 1213 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 1214 leaf = (xfs_dir2_leaf_t *)info;
a818e5de 1215 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1216 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1217 if (bytes > (state->blocksize >> 1)) {
1218 /*
1219 * Blk over 50%, don't try to join.
1220 */
1221 *action = 0;
1222 return 0;
1223 }
1224 /*
1225 * Check for the degenerate case of the block being empty.
1226 * If the block is empty, we'll simply delete it, no need to
1227 * coalesce it with a sibling block. We choose (arbitrarily)
1228 * to merge with the forward block unless it is NULL.
1229 */
1230 if (count == 0) {
1231 /*
1232 * Make altpath point to the block we want to keep and
1233 * path point to the block we want to drop (this one).
1234 */
89da0544 1235 forward = (info->forw != 0);
1da177e4
LT
1236 memcpy(&state->altpath, &state->path, sizeof(state->path));
1237 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1238 &rval);
1239 if (error)
1240 return error;
1241 *action = rval ? 2 : 0;
1242 return 0;
1243 }
1244 /*
1245 * Examine each sibling block to see if we can coalesce with
1246 * at least 25% free space to spare. We need to figure out
1247 * whether to merge with the forward or the backward block.
1248 * We prefer coalescing with the lower numbered sibling so as
1249 * to shrink a directory over time.
1250 */
89da0544 1251 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
1da177e4 1252 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
89da0544 1253 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
1da177e4
LT
1254 if (blkno == 0)
1255 continue;
1256 /*
1257 * Read the sibling leaf block.
1258 */
1259 if ((error =
1260 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1261 -1, &bp, XFS_DATA_FORK))) {
1262 return error;
1263 }
1264 ASSERT(bp != NULL);
1265 /*
1266 * Count bytes in the two blocks combined.
1267 */
1268 leaf = (xfs_dir2_leaf_t *)info;
a818e5de 1269 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1270 bytes = state->blocksize - (state->blocksize >> 2);
1271 leaf = bp->data;
89da0544 1272 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
a818e5de 1273 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1274 bytes -= count * (uint)sizeof(leaf->ents[0]);
1275 /*
1276 * Fits with at least 25% to spare.
1277 */
1278 if (bytes >= 0)
1279 break;
1280 xfs_da_brelse(state->args->trans, bp);
1281 }
1282 /*
1283 * Didn't like either block, give up.
1284 */
1285 if (i >= 2) {
1286 *action = 0;
1287 return 0;
1288 }
1289 /*
1290 * Done with the sibling leaf block here, drop the dabuf
1291 * so path_shift can get it.
1292 */
1293 xfs_da_buf_done(bp);
1294 /*
1295 * Make altpath point to the block we want to keep (the lower
1296 * numbered block) and path point to the block we want to drop.
1297 */
1298 memcpy(&state->altpath, &state->path, sizeof(state->path));
1299 if (blkno < blk->blkno)
1300 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1301 &rval);
1302 else
1303 error = xfs_da_path_shift(state, &state->path, forward, 0,
1304 &rval);
1305 if (error) {
1306 return error;
1307 }
1308 *action = rval ? 0 : 1;
1309 return 0;
1310}
1311
1312/*
1313 * Move all the leaf entries from drop_blk to save_blk.
1314 * This is done as part of a join operation.
1315 */
1316void
1317xfs_dir2_leafn_unbalance(
1318 xfs_da_state_t *state, /* cursor */
1319 xfs_da_state_blk_t *drop_blk, /* dead block */
1320 xfs_da_state_blk_t *save_blk) /* surviving block */
1321{
1322 xfs_da_args_t *args; /* operation arguments */
1323 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1324 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1325
1326 args = state->args;
1327 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1329 drop_leaf = drop_blk->bp->data;
1330 save_leaf = save_blk->bp->data;
89da0544
NS
1331 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1332 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1333 /*
1334 * If there are any stale leaf entries, take this opportunity
1335 * to purge them.
1336 */
a818e5de 1337 if (drop_leaf->hdr.stale)
1da177e4 1338 xfs_dir2_leaf_compact(args, drop_blk->bp);
a818e5de 1339 if (save_leaf->hdr.stale)
1da177e4
LT
1340 xfs_dir2_leaf_compact(args, save_blk->bp);
1341 /*
1342 * Move the entries from drop to the appropriate end of save.
1343 */
3c1f9c15 1344 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
1da177e4
LT
1345 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1346 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
a818e5de 1347 be16_to_cpu(drop_leaf->hdr.count));
1da177e4
LT
1348 else
1349 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
a818e5de 1350 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
3c1f9c15 1351 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
1da177e4
LT
1352 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1353}
1354
1355/*
1356 * Top-level node form directory addname routine.
1357 */
1358int /* error */
1359xfs_dir2_node_addname(
1360 xfs_da_args_t *args) /* operation arguments */
1361{
1362 xfs_da_state_blk_t *blk; /* leaf block for insert */
1363 int error; /* error return value */
1364 int rval; /* sub-return value */
1365 xfs_da_state_t *state; /* btree cursor */
1366
0b1b213f
CH
1367 trace_xfs_dir2_node_addname(args);
1368
1da177e4
LT
1369 /*
1370 * Allocate and initialize the state (btree cursor).
1371 */
1372 state = xfs_da_state_alloc();
1373 state->args = args;
1374 state->mp = args->dp->i_mount;
1375 state->blocksize = state->mp->m_dirblksize;
1376 state->node_ents = state->mp->m_dir_node_ents;
1377 /*
1378 * Look up the name. We're not supposed to find it, but
1379 * this gives us the insertion point.
1380 */
1381 error = xfs_da_node_lookup_int(state, &rval);
1382 if (error)
1383 rval = error;
1384 if (rval != ENOENT) {
1385 goto done;
1386 }
1387 /*
1388 * Add the data entry to a data block.
1389 * Extravalid is set to a freeblock found by lookup.
1390 */
1391 rval = xfs_dir2_node_addname_int(args,
1392 state->extravalid ? &state->extrablk : NULL);
1393 if (rval) {
1394 goto done;
1395 }
1396 blk = &state->path.blk[state->path.active - 1];
1397 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1398 /*
1399 * Add the new leaf entry.
1400 */
1401 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1402 if (rval == 0) {
1403 /*
1404 * It worked, fix the hash values up the btree.
1405 */
6a178100 1406 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1da177e4
LT
1407 xfs_da_fixhashpath(state, &state->path);
1408 } else {
1409 /*
1410 * It didn't work, we need to split the leaf block.
1411 */
1412 if (args->total == 0) {
1413 ASSERT(rval == ENOSPC);
1414 goto done;
1415 }
1416 /*
1417 * Split the leaf block and insert the new entry.
1418 */
1419 rval = xfs_da_split(state);
1420 }
1421done:
1422 xfs_da_state_free(state);
1423 return rval;
1424}
1425
1426/*
1427 * Add the data entry for a node-format directory name addition.
1428 * The leaf entry is added in xfs_dir2_leafn_add.
1429 * We may enter with a freespace block that the lookup found.
1430 */
1431static int /* error */
1432xfs_dir2_node_addname_int(
1433 xfs_da_args_t *args, /* operation arguments */
1434 xfs_da_state_blk_t *fblk) /* optional freespace block */
1435{
1436 xfs_dir2_data_t *data; /* data block structure */
1437 xfs_dir2_db_t dbno; /* data block number */
1438 xfs_dabuf_t *dbp; /* data block buffer */
1439 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1440 xfs_inode_t *dp; /* incore directory inode */
1441 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1442 int error; /* error return value */
1443 xfs_dir2_db_t fbno; /* freespace block number */
1444 xfs_dabuf_t *fbp; /* freespace buffer */
1445 int findex; /* freespace entry index */
1446 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1447 xfs_dir2_db_t ifbno; /* initial freespace block no */
1448 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1449 int length; /* length of the new entry */
1450 int logfree; /* need to log free entry */
1451 xfs_mount_t *mp; /* filesystem mount point */
1452 int needlog; /* need to log data header */
1453 int needscan; /* need to rescan data frees */
3d693c6e 1454 __be16 *tagp; /* data entry tag pointer */
1da177e4
LT
1455 xfs_trans_t *tp; /* transaction pointer */
1456
1457 dp = args->dp;
1458 mp = dp->i_mount;
1459 tp = args->trans;
bbaaf538 1460 length = xfs_dir2_data_entsize(args->namelen);
1da177e4
LT
1461 /*
1462 * If we came in with a freespace block that means that lookup
1463 * found an entry with our hash value. This is the freespace
1464 * block for that data entry.
1465 */
1466 if (fblk) {
1467 fbp = fblk->bp;
1468 /*
1469 * Remember initial freespace block number.
1470 */
1471 ifbno = fblk->blkno;
1472 free = fbp->data;
0ba962ef 1473 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1474 findex = fblk->index;
1475 /*
1476 * This means the free entry showed that the data block had
1477 * space for our entry, so we remembered it.
1478 * Use that data block.
1479 */
1480 if (findex >= 0) {
0ba962ef
NS
1481 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1482 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1483 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1484 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1da177e4
LT
1485 }
1486 /*
1487 * The data block looked at didn't have enough room.
1488 * We'll start at the beginning of the freespace entries.
1489 */
1490 else {
1491 dbno = -1;
1492 findex = 0;
1493 }
1494 }
1495 /*
1496 * Didn't come in with a freespace block, so don't have a data block.
1497 */
1498 else {
1499 ifbno = dbno = -1;
1500 fbp = NULL;
1501 findex = 0;
1502 }
1503 /*
1504 * If we don't have a data block yet, we're going to scan the
1505 * freespace blocks looking for one. Figure out what the
1506 * highest freespace block number is.
1507 */
1508 if (dbno == -1) {
1509 xfs_fileoff_t fo; /* freespace block number */
1510
1511 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1512 return error;
bbaaf538 1513 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
1da177e4
LT
1514 fbno = ifbno;
1515 }
1516 /*
1517 * While we haven't identified a data block, search the freeblock
1518 * data for a good data block. If we find a null freeblock entry,
1519 * indicating a hole in the data blocks, remember that.
1520 */
1521 while (dbno == -1) {
1522 /*
1523 * If we don't have a freeblock in hand, get the next one.
1524 */
1525 if (fbp == NULL) {
1526 /*
1527 * Happens the first time through unless lookup gave
1528 * us a freespace block to start with.
1529 */
1530 if (++fbno == 0)
1531 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1532 /*
1533 * If it's ifbno we already looked at it.
1534 */
1535 if (fbno == ifbno)
1536 fbno++;
1537 /*
1538 * If it's off the end we're done.
1539 */
1540 if (fbno >= lastfbno)
1541 break;
1542 /*
1543 * Read the block. There can be holes in the
1544 * freespace blocks, so this might not succeed.
1545 * This should be really rare, so there's no reason
1546 * to avoid it.
1547 */
1548 if ((error = xfs_da_read_buf(tp, dp,
bbaaf538 1549 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1da177e4
LT
1550 XFS_DATA_FORK))) {
1551 return error;
1552 }
1553 if (unlikely(fbp == NULL)) {
1554 continue;
1555 }
1556 free = fbp->data;
0ba962ef 1557 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1558 findex = 0;
1559 }
1560 /*
1561 * Look at the current free entry. Is it good enough?
1562 */
0ba962ef
NS
1563 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1564 be16_to_cpu(free->bests[findex]) >= length)
1565 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1da177e4
LT
1566 else {
1567 /*
1568 * Are we done with the freeblock?
1569 */
0ba962ef 1570 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
1da177e4
LT
1571 /*
1572 * Drop the block.
1573 */
1574 xfs_da_brelse(tp, fbp);
1575 fbp = NULL;
1576 if (fblk && fblk->bp)
1577 fblk->bp = NULL;
1578 }
1579 }
1580 }
1581 /*
1582 * If we don't have a data block, we need to allocate one and make
1583 * the freespace entries refer to it.
1584 */
1585 if (unlikely(dbno == -1)) {
1586 /*
1587 * Not allowed to allocate, return failure.
1588 */
6a178100
BN
1589 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1590 args->total == 0) {
1da177e4
LT
1591 /*
1592 * Drop the freespace buffer unless it came from our
1593 * caller.
1594 */
1595 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1596 xfs_da_buf_done(fbp);
1597 return XFS_ERROR(ENOSPC);
1598 }
1599 /*
1600 * Allocate and initialize the new data block.
1601 */
1602 if (unlikely((error = xfs_dir2_grow_inode(args,
1603 XFS_DIR2_DATA_SPACE,
1604 &dbno)) ||
1605 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1606 /*
1607 * Drop the freespace buffer unless it came from our
1608 * caller.
1609 */
1610 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1611 xfs_da_buf_done(fbp);
1612 return error;
1613 }
1614 /*
1615 * If (somehow) we have a freespace block, get rid of it.
1616 */
1617 if (fbp)
1618 xfs_da_brelse(tp, fbp);
1619 if (fblk && fblk->bp)
1620 fblk->bp = NULL;
1621
1622 /*
1623 * Get the freespace block corresponding to the data block
1624 * that was just allocated.
1625 */
bbaaf538 1626 fbno = xfs_dir2_db_to_fdb(mp, dbno);
1da177e4 1627 if (unlikely(error = xfs_da_read_buf(tp, dp,
bbaaf538 1628 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1da177e4
LT
1629 XFS_DATA_FORK))) {
1630 xfs_da_buf_done(dbp);
1631 return error;
1632 }
1633 /*
1634 * If there wasn't a freespace block, the read will
1635 * return a NULL fbp. Allocate and initialize a new one.
1636 */
1637 if( fbp == NULL ) {
1638 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1639 &fbno))) {
1640 return error;
1641 }
1642
bbaaf538 1643 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
1da177e4
LT
1644 cmn_err(CE_ALERT,
1645 "xfs_dir2_node_addname_int: dir ino "
1646 "%llu needed freesp block %lld for\n"
1647 " data block %lld, got %lld\n"
1648 " ifbno %llu lastfbno %d\n",
1649 (unsigned long long)dp->i_ino,
bbaaf538 1650 (long long)xfs_dir2_db_to_fdb(mp, dbno),
1da177e4
LT
1651 (long long)dbno, (long long)fbno,
1652 (unsigned long long)ifbno, lastfbno);
1653 if (fblk) {
1654 cmn_err(CE_ALERT,
1655 " fblk 0x%p blkno %llu "
1656 "index %d magic 0x%x\n",
1657 fblk,
1658 (unsigned long long)fblk->blkno,
1659 fblk->index,
1660 fblk->magic);
1661 } else {
1662 cmn_err(CE_ALERT,
1663 " ... fblk is NULL\n");
1664 }
1665 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1666 XFS_ERRLEVEL_LOW, mp);
1667 return XFS_ERROR(EFSCORRUPTED);
1668 }
1669
1670 /*
1671 * Get a buffer for the new block.
1672 */
1673 if ((error = xfs_da_get_buf(tp, dp,
bbaaf538 1674 xfs_dir2_db_to_da(mp, fbno),
1da177e4
LT
1675 -1, &fbp, XFS_DATA_FORK))) {
1676 return error;
1677 }
1678 ASSERT(fbp != NULL);
1679
1680 /*
1681 * Initialize the new block to be empty, and remember
1682 * its first slot as our empty slot.
1683 */
1684 free = fbp->data;
0ba962ef
NS
1685 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1686 free->hdr.firstdb = cpu_to_be32(
1da177e4
LT
1687 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1688 XFS_DIR2_MAX_FREE_BESTS(mp));
1689 free->hdr.nvalid = 0;
1690 free->hdr.nused = 0;
1691 } else {
1692 free = fbp->data;
0ba962ef 1693 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1694 }
1695
1696 /*
1697 * Set the freespace block index from the data block number.
1698 */
bbaaf538 1699 findex = xfs_dir2_db_to_fdindex(mp, dbno);
1da177e4
LT
1700 /*
1701 * If it's after the end of the current entries in the
1702 * freespace block, extend that table.
1703 */
0ba962ef 1704 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
1da177e4 1705 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
0ba962ef 1706 free->hdr.nvalid = cpu_to_be32(findex + 1);
1da177e4
LT
1707 /*
1708 * Tag new entry so nused will go up.
1709 */
0ba962ef 1710 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
1711 }
1712 /*
1713 * If this entry was for an empty data block
1714 * (this should always be true) then update the header.
1715 */
0ba962ef 1716 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
413d57c9 1717 be32_add_cpu(&free->hdr.nused, 1);
1da177e4
LT
1718 xfs_dir2_free_log_header(tp, fbp);
1719 }
1720 /*
1721 * Update the real value in the table.
1722 * We haven't allocated the data entry yet so this will
1723 * change again.
1724 */
1725 data = dbp->data;
70e73f59 1726 free->bests[findex] = data->hdr.bestfree[0].length;
1da177e4
LT
1727 logfree = 1;
1728 }
1729 /*
1730 * We had a data block so we don't have to make a new one.
1731 */
1732 else {
1733 /*
1734 * If just checking, we succeeded.
1735 */
6a178100 1736 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1da177e4
LT
1737 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1738 xfs_da_buf_done(fbp);
1739 return 0;
1740 }
1741 /*
1742 * Read the data block in.
1743 */
1744 if (unlikely(
bbaaf538 1745 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1da177e4
LT
1746 -1, &dbp, XFS_DATA_FORK))) {
1747 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1748 xfs_da_buf_done(fbp);
1749 return error;
1750 }
1751 data = dbp->data;
1752 logfree = 0;
1753 }
70e73f59 1754 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
1da177e4
LT
1755 /*
1756 * Point to the existing unused space.
1757 */
1758 dup = (xfs_dir2_data_unused_t *)
70e73f59 1759 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
1da177e4
LT
1760 needscan = needlog = 0;
1761 /*
1762 * Mark the first part of the unused space, inuse for us.
1763 */
1764 xfs_dir2_data_use_free(tp, dbp, dup,
1765 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1766 &needlog, &needscan);
1767 /*
1768 * Fill in the new entry and log it.
1769 */
1770 dep = (xfs_dir2_data_entry_t *)dup;
ff9901c1 1771 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
1772 dep->namelen = args->namelen;
1773 memcpy(dep->name, args->name, dep->namelen);
bbaaf538 1774 tagp = xfs_dir2_data_entry_tag_p(dep);
3d693c6e 1775 *tagp = cpu_to_be16((char *)dep - (char *)data);
1da177e4
LT
1776 xfs_dir2_data_log_entry(tp, dbp, dep);
1777 /*
1778 * Rescan the block for bestfree if needed.
1779 */
1780 if (needscan)
ef497f8a 1781 xfs_dir2_data_freescan(mp, data, &needlog);
1da177e4
LT
1782 /*
1783 * Log the data block header if needed.
1784 */
1785 if (needlog)
1786 xfs_dir2_data_log_header(tp, dbp);
1787 /*
1788 * If the freespace entry is now wrong, update it.
1789 */
0ba962ef 1790 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
70e73f59 1791 free->bests[findex] = data->hdr.bestfree[0].length;
1da177e4
LT
1792 logfree = 1;
1793 }
1794 /*
1795 * Log the freespace entry if needed.
1796 */
1797 if (logfree)
1798 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1799 /*
1800 * If the caller didn't hand us the freespace block, drop it.
1801 */
1802 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1803 xfs_da_buf_done(fbp);
1804 /*
1805 * Return the data block and offset in args, then drop the data block.
1806 */
1807 args->blkno = (xfs_dablk_t)dbno;
3d693c6e 1808 args->index = be16_to_cpu(*tagp);
1da177e4
LT
1809 xfs_da_buf_done(dbp);
1810 return 0;
1811}
1812
1813/*
1814 * Lookup an entry in a node-format directory.
1815 * All the real work happens in xfs_da_node_lookup_int.
1816 * The only real output is the inode number of the entry.
1817 */
1818int /* error */
1819xfs_dir2_node_lookup(
1820 xfs_da_args_t *args) /* operation arguments */
1821{
1822 int error; /* error return value */
1823 int i; /* btree level */
1824 int rval; /* operation return value */
1825 xfs_da_state_t *state; /* btree cursor */
1826
0b1b213f
CH
1827 trace_xfs_dir2_node_lookup(args);
1828
1da177e4
LT
1829 /*
1830 * Allocate and initialize the btree cursor.
1831 */
1832 state = xfs_da_state_alloc();
1833 state->args = args;
1834 state->mp = args->dp->i_mount;
1835 state->blocksize = state->mp->m_dirblksize;
1836 state->node_ents = state->mp->m_dir_node_ents;
1837 /*
1838 * Fill in the path to the entry in the cursor.
1839 */
1840 error = xfs_da_node_lookup_int(state, &rval);
1841 if (error)
1842 rval = error;
384f3ced
BN
1843 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1844 /* If a CI match, dup the actual name and return EEXIST */
1845 xfs_dir2_data_entry_t *dep;
1846
1847 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1848 data + state->extrablk.index);
1849 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1850 }
1da177e4
LT
1851 /*
1852 * Release the btree blocks and leaf block.
1853 */
1854 for (i = 0; i < state->path.active; i++) {
1855 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1856 state->path.blk[i].bp = NULL;
1857 }
1858 /*
1859 * Release the data block if we have it.
1860 */
1861 if (state->extravalid && state->extrablk.bp) {
1862 xfs_da_brelse(args->trans, state->extrablk.bp);
1863 state->extrablk.bp = NULL;
1864 }
1865 xfs_da_state_free(state);
1866 return rval;
1867}
1868
1869/*
1870 * Remove an entry from a node-format directory.
1871 */
1872int /* error */
1873xfs_dir2_node_removename(
1874 xfs_da_args_t *args) /* operation arguments */
1875{
1876 xfs_da_state_blk_t *blk; /* leaf block */
1877 int error; /* error return value */
1878 int rval; /* operation return value */
1879 xfs_da_state_t *state; /* btree cursor */
1880
0b1b213f
CH
1881 trace_xfs_dir2_node_removename(args);
1882
1da177e4
LT
1883 /*
1884 * Allocate and initialize the btree cursor.
1885 */
1886 state = xfs_da_state_alloc();
1887 state->args = args;
1888 state->mp = args->dp->i_mount;
1889 state->blocksize = state->mp->m_dirblksize;
1890 state->node_ents = state->mp->m_dir_node_ents;
1891 /*
1892 * Look up the entry we're deleting, set up the cursor.
1893 */
1894 error = xfs_da_node_lookup_int(state, &rval);
5163f95a 1895 if (error)
1da177e4 1896 rval = error;
1da177e4
LT
1897 /*
1898 * Didn't find it, upper layer screwed up.
1899 */
1900 if (rval != EEXIST) {
1901 xfs_da_state_free(state);
1902 return rval;
1903 }
1904 blk = &state->path.blk[state->path.active - 1];
1905 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1906 ASSERT(state->extravalid);
1907 /*
1908 * Remove the leaf and data entries.
1909 * Extrablk refers to the data block.
1910 */
1911 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1912 &state->extrablk, &rval);
5163f95a 1913 if (error)
1da177e4 1914 return error;
1da177e4
LT
1915 /*
1916 * Fix the hash values up the btree.
1917 */
1918 xfs_da_fixhashpath(state, &state->path);
1919 /*
1920 * If we need to join leaf blocks, do it.
1921 */
1922 if (rval && state->path.active > 1)
1923 error = xfs_da_join(state);
1924 /*
1925 * If no errors so far, try conversion to leaf format.
1926 */
1927 if (!error)
1928 error = xfs_dir2_node_to_leaf(state);
1929 xfs_da_state_free(state);
1930 return error;
1931}
1932
1933/*
1934 * Replace an entry's inode number in a node-format directory.
1935 */
1936int /* error */
1937xfs_dir2_node_replace(
1938 xfs_da_args_t *args) /* operation arguments */
1939{
1940 xfs_da_state_blk_t *blk; /* leaf block */
1941 xfs_dir2_data_t *data; /* data block structure */
1942 xfs_dir2_data_entry_t *dep; /* data entry changed */
1943 int error; /* error return value */
1944 int i; /* btree level */
1945 xfs_ino_t inum; /* new inode number */
1946 xfs_dir2_leaf_t *leaf; /* leaf structure */
1947 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1948 int rval; /* internal return value */
1949 xfs_da_state_t *state; /* btree cursor */
1950
0b1b213f
CH
1951 trace_xfs_dir2_node_replace(args);
1952
1da177e4
LT
1953 /*
1954 * Allocate and initialize the btree cursor.
1955 */
1956 state = xfs_da_state_alloc();
1957 state->args = args;
1958 state->mp = args->dp->i_mount;
1959 state->blocksize = state->mp->m_dirblksize;
1960 state->node_ents = state->mp->m_dir_node_ents;
1961 inum = args->inumber;
1962 /*
1963 * Lookup the entry to change in the btree.
1964 */
1965 error = xfs_da_node_lookup_int(state, &rval);
1966 if (error) {
1967 rval = error;
1968 }
1969 /*
1970 * It should be found, since the vnodeops layer has looked it up
1971 * and locked it. But paranoia is good.
1972 */
1973 if (rval == EEXIST) {
1974 /*
1975 * Find the leaf entry.
1976 */
1977 blk = &state->path.blk[state->path.active - 1];
1978 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1979 leaf = blk->bp->data;
1980 lep = &leaf->ents[blk->index];
1981 ASSERT(state->extravalid);
1982 /*
1983 * Point to the data entry.
1984 */
1985 data = state->extrablk.bp->data;
70e73f59 1986 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1da177e4
LT
1987 dep = (xfs_dir2_data_entry_t *)
1988 ((char *)data +
bbaaf538 1989 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
ff9901c1 1990 ASSERT(inum != be64_to_cpu(dep->inumber));
1da177e4
LT
1991 /*
1992 * Fill in the new inode number and log the entry.
1993 */
ff9901c1 1994 dep->inumber = cpu_to_be64(inum);
1da177e4
LT
1995 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1996 rval = 0;
1997 }
1998 /*
1999 * Didn't find it, and we're holding a data block. Drop it.
2000 */
2001 else if (state->extravalid) {
2002 xfs_da_brelse(args->trans, state->extrablk.bp);
2003 state->extrablk.bp = NULL;
2004 }
2005 /*
2006 * Release all the buffers in the cursor.
2007 */
2008 for (i = 0; i < state->path.active; i++) {
2009 xfs_da_brelse(args->trans, state->path.blk[i].bp);
2010 state->path.blk[i].bp = NULL;
2011 }
2012 xfs_da_state_free(state);
2013 return rval;
2014}
2015
2016/*
2017 * Trim off a trailing empty freespace block.
2018 * Return (in rvalp) 1 if we did it, 0 if not.
2019 */
2020int /* error */
2021xfs_dir2_node_trim_free(
2022 xfs_da_args_t *args, /* operation arguments */
2023 xfs_fileoff_t fo, /* free block number */
2024 int *rvalp) /* out: did something */
2025{
2026 xfs_dabuf_t *bp; /* freespace buffer */
2027 xfs_inode_t *dp; /* incore directory inode */
2028 int error; /* error return code */
2029 xfs_dir2_free_t *free; /* freespace structure */
2030 xfs_mount_t *mp; /* filesystem mount point */
2031 xfs_trans_t *tp; /* transaction pointer */
2032
2033 dp = args->dp;
2034 mp = dp->i_mount;
2035 tp = args->trans;
2036 /*
2037 * Read the freespace block.
2038 */
2039 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2040 XFS_DATA_FORK))) {
2041 return error;
2042 }
2043
2044 /*
2045 * There can be holes in freespace. If fo is a hole, there's
2046 * nothing to do.
2047 */
2048 if (bp == NULL) {
2049 return 0;
2050 }
2051 free = bp->data;
0ba962ef 2052 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
2053 /*
2054 * If there are used entries, there's nothing to do.
2055 */
0ba962ef 2056 if (be32_to_cpu(free->hdr.nused) > 0) {
1da177e4
LT
2057 xfs_da_brelse(tp, bp);
2058 *rvalp = 0;
2059 return 0;
2060 }
2061 /*
2062 * Blow the block away.
2063 */
2064 if ((error =
bbaaf538 2065 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
1da177e4
LT
2066 bp))) {
2067 /*
2068 * Can't fail with ENOSPC since that only happens with no
2069 * space reservation, when breaking up an extent into two
2070 * pieces. This is the last block of an extent.
2071 */
2072 ASSERT(error != ENOSPC);
2073 xfs_da_brelse(tp, bp);
2074 return error;
2075 }
2076 /*
2077 * Return that we succeeded.
2078 */
2079 *rvalp = 1;
2080 return 0;
2081}