xfs: add CRC protection to remote attributes
[deliverable/linux.git] / fs / xfs / xfs_dir2_block.c
CommitLineData
1da177e4 1/*
7b718769 2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
f5f3d9b0 3 * Copyright (c) 2013 Red Hat, Inc.
7b718769 4 * All Rights Reserved.
1da177e4 5 *
7b718769
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
1da177e4
LT
8 * published by the Free Software Foundation.
9 *
7b718769
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.
1da177e4 14 *
7b718769
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
1da177e4 18 */
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
1da177e4
LT
22#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
da353b0d 25#include "xfs_ag.h"
1da177e4 26#include "xfs_mount.h"
a844f451 27#include "xfs_da_btree.h"
1da177e4 28#include "xfs_bmap_btree.h"
1da177e4 29#include "xfs_dinode.h"
1da177e4 30#include "xfs_inode.h"
a844f451 31#include "xfs_inode_item.h"
f5f3d9b0 32#include "xfs_buf_item.h"
8d2a5e6e 33#include "xfs_dir2.h"
57926640
CH
34#include "xfs_dir2_format.h"
35#include "xfs_dir2_priv.h"
1da177e4 36#include "xfs_error.h"
0b1b213f 37#include "xfs_trace.h"
f5f3d9b0 38#include "xfs_cksum.h"
1da177e4
LT
39
40/*
41 * Local function prototypes.
42 */
1d9025e5
DC
43static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, struct xfs_buf *bp,
44 int first, int last);
45static void xfs_dir2_block_log_tail(xfs_trans_t *tp, struct xfs_buf *bp);
46static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, struct xfs_buf **bpp,
1da177e4
LT
47 int *entno);
48static int xfs_dir2_block_sort(const void *a, const void *b);
49
f6c2d1fa
NS
50static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
51
52/*
53 * One-time startup routine called from xfs_init().
54 */
55void
56xfs_dir_startup(void)
57{
4a24cb71
DC
58 xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1);
59 xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2);
f6c2d1fa
NS
60}
61
f5f3d9b0
DC
62static bool
63xfs_dir3_block_verify(
82025d7f
DC
64 struct xfs_buf *bp)
65{
66 struct xfs_mount *mp = bp->b_target->bt_mount;
f5f3d9b0
DC
67 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
68
69 if (xfs_sb_version_hascrc(&mp->m_sb)) {
70 if (hdr3->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
71 return false;
72 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
73 return false;
74 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
75 return false;
76 } else {
77 if (hdr3->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
78 return false;
82025d7f 79 }
33363fee 80 if (__xfs_dir3_data_check(NULL, bp))
f5f3d9b0
DC
81 return false;
82 return true;
612cfbfe 83}
82025d7f 84
612cfbfe 85static void
f5f3d9b0 86xfs_dir3_block_read_verify(
612cfbfe
DC
87 struct xfs_buf *bp)
88{
f5f3d9b0
DC
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90
91 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
92 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
93 XFS_DIR3_DATA_CRC_OFF)) ||
94 !xfs_dir3_block_verify(bp)) {
95 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
96 xfs_buf_ioerror(bp, EFSCORRUPTED);
97 }
612cfbfe
DC
98}
99
1813dd64 100static void
f5f3d9b0 101xfs_dir3_block_write_verify(
612cfbfe
DC
102 struct xfs_buf *bp)
103{
f5f3d9b0
DC
104 struct xfs_mount *mp = bp->b_target->bt_mount;
105 struct xfs_buf_log_item *bip = bp->b_fspriv;
106 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
107
108 if (!xfs_dir3_block_verify(bp)) {
109 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
110 xfs_buf_ioerror(bp, EFSCORRUPTED);
111 return;
112 }
113
114 if (!xfs_sb_version_hascrc(&mp->m_sb))
115 return;
116
117 if (bip)
118 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
119
120 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
82025d7f
DC
121}
122
f5f3d9b0
DC
123const struct xfs_buf_ops xfs_dir3_block_buf_ops = {
124 .verify_read = xfs_dir3_block_read_verify,
125 .verify_write = xfs_dir3_block_write_verify,
1813dd64
DC
126};
127
20f7e9f3 128static int
f5f3d9b0 129xfs_dir3_block_read(
20f7e9f3
DC
130 struct xfs_trans *tp,
131 struct xfs_inode *dp,
132 struct xfs_buf **bpp)
133{
134 struct xfs_mount *mp = dp->i_mount;
135
136 return xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, bpp,
f5f3d9b0
DC
137 XFS_DATA_FORK, &xfs_dir3_block_buf_ops);
138}
139
140static void
141xfs_dir3_block_init(
142 struct xfs_mount *mp,
143 struct xfs_buf *bp,
144 struct xfs_inode *dp)
145{
146 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
147
148 bp->b_ops = &xfs_dir3_block_buf_ops;
149
150 if (xfs_sb_version_hascrc(&mp->m_sb)) {
151 memset(hdr3, 0, sizeof(*hdr3));
152 hdr3->magic = cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
153 hdr3->blkno = cpu_to_be64(bp->b_bn);
154 hdr3->owner = cpu_to_be64(dp->i_ino);
155 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
156 return;
157
158 }
159 hdr3->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
20f7e9f3
DC
160}
161
162static void
163xfs_dir2_block_need_space(
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;
f5f3d9b0 179 bf = xfs_dir3_data_bestfree_p(hdr);
20f7e9f3
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(
275 struct xfs_trans *tp,
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));
308 xfs_dir2_data_make_free(tp, bp,
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);
312 blp += be32_to_cpu(btp->stale) - 1;
313 btp->stale = cpu_to_be32(1);
314 /*
315 * If we now need to rebuild the bestfree map, do so.
316 * This needs to happen before the next call to use_free.
317 */
318 if (needscan)
319 xfs_dir2_data_freescan(tp->t_mountp, hdr, needlog);
320}
321
1da177e4
LT
322/*
323 * Add an entry to a block directory.
324 */
325int /* error */
326xfs_dir2_block_addname(
327 xfs_da_args_t *args) /* directory op arguments */
328{
4f6ae1a4 329 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 330 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1d9025e5 331 struct xfs_buf *bp; /* buffer for block */
1da177e4
LT
332 xfs_dir2_block_tail_t *btp; /* block tail */
333 int compact; /* need to compact leaf ents */
334 xfs_dir2_data_entry_t *dep; /* block data entry */
335 xfs_inode_t *dp; /* directory inode */
336 xfs_dir2_data_unused_t *dup; /* block unused entry */
337 int error; /* error return value */
338 xfs_dir2_data_unused_t *enddup=NULL; /* unused at end of data */
339 xfs_dahash_t hash; /* hash value of found entry */
340 int high; /* high index for binary srch */
341 int highstale; /* high stale index */
342 int lfloghigh=0; /* last final leaf to log */
343 int lfloglow=0; /* first final leaf to log */
344 int len; /* length of the new entry */
345 int low; /* low index for binary srch */
346 int lowstale; /* low stale index */
347 int mid=0; /* midpoint for binary srch */
348 xfs_mount_t *mp; /* filesystem mount point */
349 int needlog; /* need to log header */
350 int needscan; /* need to rescan freespace */
3d693c6e 351 __be16 *tagp; /* pointer to tag value */
1da177e4
LT
352 xfs_trans_t *tp; /* transaction structure */
353
0b1b213f
CH
354 trace_xfs_dir2_block_addname(args);
355
1da177e4
LT
356 dp = args->dp;
357 tp = args->trans;
358 mp = dp->i_mount;
20f7e9f3
DC
359
360 /* Read the (one and only) directory block into bp. */
f5f3d9b0 361 error = xfs_dir3_block_read(tp, dp, &bp);
4bb20a83 362 if (error)
1da177e4 363 return error;
20f7e9f3 364
bbaaf538 365 len = xfs_dir2_data_entsize(args->namelen);
20f7e9f3 366
1da177e4
LT
367 /*
368 * Set up pointers to parts of the block.
369 */
20f7e9f3 370 hdr = bp->b_addr;
4f6ae1a4 371 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 372 blp = xfs_dir2_block_leaf_p(btp);
20f7e9f3 373
1da177e4 374 /*
20f7e9f3
DC
375 * Find out if we can reuse stale entries or whether we need extra
376 * space for entry and new leaf.
1da177e4 377 */
20f7e9f3
DC
378 xfs_dir2_block_need_space(hdr, btp, blp, &tagp, &dup,
379 &enddup, &compact, len);
380
1da177e4 381 /*
20f7e9f3 382 * Done everything we need for a space check now.
1da177e4 383 */
20f7e9f3 384 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1d9025e5 385 xfs_trans_brelse(tp, bp);
20f7e9f3
DC
386 if (!dup)
387 return XFS_ERROR(ENOSPC);
388 return 0;
389 }
390
1da177e4
LT
391 /*
392 * If we don't have space for the new entry & leaf ...
393 */
394 if (!dup) {
20f7e9f3
DC
395 /* Don't have a space reservation: return no-space. */
396 if (args->total == 0)
1da177e4
LT
397 return XFS_ERROR(ENOSPC);
398 /*
399 * Convert to the next larger format.
400 * Then add the new entry in that format.
401 */
402 error = xfs_dir2_block_to_leaf(args, bp);
1da177e4
LT
403 if (error)
404 return error;
405 return xfs_dir2_leaf_addname(args);
406 }
20f7e9f3 407
1da177e4 408 needlog = needscan = 0;
20f7e9f3 409
1da177e4
LT
410 /*
411 * If need to compact the leaf entries, do it now.
1da177e4 412 */
37f13561 413 if (compact) {
20f7e9f3
DC
414 xfs_dir2_block_compact(tp, bp, hdr, btp, blp, &needlog,
415 &lfloghigh, &lfloglow);
37f13561
ES
416 /* recalculate blp post-compaction */
417 blp = xfs_dir2_block_leaf_p(btp);
418 } else if (btp->stale) {
1da177e4 419 /*
20f7e9f3
DC
420 * Set leaf logging boundaries to impossible state.
421 * For the no-stale case they're set explicitly.
1da177e4 422 */
e922fffa 423 lfloglow = be32_to_cpu(btp->count);
1da177e4
LT
424 lfloghigh = -1;
425 }
20f7e9f3 426
1da177e4
LT
427 /*
428 * Find the slot that's first lower than our hash value, -1 if none.
429 */
e922fffa 430 for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
1da177e4 431 mid = (low + high) >> 1;
3c1f9c15 432 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
1da177e4
LT
433 break;
434 if (hash < args->hashval)
435 low = mid + 1;
436 else
437 high = mid - 1;
438 }
3c1f9c15 439 while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
1da177e4
LT
440 mid--;
441 }
442 /*
443 * No stale entries, will use enddup space to hold new leaf.
444 */
445 if (!btp->stale) {
446 /*
447 * Mark the space needed for the new leaf entry, now in use.
448 */
449 xfs_dir2_data_use_free(tp, bp, enddup,
450 (xfs_dir2_data_aoff_t)
4f6ae1a4 451 ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) -
1da177e4
LT
452 sizeof(*blp)),
453 (xfs_dir2_data_aoff_t)sizeof(*blp),
454 &needlog, &needscan);
455 /*
456 * Update the tail (entry count).
457 */
413d57c9 458 be32_add_cpu(&btp->count, 1);
1da177e4
LT
459 /*
460 * If we now need to rebuild the bestfree map, do so.
461 * This needs to happen before the next call to use_free.
462 */
463 if (needscan) {
c2066e26 464 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
465 needscan = 0;
466 }
467 /*
468 * Adjust pointer to the first leaf entry, we're about to move
469 * the table up one to open up space for the new leaf entry.
470 * Then adjust our index to match.
471 */
472 blp--;
473 mid++;
474 if (mid)
475 memmove(blp, &blp[1], mid * sizeof(*blp));
476 lfloglow = 0;
477 lfloghigh = mid;
478 }
479 /*
480 * Use a stale leaf for our new entry.
481 */
482 else {
483 for (lowstale = mid;
484 lowstale >= 0 &&
69ef921b
CH
485 blp[lowstale].address !=
486 cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
487 lowstale--)
488 continue;
489 for (highstale = mid + 1;
e922fffa 490 highstale < be32_to_cpu(btp->count) &&
69ef921b
CH
491 blp[highstale].address !=
492 cpu_to_be32(XFS_DIR2_NULL_DATAPTR) &&
1da177e4
LT
493 (lowstale < 0 || mid - lowstale > highstale - mid);
494 highstale++)
495 continue;
496 /*
497 * Move entries toward the low-numbered stale entry.
498 */
499 if (lowstale >= 0 &&
e922fffa 500 (highstale == be32_to_cpu(btp->count) ||
1da177e4
LT
501 mid - lowstale <= highstale - mid)) {
502 if (mid - lowstale)
503 memmove(&blp[lowstale], &blp[lowstale + 1],
504 (mid - lowstale) * sizeof(*blp));
505 lfloglow = MIN(lowstale, lfloglow);
506 lfloghigh = MAX(mid, lfloghigh);
507 }
508 /*
509 * Move entries toward the high-numbered stale entry.
510 */
511 else {
e922fffa 512 ASSERT(highstale < be32_to_cpu(btp->count));
1da177e4
LT
513 mid++;
514 if (highstale - mid)
515 memmove(&blp[mid + 1], &blp[mid],
516 (highstale - mid) * sizeof(*blp));
517 lfloglow = MIN(mid, lfloglow);
518 lfloghigh = MAX(highstale, lfloghigh);
519 }
413d57c9 520 be32_add_cpu(&btp->stale, -1);
1da177e4
LT
521 }
522 /*
523 * Point to the new data entry.
524 */
525 dep = (xfs_dir2_data_entry_t *)dup;
526 /*
527 * Fill in the leaf entry.
528 */
3c1f9c15 529 blp[mid].hashval = cpu_to_be32(args->hashval);
bbaaf538 530 blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
4f6ae1a4 531 (char *)dep - (char *)hdr));
1da177e4
LT
532 xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
533 /*
534 * Mark space for the data entry used.
535 */
536 xfs_dir2_data_use_free(tp, bp, dup,
4f6ae1a4 537 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
1da177e4
LT
538 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
539 /*
540 * Create the new data entry.
541 */
ff9901c1 542 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
543 dep->namelen = args->namelen;
544 memcpy(dep->name, args->name, args->namelen);
bbaaf538 545 tagp = xfs_dir2_data_entry_tag_p(dep);
4f6ae1a4 546 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1da177e4
LT
547 /*
548 * Clean up the bestfree array and log the header, tail, and entry.
549 */
550 if (needscan)
c2066e26 551 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
552 if (needlog)
553 xfs_dir2_data_log_header(tp, bp);
554 xfs_dir2_block_log_tail(tp, bp);
555 xfs_dir2_data_log_entry(tp, bp, dep);
33363fee 556 xfs_dir3_data_check(dp, bp);
1da177e4
LT
557 return 0;
558}
559
560/*
561 * Readdir for block directories.
562 */
563int /* error */
564xfs_dir2_block_getdents(
1da177e4 565 xfs_inode_t *dp, /* incore inode */
051e7cd4
CH
566 void *dirent,
567 xfs_off_t *offset,
568 filldir_t filldir)
1da177e4 569{
4f6ae1a4 570 xfs_dir2_data_hdr_t *hdr; /* block header */
1d9025e5 571 struct xfs_buf *bp; /* buffer for block */
1da177e4
LT
572 xfs_dir2_block_tail_t *btp; /* block tail */
573 xfs_dir2_data_entry_t *dep; /* block data entry */
574 xfs_dir2_data_unused_t *dup; /* block unused entry */
575 char *endptr; /* end of the data entries */
576 int error; /* error return value */
577 xfs_mount_t *mp; /* filesystem mount point */
1da177e4
LT
578 char *ptr; /* current data entry */
579 int wantoff; /* starting block offset */
051e7cd4 580 xfs_off_t cook;
1da177e4
LT
581
582 mp = dp->i_mount;
583 /*
584 * If the block number in the offset is out of range, we're done.
585 */
20f7e9f3 586 if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk)
1da177e4 587 return 0;
20f7e9f3 588
f5f3d9b0 589 error = xfs_dir3_block_read(NULL, dp, &bp);
051e7cd4 590 if (error)
1da177e4 591 return error;
051e7cd4 592
1da177e4
LT
593 /*
594 * Extract the byte offset we start at from the seek pointer.
595 * We'll skip entries before this.
596 */
051e7cd4 597 wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
1d9025e5 598 hdr = bp->b_addr;
33363fee 599 xfs_dir3_data_check(dp, bp);
1da177e4
LT
600 /*
601 * Set up values for the loop.
602 */
4f6ae1a4 603 btp = xfs_dir2_block_tail_p(mp, hdr);
f5f3d9b0 604 ptr = (char *)xfs_dir3_data_entry_p(hdr);
bbaaf538 605 endptr = (char *)xfs_dir2_block_leaf_p(btp);
051e7cd4 606
1da177e4
LT
607 /*
608 * Loop over the data portion of the block.
609 * Each object is a real entry (dep) or an unused one (dup).
610 */
611 while (ptr < endptr) {
612 dup = (xfs_dir2_data_unused_t *)ptr;
613 /*
614 * Unused, skip it.
615 */
ad354eb3
NS
616 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
617 ptr += be16_to_cpu(dup->length);
1da177e4
LT
618 continue;
619 }
620
621 dep = (xfs_dir2_data_entry_t *)ptr;
622
623 /*
624 * Bump pointer for the next iteration.
625 */
bbaaf538 626 ptr += xfs_dir2_data_entsize(dep->namelen);
1da177e4
LT
627 /*
628 * The entry is before the desired starting point, skip it.
629 */
4f6ae1a4 630 if ((char *)dep - (char *)hdr < wantoff)
1da177e4 631 continue;
1da177e4 632
051e7cd4 633 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
4f6ae1a4 634 (char *)dep - (char *)hdr);
1da177e4
LT
635
636 /*
637 * If it didn't fit, set the final offset to here & return.
638 */
4a24cb71
DC
639 if (filldir(dirent, (char *)dep->name, dep->namelen,
640 cook & 0x7fffffff, be64_to_cpu(dep->inumber),
641 DT_UNKNOWN)) {
15440319 642 *offset = cook & 0x7fffffff;
1d9025e5 643 xfs_trans_brelse(NULL, bp);
051e7cd4 644 return 0;
1da177e4
LT
645 }
646 }
647
648 /*
649 * Reached the end of the block.
c41564b5 650 * Set the offset to a non-existent block 1 and return.
1da177e4 651 */
15440319
CH
652 *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) &
653 0x7fffffff;
1d9025e5 654 xfs_trans_brelse(NULL, bp);
1da177e4
LT
655 return 0;
656}
657
658/*
659 * Log leaf entries from the block.
660 */
661static void
662xfs_dir2_block_log_leaf(
663 xfs_trans_t *tp, /* transaction structure */
1d9025e5 664 struct xfs_buf *bp, /* block buffer */
1da177e4
LT
665 int first, /* index of first logged leaf */
666 int last) /* index of last logged leaf */
667{
1d9025e5 668 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
4f6ae1a4
CH
669 xfs_dir2_leaf_entry_t *blp;
670 xfs_dir2_block_tail_t *btp;
1da177e4 671
4f6ae1a4 672 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
bbaaf538 673 blp = xfs_dir2_block_leaf_p(btp);
1d9025e5 674 xfs_trans_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr),
4f6ae1a4 675 (uint)((char *)&blp[last + 1] - (char *)hdr - 1));
1da177e4
LT
676}
677
678/*
679 * Log the block tail.
680 */
681static void
682xfs_dir2_block_log_tail(
683 xfs_trans_t *tp, /* transaction structure */
1d9025e5 684 struct xfs_buf *bp) /* block buffer */
1da177e4 685{
1d9025e5 686 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
4f6ae1a4 687 xfs_dir2_block_tail_t *btp;
1da177e4 688
4f6ae1a4 689 btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr);
1d9025e5 690 xfs_trans_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr),
4f6ae1a4 691 (uint)((char *)(btp + 1) - (char *)hdr - 1));
1da177e4
LT
692}
693
694/*
695 * Look up an entry in the block. This is the external routine,
696 * xfs_dir2_block_lookup_int does the real work.
697 */
698int /* error */
699xfs_dir2_block_lookup(
700 xfs_da_args_t *args) /* dir lookup arguments */
701{
4f6ae1a4 702 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 703 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1d9025e5 704 struct xfs_buf *bp; /* block buffer */
1da177e4
LT
705 xfs_dir2_block_tail_t *btp; /* block tail */
706 xfs_dir2_data_entry_t *dep; /* block data entry */
707 xfs_inode_t *dp; /* incore inode */
708 int ent; /* entry index */
709 int error; /* error return value */
710 xfs_mount_t *mp; /* filesystem mount point */
711
0b1b213f
CH
712 trace_xfs_dir2_block_lookup(args);
713
1da177e4
LT
714 /*
715 * Get the buffer, look up the entry.
716 * If not found (ENOENT) then return, have no buffer.
717 */
718 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
719 return error;
720 dp = args->dp;
721 mp = dp->i_mount;
1d9025e5 722 hdr = bp->b_addr;
33363fee 723 xfs_dir3_data_check(dp, bp);
4f6ae1a4 724 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 725 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
726 /*
727 * Get the offset from the leaf entry, to point to the data.
728 */
4f6ae1a4 729 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
384f3ced 730 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
1da177e4 731 /*
384f3ced 732 * Fill in inode number, CI name if appropriate, release the block.
1da177e4 733 */
ff9901c1 734 args->inumber = be64_to_cpu(dep->inumber);
384f3ced 735 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1d9025e5 736 xfs_trans_brelse(args->trans, bp);
384f3ced 737 return XFS_ERROR(error);
1da177e4
LT
738}
739
740/*
741 * Internal block lookup routine.
742 */
743static int /* error */
744xfs_dir2_block_lookup_int(
745 xfs_da_args_t *args, /* dir lookup arguments */
1d9025e5 746 struct xfs_buf **bpp, /* returned block buffer */
1da177e4
LT
747 int *entno) /* returned entry number */
748{
749 xfs_dir2_dataptr_t addr; /* data entry address */
4f6ae1a4 750 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 751 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1d9025e5 752 struct xfs_buf *bp; /* block buffer */
1da177e4
LT
753 xfs_dir2_block_tail_t *btp; /* block tail */
754 xfs_dir2_data_entry_t *dep; /* block data entry */
755 xfs_inode_t *dp; /* incore inode */
756 int error; /* error return value */
757 xfs_dahash_t hash; /* found hash value */
758 int high; /* binary search high index */
759 int low; /* binary search low index */
760 int mid; /* binary search current idx */
761 xfs_mount_t *mp; /* filesystem mount point */
762 xfs_trans_t *tp; /* transaction pointer */
5163f95a 763 enum xfs_dacmp cmp; /* comparison result */
1da177e4
LT
764
765 dp = args->dp;
766 tp = args->trans;
767 mp = dp->i_mount;
20f7e9f3 768
f5f3d9b0 769 error = xfs_dir3_block_read(tp, dp, &bp);
4bb20a83 770 if (error)
1da177e4 771 return error;
20f7e9f3 772
1d9025e5 773 hdr = bp->b_addr;
33363fee 774 xfs_dir3_data_check(dp, bp);
4f6ae1a4 775 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 776 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
777 /*
778 * Loop doing a binary search for our hash value.
779 * Find our entry, ENOENT if it's not there.
780 */
e922fffa 781 for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
1da177e4
LT
782 ASSERT(low <= high);
783 mid = (low + high) >> 1;
3c1f9c15 784 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
1da177e4
LT
785 break;
786 if (hash < args->hashval)
787 low = mid + 1;
788 else
789 high = mid - 1;
790 if (low > high) {
6a178100 791 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1d9025e5 792 xfs_trans_brelse(tp, bp);
1da177e4
LT
793 return XFS_ERROR(ENOENT);
794 }
795 }
796 /*
797 * Back up to the first one with the right hash value.
798 */
3c1f9c15 799 while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
1da177e4
LT
800 mid--;
801 }
802 /*
803 * Now loop forward through all the entries with the
804 * right hash value looking for our name.
805 */
806 do {
3c1f9c15 807 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
808 continue;
809 /*
810 * Get pointer to the entry from the leaf.
811 */
812 dep = (xfs_dir2_data_entry_t *)
4f6ae1a4 813 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr));
1da177e4 814 /*
5163f95a
BN
815 * Compare name and if it's an exact match, return the index
816 * and buffer. If it's the first case-insensitive match, store
817 * the index and buffer and continue looking for an exact match.
1da177e4 818 */
5163f95a
BN
819 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
820 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
821 args->cmpresult = cmp;
1da177e4
LT
822 *bpp = bp;
823 *entno = mid;
5163f95a
BN
824 if (cmp == XFS_CMP_EXACT)
825 return 0;
1da177e4 826 }
5163f95a
BN
827 } while (++mid < be32_to_cpu(btp->count) &&
828 be32_to_cpu(blp[mid].hashval) == hash);
829
6a178100 830 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
5163f95a
BN
831 /*
832 * Here, we can only be doing a lookup (not a rename or replace).
833 * If a case-insensitive match was found earlier, return success.
834 */
835 if (args->cmpresult == XFS_CMP_CASE)
836 return 0;
1da177e4
LT
837 /*
838 * No match, release the buffer and return ENOENT.
839 */
1d9025e5 840 xfs_trans_brelse(tp, bp);
1da177e4
LT
841 return XFS_ERROR(ENOENT);
842}
843
844/*
845 * Remove an entry from a block format directory.
846 * If that makes the block small enough to fit in shortform, transform it.
847 */
848int /* error */
849xfs_dir2_block_removename(
850 xfs_da_args_t *args) /* directory operation args */
851{
4f6ae1a4 852 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 853 xfs_dir2_leaf_entry_t *blp; /* block leaf pointer */
1d9025e5 854 struct xfs_buf *bp; /* block buffer */
1da177e4
LT
855 xfs_dir2_block_tail_t *btp; /* block tail */
856 xfs_dir2_data_entry_t *dep; /* block data entry */
857 xfs_inode_t *dp; /* incore inode */
858 int ent; /* block leaf entry index */
859 int error; /* error return value */
860 xfs_mount_t *mp; /* filesystem mount point */
861 int needlog; /* need to log block header */
862 int needscan; /* need to fixup bestfree */
863 xfs_dir2_sf_hdr_t sfh; /* shortform header */
864 int size; /* shortform size */
865 xfs_trans_t *tp; /* transaction pointer */
866
0b1b213f
CH
867 trace_xfs_dir2_block_removename(args);
868
1da177e4
LT
869 /*
870 * Look up the entry in the block. Gets the buffer and entry index.
871 * It will always be there, the vnodeops level does a lookup first.
872 */
873 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
874 return error;
875 }
876 dp = args->dp;
877 tp = args->trans;
878 mp = dp->i_mount;
1d9025e5 879 hdr = bp->b_addr;
4f6ae1a4 880 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 881 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
882 /*
883 * Point to the data entry using the leaf entry.
884 */
885 dep = (xfs_dir2_data_entry_t *)
4f6ae1a4 886 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
1da177e4
LT
887 /*
888 * Mark the data entry's space free.
889 */
890 needlog = needscan = 0;
891 xfs_dir2_data_make_free(tp, bp,
4f6ae1a4 892 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
bbaaf538 893 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1da177e4
LT
894 /*
895 * Fix up the block tail.
896 */
413d57c9 897 be32_add_cpu(&btp->stale, 1);
1da177e4
LT
898 xfs_dir2_block_log_tail(tp, bp);
899 /*
900 * Remove the leaf entry by marking it stale.
901 */
3c1f9c15 902 blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
903 xfs_dir2_block_log_leaf(tp, bp, ent, ent);
904 /*
905 * Fix up bestfree, log the header if necessary.
906 */
907 if (needscan)
c2066e26 908 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
909 if (needlog)
910 xfs_dir2_data_log_header(tp, bp);
33363fee 911 xfs_dir3_data_check(dp, bp);
1da177e4
LT
912 /*
913 * See if the size as a shortform is good enough.
914 */
4f6ae1a4 915 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1d9025e5 916 if (size > XFS_IFORK_DSIZE(dp))
1da177e4 917 return 0;
1d9025e5 918
1da177e4
LT
919 /*
920 * If it works, do the conversion.
921 */
922 return xfs_dir2_block_to_sf(args, bp, size, &sfh);
923}
924
925/*
926 * Replace an entry in a V2 block directory.
927 * Change the inode number to the new value.
928 */
929int /* error */
930xfs_dir2_block_replace(
931 xfs_da_args_t *args) /* directory operation args */
932{
4f6ae1a4 933 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 934 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1d9025e5 935 struct xfs_buf *bp; /* block buffer */
1da177e4
LT
936 xfs_dir2_block_tail_t *btp; /* block tail */
937 xfs_dir2_data_entry_t *dep; /* block data entry */
938 xfs_inode_t *dp; /* incore inode */
939 int ent; /* leaf entry index */
940 int error; /* error return value */
941 xfs_mount_t *mp; /* filesystem mount point */
942
0b1b213f
CH
943 trace_xfs_dir2_block_replace(args);
944
1da177e4
LT
945 /*
946 * Lookup the entry in the directory. Get buffer and entry index.
947 * This will always succeed since the caller has already done a lookup.
948 */
949 if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
950 return error;
951 }
952 dp = args->dp;
953 mp = dp->i_mount;
1d9025e5 954 hdr = bp->b_addr;
4f6ae1a4 955 btp = xfs_dir2_block_tail_p(mp, hdr);
bbaaf538 956 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
957 /*
958 * Point to the data entry we need to change.
959 */
960 dep = (xfs_dir2_data_entry_t *)
4f6ae1a4 961 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
ff9901c1 962 ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
1da177e4
LT
963 /*
964 * Change the inode number to the new value.
965 */
ff9901c1 966 dep->inumber = cpu_to_be64(args->inumber);
1da177e4 967 xfs_dir2_data_log_entry(args->trans, bp, dep);
33363fee 968 xfs_dir3_data_check(dp, bp);
1da177e4
LT
969 return 0;
970}
971
972/*
973 * Qsort comparison routine for the block leaf entries.
974 */
975static int /* sort order */
976xfs_dir2_block_sort(
977 const void *a, /* first leaf entry */
978 const void *b) /* second leaf entry */
979{
980 const xfs_dir2_leaf_entry_t *la; /* first leaf entry */
981 const xfs_dir2_leaf_entry_t *lb; /* second leaf entry */
982
983 la = a;
984 lb = b;
3c1f9c15
NS
985 return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
986 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
1da177e4
LT
987}
988
989/*
990 * Convert a V2 leaf directory to a V2 block directory if possible.
991 */
992int /* error */
993xfs_dir2_leaf_to_block(
994 xfs_da_args_t *args, /* operation arguments */
1d9025e5
DC
995 struct xfs_buf *lbp, /* leaf buffer */
996 struct xfs_buf *dbp) /* data buffer */
1da177e4 997{
68b3a102 998 __be16 *bestsp; /* leaf bests table */
4f6ae1a4 999 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4
LT
1000 xfs_dir2_block_tail_t *btp; /* block tail */
1001 xfs_inode_t *dp; /* incore directory inode */
1002 xfs_dir2_data_unused_t *dup; /* unused data entry */
1003 int error; /* error return value */
1004 int from; /* leaf from index */
1005 xfs_dir2_leaf_t *leaf; /* leaf structure */
1006 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1007 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1008 xfs_mount_t *mp; /* file system mount point */
1009 int needlog; /* need to log data header */
1010 int needscan; /* need to scan for bestfree */
1011 xfs_dir2_sf_hdr_t sfh; /* shortform header */
1012 int size; /* bytes used */
3d693c6e 1013 __be16 *tagp; /* end of entry (tag) */
1da177e4
LT
1014 int to; /* block/leaf to index */
1015 xfs_trans_t *tp; /* transaction pointer */
24df33b4
DC
1016 struct xfs_dir2_leaf_entry *ents;
1017 struct xfs_dir3_icleaf_hdr leafhdr;
1da177e4 1018
0b1b213f
CH
1019 trace_xfs_dir2_leaf_to_block(args);
1020
1da177e4
LT
1021 dp = args->dp;
1022 tp = args->trans;
1023 mp = dp->i_mount;
1d9025e5 1024 leaf = lbp->b_addr;
24df33b4
DC
1025 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1026 ents = xfs_dir3_leaf_ents_p(leaf);
bbaaf538 1027 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
24df33b4
DC
1028
1029 ASSERT(leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
1030 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
1da177e4
LT
1031 /*
1032 * If there are data blocks other than the first one, take this
1033 * opportunity to remove trailing empty data blocks that may have
1034 * been left behind during no-space-reservation operations.
1035 * These will show up in the leaf bests table.
1036 */
1037 while (dp->i_d.di_size > mp->m_dirblksize) {
f5f3d9b0
DC
1038 int hdrsz;
1039
1040 hdrsz = xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
bbaaf538 1041 bestsp = xfs_dir2_leaf_bests_p(ltp);
afbcb3f9 1042 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
f5f3d9b0 1043 mp->m_dirblksize - hdrsz) {
1da177e4
LT
1044 if ((error =
1045 xfs_dir2_leaf_trim_data(args, lbp,
afbcb3f9 1046 (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
1d9025e5
DC
1047 return error;
1048 } else
1049 return 0;
1da177e4
LT
1050 }
1051 /*
1052 * Read the data block if we don't already have it, give up if it fails.
1053 */
4bb20a83 1054 if (!dbp) {
33363fee 1055 error = xfs_dir3_data_read(tp, dp, mp->m_dirdatablk, -1, &dbp);
4bb20a83
DC
1056 if (error)
1057 return error;
1da177e4 1058 }
1d9025e5 1059 hdr = dbp->b_addr;
33363fee
DC
1060 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1061 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1062
1da177e4
LT
1063 /*
1064 * Size of the "leaf" area in the block.
1065 */
4f6ae1a4 1066 size = (uint)sizeof(xfs_dir2_block_tail_t) +
24df33b4 1067 (uint)sizeof(*lep) * (leafhdr.count - leafhdr.stale);
1da177e4
LT
1068 /*
1069 * Look at the last data entry.
1070 */
4f6ae1a4
CH
1071 tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1;
1072 dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
1da177e4
LT
1073 /*
1074 * If it's not free or is too short we can't do it.
1075 */
ad354eb3 1076 if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
1d9025e5
DC
1077 be16_to_cpu(dup->length) < size)
1078 return 0;
1079
1da177e4
LT
1080 /*
1081 * Start converting it to block form.
1082 */
f5f3d9b0
DC
1083 xfs_dir3_block_init(mp, dbp, dp);
1084
1da177e4
LT
1085 needlog = 1;
1086 needscan = 0;
1087 /*
1088 * Use up the space at the end of the block (blp/btp).
1089 */
1090 xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
1091 &needlog, &needscan);
1092 /*
1093 * Initialize the block tail.
1094 */
4f6ae1a4 1095 btp = xfs_dir2_block_tail_p(mp, hdr);
24df33b4 1096 btp->count = cpu_to_be32(leafhdr.count - leafhdr.stale);
1da177e4
LT
1097 btp->stale = 0;
1098 xfs_dir2_block_log_tail(tp, dbp);
1099 /*
1100 * Initialize the block leaf area. We compact out stale entries.
1101 */
bbaaf538 1102 lep = xfs_dir2_block_leaf_p(btp);
24df33b4
DC
1103 for (from = to = 0; from < leafhdr.count; from++) {
1104 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
1da177e4 1105 continue;
24df33b4 1106 lep[to++] = ents[from];
1da177e4 1107 }
e922fffa
NS
1108 ASSERT(to == be32_to_cpu(btp->count));
1109 xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
1da177e4
LT
1110 /*
1111 * Scan the bestfree if we need it and log the data block header.
1112 */
1113 if (needscan)
c2066e26 1114 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
1115 if (needlog)
1116 xfs_dir2_data_log_header(tp, dbp);
1117 /*
1118 * Pitch the old leaf block.
1119 */
1120 error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1d9025e5
DC
1121 if (error)
1122 return error;
1123
1da177e4
LT
1124 /*
1125 * Now see if the resulting block can be shrunken to shortform.
1126 */
4f6ae1a4 1127 size = xfs_dir2_block_sfsize(dp, hdr, &sfh);
1d9025e5
DC
1128 if (size > XFS_IFORK_DSIZE(dp))
1129 return 0;
1130
1da177e4 1131 return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1da177e4
LT
1132}
1133
1134/*
1135 * Convert the shortform directory to block form.
1136 */
1137int /* error */
1138xfs_dir2_sf_to_block(
1139 xfs_da_args_t *args) /* operation arguments */
1140{
1141 xfs_dir2_db_t blkno; /* dir-relative block # (0) */
4f6ae1a4 1142 xfs_dir2_data_hdr_t *hdr; /* block header */
1da177e4 1143 xfs_dir2_leaf_entry_t *blp; /* block leaf entries */
1d9025e5 1144 struct xfs_buf *bp; /* block buffer */
1da177e4 1145 xfs_dir2_block_tail_t *btp; /* block tail pointer */
1da177e4
LT
1146 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1147 xfs_inode_t *dp; /* incore directory inode */
1148 int dummy; /* trash */
1149 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
1150 int endoffset; /* end of data objects */
1151 int error; /* error return value */
1152 int i; /* index */
1153 xfs_mount_t *mp; /* filesystem mount point */
1154 int needlog; /* need to log block header */
1155 int needscan; /* need to scan block freespc */
1156 int newoffset; /* offset from current entry */
1157 int offset; /* target block offset */
1158 xfs_dir2_sf_entry_t *sfep; /* sf entry pointer */
ac8ba50f
CH
1159 xfs_dir2_sf_hdr_t *oldsfp; /* old shortform header */
1160 xfs_dir2_sf_hdr_t *sfp; /* shortform header */
3d693c6e 1161 __be16 *tagp; /* end of data entry */
1da177e4 1162 xfs_trans_t *tp; /* transaction pointer */
5163f95a 1163 struct xfs_name name;
1da177e4 1164
0b1b213f
CH
1165 trace_xfs_dir2_sf_to_block(args);
1166
1da177e4
LT
1167 dp = args->dp;
1168 tp = args->trans;
1169 mp = dp->i_mount;
1170 ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1171 /*
1172 * Bomb out if the shortform directory is way too short.
1173 */
1174 if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1175 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1176 return XFS_ERROR(EIO);
1177 }
ac8ba50f
CH
1178
1179 oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1180
1da177e4
LT
1181 ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1182 ASSERT(dp->i_df.if_u1.if_data != NULL);
ac8ba50f
CH
1183 ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count));
1184
1da177e4 1185 /*
ac8ba50f 1186 * Copy the directory into a temporary buffer.
1da177e4
LT
1187 * Then pitch the incore inode data so we can make extents.
1188 */
ac8ba50f
CH
1189 sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1190 memcpy(sfp, oldsfp, dp->i_df.if_bytes);
1da177e4 1191
ac8ba50f 1192 xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1da177e4
LT
1193 dp->i_d.di_size = 0;
1194 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
ac8ba50f 1195
1da177e4
LT
1196 /*
1197 * Add block 0 to the inode.
1198 */
1199 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1200 if (error) {
ac8ba50f 1201 kmem_free(sfp);
1da177e4
LT
1202 return error;
1203 }
1204 /*
f5f3d9b0 1205 * Initialize the data block, then convert it to block format.
1da177e4 1206 */
f5f3d9b0 1207 error = xfs_dir3_data_init(args, blkno, &bp);
1da177e4 1208 if (error) {
ac8ba50f 1209 kmem_free(sfp);
1da177e4
LT
1210 return error;
1211 }
f5f3d9b0 1212 xfs_dir3_block_init(mp, bp, dp);
1d9025e5 1213 hdr = bp->b_addr;
f5f3d9b0 1214
1da177e4
LT
1215 /*
1216 * Compute size of block "tail" area.
1217 */
1218 i = (uint)sizeof(*btp) +
ac8ba50f 1219 (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1da177e4
LT
1220 /*
1221 * The whole thing is initialized to free by the init routine.
1222 * Say we're using the leaf and tail area.
1223 */
f5f3d9b0 1224 dup = xfs_dir3_data_unused_p(hdr);
1da177e4
LT
1225 needlog = needscan = 0;
1226 xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1227 &needscan);
1228 ASSERT(needscan == 0);
1229 /*
1230 * Fill in the tail.
1231 */
4f6ae1a4 1232 btp = xfs_dir2_block_tail_p(mp, hdr);
ac8ba50f 1233 btp->count = cpu_to_be32(sfp->count + 2); /* ., .. */
1da177e4 1234 btp->stale = 0;
bbaaf538 1235 blp = xfs_dir2_block_leaf_p(btp);
4f6ae1a4 1236 endoffset = (uint)((char *)blp - (char *)hdr);
1da177e4
LT
1237 /*
1238 * Remove the freespace, we'll manage it.
1239 */
1240 xfs_dir2_data_use_free(tp, bp, dup,
4f6ae1a4 1241 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
ad354eb3 1242 be16_to_cpu(dup->length), &needlog, &needscan);
1da177e4
LT
1243 /*
1244 * Create entry for .
1245 */
f5f3d9b0 1246 dep = xfs_dir3_data_dot_entry_p(hdr);
ff9901c1 1247 dep->inumber = cpu_to_be64(dp->i_ino);
1da177e4
LT
1248 dep->namelen = 1;
1249 dep->name[0] = '.';
bbaaf538 1250 tagp = xfs_dir2_data_entry_tag_p(dep);
4f6ae1a4 1251 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1da177e4 1252 xfs_dir2_data_log_entry(tp, bp, dep);
3c1f9c15 1253 blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
bbaaf538 1254 blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
4f6ae1a4 1255 (char *)dep - (char *)hdr));
1da177e4
LT
1256 /*
1257 * Create entry for ..
1258 */
f5f3d9b0 1259 dep = xfs_dir3_data_dotdot_entry_p(hdr);
8bc38787 1260 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp));
1da177e4
LT
1261 dep->namelen = 2;
1262 dep->name[0] = dep->name[1] = '.';
bbaaf538 1263 tagp = xfs_dir2_data_entry_tag_p(dep);
4f6ae1a4 1264 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1da177e4 1265 xfs_dir2_data_log_entry(tp, bp, dep);
3c1f9c15 1266 blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
bbaaf538 1267 blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
4f6ae1a4 1268 (char *)dep - (char *)hdr));
f5f3d9b0 1269 offset = xfs_dir3_data_first_offset(hdr);
1da177e4
LT
1270 /*
1271 * Loop over existing entries, stuff them in.
1272 */
ac8ba50f
CH
1273 i = 0;
1274 if (!sfp->count)
1da177e4
LT
1275 sfep = NULL;
1276 else
bbaaf538 1277 sfep = xfs_dir2_sf_firstentry(sfp);
1da177e4
LT
1278 /*
1279 * Need to preserve the existing offset values in the sf directory.
1280 * Insert holes (unused entries) where necessary.
1281 */
1282 while (offset < endoffset) {
1283 /*
1284 * sfep is null when we reach the end of the list.
1285 */
1286 if (sfep == NULL)
1287 newoffset = endoffset;
1288 else
bbaaf538 1289 newoffset = xfs_dir2_sf_get_offset(sfep);
1da177e4
LT
1290 /*
1291 * There should be a hole here, make one.
1292 */
1293 if (offset < newoffset) {
4f6ae1a4 1294 dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
ad354eb3
NS
1295 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1296 dup->length = cpu_to_be16(newoffset - offset);
bbaaf538 1297 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
4f6ae1a4 1298 ((char *)dup - (char *)hdr));
1da177e4 1299 xfs_dir2_data_log_unused(tp, bp, dup);
c2066e26 1300 xfs_dir2_data_freeinsert(hdr, dup, &dummy);
ad354eb3 1301 offset += be16_to_cpu(dup->length);
1da177e4
LT
1302 continue;
1303 }
1304 /*
1305 * Copy a real entry.
1306 */
4f6ae1a4 1307 dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset);
8bc38787 1308 dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep));
1da177e4
LT
1309 dep->namelen = sfep->namelen;
1310 memcpy(dep->name, sfep->name, dep->namelen);
bbaaf538 1311 tagp = xfs_dir2_data_entry_tag_p(dep);
4f6ae1a4 1312 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1da177e4 1313 xfs_dir2_data_log_entry(tp, bp, dep);
5163f95a
BN
1314 name.name = sfep->name;
1315 name.len = sfep->namelen;
1316 blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops->
1317 hashname(&name));
bbaaf538 1318 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
4f6ae1a4
CH
1319 (char *)dep - (char *)hdr));
1320 offset = (int)((char *)(tagp + 1) - (char *)hdr);
ac8ba50f 1321 if (++i == sfp->count)
1da177e4
LT
1322 sfep = NULL;
1323 else
bbaaf538 1324 sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1da177e4
LT
1325 }
1326 /* Done with the temporary buffer */
ac8ba50f 1327 kmem_free(sfp);
1da177e4
LT
1328 /*
1329 * Sort the leaf entries by hash value.
1330 */
e922fffa 1331 xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1da177e4
LT
1332 /*
1333 * Log the leaf entry area and tail.
1334 * Already logged the header in data_init, ignore needlog.
1335 */
1336 ASSERT(needscan == 0);
e922fffa 1337 xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1da177e4 1338 xfs_dir2_block_log_tail(tp, bp);
33363fee 1339 xfs_dir3_data_check(dp, bp);
1da177e4
LT
1340 return 0;
1341}
This page took 0.718394 seconds and 5 git commands to generate.