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