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