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