]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_leaf.c
xfs: add CRC checks to block format directory blocks
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_leaf.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
da23017d 3 * All Rights Reserved.
5000d01d 4 *
da23017d
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
2bd0ea18 7 * published by the Free Software Foundation.
5000d01d 8 *
da23017d
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.
5000d01d 13 *
da23017d
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
2bd0ea18
NS
17 */
18
5e656dbb
BN
19#include <xfs.h>
20
2bd0ea18 21/*
5e656dbb 22 * Local function declarations.
2bd0ea18 23 */
5e656dbb 24#ifdef DEBUG
a2ceac1f 25static void xfs_dir2_leaf_check(struct xfs_inode *dp, struct xfs_buf *bp);
5e656dbb
BN
26#else
27#define xfs_dir2_leaf_check(dp, bp)
28#endif
a2ceac1f
DC
29static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
30 int *indexp, struct xfs_buf **dbpp);
31static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
5e656dbb 32 int first, int last);
a2ceac1f 33static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
2bd0ea18 34
a2ceac1f
DC
35static void
36xfs_dir2_leaf_verify(
37 struct xfs_buf *bp,
38 __be16 magic)
39{
40 struct xfs_mount *mp = bp->b_target->bt_mount;
41 struct xfs_dir2_leaf_hdr *hdr = bp->b_addr;
42 int block_ok = 0;
43
44 block_ok = hdr->info.magic == magic;
45 if (!block_ok) {
46 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
47 xfs_buf_ioerror(bp, EFSCORRUPTED);
48 }
49}
50
51static void
52xfs_dir2_leaf1_read_verify(
53 struct xfs_buf *bp)
54{
55 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
56}
57
58static void
59xfs_dir2_leaf1_write_verify(
60 struct xfs_buf *bp)
61{
62 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
63}
64
65void
66xfs_dir2_leafn_read_verify(
67 struct xfs_buf *bp)
68{
69 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
70}
71
72void
73xfs_dir2_leafn_write_verify(
74 struct xfs_buf *bp)
75{
76 xfs_dir2_leaf_verify(bp, cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
77}
78
79static const struct xfs_buf_ops xfs_dir2_leaf1_buf_ops = {
80 .verify_read = xfs_dir2_leaf1_read_verify,
81 .verify_write = xfs_dir2_leaf1_write_verify,
82};
83
84const struct xfs_buf_ops xfs_dir2_leafn_buf_ops = {
85 .verify_read = xfs_dir2_leafn_read_verify,
86 .verify_write = xfs_dir2_leafn_write_verify,
87};
88
89static int
90xfs_dir2_leaf_read(
91 struct xfs_trans *tp,
92 struct xfs_inode *dp,
93 xfs_dablk_t fbno,
94 xfs_daddr_t mappedbno,
95 struct xfs_buf **bpp)
96{
97 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
98 XFS_DATA_FORK, &xfs_dir2_leaf1_buf_ops);
99}
100
101int
102xfs_dir2_leafn_read(
103 struct xfs_trans *tp,
104 struct xfs_inode *dp,
105 xfs_dablk_t fbno,
106 xfs_daddr_t mappedbno,
107 struct xfs_buf **bpp)
108{
109 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
110 XFS_DATA_FORK, &xfs_dir2_leafn_buf_ops);
111}
2bd0ea18
NS
112
113/*
114 * Convert a block form directory to a leaf form directory.
115 */
116int /* error */
117xfs_dir2_block_to_leaf(
118 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 119 struct xfs_buf *dbp) /* input block's buffer */
2bd0ea18 120{
5e656dbb 121 __be16 *bestsp; /* leaf's bestsp entries */
2bd0ea18 122 xfs_dablk_t blkno; /* leaf block's bno */
a2ceac1f 123 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18
NS
124 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
125 xfs_dir2_block_tail_t *btp; /* block's tail */
126 xfs_inode_t *dp; /* incore directory inode */
127 int error; /* error return code */
a2ceac1f 128 struct xfs_buf *lbp; /* leaf block's buffer */
2bd0ea18
NS
129 xfs_dir2_db_t ldb; /* leaf block's bno */
130 xfs_dir2_leaf_t *leaf; /* leaf structure */
131 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
132 xfs_mount_t *mp; /* filesystem mount point */
133 int needlog; /* need to log block header */
134 int needscan; /* need to rescan bestfree */
135 xfs_trans_t *tp; /* transaction pointer */
693fc8f6 136 struct xfs_dir2_data_free *bf;
2bd0ea18 137
56b2de80
DC
138 trace_xfs_dir2_block_to_leaf(args);
139
2bd0ea18
NS
140 dp = args->dp;
141 mp = dp->i_mount;
142 tp = args->trans;
143 /*
144 * Add the leaf block to the inode.
145 * This interface will only put blocks in the leaf/node range.
146 * Since that's empty now, we'll get the root (block 0 in range).
147 */
0e266570 148 if ((error = xfs_da_grow_inode(args, &blkno))) {
2bd0ea18
NS
149 return error;
150 }
5e656dbb 151 ldb = xfs_dir2_da_to_db(mp, blkno);
2bd0ea18
NS
152 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
153 /*
154 * Initialize the leaf block, get a buffer for it.
155 */
0e266570 156 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
2bd0ea18
NS
157 return error;
158 }
159 ASSERT(lbp != NULL);
a2ceac1f
DC
160 leaf = lbp->b_addr;
161 hdr = dbp->b_addr;
2bd0ea18 162 xfs_dir2_data_check(dp, dbp);
a2ceac1f 163 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 164 blp = xfs_dir2_block_leaf_p(btp);
693fc8f6 165 bf = xfs_dir3_data_bestfree_p(hdr);
2bd0ea18
NS
166 /*
167 * Set the counts in the leaf header.
168 */
5e656dbb
BN
169 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
170 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
2bd0ea18
NS
171 /*
172 * Could compact these but I think we always do the conversion
173 * after squeezing out stale entries.
174 */
5e656dbb
BN
175 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
176 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
2bd0ea18
NS
177 needscan = 0;
178 needlog = 1;
179 /*
180 * Make the space formerly occupied by the leaf entries and block
181 * tail be free.
182 */
183 xfs_dir2_data_make_free(tp, dbp,
a2ceac1f
DC
184 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
185 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
2bd0ea18
NS
186 (char *)blp),
187 &needlog, &needscan);
188 /*
189 * Fix up the block header, make it a data block.
190 */
a2ceac1f
DC
191 dbp->b_ops = &xfs_dir2_data_buf_ops;
192 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
2bd0ea18 193 if (needscan)
a2ceac1f 194 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
195 /*
196 * Set up leaf tail and bests table.
197 */
5e656dbb
BN
198 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
199 ltp->bestcount = cpu_to_be32(1);
200 bestsp = xfs_dir2_leaf_bests_p(ltp);
693fc8f6 201 bestsp[0] = bf[0].length;
2bd0ea18
NS
202 /*
203 * Log the data header and leaf bests table.
204 */
205 if (needlog)
206 xfs_dir2_data_log_header(tp, dbp);
207 xfs_dir2_leaf_check(dp, lbp);
208 xfs_dir2_data_check(dp, dbp);
209 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
2bd0ea18
NS
210 return 0;
211}
212
a2ceac1f
DC
213STATIC void
214xfs_dir2_leaf_find_stale(
215 struct xfs_dir2_leaf *leaf,
216 int index,
217 int *lowstale,
218 int *highstale)
219{
220 /*
221 * Find the first stale entry before our index, if any.
222 */
223 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
224 if (leaf->ents[*lowstale].address ==
225 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
226 break;
227 }
228
229 /*
230 * Find the first stale entry at or after our index, if any.
231 * Stop if the result would require moving more entries than using
232 * lowstale.
233 */
234 for (*highstale = index;
235 *highstale < be16_to_cpu(leaf->hdr.count);
236 ++*highstale) {
237 if (leaf->ents[*highstale].address ==
238 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
239 break;
240 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
241 break;
242 }
243}
244
245struct xfs_dir2_leaf_entry *
246xfs_dir2_leaf_find_entry(
247 xfs_dir2_leaf_t *leaf, /* leaf structure */
248 int index, /* leaf table position */
249 int compact, /* need to compact leaves */
250 int lowstale, /* index of prev stale leaf */
251 int highstale, /* index of next stale leaf */
252 int *lfloglow, /* low leaf logging index */
253 int *lfloghigh) /* high leaf logging index */
254{
255 if (!leaf->hdr.stale) {
256 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
257
258 /*
259 * Now we need to make room to insert the leaf entry.
260 *
261 * If there are no stale entries, just insert a hole at index.
262 */
263 lep = &leaf->ents[index];
264 if (index < be16_to_cpu(leaf->hdr.count))
265 memmove(lep + 1, lep,
266 (be16_to_cpu(leaf->hdr.count) - index) *
267 sizeof(*lep));
268
269 /*
270 * Record low and high logging indices for the leaf.
271 */
272 *lfloglow = index;
273 *lfloghigh = be16_to_cpu(leaf->hdr.count);
274 be16_add_cpu(&leaf->hdr.count, 1);
275 return lep;
276 }
277
278 /*
279 * There are stale entries.
280 *
281 * We will use one of them for the new entry. It's probably not at
282 * the right location, so we'll have to shift some up or down first.
283 *
284 * If we didn't compact before, we need to find the nearest stale
285 * entries before and after our insertion point.
286 */
287 if (compact == 0)
288 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
289
290 /*
291 * If the low one is better, use it.
292 */
293 if (lowstale >= 0 &&
294 (highstale == be16_to_cpu(leaf->hdr.count) ||
295 index - lowstale - 1 < highstale - index)) {
296 ASSERT(index - lowstale - 1 >= 0);
297 ASSERT(leaf->ents[lowstale].address ==
298 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
299
300 /*
301 * Copy entries up to cover the stale entry and make room
302 * for the new entry.
303 */
304 if (index - lowstale - 1 > 0) {
305 memmove(&leaf->ents[lowstale],
306 &leaf->ents[lowstale + 1],
307 (index - lowstale - 1) *
308 sizeof(xfs_dir2_leaf_entry_t));
309 }
310 *lfloglow = MIN(lowstale, *lfloglow);
311 *lfloghigh = MAX(index - 1, *lfloghigh);
312 be16_add_cpu(&leaf->hdr.stale, -1);
313 return &leaf->ents[index - 1];
314 }
315
316 /*
317 * The high one is better, so use that one.
318 */
319 ASSERT(highstale - index >= 0);
320 ASSERT(leaf->ents[highstale].address ==
321 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
322
323 /*
324 * Copy entries down to cover the stale entry and make room for the
325 * new entry.
326 */
327 if (highstale - index > 0) {
328 memmove(&leaf->ents[index + 1],
329 &leaf->ents[index],
330 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
331 }
332 *lfloglow = MIN(index, *lfloglow);
333 *lfloghigh = MAX(highstale, *lfloghigh);
334 be16_add_cpu(&leaf->hdr.stale, -1);
335 return &leaf->ents[index];
336}
337
2bd0ea18
NS
338/*
339 * Add an entry to a leaf form directory.
340 */
341int /* error */
342xfs_dir2_leaf_addname(
343 xfs_da_args_t *args) /* operation arguments */
344{
5e656dbb 345 __be16 *bestsp; /* freespace table in leaf */
2bd0ea18 346 int compact; /* need to compact leaves */
a2ceac1f
DC
347 xfs_dir2_data_hdr_t *hdr; /* data block header */
348 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
349 xfs_dir2_data_entry_t *dep; /* data block entry */
350 xfs_inode_t *dp; /* incore directory inode */
351 xfs_dir2_data_unused_t *dup; /* data unused entry */
352 int error; /* error return value */
353 int grown; /* allocated new data block */
354 int highstale; /* index of next stale leaf */
355 int i; /* temporary, index */
356 int index; /* leaf table position */
a2ceac1f 357 struct xfs_buf *lbp; /* leaf's buffer */
2bd0ea18
NS
358 xfs_dir2_leaf_t *leaf; /* leaf structure */
359 int length; /* length of new entry */
360 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
361 int lfloglow; /* low leaf logging index */
362 int lfloghigh; /* high leaf logging index */
363 int lowstale; /* index of prev stale leaf */
364 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
365 xfs_mount_t *mp; /* filesystem mount point */
366 int needbytes; /* leaf block bytes needed */
367 int needlog; /* need to log data header */
368 int needscan; /* need to rescan data free */
5e656dbb 369 __be16 *tagp; /* end of data entry */
2bd0ea18
NS
370 xfs_trans_t *tp; /* transaction pointer */
371 xfs_dir2_db_t use_block; /* data block number */
372
56b2de80
DC
373 trace_xfs_dir2_leaf_addname(args);
374
2bd0ea18
NS
375 dp = args->dp;
376 tp = args->trans;
377 mp = dp->i_mount;
a2ceac1f
DC
378
379 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
380 if (error)
2bd0ea18 381 return error;
a2ceac1f 382
2bd0ea18
NS
383 /*
384 * Look up the entry by hash value and name.
385 * We know it's not there, our caller has already done a lookup.
386 * So the index is of the entry to insert in front of.
387 * But if there are dup hash values the index is of the first of those.
388 */
389 index = xfs_dir2_leaf_search_hash(args, lbp);
a2ceac1f 390 leaf = lbp->b_addr;
5e656dbb
BN
391 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
392 bestsp = xfs_dir2_leaf_bests_p(ltp);
393 length = xfs_dir2_data_entsize(args->namelen);
2bd0ea18
NS
394 /*
395 * See if there are any entries with the same hash value
396 * and space in their block for the new entry.
397 * This is good because it puts multiple same-hash value entries
398 * in a data block, improving the lookup of those entries.
399 */
400 for (use_block = -1, lep = &leaf->ents[index];
5e656dbb 401 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
2bd0ea18 402 index++, lep++) {
5e656dbb 403 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18 404 continue;
5e656dbb
BN
405 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
406 ASSERT(i < be32_to_cpu(ltp->bestcount));
a2ceac1f 407 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
5e656dbb 408 if (be16_to_cpu(bestsp[i]) >= length) {
2bd0ea18
NS
409 use_block = i;
410 break;
411 }
412 }
413 /*
414 * Didn't find a block yet, linear search all the data blocks.
415 */
416 if (use_block == -1) {
5e656dbb 417 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
2bd0ea18
NS
418 /*
419 * Remember a block we see that's missing.
420 */
a2ceac1f
DC
421 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
422 use_block == -1)
2bd0ea18 423 use_block = i;
5e656dbb 424 else if (be16_to_cpu(bestsp[i]) >= length) {
2bd0ea18
NS
425 use_block = i;
426 break;
427 }
428 }
429 }
430 /*
431 * How many bytes do we need in the leaf block?
432 */
a2ceac1f
DC
433 needbytes = 0;
434 if (!leaf->hdr.stale)
435 needbytes += sizeof(xfs_dir2_leaf_entry_t);
436 if (use_block == -1)
437 needbytes += sizeof(xfs_dir2_data_off_t);
438
2bd0ea18
NS
439 /*
440 * Now kill use_block if it refers to a missing block, so we
441 * can use it as an indication of allocation needed.
442 */
a2ceac1f 443 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
2bd0ea18
NS
444 use_block = -1;
445 /*
446 * If we don't have enough free bytes but we can make enough
447 * by compacting out stale entries, we'll do that.
448 */
5e656dbb
BN
449 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
450 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
2bd0ea18
NS
451 compact = 1;
452 }
453 /*
454 * Otherwise if we don't have enough free bytes we need to
455 * convert to node form.
456 */
5e656dbb
BN
457 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
458 leaf->hdr.count)] < needbytes) {
2bd0ea18
NS
459 /*
460 * Just checking or no space reservation, give up.
461 */
5e656dbb
BN
462 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
463 args->total == 0) {
a2ceac1f 464 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
465 return XFS_ERROR(ENOSPC);
466 }
467 /*
468 * Convert to node form.
469 */
470 error = xfs_dir2_leaf_to_node(args, lbp);
2bd0ea18
NS
471 if (error)
472 return error;
473 /*
474 * Then add the new entry.
475 */
476 return xfs_dir2_node_addname(args);
477 }
478 /*
479 * Otherwise it will fit without compaction.
480 */
481 else
482 compact = 0;
483 /*
484 * If just checking, then it will fit unless we needed to allocate
485 * a new data block.
486 */
5e656dbb 487 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
a2ceac1f 488 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
489 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
490 }
491 /*
492 * If no allocations are allowed, return now before we've
493 * changed anything.
494 */
495 if (args->total == 0 && use_block == -1) {
a2ceac1f 496 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
497 return XFS_ERROR(ENOSPC);
498 }
499 /*
500 * Need to compact the leaf entries, removing stale ones.
501 * Leave one stale entry behind - the one closest to our
502 * insertion index - and we'll shift that one to our insertion
503 * point later.
504 */
505 if (compact) {
2bd0ea18
NS
506 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
507 &lfloglow, &lfloghigh);
508 }
509 /*
510 * There are stale entries, so we'll need log-low and log-high
511 * impossibly bad values later.
512 */
5e656dbb
BN
513 else if (be16_to_cpu(leaf->hdr.stale)) {
514 lfloglow = be16_to_cpu(leaf->hdr.count);
2bd0ea18
NS
515 lfloghigh = -1;
516 }
517 /*
518 * If there was no data block space found, we need to allocate
519 * a new one.
520 */
521 if (use_block == -1) {
2bd0ea18
NS
522 /*
523 * Add the new data block.
524 */
0e266570
NS
525 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
526 &use_block))) {
a2ceac1f 527 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
528 return error;
529 }
530 /*
531 * Initialize the block.
532 */
693fc8f6 533 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
a2ceac1f 534 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
535 return error;
536 }
537 /*
538 * If we're adding a new data block on the end we need to
539 * extend the bests table. Copy it up one entry.
540 */
5e656dbb 541 if (use_block >= be32_to_cpu(ltp->bestcount)) {
2bd0ea18 542 bestsp--;
32181a02 543 memmove(&bestsp[0], &bestsp[1],
5e656dbb
BN
544 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
545 be32_add_cpu(&ltp->bestcount, 1);
2bd0ea18 546 xfs_dir2_leaf_log_tail(tp, lbp);
5e656dbb 547 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18
NS
548 }
549 /*
550 * If we're filling in a previously empty block just log it.
551 */
552 else
553 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
a2ceac1f
DC
554 hdr = dbp->b_addr;
555 bestsp[use_block] = hdr->bestfree[0].length;
2bd0ea18 556 grown = 1;
a2ceac1f
DC
557 } else {
558 /*
559 * Already had space in some data block.
560 * Just read that one in.
561 */
562 error = xfs_dir2_data_read(tp, dp,
563 xfs_dir2_db_to_da(mp, use_block),
564 -1, &dbp);
565 if (error) {
566 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
567 return error;
568 }
a2ceac1f 569 hdr = dbp->b_addr;
2bd0ea18
NS
570 grown = 0;
571 }
2bd0ea18
NS
572 /*
573 * Point to the biggest freespace in our data block.
574 */
575 dup = (xfs_dir2_data_unused_t *)
a2ceac1f 576 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
5e656dbb 577 ASSERT(be16_to_cpu(dup->length) >= length);
2bd0ea18
NS
578 needscan = needlog = 0;
579 /*
580 * Mark the initial part of our freespace in use for the new entry.
581 */
582 xfs_dir2_data_use_free(tp, dbp, dup,
a2ceac1f 583 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
2bd0ea18
NS
584 &needlog, &needscan);
585 /*
586 * Initialize our new entry (at last).
587 */
588 dep = (xfs_dir2_data_entry_t *)dup;
5e656dbb 589 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18 590 dep->namelen = args->namelen;
32181a02 591 memcpy(dep->name, args->name, dep->namelen);
5e656dbb 592 tagp = xfs_dir2_data_entry_tag_p(dep);
a2ceac1f 593 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18
NS
594 /*
595 * Need to scan fix up the bestfree table.
596 */
597 if (needscan)
a2ceac1f 598 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
599 /*
600 * Need to log the data block's header.
601 */
602 if (needlog)
603 xfs_dir2_data_log_header(tp, dbp);
604 xfs_dir2_data_log_entry(tp, dbp, dep);
605 /*
606 * If the bests table needs to be changed, do it.
607 * Log the change unless we've already done that.
608 */
a2ceac1f
DC
609 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
610 bestsp[use_block] = hdr->bestfree[0].length;
2bd0ea18
NS
611 if (!grown)
612 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
613 }
a2ceac1f
DC
614
615 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
616 highstale, &lfloglow, &lfloghigh);
617
2bd0ea18
NS
618 /*
619 * Fill in the new leaf entry.
620 */
5e656dbb
BN
621 lep->hashval = cpu_to_be32(args->hashval);
622 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
623 be16_to_cpu(*tagp)));
2bd0ea18
NS
624 /*
625 * Log the leaf fields and give up the buffers.
626 */
627 xfs_dir2_leaf_log_header(tp, lbp);
628 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
629 xfs_dir2_leaf_check(dp, lbp);
2bd0ea18 630 xfs_dir2_data_check(dp, dbp);
2bd0ea18
NS
631 return 0;
632}
633
2bd0ea18
NS
634#ifdef DEBUG
635/*
636 * Check the internal consistency of a leaf1 block.
637 * Pop an assert if something is wrong.
638 */
56b2de80 639STATIC void
2bd0ea18 640xfs_dir2_leaf_check(
a2ceac1f
DC
641 struct xfs_inode *dp, /* incore directory inode */
642 struct xfs_buf *bp) /* leaf's buffer */
2bd0ea18
NS
643{
644 int i; /* leaf index */
645 xfs_dir2_leaf_t *leaf; /* leaf structure */
646 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
647 xfs_mount_t *mp; /* filesystem mount point */
648 int stale; /* count of stale leaves */
649
a2ceac1f 650 leaf = bp->b_addr;
2bd0ea18 651 mp = dp->i_mount;
a2ceac1f 652 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
2bd0ea18
NS
653 /*
654 * This value is not restrictive enough.
655 * Should factor in the size of the bests table as well.
656 * We can deduce a value for that from di_size.
657 */
5e656dbb
BN
658 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
659 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
2bd0ea18
NS
660 /*
661 * Leaves and bests don't overlap.
662 */
5e656dbb
BN
663 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
664 (char *)xfs_dir2_leaf_bests_p(ltp));
2bd0ea18
NS
665 /*
666 * Check hash value order, count stale entries.
667 */
5e656dbb
BN
668 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
669 if (i + 1 < be16_to_cpu(leaf->hdr.count))
670 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
671 be32_to_cpu(leaf->ents[i + 1].hashval));
a2ceac1f 672 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
2bd0ea18
NS
673 stale++;
674 }
5e656dbb 675 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
2bd0ea18
NS
676}
677#endif /* DEBUG */
678
679/*
680 * Compact out any stale entries in the leaf.
681 * Log the header and changed leaf entries, if any.
682 */
683void
684xfs_dir2_leaf_compact(
685 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 686 struct xfs_buf *bp) /* leaf buffer */
2bd0ea18
NS
687{
688 int from; /* source leaf index */
dfc130f3 689 xfs_dir2_leaf_t *leaf; /* leaf structure */
2bd0ea18
NS
690 int loglow; /* first leaf entry to log */
691 int to; /* target leaf index */
692
a2ceac1f 693 leaf = bp->b_addr;
46eca962 694 if (!leaf->hdr.stale) {
2bd0ea18
NS
695 return;
696 }
697 /*
698 * Compress out the stale entries in place.
699 */
5e656dbb 700 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
a2ceac1f
DC
701 if (leaf->ents[from].address ==
702 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
2bd0ea18
NS
703 continue;
704 /*
705 * Only actually copy the entries that are different.
706 */
707 if (from > to) {
708 if (loglow == -1)
709 loglow = to;
710 leaf->ents[to] = leaf->ents[from];
711 }
712 to++;
713 }
714 /*
715 * Update and log the header, log the leaf entries.
716 */
5e656dbb
BN
717 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
718 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
46eca962 719 leaf->hdr.stale = 0;
2bd0ea18
NS
720 xfs_dir2_leaf_log_header(args->trans, bp);
721 if (loglow != -1)
722 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
723}
724
725/*
726 * Compact the leaf entries, removing stale ones.
727 * Leave one stale entry behind - the one closest to our
728 * insertion index - and the caller will shift that one to our insertion
729 * point later.
730 * Return new insertion index, where the remaining stale entry is,
731 * and leaf logging indices.
732 */
733void
734xfs_dir2_leaf_compact_x1(
a2ceac1f 735 struct xfs_buf *bp, /* leaf buffer */
2bd0ea18
NS
736 int *indexp, /* insertion index */
737 int *lowstalep, /* out: stale entry before us */
738 int *highstalep, /* out: stale entry after us */
739 int *lowlogp, /* out: low log index */
740 int *highlogp) /* out: high log index */
741{
742 int from; /* source copy index */
743 int highstale; /* stale entry at/after index */
744 int index; /* insertion index */
745 int keepstale; /* source index of kept stale */
dfc130f3 746 xfs_dir2_leaf_t *leaf; /* leaf structure */
2bd0ea18 747 int lowstale; /* stale entry before index */
0e266570 748 int newindex=0; /* new insertion index */
2bd0ea18
NS
749 int to; /* destination copy index */
750
a2ceac1f 751 leaf = bp->b_addr;
5e656dbb 752 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
2bd0ea18 753 index = *indexp;
a2ceac1f
DC
754
755 xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
756
2bd0ea18
NS
757 /*
758 * Pick the better of lowstale and highstale.
759 */
760 if (lowstale >= 0 &&
5e656dbb 761 (highstale == be16_to_cpu(leaf->hdr.count) ||
2bd0ea18
NS
762 index - lowstale <= highstale - index))
763 keepstale = lowstale;
764 else
765 keepstale = highstale;
766 /*
767 * Copy the entries in place, removing all the stale entries
768 * except keepstale.
769 */
5e656dbb 770 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
2bd0ea18
NS
771 /*
772 * Notice the new value of index.
773 */
774 if (index == from)
775 newindex = to;
776 if (from != keepstale &&
a2ceac1f
DC
777 leaf->ents[from].address ==
778 cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
2bd0ea18
NS
779 if (from == to)
780 *lowlogp = to;
781 continue;
782 }
783 /*
784 * Record the new keepstale value for the insertion.
785 */
786 if (from == keepstale)
787 lowstale = highstale = to;
788 /*
789 * Copy only the entries that have moved.
790 */
791 if (from > to)
792 leaf->ents[to] = leaf->ents[from];
793 to++;
794 }
795 ASSERT(from > to);
796 /*
797 * If the insertion point was past the last entry,
798 * set the new insertion point accordingly.
799 */
800 if (index == from)
801 newindex = to;
802 *indexp = newindex;
803 /*
804 * Adjust the leaf header values.
805 */
5e656dbb
BN
806 be16_add_cpu(&leaf->hdr.count, -(from - to));
807 leaf->hdr.stale = cpu_to_be16(1);
2bd0ea18
NS
808 /*
809 * Remember the low/high stale value only in the "right"
810 * direction.
811 */
812 if (lowstale >= newindex)
813 lowstale = -1;
814 else
5e656dbb
BN
815 highstale = be16_to_cpu(leaf->hdr.count);
816 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
2bd0ea18
NS
817 *lowstalep = lowstale;
818 *highstalep = highstale;
819}
820
821/*
822 * Initialize a new leaf block, leaf1 or leafn magic accepted.
823 */
824int
825xfs_dir2_leaf_init(
826 xfs_da_args_t *args, /* operation arguments */
827 xfs_dir2_db_t bno, /* directory block number */
a2ceac1f 828 struct xfs_buf **bpp, /* out: leaf buffer */
2bd0ea18
NS
829 int magic) /* magic number for block */
830{
a2ceac1f 831 struct xfs_buf *bp; /* leaf buffer */
2bd0ea18
NS
832 xfs_inode_t *dp; /* incore directory inode */
833 int error; /* error return code */
834 xfs_dir2_leaf_t *leaf; /* leaf structure */
835 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
836 xfs_mount_t *mp; /* filesystem mount point */
837 xfs_trans_t *tp; /* transaction pointer */
838
839 dp = args->dp;
840 ASSERT(dp != NULL);
841 tp = args->trans;
842 mp = dp->i_mount;
843 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
844 bno < XFS_DIR2_FREE_FIRSTDB(mp));
845 /*
846 * Get the buffer for the block.
847 */
5e656dbb 848 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
a2ceac1f
DC
849 XFS_DATA_FORK);
850 if (error)
2bd0ea18 851 return error;
a2ceac1f 852
2bd0ea18
NS
853 /*
854 * Initialize the header.
855 */
a2ceac1f 856 leaf = bp->b_addr;
5e656dbb 857 leaf->hdr.info.magic = cpu_to_be16(magic);
46eca962
NS
858 leaf->hdr.info.forw = 0;
859 leaf->hdr.info.back = 0;
860 leaf->hdr.count = 0;
861 leaf->hdr.stale = 0;
2bd0ea18
NS
862 xfs_dir2_leaf_log_header(tp, bp);
863 /*
864 * If it's a leaf-format directory initialize the tail.
865 * In this case our caller has the real bests table to copy into
866 * the block.
867 */
868 if (magic == XFS_DIR2_LEAF1_MAGIC) {
a2ceac1f 869 bp->b_ops = &xfs_dir2_leaf1_buf_ops;
5e656dbb 870 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
46eca962 871 ltp->bestcount = 0;
2bd0ea18 872 xfs_dir2_leaf_log_tail(tp, bp);
a2ceac1f
DC
873 } else
874 bp->b_ops = &xfs_dir2_leafn_buf_ops;
2bd0ea18
NS
875 *bpp = bp;
876 return 0;
877}
878
879/*
880 * Log the bests entries indicated from a leaf1 block.
881 */
5e656dbb 882static void
2bd0ea18
NS
883xfs_dir2_leaf_log_bests(
884 xfs_trans_t *tp, /* transaction pointer */
a2ceac1f 885 struct xfs_buf *bp, /* leaf buffer */
2bd0ea18
NS
886 int first, /* first entry to log */
887 int last) /* last entry to log */
888{
5e656dbb
BN
889 __be16 *firstb; /* pointer to first entry */
890 __be16 *lastb; /* pointer to last entry */
2bd0ea18
NS
891 xfs_dir2_leaf_t *leaf; /* leaf structure */
892 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
893
a2ceac1f
DC
894 leaf = bp->b_addr;
895 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
5e656dbb
BN
896 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
897 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
898 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
a2ceac1f 899 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
2bd0ea18
NS
900 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
901}
902
903/*
904 * Log the leaf entries indicated from a leaf1 or leafn block.
905 */
906void
907xfs_dir2_leaf_log_ents(
908 xfs_trans_t *tp, /* transaction pointer */
a2ceac1f 909 struct xfs_buf *bp, /* leaf buffer */
2bd0ea18
NS
910 int first, /* first entry to log */
911 int last) /* last entry to log */
912{
913 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
914 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
915 xfs_dir2_leaf_t *leaf; /* leaf structure */
916
a2ceac1f
DC
917 leaf = bp->b_addr;
918 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
919 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
2bd0ea18
NS
920 firstlep = &leaf->ents[first];
921 lastlep = &leaf->ents[last];
a2ceac1f 922 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
2bd0ea18
NS
923 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
924}
925
926/*
927 * Log the header of the leaf1 or leafn block.
928 */
929void
930xfs_dir2_leaf_log_header(
a2ceac1f
DC
931 struct xfs_trans *tp,
932 struct xfs_buf *bp)
2bd0ea18
NS
933{
934 xfs_dir2_leaf_t *leaf; /* leaf structure */
935
a2ceac1f
DC
936 leaf = bp->b_addr;
937 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
938 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
939 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
2bd0ea18
NS
940 (uint)(sizeof(leaf->hdr) - 1));
941}
942
943/*
944 * Log the tail of the leaf1 block.
945 */
5e656dbb 946STATIC void
2bd0ea18 947xfs_dir2_leaf_log_tail(
a2ceac1f
DC
948 struct xfs_trans *tp,
949 struct xfs_buf *bp)
2bd0ea18
NS
950{
951 xfs_dir2_leaf_t *leaf; /* leaf structure */
952 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
953 xfs_mount_t *mp; /* filesystem mount point */
954
955 mp = tp->t_mountp;
a2ceac1f
DC
956 leaf = bp->b_addr;
957 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
5e656dbb 958 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
a2ceac1f 959 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
2bd0ea18
NS
960 (uint)(mp->m_dirblksize - 1));
961}
962
963/*
964 * Look up the entry referred to by args in the leaf format directory.
965 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
966 * is also used by the node-format code.
967 */
968int
969xfs_dir2_leaf_lookup(
970 xfs_da_args_t *args) /* operation arguments */
971{
a2ceac1f 972 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
973 xfs_dir2_data_entry_t *dep; /* data block entry */
974 xfs_inode_t *dp; /* incore directory inode */
975 int error; /* error return code */
976 int index; /* found entry index */
a2ceac1f 977 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
978 xfs_dir2_leaf_t *leaf; /* leaf structure */
979 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
980 xfs_trans_t *tp; /* transaction pointer */
981
56b2de80
DC
982 trace_xfs_dir2_leaf_lookup(args);
983
2bd0ea18
NS
984 /*
985 * Look up name in the leaf block, returning both buffers and index.
986 */
0e266570 987 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
988 return error;
989 }
990 tp = args->trans;
991 dp = args->dp;
992 xfs_dir2_leaf_check(dp, lbp);
a2ceac1f 993 leaf = lbp->b_addr;
2bd0ea18
NS
994 /*
995 * Get to the leaf entry and contained data entry address.
996 */
997 lep = &leaf->ents[index];
998 /*
999 * Point to the data entry.
1000 */
1001 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1002 ((char *)dbp->b_addr +
5e656dbb 1003 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
2bd0ea18 1004 /*
5e656dbb 1005 * Return the found inode number & CI name if appropriate
2bd0ea18 1006 */
5e656dbb
BN
1007 args->inumber = be64_to_cpu(dep->inumber);
1008 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
a2ceac1f
DC
1009 xfs_trans_brelse(tp, dbp);
1010 xfs_trans_brelse(tp, lbp);
5e656dbb 1011 return XFS_ERROR(error);
2bd0ea18
NS
1012}
1013
1014/*
1015 * Look up name/hash in the leaf block.
1016 * Fill in indexp with the found index, and dbpp with the data buffer.
1017 * If not found dbpp will be NULL, and ENOENT comes back.
1018 * lbpp will always be filled in with the leaf buffer unless there's an error.
1019 */
5e656dbb 1020static int /* error */
2bd0ea18
NS
1021xfs_dir2_leaf_lookup_int(
1022 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1023 struct xfs_buf **lbpp, /* out: leaf buffer */
2bd0ea18 1024 int *indexp, /* out: index in leaf block */
a2ceac1f 1025 struct xfs_buf **dbpp) /* out: data buffer */
2bd0ea18 1026{
5e656dbb 1027 xfs_dir2_db_t curdb = -1; /* current data block number */
a2ceac1f 1028 struct xfs_buf *dbp = NULL; /* data buffer */
2bd0ea18
NS
1029 xfs_dir2_data_entry_t *dep; /* data entry */
1030 xfs_inode_t *dp; /* incore directory inode */
1031 int error; /* error return code */
1032 int index; /* index in leaf block */
a2ceac1f 1033 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1034 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1035 xfs_dir2_leaf_t *leaf; /* leaf structure */
1036 xfs_mount_t *mp; /* filesystem mount point */
1037 xfs_dir2_db_t newdb; /* new data block number */
1038 xfs_trans_t *tp; /* transaction pointer */
5e656dbb
BN
1039 xfs_dir2_db_t cidb = -1; /* case match data block no. */
1040 enum xfs_dacmp cmp; /* name compare result */
2bd0ea18
NS
1041
1042 dp = args->dp;
1043 tp = args->trans;
1044 mp = dp->i_mount;
a2ceac1f
DC
1045
1046 error = xfs_dir2_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
5e656dbb 1047 if (error)
2bd0ea18 1048 return error;
a2ceac1f 1049
2bd0ea18 1050 *lbpp = lbp;
a2ceac1f 1051 leaf = lbp->b_addr;
2bd0ea18
NS
1052 xfs_dir2_leaf_check(dp, lbp);
1053 /*
1054 * Look for the first leaf entry with our hash value.
1055 */
1056 index = xfs_dir2_leaf_search_hash(args, lbp);
1057 /*
1058 * Loop over all the entries with the right hash value
1059 * looking to match the name.
1060 */
5e656dbb
BN
1061 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
1062 be32_to_cpu(lep->hashval) == args->hashval;
1063 lep++, index++) {
2bd0ea18
NS
1064 /*
1065 * Skip over stale leaf entries.
1066 */
5e656dbb 1067 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
1068 continue;
1069 /*
1070 * Get the new data block number.
1071 */
5e656dbb 1072 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
2bd0ea18
NS
1073 /*
1074 * If it's not the same as the old data block number,
1075 * need to pitch the old one and read the new one.
1076 */
1077 if (newdb != curdb) {
1078 if (dbp)
a2ceac1f
DC
1079 xfs_trans_brelse(tp, dbp);
1080 error = xfs_dir2_data_read(tp, dp,
1081 xfs_dir2_db_to_da(mp, newdb),
1082 -1, &dbp);
5e656dbb 1083 if (error) {
a2ceac1f 1084 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1085 return error;
1086 }
2bd0ea18
NS
1087 curdb = newdb;
1088 }
1089 /*
1090 * Point to the data entry.
1091 */
a2ceac1f 1092 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
5e656dbb 1093 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
2bd0ea18 1094 /*
5e656dbb
BN
1095 * Compare name and if it's an exact match, return the index
1096 * and buffer. If it's the first case-insensitive match, store
1097 * the index and buffer and continue looking for an exact match.
2bd0ea18 1098 */
5e656dbb
BN
1099 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1100 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1101 args->cmpresult = cmp;
2bd0ea18 1102 *indexp = index;
5e656dbb
BN
1103 /* case exact match: return the current buffer. */
1104 if (cmp == XFS_CMP_EXACT) {
1105 *dbpp = dbp;
1106 return 0;
1107 }
1108 cidb = curdb;
2bd0ea18
NS
1109 }
1110 }
5e656dbb
BN
1111 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1112 /*
1113 * Here, we can only be doing a lookup (not a rename or remove).
1114 * If a case-insensitive match was found earlier, re-read the
1115 * appropriate data block if required and return it.
1116 */
1117 if (args->cmpresult == XFS_CMP_CASE) {
1118 ASSERT(cidb != -1);
1119 if (cidb != curdb) {
a2ceac1f
DC
1120 xfs_trans_brelse(tp, dbp);
1121 error = xfs_dir2_data_read(tp, dp,
1122 xfs_dir2_db_to_da(mp, cidb),
1123 -1, &dbp);
5e656dbb 1124 if (error) {
a2ceac1f 1125 xfs_trans_brelse(tp, lbp);
5e656dbb
BN
1126 return error;
1127 }
1128 }
1129 *dbpp = dbp;
1130 return 0;
1131 }
2bd0ea18
NS
1132 /*
1133 * No match found, return ENOENT.
1134 */
5e656dbb 1135 ASSERT(cidb == -1);
2bd0ea18 1136 if (dbp)
a2ceac1f
DC
1137 xfs_trans_brelse(tp, dbp);
1138 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1139 return XFS_ERROR(ENOENT);
1140}
1141
1142/*
1143 * Remove an entry from a leaf format directory.
1144 */
1145int /* error */
1146xfs_dir2_leaf_removename(
1147 xfs_da_args_t *args) /* operation arguments */
1148{
5e656dbb 1149 __be16 *bestsp; /* leaf block best freespace */
a2ceac1f 1150 xfs_dir2_data_hdr_t *hdr; /* data block header */
2bd0ea18 1151 xfs_dir2_db_t db; /* data block number */
a2ceac1f 1152 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1153 xfs_dir2_data_entry_t *dep; /* data entry structure */
1154 xfs_inode_t *dp; /* incore directory inode */
1155 int error; /* error return code */
1156 xfs_dir2_db_t i; /* temporary data block # */
1157 int index; /* index into leaf entries */
a2ceac1f 1158 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1159 xfs_dir2_leaf_t *leaf; /* leaf structure */
1160 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1161 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1162 xfs_mount_t *mp; /* filesystem mount point */
1163 int needlog; /* need to log data header */
1164 int needscan; /* need to rescan data frees */
1165 xfs_dir2_data_off_t oldbest; /* old value of best free */
1166 xfs_trans_t *tp; /* transaction pointer */
1167
56b2de80
DC
1168 trace_xfs_dir2_leaf_removename(args);
1169
2bd0ea18
NS
1170 /*
1171 * Lookup the leaf entry, get the leaf and data blocks read in.
1172 */
0e266570 1173 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
1174 return error;
1175 }
1176 dp = args->dp;
1177 tp = args->trans;
1178 mp = dp->i_mount;
a2ceac1f
DC
1179 leaf = lbp->b_addr;
1180 hdr = dbp->b_addr;
2bd0ea18
NS
1181 xfs_dir2_data_check(dp, dbp);
1182 /*
1183 * Point to the leaf entry, use that to point to the data entry.
1184 */
1185 lep = &leaf->ents[index];
5e656dbb 1186 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
2bd0ea18 1187 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1188 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
2bd0ea18 1189 needscan = needlog = 0;
a2ceac1f 1190 oldbest = be16_to_cpu(hdr->bestfree[0].length);
5e656dbb
BN
1191 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1192 bestsp = xfs_dir2_leaf_bests_p(ltp);
1193 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
2bd0ea18
NS
1194 /*
1195 * Mark the former data entry unused.
1196 */
1197 xfs_dir2_data_make_free(tp, dbp,
a2ceac1f 1198 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
5e656dbb 1199 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
2bd0ea18
NS
1200 /*
1201 * We just mark the leaf entry stale by putting a null in it.
1202 */
5e656dbb 1203 be16_add_cpu(&leaf->hdr.stale, 1);
2bd0ea18 1204 xfs_dir2_leaf_log_header(tp, lbp);
5e656dbb 1205 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
1206 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1207 /*
1208 * Scan the freespace in the data block again if necessary,
1209 * log the data block header if necessary.
1210 */
1211 if (needscan)
a2ceac1f 1212 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
1213 if (needlog)
1214 xfs_dir2_data_log_header(tp, dbp);
1215 /*
1216 * If the longest freespace in the data block has changed,
1217 * put the new value in the bests table and log that.
1218 */
a2ceac1f
DC
1219 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1220 bestsp[db] = hdr->bestfree[0].length;
2bd0ea18
NS
1221 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1222 }
1223 xfs_dir2_data_check(dp, dbp);
1224 /*
1225 * If the data block is now empty then get rid of the data block.
1226 */
a2ceac1f
DC
1227 if (be16_to_cpu(hdr->bestfree[0].length) ==
1228 mp->m_dirblksize - (uint)sizeof(*hdr)) {
2bd0ea18 1229 ASSERT(db != mp->m_dirdatablk);
0e266570 1230 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2bd0ea18
NS
1231 /*
1232 * Nope, can't get rid of it because it caused
1233 * allocation of a bmap btree block to do so.
1234 * Just go on, returning success, leaving the
1235 * empty block in place.
1236 */
a2ceac1f 1237 if (error == ENOSPC && args->total == 0)
2bd0ea18 1238 error = 0;
2bd0ea18 1239 xfs_dir2_leaf_check(dp, lbp);
2bd0ea18
NS
1240 return error;
1241 }
1242 dbp = NULL;
1243 /*
1244 * If this is the last data block then compact the
1245 * bests table by getting rid of entries.
1246 */
5e656dbb 1247 if (db == be32_to_cpu(ltp->bestcount) - 1) {
2bd0ea18
NS
1248 /*
1249 * Look for the last active entry (i).
1250 */
1251 for (i = db - 1; i > 0; i--) {
a2ceac1f 1252 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
2bd0ea18
NS
1253 break;
1254 }
1255 /*
1256 * Copy the table down so inactive entries at the
1257 * end are removed.
1258 */
32181a02 1259 memmove(&bestsp[db - i], bestsp,
5e656dbb
BN
1260 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1261 be32_add_cpu(&ltp->bestcount, -(db - i));
2bd0ea18 1262 xfs_dir2_leaf_log_tail(tp, lbp);
5e656dbb 1263 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18 1264 } else
5e656dbb 1265 bestsp[db] = cpu_to_be16(NULLDATAOFF);
2bd0ea18
NS
1266 }
1267 /*
1268 * If the data block was not the first one, drop it.
1269 */
a2ceac1f 1270 else if (db != mp->m_dirdatablk)
2bd0ea18 1271 dbp = NULL;
a2ceac1f 1272
2bd0ea18
NS
1273 xfs_dir2_leaf_check(dp, lbp);
1274 /*
1275 * See if we can convert to block form.
1276 */
1277 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1278}
1279
1280/*
1281 * Replace the inode number in a leaf format directory entry.
1282 */
1283int /* error */
1284xfs_dir2_leaf_replace(
1285 xfs_da_args_t *args) /* operation arguments */
1286{
a2ceac1f 1287 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1288 xfs_dir2_data_entry_t *dep; /* data block entry */
1289 xfs_inode_t *dp; /* incore directory inode */
1290 int error; /* error return code */
1291 int index; /* index of leaf entry */
a2ceac1f 1292 struct xfs_buf *lbp; /* leaf buffer */
2bd0ea18
NS
1293 xfs_dir2_leaf_t *leaf; /* leaf structure */
1294 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1295 xfs_trans_t *tp; /* transaction pointer */
1296
56b2de80
DC
1297 trace_xfs_dir2_leaf_replace(args);
1298
2bd0ea18
NS
1299 /*
1300 * Look up the entry.
1301 */
0e266570 1302 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
2bd0ea18
NS
1303 return error;
1304 }
1305 dp = args->dp;
a2ceac1f 1306 leaf = lbp->b_addr;
2bd0ea18
NS
1307 /*
1308 * Point to the leaf entry, get data address from it.
1309 */
1310 lep = &leaf->ents[index];
1311 /*
1312 * Point to the data entry.
1313 */
1314 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 1315 ((char *)dbp->b_addr +
5e656dbb
BN
1316 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1317 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
2bd0ea18
NS
1318 /*
1319 * Put the new inode number in, log it.
1320 */
5e656dbb 1321 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18
NS
1322 tp = args->trans;
1323 xfs_dir2_data_log_entry(tp, dbp, dep);
2bd0ea18 1324 xfs_dir2_leaf_check(dp, lbp);
a2ceac1f 1325 xfs_trans_brelse(tp, lbp);
2bd0ea18
NS
1326 return 0;
1327}
1328
1329/*
1330 * Return index in the leaf block (lbp) which is either the first
1331 * one with this hash value, or if there are none, the insert point
1332 * for that hash value.
1333 */
1334int /* index value */
1335xfs_dir2_leaf_search_hash(
1336 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1337 struct xfs_buf *lbp) /* leaf buffer */
2bd0ea18 1338{
0e266570 1339 xfs_dahash_t hash=0; /* hash from this entry */
2bd0ea18
NS
1340 xfs_dahash_t hashwant; /* hash value looking for */
1341 int high; /* high leaf index */
1342 int low; /* low leaf index */
1343 xfs_dir2_leaf_t *leaf; /* leaf structure */
1344 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
0e266570 1345 int mid=0; /* current leaf index */
2bd0ea18 1346
a2ceac1f 1347 leaf = lbp->b_addr;
2bd0ea18 1348#ifndef __KERNEL__
46eca962 1349 if (!leaf->hdr.count)
2bd0ea18
NS
1350 return 0;
1351#endif
1352 /*
1353 * Note, the table cannot be empty, so we have to go through the loop.
1354 * Binary search the leaf entries looking for our hash value.
1355 */
5e656dbb 1356 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
2bd0ea18
NS
1357 hashwant = args->hashval;
1358 low <= high; ) {
1359 mid = (low + high) >> 1;
5e656dbb 1360 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
2bd0ea18
NS
1361 break;
1362 if (hash < hashwant)
1363 low = mid + 1;
1364 else
1365 high = mid - 1;
1366 }
1367 /*
1368 * Found one, back up through all the equal hash values.
1369 */
1370 if (hash == hashwant) {
5e656dbb 1371 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
2bd0ea18
NS
1372 mid--;
1373 }
1374 }
1375 /*
1376 * Need to point to an entry higher than ours.
1377 */
1378 else if (hash < hashwant)
1379 mid++;
1380 return mid;
1381}
1382
1383/*
1384 * Trim off a trailing data block. We know it's empty since the leaf
1385 * freespace table says so.
1386 */
1387int /* error */
1388xfs_dir2_leaf_trim_data(
1389 xfs_da_args_t *args, /* operation arguments */
a2ceac1f 1390 struct xfs_buf *lbp, /* leaf buffer */
2bd0ea18
NS
1391 xfs_dir2_db_t db) /* data block number */
1392{
5e656dbb 1393 __be16 *bestsp; /* leaf bests table */
a2ceac1f 1394 struct xfs_buf *dbp; /* data block buffer */
2bd0ea18
NS
1395 xfs_inode_t *dp; /* incore directory inode */
1396 int error; /* error return value */
1397 xfs_dir2_leaf_t *leaf; /* leaf structure */
1398 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1399 xfs_mount_t *mp; /* filesystem mount point */
1400 xfs_trans_t *tp; /* transaction pointer */
1401
1402 dp = args->dp;
1403 mp = dp->i_mount;
1404 tp = args->trans;
1405 /*
1406 * Read the offending data block. We need its buffer.
1407 */
a2ceac1f
DC
1408 error = xfs_dir2_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
1409 if (error)
2bd0ea18 1410 return error;
2bd0ea18 1411
a2ceac1f 1412 leaf = lbp->b_addr;
5e656dbb 1413 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
a2ceac1f
DC
1414
1415#ifdef DEBUG
1416{
1417 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1418
1419 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
1420 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1421 mp->m_dirblksize - (uint)sizeof(*hdr));
5e656dbb 1422 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
a2ceac1f
DC
1423}
1424#endif
1425
2bd0ea18
NS
1426 /*
1427 * Get rid of the data block.
1428 */
0e266570 1429 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2bd0ea18 1430 ASSERT(error != ENOSPC);
a2ceac1f 1431 xfs_trans_brelse(tp, dbp);
2bd0ea18
NS
1432 return error;
1433 }
1434 /*
1435 * Eliminate the last bests entry from the table.
1436 */
5e656dbb
BN
1437 bestsp = xfs_dir2_leaf_bests_p(ltp);
1438 be32_add_cpu(&ltp->bestcount, -1);
1439 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
2bd0ea18 1440 xfs_dir2_leaf_log_tail(tp, lbp);
5e656dbb 1441 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18
NS
1442 return 0;
1443}
1444
a2ceac1f
DC
1445static inline size_t
1446xfs_dir2_leaf_size(
1447 struct xfs_dir2_leaf_hdr *hdr,
1448 int counts)
1449{
1450 int entries;
1451
1452 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1453 return sizeof(xfs_dir2_leaf_hdr_t) +
1454 entries * sizeof(xfs_dir2_leaf_entry_t) +
1455 counts * sizeof(xfs_dir2_data_off_t) +
1456 sizeof(xfs_dir2_leaf_tail_t);
1457}
1458
2bd0ea18
NS
1459/*
1460 * Convert node form directory to leaf form directory.
1461 * The root of the node form dir needs to already be a LEAFN block.
1462 * Just return if we can't do anything.
1463 */
1464int /* error */
1465xfs_dir2_node_to_leaf(
1466 xfs_da_state_t *state) /* directory operation state */
1467{
1468 xfs_da_args_t *args; /* operation arguments */
1469 xfs_inode_t *dp; /* incore directory inode */
1470 int error; /* error return code */
a2ceac1f 1471 struct xfs_buf *fbp; /* buffer for freespace block */
2bd0ea18
NS
1472 xfs_fileoff_t fo; /* freespace file offset */
1473 xfs_dir2_free_t *free; /* freespace structure */
a2ceac1f 1474 struct xfs_buf *lbp; /* buffer for leaf block */
2bd0ea18
NS
1475 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1476 xfs_dir2_leaf_t *leaf; /* leaf structure */
1477 xfs_mount_t *mp; /* filesystem mount point */
1478 int rval; /* successful free trim? */
1479 xfs_trans_t *tp; /* transaction pointer */
1480
1481 /*
1482 * There's more than a leaf level in the btree, so there must
1483 * be multiple leafn blocks. Give up.
1484 */
1485 if (state->path.active > 1)
1486 return 0;
1487 args = state->args;
56b2de80
DC
1488
1489 trace_xfs_dir2_node_to_leaf(args);
1490
2bd0ea18
NS
1491 mp = state->mp;
1492 dp = args->dp;
1493 tp = args->trans;
1494 /*
1495 * Get the last offset in the file.
1496 */
0e266570 1497 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
2bd0ea18
NS
1498 return error;
1499 }
1500 fo -= mp->m_dirblkfsbs;
1501 /*
1502 * If there are freespace blocks other than the first one,
1503 * take this opportunity to remove trailing empty freespace blocks
1504 * that may have been left behind during no-space-reservation
1505 * operations.
1506 */
1507 while (fo > mp->m_dirfreeblk) {
0e266570 1508 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
2bd0ea18
NS
1509 return error;
1510 }
1511 if (rval)
1512 fo -= mp->m_dirblkfsbs;
1513 else
1514 return 0;
1515 }
1516 /*
1517 * Now find the block just before the freespace block.
1518 */
0e266570 1519 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
2bd0ea18
NS
1520 return error;
1521 }
1522 /*
1523 * If it's not the single leaf block, give up.
1524 */
1525 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1526 return 0;
1527 lbp = state->path.blk[0].bp;
a2ceac1f
DC
1528 leaf = lbp->b_addr;
1529 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
2bd0ea18
NS
1530 /*
1531 * Read the freespace block.
1532 */
a2ceac1f
DC
1533 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
1534 if (error)
2bd0ea18 1535 return error;
a2ceac1f
DC
1536 free = fbp->b_addr;
1537 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
46eca962 1538 ASSERT(!free->hdr.firstdb);
a2ceac1f 1539
2bd0ea18
NS
1540 /*
1541 * Now see if the leafn and free data will fit in a leaf1.
1542 * If not, release the buffer and give up.
1543 */
a2ceac1f
DC
1544 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1545 mp->m_dirblksize) {
1546 xfs_trans_brelse(tp, fbp);
2bd0ea18
NS
1547 return 0;
1548 }
a2ceac1f 1549
2bd0ea18
NS
1550 /*
1551 * If the leaf has any stale entries in it, compress them out.
1552 * The compact routine will log the header.
1553 */
5e656dbb 1554 if (be16_to_cpu(leaf->hdr.stale))
2bd0ea18
NS
1555 xfs_dir2_leaf_compact(args, lbp);
1556 else
1557 xfs_dir2_leaf_log_header(tp, lbp);
a2ceac1f
DC
1558
1559 lbp->b_ops = &xfs_dir2_leaf1_buf_ops;
5e656dbb 1560 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
a2ceac1f 1561
2bd0ea18
NS
1562 /*
1563 * Set up the leaf tail from the freespace block.
1564 */
5e656dbb
BN
1565 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1566 ltp->bestcount = free->hdr.nvalid;
2bd0ea18
NS
1567 /*
1568 * Set up the leaf bests table.
1569 */
5e656dbb 1570 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
a2ceac1f 1571 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
5e656dbb 1572 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2bd0ea18
NS
1573 xfs_dir2_leaf_log_tail(tp, lbp);
1574 xfs_dir2_leaf_check(dp, lbp);
1575 /*
1576 * Get rid of the freespace block.
1577 */
1578 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1579 if (error) {
2bd0ea18
NS
1580 /*
1581 * This can't fail here because it can only happen when
1582 * punching out the middle of an extent, and this is an
1583 * isolated block.
1584 */
1585 ASSERT(error != ENOSPC);
1586 return error;
1587 }
1588 fbp = NULL;
1589 /*
1590 * Now see if we can convert the single-leaf directory
1591 * down to a block form directory.
1592 * This routine always kills the dabuf for the leaf, so
1593 * eliminate it from the path.
1594 */
1595 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1596 state->path.blk[0].bp = NULL;
1597 return error;
1598}