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