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