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