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