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