]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_block.c
libxlog: sync up with 2.6.38 kernel code
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_block.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 prototypes.
2bd0ea18 23 */
5e656dbb
BN
24static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
25 int last);
26static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
27static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
28 int *entno);
29static int xfs_dir2_block_sort(const void *a, const void *b);
2bd0ea18 30
5e656dbb
BN
31static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
32
33/*
34 * One-time startup routine called from xfs_init().
35 */
36void
37xfs_dir_startup(void)
38{
39 xfs_dir_hash_dot = xfs_da_hashname(".", 1);
40 xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
41}
2bd0ea18
NS
42
43/*
44 * Add an entry to a block directory.
45 */
46int /* error */
47xfs_dir2_block_addname(
48 xfs_da_args_t *args) /* directory op arguments */
49{
50 xfs_dir2_data_free_t *bf; /* bestfree table in block */
51 xfs_dir2_block_t *block; /* directory block structure */
52 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
53 xfs_dabuf_t *bp; /* buffer for block */
54 xfs_dir2_block_tail_t *btp; /* block tail */
55 int compact; /* need to compact leaf ents */
56 xfs_dir2_data_entry_t *dep; /* block data entry */
57 xfs_inode_t *dp; /* directory inode */
58 xfs_dir2_data_unused_t *dup; /* block unused entry */
59 int error; /* error return value */
0e266570 60 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
2bd0ea18
NS
61 xfs_dahash_t hash; /* hash value of found entry */
62 int high; /* high index for binary srch */
63 int highstale; /* high stale index */
0e266570
NS
64 int lfloghigh=0; /* last final leaf to log */
65 int lfloglow=0; /* first final leaf to log */
2bd0ea18
NS
66 int len; /* length of the new entry */
67 int low; /* low index for binary srch */
68 int lowstale; /* low stale index */
0e266570 69 int mid=0; /* midpoint for binary srch */
2bd0ea18
NS
70 xfs_mount_t *mp; /* filesystem mount point */
71 int needlog; /* need to log header */
72 int needscan; /* need to rescan freespace */
5e656dbb 73 __be16 *tagp; /* pointer to tag value */
2bd0ea18
NS
74 xfs_trans_t *tp; /* transaction structure */
75
76 xfs_dir2_trace_args("block_addname", args);
77 dp = args->dp;
78 tp = args->trans;
79 mp = dp->i_mount;
80 /*
81 * Read the (one and only) directory block into dabuf bp.
82 */
0e266570
NS
83 if ((error =
84 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
2bd0ea18
NS
85 return error;
86 }
87 ASSERT(bp != NULL);
88 block = bp->data;
89 /*
90 * Check the magic number, corrupted if wrong.
91 */
5e656dbb 92 if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
4ca431fc
NS
93 XFS_CORRUPTION_ERROR("xfs_dir2_block_addname",
94 XFS_ERRLEVEL_LOW, mp, block);
2bd0ea18
NS
95 xfs_da_brelse(tp, bp);
96 return XFS_ERROR(EFSCORRUPTED);
97 }
5e656dbb 98 len = xfs_dir2_data_entsize(args->namelen);
2bd0ea18
NS
99 /*
100 * Set up pointers to parts of the block.
101 */
102 bf = block->hdr.bestfree;
5e656dbb
BN
103 btp = xfs_dir2_block_tail_p(mp, block);
104 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
105 /*
106 * No stale entries? Need space for entry and new leaf.
107 */
46eca962 108 if (!btp->stale) {
2bd0ea18
NS
109 /*
110 * Tag just before the first leaf entry.
111 */
5e656dbb 112 tagp = (__be16 *)blp - 1;
2bd0ea18
NS
113 /*
114 * Data object just before the first leaf entry.
115 */
5e656dbb 116 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
2bd0ea18
NS
117 /*
118 * If it's not free then can't do this add without cleaning up:
119 * the space before the first leaf entry needs to be free so it
120 * can be expanded to hold the pointer to the new entry.
121 */
5e656dbb 122 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
2bd0ea18
NS
123 dup = enddup = NULL;
124 /*
125 * Check out the biggest freespace and see if it's the same one.
126 */
127 else {
128 dup = (xfs_dir2_data_unused_t *)
5e656dbb 129 ((char *)block + be16_to_cpu(bf[0].offset));
2bd0ea18
NS
130 if (dup == enddup) {
131 /*
132 * It is the biggest freespace, is it too small
133 * to hold the new leaf too?
134 */
5e656dbb 135 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
2bd0ea18
NS
136 /*
137 * Yes, we use the second-largest
138 * entry instead if it works.
139 */
5e656dbb 140 if (be16_to_cpu(bf[1].length) >= len)
2bd0ea18
NS
141 dup = (xfs_dir2_data_unused_t *)
142 ((char *)block +
5e656dbb 143 be16_to_cpu(bf[1].offset));
2bd0ea18
NS
144 else
145 dup = NULL;
146 }
147 } else {
148 /*
149 * Not the same free entry,
150 * just check its length.
151 */
5e656dbb 152 if (be16_to_cpu(dup->length) < len) {
2bd0ea18
NS
153 dup = NULL;
154 }
155 }
156 }
157 compact = 0;
158 }
159 /*
160 * If there are stale entries we'll use one for the leaf.
161 * Is the biggest entry enough to avoid compaction?
162 */
5e656dbb 163 else if (be16_to_cpu(bf[0].length) >= len) {
2bd0ea18 164 dup = (xfs_dir2_data_unused_t *)
5e656dbb 165 ((char *)block + be16_to_cpu(bf[0].offset));
2bd0ea18
NS
166 compact = 0;
167 }
168 /*
169 * Will need to compact to make this work.
170 */
171 else {
2bd0ea18
NS
172 /*
173 * Tag just before the first leaf entry.
174 */
5e656dbb 175 tagp = (__be16 *)blp - 1;
2bd0ea18
NS
176 /*
177 * Data object just before the first leaf entry.
178 */
5e656dbb 179 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
2bd0ea18
NS
180 /*
181 * If it's not free then the data will go where the
182 * leaf data starts now, if it works at all.
183 */
5e656dbb
BN
184 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
185 if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
2bd0ea18
NS
186 (uint)sizeof(*blp) < len)
187 dup = NULL;
5e656dbb 188 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
2bd0ea18
NS
189 dup = NULL;
190 else
191 dup = (xfs_dir2_data_unused_t *)blp;
192 compact = 1;
193 }
194 /*
195 * If this isn't a real add, we're done with the buffer.
196 */
5e656dbb 197 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
2bd0ea18
NS
198 xfs_da_brelse(tp, bp);
199 /*
200 * If we don't have space for the new entry & leaf ...
201 */
202 if (!dup) {
2bd0ea18
NS
203 /*
204 * Not trying to actually do anything, or don't have
205 * a space reservation: return no-space.
206 */
5e656dbb 207 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
2bd0ea18
NS
208 return XFS_ERROR(ENOSPC);
209 /*
210 * Convert to the next larger format.
211 * Then add the new entry in that format.
212 */
213 error = xfs_dir2_block_to_leaf(args, bp);
214 xfs_da_buf_done(bp);
215 if (error)
216 return error;
217 return xfs_dir2_leaf_addname(args);
218 }
219 /*
220 * Just checking, and it would work, so say so.
221 */
5e656dbb 222 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
2bd0ea18
NS
223 return 0;
224 needlog = needscan = 0;
225 /*
226 * If need to compact the leaf entries, do it now.
227 * Leave the highest-numbered stale entry stale.
228 * XXX should be the one closest to mid but mid is not yet computed.
229 */
230 if (compact) {
2bd0ea18
NS
231 int fromidx; /* source leaf index */
232 int toidx; /* target leaf index */
233
5e656dbb 234 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
2bd0ea18
NS
235 highstale = lfloghigh = -1;
236 fromidx >= 0;
237 fromidx--) {
5e656dbb 238 if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
2bd0ea18
NS
239 if (highstale == -1)
240 highstale = toidx;
241 else {
242 if (lfloghigh == -1)
243 lfloghigh = toidx;
244 continue;
245 }
246 }
247 if (fromidx < toidx)
248 blp[toidx] = blp[fromidx];
249 toidx--;
250 }
5e656dbb
BN
251 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
252 lfloghigh -= be32_to_cpu(btp->stale) - 1;
253 be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1));
2bd0ea18
NS
254 xfs_dir2_data_make_free(tp, bp,
255 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
5e656dbb 256 (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
2bd0ea18 257 &needlog, &needscan);
5e656dbb
BN
258 blp += be32_to_cpu(btp->stale) - 1;
259 btp->stale = cpu_to_be32(1);
2bd0ea18
NS
260 /*
261 * If we now need to rebuild the bestfree map, do so.
262 * This needs to happen before the next call to use_free.
263 */
264 if (needscan) {
5e656dbb 265 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
2bd0ea18
NS
266 needscan = 0;
267 }
268 }
269 /*
270 * Set leaf logging boundaries to impossible state.
271 * For the no-stale case they're set explicitly.
272 */
5e656dbb
BN
273 else if (btp->stale) {
274 lfloglow = be32_to_cpu(btp->count);
2bd0ea18
NS
275 lfloghigh = -1;
276 }
277 /*
278 * Find the slot that's first lower than our hash value, -1 if none.
279 */
5e656dbb 280 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
2bd0ea18 281 mid = (low + high) >> 1;
5e656dbb 282 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
283 break;
284 if (hash < args->hashval)
285 low = mid + 1;
286 else
287 high = mid - 1;
288 }
5e656dbb 289 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
2bd0ea18
NS
290 mid--;
291 }
292 /*
293 * No stale entries, will use enddup space to hold new leaf.
294 */
46eca962 295 if (!btp->stale) {
2bd0ea18
NS
296 /*
297 * Mark the space needed for the new leaf entry, now in use.
298 */
299 xfs_dir2_data_use_free(tp, bp, enddup,
300 (xfs_dir2_data_aoff_t)
5e656dbb 301 ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
2bd0ea18
NS
302 sizeof(*blp)),
303 (xfs_dir2_data_aoff_t)sizeof(*blp),
304 &needlog, &needscan);
305 /*
306 * Update the tail (entry count).
307 */
5e656dbb 308 be32_add_cpu(&btp->count, 1);
2bd0ea18
NS
309 /*
310 * If we now need to rebuild the bestfree map, do so.
311 * This needs to happen before the next call to use_free.
312 */
313 if (needscan) {
314 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
5e656dbb 315 &needlog);
2bd0ea18
NS
316 needscan = 0;
317 }
318 /*
319 * Adjust pointer to the first leaf entry, we're about to move
320 * the table up one to open up space for the new leaf entry.
321 * Then adjust our index to match.
322 */
323 blp--;
324 mid++;
325 if (mid)
32181a02 326 memmove(blp, &blp[1], mid * sizeof(*blp));
2bd0ea18
NS
327 lfloglow = 0;
328 lfloghigh = mid;
329 }
330 /*
331 * Use a stale leaf for our new entry.
332 */
333 else {
334 for (lowstale = mid;
335 lowstale >= 0 &&
5e656dbb 336 be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
2bd0ea18
NS
337 lowstale--)
338 continue;
339 for (highstale = mid + 1;
5e656dbb
BN
340 highstale < be32_to_cpu(btp->count) &&
341 be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
2bd0ea18
NS
342 (lowstale < 0 || mid - lowstale > highstale - mid);
343 highstale++)
344 continue;
345 /*
346 * Move entries toward the low-numbered stale entry.
347 */
348 if (lowstale >= 0 &&
5e656dbb 349 (highstale == be32_to_cpu(btp->count) ||
2bd0ea18
NS
350 mid - lowstale <= highstale - mid)) {
351 if (mid - lowstale)
32181a02 352 memmove(&blp[lowstale], &blp[lowstale + 1],
2bd0ea18
NS
353 (mid - lowstale) * sizeof(*blp));
354 lfloglow = MIN(lowstale, lfloglow);
355 lfloghigh = MAX(mid, lfloghigh);
356 }
357 /*
358 * Move entries toward the high-numbered stale entry.
359 */
360 else {
5e656dbb 361 ASSERT(highstale < be32_to_cpu(btp->count));
2bd0ea18
NS
362 mid++;
363 if (highstale - mid)
32181a02 364 memmove(&blp[mid + 1], &blp[mid],
2bd0ea18
NS
365 (highstale - mid) * sizeof(*blp));
366 lfloglow = MIN(mid, lfloglow);
367 lfloghigh = MAX(highstale, lfloghigh);
368 }
5e656dbb 369 be32_add_cpu(&btp->stale, -1);
2bd0ea18
NS
370 }
371 /*
372 * Point to the new data entry.
373 */
374 dep = (xfs_dir2_data_entry_t *)dup;
375 /*
376 * Fill in the leaf entry.
377 */
5e656dbb
BN
378 blp[mid].hashval = cpu_to_be32(args->hashval);
379 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
380 (char *)dep - (char *)block));
2bd0ea18
NS
381 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
382 /*
383 * Mark space for the data entry used.
384 */
385 xfs_dir2_data_use_free(tp, bp, dup,
386 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
387 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
388 /*
389 * Create the new data entry.
390 */
5e656dbb 391 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18 392 dep->namelen = args->namelen;
32181a02 393 memcpy(dep->name, args->name, args->namelen);
5e656dbb
BN
394 tagp = xfs_dir2_data_entry_tag_p(dep);
395 *tagp = cpu_to_be16((char *)dep - (char *)block);
2bd0ea18
NS
396 /*
397 * Clean up the bestfree array and log the header, tail, and entry.
398 */
399 if (needscan)
5e656dbb 400 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
2bd0ea18
NS
401 if (needlog)
402 xfs_dir2_data_log_header(tp, bp);
403 xfs_dir2_block_log_tail(tp, bp);
404 xfs_dir2_data_log_entry(tp, bp, dep);
405 xfs_dir2_data_check(dp, bp);
406 xfs_da_buf_done(bp);
407 return 0;
408}
409
410/*
411 * Log leaf entries from the block.
412 */
5e656dbb 413static void
2bd0ea18
NS
414xfs_dir2_block_log_leaf(
415 xfs_trans_t *tp, /* transaction structure */
416 xfs_dabuf_t *bp, /* block buffer */
417 int first, /* index of first logged leaf */
418 int last) /* index of last logged leaf */
419{
420 xfs_dir2_block_t *block; /* directory block structure */
421 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
422 xfs_dir2_block_tail_t *btp; /* block tail */
423 xfs_mount_t *mp; /* filesystem mount point */
424
425 mp = tp->t_mountp;
426 block = bp->data;
5e656dbb
BN
427 btp = xfs_dir2_block_tail_p(mp, block);
428 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
429 xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
430 (uint)((char *)&blp[last + 1] - (char *)block - 1));
431}
432
433/*
434 * Log the block tail.
435 */
5e656dbb 436static void
2bd0ea18
NS
437xfs_dir2_block_log_tail(
438 xfs_trans_t *tp, /* transaction structure */
439 xfs_dabuf_t *bp) /* block buffer */
440{
441 xfs_dir2_block_t *block; /* directory block structure */
442 xfs_dir2_block_tail_t *btp; /* block tail */
443 xfs_mount_t *mp; /* filesystem mount point */
444
445 mp = tp->t_mountp;
446 block = bp->data;
5e656dbb 447 btp = xfs_dir2_block_tail_p(mp, block);
2bd0ea18
NS
448 xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
449 (uint)((char *)(btp + 1) - (char *)block - 1));
450}
451
452/*
453 * Look up an entry in the block. This is the external routine,
454 * xfs_dir2_block_lookup_int does the real work.
455 */
456int /* error */
457xfs_dir2_block_lookup(
458 xfs_da_args_t *args) /* dir lookup arguments */
459{
460 xfs_dir2_block_t *block; /* block structure */
461 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
462 xfs_dabuf_t *bp; /* block buffer */
463 xfs_dir2_block_tail_t *btp; /* block tail */
464 xfs_dir2_data_entry_t *dep; /* block data entry */
465 xfs_inode_t *dp; /* incore inode */
466 int ent; /* entry index */
467 int error; /* error return value */
468 xfs_mount_t *mp; /* filesystem mount point */
469
470 xfs_dir2_trace_args("block_lookup", args);
471 /*
472 * Get the buffer, look up the entry.
473 * If not found (ENOENT) then return, have no buffer.
474 */
0e266570 475 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
2bd0ea18
NS
476 return error;
477 dp = args->dp;
478 mp = dp->i_mount;
479 block = bp->data;
480 xfs_dir2_data_check(dp, bp);
5e656dbb
BN
481 btp = xfs_dir2_block_tail_p(mp, block);
482 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
483 /*
484 * Get the offset from the leaf entry, to point to the data.
485 */
5e656dbb
BN
486 dep = (xfs_dir2_data_entry_t *)((char *)block +
487 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
2bd0ea18 488 /*
5e656dbb 489 * Fill in inode number, CI name if appropriate, release the block.
2bd0ea18 490 */
5e656dbb
BN
491 args->inumber = be64_to_cpu(dep->inumber);
492 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2bd0ea18 493 xfs_da_brelse(args->trans, bp);
5e656dbb 494 return XFS_ERROR(error);
2bd0ea18
NS
495}
496
497/*
498 * Internal block lookup routine.
499 */
5e656dbb 500static int /* error */
2bd0ea18
NS
501xfs_dir2_block_lookup_int(
502 xfs_da_args_t *args, /* dir lookup arguments */
503 xfs_dabuf_t **bpp, /* returned block buffer */
504 int *entno) /* returned entry number */
505{
506 xfs_dir2_dataptr_t addr; /* data entry address */
507 xfs_dir2_block_t *block; /* block structure */
508 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
509 xfs_dabuf_t *bp; /* block buffer */
510 xfs_dir2_block_tail_t *btp; /* block tail */
511 xfs_dir2_data_entry_t *dep; /* block data entry */
512 xfs_inode_t *dp; /* incore inode */
513 int error; /* error return value */
514 xfs_dahash_t hash; /* found hash value */
515 int high; /* binary search high index */
516 int low; /* binary search low index */
517 int mid; /* binary search current idx */
518 xfs_mount_t *mp; /* filesystem mount point */
519 xfs_trans_t *tp; /* transaction pointer */
5e656dbb 520 enum xfs_dacmp cmp; /* comparison result */
2bd0ea18
NS
521
522 dp = args->dp;
523 tp = args->trans;
524 mp = dp->i_mount;
525 /*
526 * Read the buffer, return error if we can't get it.
527 */
0e266570
NS
528 if ((error =
529 xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
2bd0ea18
NS
530 return error;
531 }
532 ASSERT(bp != NULL);
533 block = bp->data;
534 xfs_dir2_data_check(dp, bp);
5e656dbb
BN
535 btp = xfs_dir2_block_tail_p(mp, block);
536 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
537 /*
538 * Loop doing a binary search for our hash value.
539 * Find our entry, ENOENT if it's not there.
540 */
5e656dbb 541 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
2bd0ea18
NS
542 ASSERT(low <= high);
543 mid = (low + high) >> 1;
5e656dbb 544 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
545 break;
546 if (hash < args->hashval)
547 low = mid + 1;
548 else
549 high = mid - 1;
550 if (low > high) {
5e656dbb 551 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
2bd0ea18
NS
552 xfs_da_brelse(tp, bp);
553 return XFS_ERROR(ENOENT);
554 }
555 }
556 /*
557 * Back up to the first one with the right hash value.
558 */
5e656dbb 559 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
2bd0ea18
NS
560 mid--;
561 }
562 /*
563 * Now loop forward through all the entries with the
564 * right hash value looking for our name.
565 */
566 do {
5e656dbb 567 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
568 continue;
569 /*
570 * Get pointer to the entry from the leaf.
571 */
572 dep = (xfs_dir2_data_entry_t *)
5e656dbb 573 ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
2bd0ea18 574 /*
5e656dbb
BN
575 * Compare name and if it's an exact match, return the index
576 * and buffer. If it's the first case-insensitive match, store
577 * the index and buffer and continue looking for an exact match.
2bd0ea18 578 */
5e656dbb
BN
579 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
580 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
581 args->cmpresult = cmp;
2bd0ea18
NS
582 *bpp = bp;
583 *entno = mid;
5e656dbb
BN
584 if (cmp == XFS_CMP_EXACT)
585 return 0;
2bd0ea18 586 }
5e656dbb
BN
587 } while (++mid < be32_to_cpu(btp->count) &&
588 be32_to_cpu(blp[mid].hashval) == hash);
589
590 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
591 /*
592 * Here, we can only be doing a lookup (not a rename or replace).
593 * If a case-insensitive match was found earlier, return success.
594 */
595 if (args->cmpresult == XFS_CMP_CASE)
596 return 0;
2bd0ea18
NS
597 /*
598 * No match, release the buffer and return ENOENT.
599 */
2bd0ea18
NS
600 xfs_da_brelse(tp, bp);
601 return XFS_ERROR(ENOENT);
602}
603
604/*
605 * Remove an entry from a block format directory.
606 * If that makes the block small enough to fit in shortform, transform it.
607 */
608int /* error */
609xfs_dir2_block_removename(
610 xfs_da_args_t *args) /* directory operation args */
611{
612 xfs_dir2_block_t *block; /* block structure */
613 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
614 xfs_dabuf_t *bp; /* block buffer */
615 xfs_dir2_block_tail_t *btp; /* block tail */
616 xfs_dir2_data_entry_t *dep; /* block data entry */
617 xfs_inode_t *dp; /* incore inode */
618 int ent; /* block leaf entry index */
619 int error; /* error return value */
620 xfs_mount_t *mp; /* filesystem mount point */
621 int needlog; /* need to log block header */
622 int needscan; /* need to fixup bestfree */
623 xfs_dir2_sf_hdr_t sfh; /* shortform header */
624 int size; /* shortform size */
625 xfs_trans_t *tp; /* transaction pointer */
626
627 xfs_dir2_trace_args("block_removename", args);
628 /*
629 * Look up the entry in the block. Gets the buffer and entry index.
630 * It will always be there, the vnodeops level does a lookup first.
631 */
0e266570 632 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
633 return error;
634 }
635 dp = args->dp;
636 tp = args->trans;
637 mp = dp->i_mount;
638 block = bp->data;
5e656dbb
BN
639 btp = xfs_dir2_block_tail_p(mp, block);
640 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
641 /*
642 * Point to the data entry using the leaf entry.
643 */
644 dep = (xfs_dir2_data_entry_t *)
5e656dbb 645 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
2bd0ea18
NS
646 /*
647 * Mark the data entry's space free.
648 */
649 needlog = needscan = 0;
650 xfs_dir2_data_make_free(tp, bp,
651 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
5e656dbb 652 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
2bd0ea18
NS
653 /*
654 * Fix up the block tail.
655 */
5e656dbb 656 be32_add_cpu(&btp->stale, 1);
2bd0ea18
NS
657 xfs_dir2_block_log_tail(tp, bp);
658 /*
659 * Remove the leaf entry by marking it stale.
660 */
5e656dbb 661 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
662 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
663 /*
664 * Fix up bestfree, log the header if necessary.
665 */
666 if (needscan)
5e656dbb 667 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
2bd0ea18
NS
668 if (needlog)
669 xfs_dir2_data_log_header(tp, bp);
670 xfs_dir2_data_check(dp, bp);
671 /*
672 * See if the size as a shortform is good enough.
673 */
674 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
675 XFS_IFORK_DSIZE(dp)) {
676 xfs_da_buf_done(bp);
677 return 0;
678 }
679 /*
680 * If it works, do the conversion.
681 */
682 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
683}
684
685/*
686 * Replace an entry in a V2 block directory.
687 * Change the inode number to the new value.
688 */
689int /* error */
690xfs_dir2_block_replace(
691 xfs_da_args_t *args) /* directory operation args */
692{
693 xfs_dir2_block_t *block; /* block structure */
694 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
695 xfs_dabuf_t *bp; /* block buffer */
696 xfs_dir2_block_tail_t *btp; /* block tail */
697 xfs_dir2_data_entry_t *dep; /* block data entry */
698 xfs_inode_t *dp; /* incore inode */
699 int ent; /* leaf entry index */
700 int error; /* error return value */
701 xfs_mount_t *mp; /* filesystem mount point */
702
703 xfs_dir2_trace_args("block_replace", args);
704 /*
705 * Lookup the entry in the directory. Get buffer and entry index.
706 * This will always succeed since the caller has already done a lookup.
707 */
0e266570 708 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
709 return error;
710 }
711 dp = args->dp;
712 mp = dp->i_mount;
713 block = bp->data;
5e656dbb
BN
714 btp = xfs_dir2_block_tail_p(mp, block);
715 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
716 /*
717 * Point to the data entry we need to change.
718 */
719 dep = (xfs_dir2_data_entry_t *)
5e656dbb
BN
720 ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
721 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
2bd0ea18
NS
722 /*
723 * Change the inode number to the new value.
724 */
5e656dbb 725 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18
NS
726 xfs_dir2_data_log_entry(args->trans, bp, dep);
727 xfs_dir2_data_check(dp, bp);
728 xfs_da_buf_done(bp);
729 return 0;
730}
731
732/*
733 * Qsort comparison routine for the block leaf entries.
734 */
735static int /* sort order */
736xfs_dir2_block_sort(
737 const void *a, /* first leaf entry */
738 const void *b) /* second leaf entry */
739{
740 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
741 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
742
743 la = a;
744 lb = b;
5e656dbb
BN
745 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
746 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
2bd0ea18
NS
747}
748
749/*
750 * Convert a V2 leaf directory to a V2 block directory if possible.
751 */
752int /* error */
753xfs_dir2_leaf_to_block(
754 xfs_da_args_t *args, /* operation arguments */
755 xfs_dabuf_t *lbp, /* leaf buffer */
756 xfs_dabuf_t *dbp) /* data buffer */
757{
5e656dbb 758 __be16 *bestsp; /* leaf bests table */
2bd0ea18
NS
759 xfs_dir2_block_t *block; /* block structure */
760 xfs_dir2_block_tail_t *btp; /* block tail */
761 xfs_inode_t *dp; /* incore directory inode */
762 xfs_dir2_data_unused_t *dup; /* unused data entry */
763 int error; /* error return value */
764 int from; /* leaf from index */
765 xfs_dir2_leaf_t *leaf; /* leaf structure */
766 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
767 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
768 xfs_mount_t *mp; /* file system mount point */
769 int needlog; /* need to log data header */
770 int needscan; /* need to scan for bestfree */
771 xfs_dir2_sf_hdr_t sfh; /* shortform header */
772 int size; /* bytes used */
5e656dbb 773 __be16 *tagp; /* end of entry (tag) */
2bd0ea18
NS
774 int to; /* block/leaf to index */
775 xfs_trans_t *tp; /* transaction pointer */
776
777 xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
778 dp = args->dp;
779 tp = args->trans;
780 mp = dp->i_mount;
781 leaf = lbp->data;
5e656dbb
BN
782 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
783 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
2bd0ea18
NS
784 /*
785 * If there are data blocks other than the first one, take this
786 * opportunity to remove trailing empty data blocks that may have
787 * been left behind during no-space-reservation operations.
788 * These will show up in the leaf bests table.
789 */
790 while (dp->i_d.di_size > mp->m_dirblksize) {
5e656dbb
BN
791 bestsp = xfs_dir2_leaf_bests_p(ltp);
792 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
2bd0ea18 793 mp->m_dirblksize - (uint)sizeof(block->hdr)) {
0e266570 794 if ((error =
2bd0ea18 795 xfs_dir2_leaf_trim_data(args, lbp,
5e656dbb 796 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
2bd0ea18
NS
797 goto out;
798 } else {
799 error = 0;
800 goto out;
801 }
802 }
803 /*
804 * Read the data block if we don't already have it, give up if it fails.
805 */
806 if (dbp == NULL &&
807 (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
808 XFS_DATA_FORK))) {
2bd0ea18
NS
809 goto out;
810 }
811 block = dbp->data;
5e656dbb 812 ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
2bd0ea18
NS
813 /*
814 * Size of the "leaf" area in the block.
815 */
816 size = (uint)sizeof(block->tail) +
5e656dbb 817 (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
2bd0ea18
NS
818 /*
819 * Look at the last data entry.
820 */
5e656dbb
BN
821 tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
822 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
2bd0ea18
NS
823 /*
824 * If it's not free or is too short we can't do it.
825 */
5e656dbb
BN
826 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
827 be16_to_cpu(dup->length) < size) {
2bd0ea18
NS
828 error = 0;
829 goto out;
830 }
831 /*
832 * Start converting it to block form.
833 */
5e656dbb 834 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
2bd0ea18
NS
835 needlog = 1;
836 needscan = 0;
837 /*
838 * Use up the space at the end of the block (blp/btp).
839 */
840 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
841 &needlog, &needscan);
842 /*
843 * Initialize the block tail.
844 */
5e656dbb
BN
845 btp = xfs_dir2_block_tail_p(mp, block);
846 btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
46eca962 847 btp->stale = 0;
2bd0ea18
NS
848 xfs_dir2_block_log_tail(tp, dbp);
849 /*
850 * Initialize the block leaf area. We compact out stale entries.
851 */
5e656dbb
BN
852 lep = xfs_dir2_block_leaf_p(btp);
853 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
854 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
855 continue;
856 lep[to++] = leaf->ents[from];
857 }
5e656dbb
BN
858 ASSERT(to == be32_to_cpu(btp->count));
859 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18
NS
860 /*
861 * Scan the bestfree if we need it and log the data block header.
862 */
863 if (needscan)
5e656dbb 864 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
2bd0ea18
NS
865 if (needlog)
866 xfs_dir2_data_log_header(tp, dbp);
867 /*
868 * Pitch the old leaf block.
869 */
870 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
871 lbp = NULL;
872 if (error) {
2bd0ea18
NS
873 goto out;
874 }
875 /*
876 * Now see if the resulting block can be shrunken to shortform.
877 */
878 if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
879 XFS_IFORK_DSIZE(dp)) {
880 error = 0;
881 goto out;
882 }
883 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
884out:
885 if (lbp)
886 xfs_da_buf_done(lbp);
887 if (dbp)
888 xfs_da_buf_done(dbp);
889 return error;
890}
891
892/*
893 * Convert the shortform directory to block form.
894 */
895int /* error */
896xfs_dir2_sf_to_block(
897 xfs_da_args_t *args) /* operation arguments */
898{
899 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
900 xfs_dir2_block_t *block; /* block structure */
901 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
902 xfs_dabuf_t *bp; /* block buffer */
903 xfs_dir2_block_tail_t *btp; /* block tail pointer */
d663096d
SL
904 char *buf; /* sf buffer */
905 int buf_len;
2bd0ea18
NS
906 xfs_dir2_data_entry_t *dep; /* data entry pointer */
907 xfs_inode_t *dp; /* incore directory inode */
908 int dummy; /* trash */
909 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
910 int endoffset; /* end of data objects */
911 int error; /* error return value */
912 int i; /* index */
913 xfs_mount_t *mp; /* filesystem mount point */
914 int needlog; /* need to log block header */
915 int needscan; /* need to scan block freespc */
916 int newoffset; /* offset from current entry */
917 int offset; /* target block offset */
918 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
919 xfs_dir2_sf_t *sfp; /* shortform structure */
5e656dbb 920 __be16 *tagp; /* end of data entry */
2bd0ea18 921 xfs_trans_t *tp; /* transaction pointer */
5e656dbb 922 struct xfs_name name;
2bd0ea18
NS
923
924 xfs_dir2_trace_args("sf_to_block", args);
925 dp = args->dp;
926 tp = args->trans;
927 mp = dp->i_mount;
928 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
929 /*
930 * Bomb out if the shortform directory is way too short.
931 */
932 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
2bd0ea18
NS
933 ASSERT(XFS_FORCED_SHUTDOWN(mp));
934 return XFS_ERROR(EIO);
935 }
936 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
937 ASSERT(dp->i_df.if_u1.if_data != NULL);
938 sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
5e656dbb 939 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
2bd0ea18
NS
940 /*
941 * Copy the directory into the stack buffer.
942 * Then pitch the incore inode data so we can make extents.
943 */
d663096d
SL
944
945 buf_len = dp->i_df.if_bytes;
946 buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
947
32181a02 948 memcpy(buf, sfp, dp->i_df.if_bytes);
2bd0ea18
NS
949 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
950 dp->i_d.di_size = 0;
951 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
952 /*
953 * Reset pointer - old sfp is gone.
954 */
955 sfp = (xfs_dir2_sf_t *)buf;
956 /*
957 * Add block 0 to the inode.
958 */
959 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
960 if (error) {
5e656dbb 961 kmem_free(buf);
2bd0ea18
NS
962 return error;
963 }
964 /*
965 * Initialize the data block.
966 */
967 error = xfs_dir2_data_init(args, blkno, &bp);
968 if (error) {
5e656dbb 969 kmem_free(buf);
2bd0ea18
NS
970 return error;
971 }
972 block = bp->data;
5e656dbb 973 block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
2bd0ea18
NS
974 /*
975 * Compute size of block "tail" area.
976 */
977 i = (uint)sizeof(*btp) +
5e656dbb 978 (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
2bd0ea18
NS
979 /*
980 * The whole thing is initialized to free by the init routine.
981 * Say we're using the leaf and tail area.
982 */
983 dup = (xfs_dir2_data_unused_t *)block->u;
984 needlog = needscan = 0;
985 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
986 &needscan);
987 ASSERT(needscan == 0);
988 /*
989 * Fill in the tail.
990 */
5e656dbb
BN
991 btp = xfs_dir2_block_tail_p(mp, block);
992 btp->count = cpu_to_be32(sfp->hdr.count + 2); /* ., .. */
46eca962 993 btp->stale = 0;
5e656dbb 994 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
995 endoffset = (uint)((char *)blp - (char *)block);
996 /*
997 * Remove the freespace, we'll manage it.
998 */
999 xfs_dir2_data_use_free(tp, bp, dup,
1000 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
5e656dbb 1001 be16_to_cpu(dup->length), &needlog, &needscan);
2bd0ea18
NS
1002 /*
1003 * Create entry for .
1004 */
1005 dep = (xfs_dir2_data_entry_t *)
1006 ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
5e656dbb 1007 dep->inumber = cpu_to_be64(dp->i_ino);
2bd0ea18
NS
1008 dep->namelen = 1;
1009 dep->name[0] = '.';
5e656dbb
BN
1010 tagp = xfs_dir2_data_entry_tag_p(dep);
1011 *tagp = cpu_to_be16((char *)dep - (char *)block);
2bd0ea18 1012 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1013 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1014 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1015 (char *)dep - (char *)block));
2bd0ea18
NS
1016 /*
1017 * Create entry for ..
1018 */
1019 dep = (xfs_dir2_data_entry_t *)
1020 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
5e656dbb 1021 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
2bd0ea18
NS
1022 dep->namelen = 2;
1023 dep->name[0] = dep->name[1] = '.';
5e656dbb
BN
1024 tagp = xfs_dir2_data_entry_tag_p(dep);
1025 *tagp = cpu_to_be16((char *)dep - (char *)block);
2bd0ea18 1026 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1027 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1028 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1029 (char *)dep - (char *)block));
2bd0ea18
NS
1030 offset = XFS_DIR2_DATA_FIRST_OFFSET;
1031 /*
1032 * Loop over existing entries, stuff them in.
1033 */
5e656dbb 1034 if ((i = 0) == sfp->hdr.count)
2bd0ea18
NS
1035 sfep = NULL;
1036 else
5e656dbb 1037 sfep = xfs_dir2_sf_firstentry(sfp);
2bd0ea18
NS
1038 /*
1039 * Need to preserve the existing offset values in the sf directory.
1040 * Insert holes (unused entries) where necessary.
1041 */
1042 while (offset < endoffset) {
1043 /*
1044 * sfep is null when we reach the end of the list.
1045 */
1046 if (sfep == NULL)
1047 newoffset = endoffset;
1048 else
5e656dbb 1049 newoffset = xfs_dir2_sf_get_offset(sfep);
2bd0ea18
NS
1050 /*
1051 * There should be a hole here, make one.
1052 */
1053 if (offset < newoffset) {
1054 dup = (xfs_dir2_data_unused_t *)
1055 ((char *)block + offset);
5e656dbb
BN
1056 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1057 dup->length = cpu_to_be16(newoffset - offset);
1058 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
2bd0ea18
NS
1059 ((char *)dup - (char *)block));
1060 xfs_dir2_data_log_unused(tp, bp, dup);
1061 (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1062 dup, &dummy);
5e656dbb 1063 offset += be16_to_cpu(dup->length);
2bd0ea18
NS
1064 continue;
1065 }
1066 /*
1067 * Copy a real entry.
1068 */
1069 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
5e656dbb
BN
1070 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1071 xfs_dir2_sf_inumberp(sfep)));
2bd0ea18 1072 dep->namelen = sfep->namelen;
32181a02 1073 memcpy(dep->name, sfep->name, dep->namelen);
5e656dbb
BN
1074 tagp = xfs_dir2_data_entry_tag_p(dep);
1075 *tagp = cpu_to_be16((char *)dep - (char *)block);
2bd0ea18 1076 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1077 name.name = sfep->name;
1078 name.len = sfep->namelen;
1079 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1080 hashname(&name));
51ca7008 1081 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
2bd0ea18
NS
1082 (char *)dep - (char *)block));
1083 offset = (int)((char *)(tagp + 1) - (char *)block);
5e656dbb 1084 if (++i == sfp->hdr.count)
2bd0ea18
NS
1085 sfep = NULL;
1086 else
5e656dbb 1087 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
2bd0ea18 1088 }
d663096d 1089 /* Done with the temporary buffer */
5e656dbb 1090 kmem_free(buf);
2bd0ea18
NS
1091 /*
1092 * Sort the leaf entries by hash value.
1093 */
5e656dbb 1094 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
5000d01d 1095 /*
2bd0ea18
NS
1096 * Log the leaf entry area and tail.
1097 * Already logged the header in data_init, ignore needlog.
1098 */
1099 ASSERT(needscan == 0);
5e656dbb 1100 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18
NS
1101 xfs_dir2_block_log_tail(tp, bp);
1102 xfs_dir2_data_check(dp, bp);
1103 xfs_da_buf_done(bp);
1104 return 0;
1105}