]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_dir2_block.c
libxfs: add helper for verifying checksums on xfs_bufs
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dir2_block.c
CommitLineData
2bd0ea18 1/*
5e656dbb 2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
693fc8f6 3 * Copyright (c) 2013 Red Hat, Inc.
da23017d 4 * All Rights Reserved.
5000d01d 5 *
da23017d
NS
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
2bd0ea18 8 * published by the Free Software Foundation.
5000d01d 9 *
da23017d
NS
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.
5000d01d 14 *
da23017d
NS
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
2bd0ea18
NS
18 */
19
5e656dbb
BN
20#include <xfs.h>
21
2bd0ea18 22/*
5e656dbb 23 * Local function prototypes.
2bd0ea18 24 */
a2ceac1f
DC
25static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
26 int first, int last);
27static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
28static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
5e656dbb
BN
29 int *entno);
30static int xfs_dir2_block_sort(const void *a, const void *b);
2bd0ea18 31
5e656dbb
BN
32static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
33
34/*
35 * One-time startup routine called from xfs_init().
36 */
37void
38xfs_dir_startup(void)
39{
56b2de80
DC
40 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
41 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
5e656dbb 42}
2bd0ea18 43
693fc8f6
DC
44static bool
45xfs_dir3_block_verify(
a2ceac1f
DC
46 struct xfs_buf *bp)
47{
48 struct xfs_mount *mp = bp->b_target->bt_mount;
693fc8f6
DC
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;
a2ceac1f 61 }
90ea28c3 62 if (__xfs_dir3_data_check(NULL, bp))
693fc8f6
DC
63 return false;
64 return true;
a2ceac1f
DC
65}
66
67static void
693fc8f6 68xfs_dir3_block_read_verify(
a2ceac1f
DC
69 struct xfs_buf *bp)
70{
693fc8f6
DC
71 struct xfs_mount *mp = bp->b_target->bt_mount;
72
73 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
d21ca64d 74 !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) ||
693fc8f6
DC
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 }
a2ceac1f
DC
79}
80
81static void
693fc8f6 82xfs_dir3_block_write_verify(
a2ceac1f
DC
83 struct xfs_buf *bp)
84{
693fc8f6
DC
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_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
a2ceac1f
DC
102}
103
693fc8f6
DC
104const 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,
a2ceac1f
DC
107};
108
494434d7 109int
693fc8f6 110xfs_dir3_block_read(
a2ceac1f
DC
111 struct xfs_trans *tp,
112 struct xfs_inode *dp,
113 struct xfs_buf **bpp)
114{
115 struct xfs_mount *mp = dp->i_mount;
8b4dc4a9 116 int err;
a2ceac1f 117
8b4dc4a9 118 err = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
693fc8f6 119 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
8b4dc4a9 120 if (!err && tp)
bdc16ee5 121 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_BLOCK_BUF);
8b4dc4a9 122 return err;
693fc8f6
DC
123}
124
125static void
126xfs_dir3_block_init(
127 struct xfs_mount *mp,
8b4dc4a9 128 struct xfs_trans *tp,
693fc8f6
DC
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;
bdc16ee5 135 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_BLOCK_BUF);
693fc8f6
DC
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);
a2ceac1f
DC
147}
148
149static void
150xfs_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;
693fc8f6 166 bf = xfs_dir3_data_bestfree_p(hdr);
a2ceac1f
DC
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 }
249out:
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 */
260static void
261xfs_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
2bd0ea18
NS
309/*
310 * Add an entry to a block directory.
311 */
312int /* error */
313xfs_dir2_block_addname(
314 xfs_da_args_t *args) /* directory op arguments */
315{
a2ceac1f 316 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 317 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 318 struct xfs_buf *bp; /* buffer for block */
2bd0ea18
NS
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 */
0e266570 325 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
2bd0ea18
NS
326 xfs_dahash_t hash; /* hash value of found entry */
327 int high; /* high index for binary srch */
328 int highstale; /* high stale index */
0e266570
NS
329 int lfloghigh=0; /* last final leaf to log */
330 int lfloglow=0; /* first final leaf to log */
2bd0ea18
NS
331 int len; /* length of the new entry */
332 int low; /* low index for binary srch */
333 int lowstale; /* low stale index */
0e266570 334 int mid=0; /* midpoint for binary srch */
2bd0ea18
NS
335 xfs_mount_t *mp; /* filesystem mount point */
336 int needlog; /* need to log header */
337 int needscan; /* need to rescan freespace */
5e656dbb 338 __be16 *tagp; /* pointer to tag value */
2bd0ea18
NS
339 xfs_trans_t *tp; /* transaction structure */
340
56b2de80
DC
341 trace_xfs_dir2_block_addname(args);
342
2bd0ea18
NS
343 dp = args->dp;
344 tp = args->trans;
345 mp = dp->i_mount;
a2ceac1f
DC
346
347 /* Read the (one and only) directory block into bp. */
693fc8f6 348 error = xfs_dir3_block_read(tp, dp, &bp);
a2ceac1f 349 if (error)
2bd0ea18 350 return error;
a2ceac1f 351
494434d7 352 len = xfs_dir3_data_entsize(mp, args->namelen);
a2ceac1f 353
2bd0ea18
NS
354 /*
355 * Set up pointers to parts of the block.
356 */
a2ceac1f
DC
357 hdr = bp->b_addr;
358 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 359 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f 360
2bd0ea18 361 /*
a2ceac1f
DC
362 * Find out if we can reuse stale entries or whether we need extra
363 * space for entry and new leaf.
2bd0ea18 364 */
a2ceac1f
DC
365 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
366 &enddup, &compact, len);
367
2bd0ea18 368 /*
a2ceac1f 369 * Done everything we need for a space check now.
2bd0ea18 370 */
a2ceac1f
DC
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;
2bd0ea18 376 }
a2ceac1f 377
2bd0ea18
NS
378 /*
379 * If we don't have space for the new entry & leaf ...
380 */
381 if (!dup) {
a2ceac1f
DC
382 /* Don't have a space reservation: return no-space. */
383 if (args->total == 0)
2bd0ea18
NS
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);
2bd0ea18
NS
390 if (error)
391 return error;
392 return xfs_dir2_leaf_addname(args);
393 }
a2ceac1f 394
2bd0ea18 395 needlog = needscan = 0;
a2ceac1f 396
2bd0ea18
NS
397 /*
398 * If need to compact the leaf entries, do it now.
2bd0ea18 399 */
49f693fa 400 if (compact) {
a2ceac1f
DC
401 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
402 &lfloghigh, &lfloglow);
49f693fa
DC
403 /* recalculate blp post-compaction */
404 blp = xfs_dir2_block_leaf_p(btp);
405 } else if (btp->stale) {
2bd0ea18 406 /*
a2ceac1f
DC
407 * Set leaf logging boundaries to impossible state.
408 * For the no-stale case they're set explicitly.
2bd0ea18 409 */
5e656dbb 410 lfloglow = be32_to_cpu(btp->count);
2bd0ea18
NS
411 lfloghigh = -1;
412 }
a2ceac1f 413
2bd0ea18
NS
414 /*
415 * Find the slot that's first lower than our hash value, -1 if none.
416 */
5e656dbb 417 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
2bd0ea18 418 mid = (low + high) >> 1;
5e656dbb 419 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
420 break;
421 if (hash < args->hashval)
422 low = mid + 1;
423 else
424 high = mid - 1;
425 }
5e656dbb 426 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
2bd0ea18
NS
427 mid--;
428 }
429 /*
430 * No stale entries, will use enddup space to hold new leaf.
431 */
46eca962 432 if (!btp->stale) {
2bd0ea18
NS
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)
a2ceac1f 438 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
2bd0ea18
NS
439 sizeof(*blp)),
440 (xfs_dir2_data_aoff_t)sizeof(*blp),
441 &needlog, &needscan);
442 /*
443 * Update the tail (entry count).
444 */
5e656dbb 445 be32_add_cpu(&btp->count, 1);
2bd0ea18
NS
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) {
a2ceac1f 451 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
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)
32181a02 462 memmove(blp, &blp[1], mid * sizeof(*blp));
2bd0ea18
NS
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 &&
a2ceac1f
DC
472 blp[lowstale].address !=
473 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
474 lowstale--)
475 continue;
476 for (highstale = mid + 1;
5e656dbb 477 highstale < be32_to_cpu(btp->count) &&
a2ceac1f
DC
478 blp[highstale].address !=
479 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
2bd0ea18
NS
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 &&
5e656dbb 487 (highstale == be32_to_cpu(btp->count) ||
2bd0ea18
NS
488 mid - lowstale <= highstale - mid)) {
489 if (mid - lowstale)
32181a02 490 memmove(&blp[lowstale], &blp[lowstale + 1],
2bd0ea18
NS
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 {
5e656dbb 499 ASSERT(highstale < be32_to_cpu(btp->count));
2bd0ea18
NS
500 mid++;
501 if (highstale - mid)
32181a02 502 memmove(&blp[mid + 1], &blp[mid],
2bd0ea18
NS
503 (highstale - mid) * sizeof(*blp));
504 lfloglow = MIN(mid, lfloglow);
505 lfloghigh = MAX(highstale, lfloghigh);
506 }
5e656dbb 507 be32_add_cpu(&btp->stale, -1);
2bd0ea18
NS
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 */
5e656dbb
BN
516 blp[mid].hashval = cpu_to_be32(args->hashval);
517 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
a2ceac1f 518 (char *)dep - (char *)hdr));
2bd0ea18
NS
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,
a2ceac1f 524 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
2bd0ea18
NS
525 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
526 /*
527 * Create the new data entry.
528 */
5e656dbb 529 dep->inumber = cpu_to_be64(args->inumber);
2bd0ea18 530 dep->namelen = args->namelen;
32181a02 531 memcpy(dep->name, args->name, args->namelen);
906b762f 532 xfs_dir3_dirent_put_ftype(mp, dep, args->filetype);
494434d7 533 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
a2ceac1f 534 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18
NS
535 /*
536 * Clean up the bestfree array and log the header, tail, and entry.
537 */
538 if (needscan)
a2ceac1f 539 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
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);
90ea28c3 544 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
545 return 0;
546}
547
548/*
549 * Log leaf entries from the block.
550 */
5e656dbb 551static void
2bd0ea18
NS
552xfs_dir2_block_log_leaf(
553 xfs_trans_t *tp, /* transaction structure */
a2ceac1f 554 struct xfs_buf *bp, /* block buffer */
2bd0ea18
NS
555 int first, /* index of first logged leaf */
556 int last) /* index of last logged leaf */
557{
a2ceac1f
DC
558 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
559 xfs_dir2_leaf_entry_t *blp;
560 xfs_dir2_block_tail_t *btp;
2bd0ea18 561
a2ceac1f 562 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
5e656dbb 563 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f
DC
564 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
565 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
2bd0ea18
NS
566}
567
568/*
569 * Log the block tail.
570 */
5e656dbb 571static void
2bd0ea18
NS
572xfs_dir2_block_log_tail(
573 xfs_trans_t *tp, /* transaction structure */
a2ceac1f 574 struct xfs_buf *bp) /* block buffer */
2bd0ea18 575{
a2ceac1f
DC
576 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
577 xfs_dir2_block_tail_t *btp;
2bd0ea18 578
a2ceac1f
DC
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));
2bd0ea18
NS
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 */
588int /* error */
589xfs_dir2_block_lookup(
590 xfs_da_args_t *args) /* dir lookup arguments */
591{
a2ceac1f 592 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 593 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 594 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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
56b2de80
DC
602 trace_xfs_dir2_block_lookup(args);
603
2bd0ea18
NS
604 /*
605 * Get the buffer, look up the entry.
606 * If not found (ENOENT) then return, have no buffer.
607 */
0e266570 608 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
2bd0ea18
NS
609 return error;
610 dp = args->dp;
611 mp = dp->i_mount;
a2ceac1f 612 hdr = bp->b_addr;
90ea28c3 613 xfs_dir3_data_check(dp, bp);
a2ceac1f 614 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 615 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
616 /*
617 * Get the offset from the leaf entry, to point to the data.
618 */
a2ceac1f 619 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
5e656dbb 620 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
2bd0ea18 621 /*
5e656dbb 622 * Fill in inode number, CI name if appropriate, release the block.
2bd0ea18 623 */
5e656dbb 624 args->inumber = be64_to_cpu(dep->inumber);
906b762f 625 args->filetype = xfs_dir3_dirent_get_ftype(mp, dep);
5e656dbb 626 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
a2ceac1f 627 xfs_trans_brelse(args->trans, bp);
5e656dbb 628 return XFS_ERROR(error);
2bd0ea18
NS
629}
630
631/*
632 * Internal block lookup routine.
633 */
5e656dbb 634static int /* error */
2bd0ea18
NS
635xfs_dir2_block_lookup_int(
636 xfs_da_args_t *args, /* dir lookup arguments */
a2ceac1f 637 struct xfs_buf **bpp, /* returned block buffer */
2bd0ea18
NS
638 int *entno) /* returned entry number */
639{
640 xfs_dir2_dataptr_t addr; /* data entry address */
a2ceac1f 641 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 642 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 643 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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 */
5e656dbb 654 enum xfs_dacmp cmp; /* comparison result */
2bd0ea18
NS
655
656 dp = args->dp;
657 tp = args->trans;
658 mp = dp->i_mount;
a2ceac1f 659
693fc8f6 660 error = xfs_dir3_block_read(tp, dp, &bp);
a2ceac1f 661 if (error)
2bd0ea18 662 return error;
a2ceac1f
DC
663
664 hdr = bp->b_addr;
90ea28c3 665 xfs_dir3_data_check(dp, bp);
a2ceac1f 666 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 667 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
668 /*
669 * Loop doing a binary search for our hash value.
670 * Find our entry, ENOENT if it's not there.
671 */
5e656dbb 672 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
2bd0ea18
NS
673 ASSERT(low <= high);
674 mid = (low + high) >> 1;
5e656dbb 675 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
2bd0ea18
NS
676 break;
677 if (hash < args->hashval)
678 low = mid + 1;
679 else
680 high = mid - 1;
681 if (low > high) {
5e656dbb 682 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
a2ceac1f 683 xfs_trans_brelse(tp, bp);
2bd0ea18
NS
684 return XFS_ERROR(ENOENT);
685 }
686 }
687 /*
688 * Back up to the first one with the right hash value.
689 */
5e656dbb 690 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
2bd0ea18
NS
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 {
5e656dbb 698 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
2bd0ea18
NS
699 continue;
700 /*
701 * Get pointer to the entry from the leaf.
702 */
703 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 704 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
2bd0ea18 705 /*
5e656dbb
BN
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.
2bd0ea18 709 */
5e656dbb
BN
710 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
711 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
712 args->cmpresult = cmp;
2bd0ea18
NS
713 *bpp = bp;
714 *entno = mid;
5e656dbb
BN
715 if (cmp == XFS_CMP_EXACT)
716 return 0;
2bd0ea18 717 }
5e656dbb
BN
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;
2bd0ea18
NS
728 /*
729 * No match, release the buffer and return ENOENT.
730 */
a2ceac1f 731 xfs_trans_brelse(tp, bp);
2bd0ea18
NS
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 */
739int /* error */
740xfs_dir2_block_removename(
741 xfs_da_args_t *args) /* directory operation args */
742{
a2ceac1f 743 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 744 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
a2ceac1f 745 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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
56b2de80
DC
758 trace_xfs_dir2_block_removename(args);
759
2bd0ea18
NS
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 */
0e266570 764 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
765 return error;
766 }
767 dp = args->dp;
768 tp = args->trans;
769 mp = dp->i_mount;
a2ceac1f
DC
770 hdr = bp->b_addr;
771 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 772 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
773 /*
774 * Point to the data entry using the leaf entry.
775 */
776 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 777 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
2bd0ea18
NS
778 /*
779 * Mark the data entry's space free.
780 */
781 needlog = needscan = 0;
782 xfs_dir2_data_make_free(tp, bp,
a2ceac1f 783 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
494434d7 784 xfs_dir3_data_entsize(mp, dep->namelen), &needlog, &needscan);
2bd0ea18
NS
785 /*
786 * Fix up the block tail.
787 */
5e656dbb 788 be32_add_cpu(&btp->stale, 1);
2bd0ea18
NS
789 xfs_dir2_block_log_tail(tp, bp);
790 /*
791 * Remove the leaf entry by marking it stale.
792 */
5e656dbb 793 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
2bd0ea18
NS
794 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
795 /*
796 * Fix up bestfree, log the header if necessary.
797 */
798 if (needscan)
a2ceac1f 799 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
800 if (needlog)
801 xfs_dir2_data_log_header(tp, bp);
90ea28c3 802 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
803 /*
804 * See if the size as a shortform is good enough.
805 */
a2ceac1f
DC
806 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
807 if (size > XFS_IFORK_DSIZE(dp))
2bd0ea18 808 return 0;
a2ceac1f 809
2bd0ea18
NS
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 */
820int /* error */
821xfs_dir2_block_replace(
822 xfs_da_args_t *args) /* directory operation args */
823{
a2ceac1f 824 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 825 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 826 struct xfs_buf *bp; /* block buffer */
2bd0ea18
NS
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
56b2de80
DC
834 trace_xfs_dir2_block_replace(args);
835
2bd0ea18
NS
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 */
0e266570 840 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
2bd0ea18
NS
841 return error;
842 }
843 dp = args->dp;
844 mp = dp->i_mount;
a2ceac1f
DC
845 hdr = bp->b_addr;
846 btp = xfs_dir2_block_tail_p(mp, hdr);
5e656dbb 847 blp = xfs_dir2_block_leaf_p(btp);
2bd0ea18
NS
848 /*
849 * Point to the data entry we need to change.
850 */
851 dep = (xfs_dir2_data_entry_t *)
a2ceac1f 852 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
5e656dbb 853 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
2bd0ea18
NS
854 /*
855 * Change the inode number to the new value.
856 */
5e656dbb 857 dep->inumber = cpu_to_be64(args->inumber);
906b762f 858 xfs_dir3_dirent_put_ftype(mp, dep, args->filetype);
2bd0ea18 859 xfs_dir2_data_log_entry(args->trans, bp, dep);
90ea28c3 860 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
861 return 0;
862}
863
864/*
865 * Qsort comparison routine for the block leaf entries.
866 */
867static int /* sort order */
868xfs_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;
5e656dbb
BN
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);
2bd0ea18
NS
879}
880
881/*
882 * Convert a V2 leaf directory to a V2 block directory if possible.
883 */
884int /* error */
885xfs_dir2_leaf_to_block(
886 xfs_da_args_t *args, /* operation arguments */
a2ceac1f
DC
887 struct xfs_buf *lbp, /* leaf buffer */
888 struct xfs_buf *dbp) /* data buffer */
2bd0ea18 889{
5e656dbb 890 __be16 *bestsp; /* leaf bests table */
a2ceac1f 891 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18
NS
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 */
5e656dbb 905 __be16 *tagp; /* end of entry (tag) */
2bd0ea18
NS
906 int to; /* block/leaf to index */
907 xfs_trans_t *tp; /* transaction pointer */
65b80c98
DC
908 struct xfs_dir2_leaf_entry *ents;
909 struct xfs_dir3_icleaf_hdr leafhdr;
2bd0ea18 910
56b2de80
DC
911 trace_xfs_dir2_leaf_to_block(args);
912
2bd0ea18
NS
913 dp = args->dp;
914 tp = args->trans;
915 mp = dp->i_mount;
a2ceac1f 916 leaf = lbp->b_addr;
65b80c98
DC
917 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
918 ents = xfs_dir3_leaf_ents_p(leaf);
5e656dbb 919 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
65b80c98
DC
920
921 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
922 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
2bd0ea18
NS
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) {
693fc8f6
DC
930 int hdrsz;
931
932 hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
5e656dbb
BN
933 bestsp = xfs_dir2_leaf_bests_p(ltp);
934 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
693fc8f6 935 mp->m_dirblksize - hdrsz) {
0e266570 936 if ((error =
2bd0ea18 937 xfs_dir2_leaf_trim_data(args, lbp,
5e656dbb 938 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
a2ceac1f
DC
939 return error;
940 } else
941 return 0;
2bd0ea18
NS
942 }
943 /*
944 * Read the data block if we don't already have it, give up if it fails.
945 */
a2ceac1f 946 if (!dbp) {
90ea28c3 947 error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
a2ceac1f
DC
948 if (error)
949 return error;
2bd0ea18 950 }
a2ceac1f 951 hdr = dbp->b_addr;
90ea28c3
DC
952 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
953 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
954
2bd0ea18
NS
955 /*
956 * Size of the "leaf" area in the block.
957 */
a2ceac1f 958 size = (uint)sizeof(xfs_dir2_block_tail_t) +
65b80c98 959 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
2bd0ea18
NS
960 /*
961 * Look at the last data entry.
962 */
a2ceac1f
DC
963 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
964 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
2bd0ea18
NS
965 /*
966 * If it's not free or is too short we can't do it.
967 */
5e656dbb 968 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
a2ceac1f
DC
969 be16_to_cpu(dup->length) < size)
970 return 0;
971
2bd0ea18
NS
972 /*
973 * Start converting it to block form.
974 */
8b4dc4a9 975 xfs_dir3_block_init(mp, tp, dbp, dp);
693fc8f6 976
2bd0ea18
NS
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 */
a2ceac1f 987 btp = xfs_dir2_block_tail_p(mp, hdr);
65b80c98 988 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
46eca962 989 btp->stale = 0;
2bd0ea18
NS
990 xfs_dir2_block_log_tail(tp, dbp);
991 /*
992 * Initialize the block leaf area. We compact out stale entries.
993 */
5e656dbb 994 lep = xfs_dir2_block_leaf_p(btp);
65b80c98
DC
995 for (from = to = 0; from < leafhdr.count; from++) {
996 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
2bd0ea18 997 continue;
65b80c98 998 lep[to++] = ents[from];
2bd0ea18 999 }
5e656dbb
BN
1000 ASSERT(to == be32_to_cpu(btp->count));
1001 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18
NS
1002 /*
1003 * Scan the bestfree if we need it and log the data block header.
1004 */
1005 if (needscan)
a2ceac1f 1006 xfs_dir2_data_freescan(mp, hdr, &needlog);
2bd0ea18
NS
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);
a2ceac1f
DC
1013 if (error)
1014 return error;
1015
2bd0ea18
NS
1016 /*
1017 * Now see if the resulting block can be shrunken to shortform.
1018 */
a2ceac1f
DC
1019 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1020 if (size > XFS_IFORK_DSIZE(dp))
1021 return 0;
1022
2bd0ea18 1023 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
2bd0ea18
NS
1024}
1025
1026/*
1027 * Convert the shortform directory to block form.
1028 */
1029int /* error */
1030xfs_dir2_sf_to_block(
1031 xfs_da_args_t *args) /* operation arguments */
1032{
1033 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
a2ceac1f 1034 xfs_dir2_data_hdr_t *hdr; /* block header */
2bd0ea18 1035 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
a2ceac1f 1036 struct xfs_buf *bp; /* block buffer */
2bd0ea18 1037 xfs_dir2_block_tail_t *btp; /* block tail pointer */
2bd0ea18
NS
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 */
a2ceac1f
DC
1051 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1052 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
5e656dbb 1053 __be16 *tagp; /* end of data entry */
2bd0ea18 1054 xfs_trans_t *tp; /* transaction pointer */
5e656dbb 1055 struct xfs_name name;
3f17ed4b 1056 struct xfs_ifork *ifp;
2bd0ea18 1057
56b2de80
DC
1058 trace_xfs_dir2_sf_to_block(args);
1059
2bd0ea18
NS
1060 dp = args->dp;
1061 tp = args->trans;
1062 mp = dp->i_mount;
3f17ed4b
DC
1063 ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK);
1064 ASSERT(ifp->if_flags & XFS_IFINLINE);
2bd0ea18
NS
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)) {
2bd0ea18
NS
1069 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1070 return XFS_ERROR(EIO);
1071 }
a2ceac1f 1072
3f17ed4b 1073 oldsfp = (xfs_dir2_sf_hdr_t *)ifp->if_u1.if_data;
a2ceac1f 1074
3f17ed4b
DC
1075 ASSERT(ifp->if_bytes == dp->i_d.di_size);
1076 ASSERT(ifp->if_u1.if_data != NULL);
a2ceac1f 1077 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
3f17ed4b 1078 ASSERT(dp->i_d.di_nextents == 0);
a2ceac1f 1079
2bd0ea18 1080 /*
a2ceac1f 1081 * Copy the directory into a temporary buffer.
2bd0ea18
NS
1082 * Then pitch the incore inode data so we can make extents.
1083 */
3f17ed4b
DC
1084 sfp = kmem_alloc(ifp->if_bytes, KM_SLEEP);
1085 memcpy(sfp, oldsfp, ifp->if_bytes);
d663096d 1086
3f17ed4b
DC
1087 xfs_idata_realloc(dp, -ifp->if_bytes, XFS_DATA_FORK);
1088 xfs_bmap_local_to_extents_empty(dp, XFS_DATA_FORK);
2bd0ea18 1089 dp->i_d.di_size = 0;
a2ceac1f 1090
2bd0ea18
NS
1091 /*
1092 * Add block 0 to the inode.
1093 */
1094 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1095 if (error) {
a2ceac1f 1096 kmem_free(sfp);
2bd0ea18
NS
1097 return error;
1098 }
1099 /*
693fc8f6 1100 * Initialize the data block, then convert it to block format.
2bd0ea18 1101 */
693fc8f6 1102 error = xfs_dir3_data_init(args, blkno, &bp);
2bd0ea18 1103 if (error) {
a2ceac1f 1104 kmem_free(sfp);
2bd0ea18
NS
1105 return error;
1106 }
8b4dc4a9 1107 xfs_dir3_block_init(mp, tp, bp, dp);
a2ceac1f 1108 hdr = bp->b_addr;
693fc8f6 1109
2bd0ea18
NS
1110 /*
1111 * Compute size of block "tail" area.
1112 */
1113 i = (uint)sizeof(*btp) +
a2ceac1f 1114 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
2bd0ea18
NS
1115 /*
1116 * The whole thing is initialized to free by the init routine.
1117 * Say we're using the leaf and tail area.
1118 */
693fc8f6 1119 dup = xfs_dir3_data_unused_p(hdr);
2bd0ea18
NS
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 */
a2ceac1f
DC
1127 btp = xfs_dir2_block_tail_p(mp, hdr);
1128 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
46eca962 1129 btp->stale = 0;
5e656dbb 1130 blp = xfs_dir2_block_leaf_p(btp);
a2ceac1f 1131 endoffset = (uint)((char *)blp - (char *)hdr);
2bd0ea18
NS
1132 /*
1133 * Remove the freespace, we'll manage it.
1134 */
1135 xfs_dir2_data_use_free(tp, bp, dup,
a2ceac1f 1136 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
5e656dbb 1137 be16_to_cpu(dup->length), &needlog, &needscan);
2bd0ea18
NS
1138 /*
1139 * Create entry for .
1140 */
41315687 1141 dep = xfs_dir3_data_dot_entry_p(mp, hdr);
5e656dbb 1142 dep->inumber = cpu_to_be64(dp->i_ino);
2bd0ea18
NS
1143 dep->namelen = 1;
1144 dep->name[0] = '.';
906b762f 1145 xfs_dir3_dirent_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
494434d7 1146 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
a2ceac1f 1147 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18 1148 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1149 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1150 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
a2ceac1f 1151 (char *)dep - (char *)hdr));
2bd0ea18
NS
1152 /*
1153 * Create entry for ..
1154 */
41315687 1155 dep = xfs_dir3_data_dotdot_entry_p(mp, hdr);
a2ceac1f 1156 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
2bd0ea18
NS
1157 dep->namelen = 2;
1158 dep->name[0] = dep->name[1] = '.';
906b762f 1159 xfs_dir3_dirent_put_ftype(mp, dep, XFS_DIR3_FT_DIR);
494434d7 1160 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
a2ceac1f 1161 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18 1162 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1163 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1164 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
a2ceac1f 1165 (char *)dep - (char *)hdr));
41315687 1166 offset = xfs_dir3_data_first_offset(mp);
2bd0ea18
NS
1167 /*
1168 * Loop over existing entries, stuff them in.
1169 */
a2ceac1f
DC
1170 i = 0;
1171 if (!sfp->count)
2bd0ea18
NS
1172 sfep = NULL;
1173 else
5e656dbb 1174 sfep = xfs_dir2_sf_firstentry(sfp);
2bd0ea18
NS
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
5e656dbb 1186 newoffset = xfs_dir2_sf_get_offset(sfep);
2bd0ea18
NS
1187 /*
1188 * There should be a hole here, make one.
1189 */
1190 if (offset < newoffset) {
a2ceac1f 1191 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
5e656dbb
BN
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(
a2ceac1f 1195 ((char *)dup - (char *)hdr));
2bd0ea18 1196 xfs_dir2_data_log_unused(tp, bp, dup);
a2ceac1f 1197 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
5e656dbb 1198 offset += be16_to_cpu(dup->length);
2bd0ea18
NS
1199 continue;
1200 }
1201 /*
1202 * Copy a real entry.
1203 */
a2ceac1f 1204 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
494434d7 1205 dep->inumber = cpu_to_be64(xfs_dir3_sfe_get_ino(mp, sfp, sfep));
2bd0ea18 1206 dep->namelen = sfep->namelen;
906b762f
DC
1207 xfs_dir3_dirent_put_ftype(mp, dep,
1208 xfs_dir3_sfe_get_ftype(mp, sfp, sfep));
32181a02 1209 memcpy(dep->name, sfep->name, dep->namelen);
494434d7 1210 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
a2ceac1f 1211 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
2bd0ea18 1212 xfs_dir2_data_log_entry(tp, bp, dep);
5e656dbb
BN
1213 name.name = sfep->name;
1214 name.len = sfep->namelen;
1215 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1216 hashname(&name));
51ca7008 1217 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
a2ceac1f
DC
1218 (char *)dep - (char *)hdr));
1219 offset = (int)((char *)(tagp + 1) - (char *)hdr);
1220 if (++i == sfp->count)
2bd0ea18
NS
1221 sfep = NULL;
1222 else
494434d7 1223 sfep = xfs_dir3_sf_nextentry(mp, sfp, sfep);
2bd0ea18 1224 }
d663096d 1225 /* Done with the temporary buffer */
a2ceac1f 1226 kmem_free(sfp);
2bd0ea18
NS
1227 /*
1228 * Sort the leaf entries by hash value.
1229 */
5e656dbb 1230 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
5000d01d 1231 /*
2bd0ea18
NS
1232 * Log the leaf entry area and tail.
1233 * Already logged the header in data_init, ignore needlog.
1234 */
1235 ASSERT(needscan == 0);
5e656dbb 1236 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
2bd0ea18 1237 xfs_dir2_block_log_tail(tp, bp);
90ea28c3 1238 xfs_dir3_data_check(dp, bp);
2bd0ea18
NS
1239 return 0;
1240}