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