Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / xfs / xfs_dir2_block.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_dir2.h"
26 #include "xfs_dmapi.h"
27 #include "xfs_mount.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_dir2_sf.h"
31 #include "xfs_attr_sf.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_dir2_data.h"
36 #include "xfs_dir2_leaf.h"
37 #include "xfs_dir2_block.h"
38 #include "xfs_dir2_trace.h"
39 #include "xfs_error.h"
40
41 /*
42  * Local function prototypes.
43  */
44 static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first,
45                                     int last);
46 static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp);
47 static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp,
48                                      int *entno);
49 static int xfs_dir2_block_sort(const void *a, const void *b);
50
51 static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot;
52
53 /*
54  * One-time startup routine called from xfs_init().
55  */
56 void
57 xfs_dir_startup(void)
58 {
59         xfs_dir_hash_dot = xfs_da_hashname(".", 1);
60         xfs_dir_hash_dotdot = xfs_da_hashname("..", 2);
61 }
62
63 /*
64  * Add an entry to a block directory.
65  */
66 int                                             /* error */
67 xfs_dir2_block_addname(
68         xfs_da_args_t           *args)          /* directory op arguments */
69 {
70         xfs_dir2_data_free_t    *bf;            /* bestfree table in block */
71         xfs_dir2_block_t        *block;         /* directory block structure */
72         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
73         xfs_dabuf_t             *bp;            /* buffer for block */
74         xfs_dir2_block_tail_t   *btp;           /* block tail */
75         int                     compact;        /* need to compact leaf ents */
76         xfs_dir2_data_entry_t   *dep;           /* block data entry */
77         xfs_inode_t             *dp;            /* directory inode */
78         xfs_dir2_data_unused_t  *dup;           /* block unused entry */
79         int                     error;          /* error return value */
80         xfs_dir2_data_unused_t  *enddup=NULL;   /* unused at end of data */
81         xfs_dahash_t            hash;           /* hash value of found entry */
82         int                     high;           /* high index for binary srch */
83         int                     highstale;      /* high stale index */
84         int                     lfloghigh=0;    /* last final leaf to log */
85         int                     lfloglow=0;     /* first final leaf to log */
86         int                     len;            /* length of the new entry */
87         int                     low;            /* low index for binary srch */
88         int                     lowstale;       /* low stale index */
89         int                     mid=0;          /* midpoint for binary srch */
90         xfs_mount_t             *mp;            /* filesystem mount point */
91         int                     needlog;        /* need to log header */
92         int                     needscan;       /* need to rescan freespace */
93         __be16                  *tagp;          /* pointer to tag value */
94         xfs_trans_t             *tp;            /* transaction structure */
95
96         xfs_dir2_trace_args("block_addname", args);
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          */
112         if (unlikely(be32_to_cpu(block->hdr.magic) != XFS_DIR2_BLOCK_MAGIC)) {
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         }
118         len = XFS_DIR2_DATA_ENTSIZE(args->namelen);
119         /*
120          * Set up pointers to parts of the block.
121          */
122         bf = block->hdr.bestfree;
123         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
124         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
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                  */
132                 tagp = (__be16 *)blp - 1;
133                 /*
134                  * Data object just before the first leaf entry.
135                  */
136                 enddup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
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                  */
142                 if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG)
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 *)
149                               ((char *)block + be16_to_cpu(bf[0].offset));
150                         if (dup == enddup) {
151                                 /*
152                                  * It is the biggest freespace, is it too small
153                                  * to hold the new leaf too?
154                                  */
155                                 if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) {
156                                         /*
157                                          * Yes, we use the second-largest
158                                          * entry instead if it works.
159                                          */
160                                         if (be16_to_cpu(bf[1].length) >= len)
161                                                 dup = (xfs_dir2_data_unused_t *)
162                                                       ((char *)block +
163                                                        be16_to_cpu(bf[1].offset));
164                                         else
165                                                 dup = NULL;
166                                 }
167                         } else {
168                                 /*
169                                  * Not the same free entry,
170                                  * just check its length.
171                                  */
172                                 if (be16_to_cpu(dup->length) < len) {
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          */
183         else if (be16_to_cpu(bf[0].length) >= len) {
184                 dup = (xfs_dir2_data_unused_t *)
185                       ((char *)block + be16_to_cpu(bf[0].offset));
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                  */
195                 tagp = (__be16 *)blp - 1;
196                 /*
197                  * Data object just before the first leaf entry.
198                  */
199                 dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
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                  */
204                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
205                         if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) *
206                             (uint)sizeof(*blp) < len)
207                                 dup = NULL;
208                 } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len)
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          */
217         if (args->justcheck)
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                  */
227                 if (args->justcheck || args->total == 0)
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          */
242         if (args->justcheck)
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
254                 for (fromidx = toidx = be32_to_cpu(btp->count) - 1,
255                         highstale = lfloghigh = -1;
256                      fromidx >= 0;
257                      fromidx--) {
258                         if (be32_to_cpu(blp[fromidx].address) == XFS_DIR2_NULL_DATAPTR) {
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                 }
271                 lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1);
272                 lfloghigh -= be32_to_cpu(btp->stale) - 1;
273                 be32_add(&btp->count, -(be32_to_cpu(btp->stale) - 1));
274                 xfs_dir2_data_make_free(tp, bp,
275                         (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
276                         (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)),
277                         &needlog, &needscan);
278                 blp += be32_to_cpu(btp->stale) - 1;
279                 btp->stale = cpu_to_be32(1);
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) {
285                         xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
286                                 &needlog, NULL);
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          */
294         else if (btp->stale) {
295                 lfloglow = be32_to_cpu(btp->count);
296                 lfloghigh = -1;
297         }
298         /*
299          * Find the slot that's first lower than our hash value, -1 if none.
300          */
301         for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
302                 mid = (low + high) >> 1;
303                 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
304                         break;
305                 if (hash < args->hashval)
306                         low = mid + 1;
307                 else
308                         high = mid - 1;
309         }
310         while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
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)
322                         ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
323                          sizeof(*blp)),
324                         (xfs_dir2_data_aoff_t)sizeof(*blp),
325                         &needlog, &needscan);
326                 /*
327                  * Update the tail (entry count).
328                  */
329                 be32_add(&btp->count, 1);
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,
336                                 &needlog, NULL);
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 &&
357                         be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
358                      lowstale--)
359                         continue;
360                 for (highstale = mid + 1;
361                      highstale < be32_to_cpu(btp->count) &&
362                         be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
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 &&
370                     (highstale == be32_to_cpu(btp->count) ||
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 {
382                         ASSERT(highstale < be32_to_cpu(btp->count));
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                 }
390                 be32_add(&btp->stale, -1);
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          */
399         blp[mid].hashval = cpu_to_be32(args->hashval);
400         blp[mid].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
401                                 (char *)dep - (char *)block));
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          */
412         dep->inumber = cpu_to_be64(args->inumber);
413         dep->namelen = args->namelen;
414         memcpy(dep->name, args->name, args->namelen);
415         tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
416         *tagp = cpu_to_be16((char *)dep - (char *)block);
417         /*
418          * Clean up the bestfree array and log the header, tail, and entry.
419          */
420         if (needscan)
421                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
422                         NULL);
423         if (needlog)
424                 xfs_dir2_data_log_header(tp, bp);
425         xfs_dir2_block_log_tail(tp, bp);
426         xfs_dir2_data_log_entry(tp, bp, dep);
427         xfs_dir2_data_check(dp, bp);
428         xfs_da_buf_done(bp);
429         return 0;
430 }
431
432 /*
433  * Readdir for block directories.
434  */
435 int                                             /* error */
436 xfs_dir2_block_getdents(
437         xfs_trans_t             *tp,            /* transaction (NULL) */
438         xfs_inode_t             *dp,            /* incore inode */
439         uio_t                   *uio,           /* caller's buffer control */
440         int                     *eofp,          /* eof reached? (out) */
441         xfs_dirent_t            *dbp,           /* caller's buffer */
442         xfs_dir2_put_t          put)            /* abi's formatting function */
443 {
444         xfs_dir2_block_t        *block;         /* directory block structure */
445         xfs_dabuf_t             *bp;            /* buffer for block */
446         xfs_dir2_block_tail_t   *btp;           /* block tail */
447         xfs_dir2_data_entry_t   *dep;           /* block data entry */
448         xfs_dir2_data_unused_t  *dup;           /* block unused entry */
449         char                    *endptr;        /* end of the data entries */
450         int                     error;          /* error return value */
451         xfs_mount_t             *mp;            /* filesystem mount point */
452         xfs_dir2_put_args_t     p;              /* arg package for put rtn */
453         char                    *ptr;           /* current data entry */
454         int                     wantoff;        /* starting block offset */
455
456         mp = dp->i_mount;
457         /*
458          * If the block number in the offset is out of range, we're done.
459          */
460         if (XFS_DIR2_DATAPTR_TO_DB(mp, uio->uio_offset) > mp->m_dirdatablk) {
461                 *eofp = 1;
462                 return 0;
463         }
464         /*
465          * Can't read the block, give up, else get dabuf in bp.
466          */
467         if ((error =
468             xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
469                 return error;
470         }
471         ASSERT(bp != NULL);
472         /*
473          * Extract the byte offset we start at from the seek pointer.
474          * We'll skip entries before this.
475          */
476         wantoff = XFS_DIR2_DATAPTR_TO_OFF(mp, uio->uio_offset);
477         block = bp->data;
478         xfs_dir2_data_check(dp, bp);
479         /*
480          * Set up values for the loop.
481          */
482         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
483         ptr = (char *)block->u;
484         endptr = (char *)XFS_DIR2_BLOCK_LEAF_P(btp);
485         p.dbp = dbp;
486         p.put = put;
487         p.uio = uio;
488         /*
489          * Loop over the data portion of the block.
490          * Each object is a real entry (dep) or an unused one (dup).
491          */
492         while (ptr < endptr) {
493                 dup = (xfs_dir2_data_unused_t *)ptr;
494                 /*
495                  * Unused, skip it.
496                  */
497                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
498                         ptr += be16_to_cpu(dup->length);
499                         continue;
500                 }
501
502                 dep = (xfs_dir2_data_entry_t *)ptr;
503
504                 /*
505                  * Bump pointer for the next iteration.
506                  */
507                 ptr += XFS_DIR2_DATA_ENTSIZE(dep->namelen);
508                 /*
509                  * The entry is before the desired starting point, skip it.
510                  */
511                 if ((char *)dep - (char *)block < wantoff)
512                         continue;
513                 /*
514                  * Set up argument structure for put routine.
515                  */
516                 p.namelen = dep->namelen;
517
518                 p.cook = XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
519                                                     ptr - (char *)block);
520                 p.ino = be64_to_cpu(dep->inumber);
521 #if XFS_BIG_INUMS
522                 p.ino += mp->m_inoadd;
523 #endif
524                 p.name = (char *)dep->name;
525
526                 /*
527                  * Put the entry in the caller's buffer.
528                  */
529                 error = p.put(&p);
530
531                 /*
532                  * If it didn't fit, set the final offset to here & return.
533                  */
534                 if (!p.done) {
535                         uio->uio_offset =
536                                 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk,
537                                         (char *)dep - (char *)block);
538                         xfs_da_brelse(tp, bp);
539                         return error;
540                 }
541         }
542
543         /*
544          * Reached the end of the block.
545          * Set the offset to a non-existent block 1 and return.
546          */
547         *eofp = 1;
548
549         uio->uio_offset =
550                 XFS_DIR2_DB_OFF_TO_DATAPTR(mp, mp->m_dirdatablk + 1, 0);
551
552         xfs_da_brelse(tp, bp);
553
554         return 0;
555 }
556
557 /*
558  * Log leaf entries from the block.
559  */
560 static void
561 xfs_dir2_block_log_leaf(
562         xfs_trans_t             *tp,            /* transaction structure */
563         xfs_dabuf_t             *bp,            /* block buffer */
564         int                     first,          /* index of first logged leaf */
565         int                     last)           /* index of last logged leaf */
566 {
567         xfs_dir2_block_t        *block;         /* directory block structure */
568         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
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;
574         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
575         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
576         xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
577                 (uint)((char *)&blp[last + 1] - (char *)block - 1));
578 }
579
580 /*
581  * Log the block tail.
582  */
583 static void
584 xfs_dir2_block_log_tail(
585         xfs_trans_t             *tp,            /* transaction structure */
586         xfs_dabuf_t             *bp)            /* block buffer */
587 {
588         xfs_dir2_block_t        *block;         /* directory block structure */
589         xfs_dir2_block_tail_t   *btp;           /* block tail */
590         xfs_mount_t             *mp;            /* filesystem mount point */
591
592         mp = tp->t_mountp;
593         block = bp->data;
594         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
595         xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
596                 (uint)((char *)(btp + 1) - (char *)block - 1));
597 }
598
599 /*
600  * Look up an entry in the block.  This is the external routine,
601  * xfs_dir2_block_lookup_int does the real work.
602  */
603 int                                             /* error */
604 xfs_dir2_block_lookup(
605         xfs_da_args_t           *args)          /* dir lookup arguments */
606 {
607         xfs_dir2_block_t        *block;         /* block structure */
608         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
609         xfs_dabuf_t             *bp;            /* block buffer */
610         xfs_dir2_block_tail_t   *btp;           /* block tail */
611         xfs_dir2_data_entry_t   *dep;           /* block data entry */
612         xfs_inode_t             *dp;            /* incore inode */
613         int                     ent;            /* entry index */
614         int                     error;          /* error return value */
615         xfs_mount_t             *mp;            /* filesystem mount point */
616
617         xfs_dir2_trace_args("block_lookup", args);
618         /*
619          * Get the buffer, look up the entry.
620          * If not found (ENOENT) then return, have no buffer.
621          */
622         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
623                 return error;
624         dp = args->dp;
625         mp = dp->i_mount;
626         block = bp->data;
627         xfs_dir2_data_check(dp, bp);
628         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
629         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
630         /*
631          * Get the offset from the leaf entry, to point to the data.
632          */
633         dep = (xfs_dir2_data_entry_t *)
634               ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
635         /*
636          * Fill in inode number, release the block.
637          */
638         args->inumber = be64_to_cpu(dep->inumber);
639         xfs_da_brelse(args->trans, bp);
640         return XFS_ERROR(EEXIST);
641 }
642
643 /*
644  * Internal block lookup routine.
645  */
646 static int                                      /* error */
647 xfs_dir2_block_lookup_int(
648         xfs_da_args_t           *args,          /* dir lookup arguments */
649         xfs_dabuf_t             **bpp,          /* returned block buffer */
650         int                     *entno)         /* returned entry number */
651 {
652         xfs_dir2_dataptr_t      addr;           /* data entry address */
653         xfs_dir2_block_t        *block;         /* block structure */
654         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
655         xfs_dabuf_t             *bp;            /* block buffer */
656         xfs_dir2_block_tail_t   *btp;           /* block tail */
657         xfs_dir2_data_entry_t   *dep;           /* block data entry */
658         xfs_inode_t             *dp;            /* incore inode */
659         int                     error;          /* error return value */
660         xfs_dahash_t            hash;           /* found hash value */
661         int                     high;           /* binary search high index */
662         int                     low;            /* binary search low index */
663         int                     mid;            /* binary search current idx */
664         xfs_mount_t             *mp;            /* filesystem mount point */
665         xfs_trans_t             *tp;            /* transaction pointer */
666
667         dp = args->dp;
668         tp = args->trans;
669         mp = dp->i_mount;
670         /*
671          * Read the buffer, return error if we can't get it.
672          */
673         if ((error =
674             xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
675                 return error;
676         }
677         ASSERT(bp != NULL);
678         block = bp->data;
679         xfs_dir2_data_check(dp, bp);
680         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
681         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
682         /*
683          * Loop doing a binary search for our hash value.
684          * Find our entry, ENOENT if it's not there.
685          */
686         for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
687                 ASSERT(low <= high);
688                 mid = (low + high) >> 1;
689                 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
690                         break;
691                 if (hash < args->hashval)
692                         low = mid + 1;
693                 else
694                         high = mid - 1;
695                 if (low > high) {
696                         ASSERT(args->oknoent);
697                         xfs_da_brelse(tp, bp);
698                         return XFS_ERROR(ENOENT);
699                 }
700         }
701         /*
702          * Back up to the first one with the right hash value.
703          */
704         while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
705                 mid--;
706         }
707         /*
708          * Now loop forward through all the entries with the
709          * right hash value looking for our name.
710          */
711         do {
712                 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
713                         continue;
714                 /*
715                  * Get pointer to the entry from the leaf.
716                  */
717                 dep = (xfs_dir2_data_entry_t *)
718                         ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, addr));
719                 /*
720                  * Compare, if it's right give back buffer & entry number.
721                  */
722                 if (dep->namelen == args->namelen &&
723                     dep->name[0] == args->name[0] &&
724                     memcmp(dep->name, args->name, args->namelen) == 0) {
725                         *bpp = bp;
726                         *entno = mid;
727                         return 0;
728                 }
729         } while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
730         /*
731          * No match, release the buffer and return ENOENT.
732          */
733         ASSERT(args->oknoent);
734         xfs_da_brelse(tp, bp);
735         return XFS_ERROR(ENOENT);
736 }
737
738 /*
739  * Remove an entry from a block format directory.
740  * If that makes the block small enough to fit in shortform, transform it.
741  */
742 int                                             /* error */
743 xfs_dir2_block_removename(
744         xfs_da_args_t           *args)          /* directory operation args */
745 {
746         xfs_dir2_block_t        *block;         /* block structure */
747         xfs_dir2_leaf_entry_t   *blp;           /* block leaf pointer */
748         xfs_dabuf_t             *bp;            /* block buffer */
749         xfs_dir2_block_tail_t   *btp;           /* block tail */
750         xfs_dir2_data_entry_t   *dep;           /* block data entry */
751         xfs_inode_t             *dp;            /* incore inode */
752         int                     ent;            /* block leaf entry index */
753         int                     error;          /* error return value */
754         xfs_mount_t             *mp;            /* filesystem mount point */
755         int                     needlog;        /* need to log block header */
756         int                     needscan;       /* need to fixup bestfree */
757         xfs_dir2_sf_hdr_t       sfh;            /* shortform header */
758         int                     size;           /* shortform size */
759         xfs_trans_t             *tp;            /* transaction pointer */
760
761         xfs_dir2_trace_args("block_removename", args);
762         /*
763          * Look up the entry in the block.  Gets the buffer and entry index.
764          * It will always be there, the vnodeops level does a lookup first.
765          */
766         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
767                 return error;
768         }
769         dp = args->dp;
770         tp = args->trans;
771         mp = dp->i_mount;
772         block = bp->data;
773         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
774         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
775         /*
776          * Point to the data entry using the leaf entry.
777          */
778         dep = (xfs_dir2_data_entry_t *)
779               ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
780         /*
781          * Mark the data entry's space free.
782          */
783         needlog = needscan = 0;
784         xfs_dir2_data_make_free(tp, bp,
785                 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
786                 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
787         /*
788          * Fix up the block tail.
789          */
790         be32_add(&btp->stale, 1);
791         xfs_dir2_block_log_tail(tp, bp);
792         /*
793          * Remove the leaf entry by marking it stale.
794          */
795         blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
796         xfs_dir2_block_log_leaf(tp, bp, ent, ent);
797         /*
798          * Fix up bestfree, log the header if necessary.
799          */
800         if (needscan)
801                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
802                         NULL);
803         if (needlog)
804                 xfs_dir2_data_log_header(tp, bp);
805         xfs_dir2_data_check(dp, bp);
806         /*
807          * See if the size as a shortform is good enough.
808          */
809         if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
810             XFS_IFORK_DSIZE(dp)) {
811                 xfs_da_buf_done(bp);
812                 return 0;
813         }
814         /*
815          * If it works, do the conversion.
816          */
817         return xfs_dir2_block_to_sf(args, bp, size, &sfh);
818 }
819
820 /*
821  * Replace an entry in a V2 block directory.
822  * Change the inode number to the new value.
823  */
824 int                                             /* error */
825 xfs_dir2_block_replace(
826         xfs_da_args_t           *args)          /* directory operation args */
827 {
828         xfs_dir2_block_t        *block;         /* block structure */
829         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
830         xfs_dabuf_t             *bp;            /* block buffer */
831         xfs_dir2_block_tail_t   *btp;           /* block tail */
832         xfs_dir2_data_entry_t   *dep;           /* block data entry */
833         xfs_inode_t             *dp;            /* incore inode */
834         int                     ent;            /* leaf entry index */
835         int                     error;          /* error return value */
836         xfs_mount_t             *mp;            /* filesystem mount point */
837
838         xfs_dir2_trace_args("block_replace", args);
839         /*
840          * Lookup the entry in the directory.  Get buffer and entry index.
841          * This will always succeed since the caller has already done a lookup.
842          */
843         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
844                 return error;
845         }
846         dp = args->dp;
847         mp = dp->i_mount;
848         block = bp->data;
849         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
850         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
851         /*
852          * Point to the data entry we need to change.
853          */
854         dep = (xfs_dir2_data_entry_t *)
855               ((char *)block + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(blp[ent].address)));
856         ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
857         /*
858          * Change the inode number to the new value.
859          */
860         dep->inumber = cpu_to_be64(args->inumber);
861         xfs_dir2_data_log_entry(args->trans, bp, dep);
862         xfs_dir2_data_check(dp, bp);
863         xfs_da_buf_done(bp);
864         return 0;
865 }
866
867 /*
868  * Qsort comparison routine for the block leaf entries.
869  */
870 static int                                      /* sort order */
871 xfs_dir2_block_sort(
872         const void                      *a,     /* first leaf entry */
873         const void                      *b)     /* second leaf entry */
874 {
875         const xfs_dir2_leaf_entry_t     *la;    /* first leaf entry */
876         const xfs_dir2_leaf_entry_t     *lb;    /* second leaf entry */
877
878         la = a;
879         lb = b;
880         return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
881                 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
882 }
883
884 /*
885  * Convert a V2 leaf directory to a V2 block directory if possible.
886  */
887 int                                             /* error */
888 xfs_dir2_leaf_to_block(
889         xfs_da_args_t           *args,          /* operation arguments */
890         xfs_dabuf_t             *lbp,           /* leaf buffer */
891         xfs_dabuf_t             *dbp)           /* data buffer */
892 {
893         __be16                  *bestsp;        /* leaf bests table */
894         xfs_dir2_block_t        *block;         /* block structure */
895         xfs_dir2_block_tail_t   *btp;           /* block tail */
896         xfs_inode_t             *dp;            /* incore directory inode */
897         xfs_dir2_data_unused_t  *dup;           /* unused data entry */
898         int                     error;          /* error return value */
899         int                     from;           /* leaf from index */
900         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
901         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
902         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
903         xfs_mount_t             *mp;            /* file system mount point */
904         int                     needlog;        /* need to log data header */
905         int                     needscan;       /* need to scan for bestfree */
906         xfs_dir2_sf_hdr_t       sfh;            /* shortform header */
907         int                     size;           /* bytes used */
908         __be16                  *tagp;          /* end of entry (tag) */
909         int                     to;             /* block/leaf to index */
910         xfs_trans_t             *tp;            /* transaction pointer */
911
912         xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
913         dp = args->dp;
914         tp = args->trans;
915         mp = dp->i_mount;
916         leaf = lbp->data;
917         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
918         ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
919         /*
920          * If there are data blocks other than the first one, take this
921          * opportunity to remove trailing empty data blocks that may have
922          * been left behind during no-space-reservation operations.
923          * These will show up in the leaf bests table.
924          */
925         while (dp->i_d.di_size > mp->m_dirblksize) {
926                 bestsp = XFS_DIR2_LEAF_BESTS_P(ltp);
927                 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
928                     mp->m_dirblksize - (uint)sizeof(block->hdr)) {
929                         if ((error =
930                             xfs_dir2_leaf_trim_data(args, lbp,
931                                     (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
932                                 goto out;
933                 } else {
934                         error = 0;
935                         goto out;
936                 }
937         }
938         /*
939          * Read the data block if we don't already have it, give up if it fails.
940          */
941         if (dbp == NULL &&
942             (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
943                     XFS_DATA_FORK))) {
944                 goto out;
945         }
946         block = dbp->data;
947         ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
948         /*
949          * Size of the "leaf" area in the block.
950          */
951         size = (uint)sizeof(block->tail) +
952                (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
953         /*
954          * Look at the last data entry.
955          */
956         tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
957         dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
958         /*
959          * If it's not free or is too short we can't do it.
960          */
961         if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
962             be16_to_cpu(dup->length) < size) {
963                 error = 0;
964                 goto out;
965         }
966         /*
967          * Start converting it to block form.
968          */
969         block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
970         needlog = 1;
971         needscan = 0;
972         /*
973          * Use up the space at the end of the block (blp/btp).
974          */
975         xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
976                 &needlog, &needscan);
977         /*
978          * Initialize the block tail.
979          */
980         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
981         btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
982         btp->stale = 0;
983         xfs_dir2_block_log_tail(tp, dbp);
984         /*
985          * Initialize the block leaf area.  We compact out stale entries.
986          */
987         lep = XFS_DIR2_BLOCK_LEAF_P(btp);
988         for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
989                 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
990                         continue;
991                 lep[to++] = leaf->ents[from];
992         }
993         ASSERT(to == be32_to_cpu(btp->count));
994         xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
995         /*
996          * Scan the bestfree if we need it and log the data block header.
997          */
998         if (needscan)
999                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog,
1000                         NULL);
1001         if (needlog)
1002                 xfs_dir2_data_log_header(tp, dbp);
1003         /*
1004          * Pitch the old leaf block.
1005          */
1006         error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
1007         lbp = NULL;
1008         if (error) {
1009                 goto out;
1010         }
1011         /*
1012          * Now see if the resulting block can be shrunken to shortform.
1013          */
1014         if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
1015             XFS_IFORK_DSIZE(dp)) {
1016                 error = 0;
1017                 goto out;
1018         }
1019         return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
1020 out:
1021         if (lbp)
1022                 xfs_da_buf_done(lbp);
1023         if (dbp)
1024                 xfs_da_buf_done(dbp);
1025         return error;
1026 }
1027
1028 /*
1029  * Convert the shortform directory to block form.
1030  */
1031 int                                             /* error */
1032 xfs_dir2_sf_to_block(
1033         xfs_da_args_t           *args)          /* operation arguments */
1034 {
1035         xfs_dir2_db_t           blkno;          /* dir-relative block # (0) */
1036         xfs_dir2_block_t        *block;         /* block structure */
1037         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
1038         xfs_dabuf_t             *bp;            /* block buffer */
1039         xfs_dir2_block_tail_t   *btp;           /* block tail pointer */
1040         char                    *buf;           /* sf buffer */
1041         int                     buf_len;
1042         xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
1043         xfs_inode_t             *dp;            /* incore directory inode */
1044         int                     dummy;          /* trash */
1045         xfs_dir2_data_unused_t  *dup;           /* unused entry pointer */
1046         int                     endoffset;      /* end of data objects */
1047         int                     error;          /* error return value */
1048         int                     i;              /* index */
1049         xfs_mount_t             *mp;            /* filesystem mount point */
1050         int                     needlog;        /* need to log block header */
1051         int                     needscan;       /* need to scan block freespc */
1052         int                     newoffset;      /* offset from current entry */
1053         int                     offset;         /* target block offset */
1054         xfs_dir2_sf_entry_t     *sfep;          /* sf entry pointer */
1055         xfs_dir2_sf_t           *sfp;           /* shortform structure */
1056         __be16                  *tagp;          /* end of data entry */
1057         xfs_trans_t             *tp;            /* transaction pointer */
1058
1059         xfs_dir2_trace_args("sf_to_block", args);
1060         dp = args->dp;
1061         tp = args->trans;
1062         mp = dp->i_mount;
1063         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1064         /*
1065          * Bomb out if the shortform directory is way too short.
1066          */
1067         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1068                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1069                 return XFS_ERROR(EIO);
1070         }
1071         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1072         ASSERT(dp->i_df.if_u1.if_data != NULL);
1073         sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1074         ASSERT(dp->i_d.di_size >= XFS_DIR2_SF_HDR_SIZE(sfp->hdr.i8count));
1075         /*
1076          * Copy the directory into the stack buffer.
1077          * Then pitch the incore inode data so we can make extents.
1078          */
1079
1080         buf_len = dp->i_df.if_bytes;
1081         buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1082
1083         memcpy(buf, sfp, dp->i_df.if_bytes);
1084         xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1085         dp->i_d.di_size = 0;
1086         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1087         /*
1088          * Reset pointer - old sfp is gone.
1089          */
1090         sfp = (xfs_dir2_sf_t *)buf;
1091         /*
1092          * Add block 0 to the inode.
1093          */
1094         error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1095         if (error) {
1096                 kmem_free(buf, buf_len);
1097                 return error;
1098         }
1099         /*
1100          * Initialize the data block.
1101          */
1102         error = xfs_dir2_data_init(args, blkno, &bp);
1103         if (error) {
1104                 kmem_free(buf, buf_len);
1105                 return error;
1106         }
1107         block = bp->data;
1108         block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1109         /*
1110          * Compute size of block "tail" area.
1111          */
1112         i = (uint)sizeof(*btp) +
1113             (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1114         /*
1115          * The whole thing is initialized to free by the init routine.
1116          * Say we're using the leaf and tail area.
1117          */
1118         dup = (xfs_dir2_data_unused_t *)block->u;
1119         needlog = needscan = 0;
1120         xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1121                 &needscan);
1122         ASSERT(needscan == 0);
1123         /*
1124          * Fill in the tail.
1125          */
1126         btp = XFS_DIR2_BLOCK_TAIL_P(mp, block);
1127         btp->count = cpu_to_be32(sfp->hdr.count + 2);   /* ., .. */
1128         btp->stale = 0;
1129         blp = XFS_DIR2_BLOCK_LEAF_P(btp);
1130         endoffset = (uint)((char *)blp - (char *)block);
1131         /*
1132          * Remove the freespace, we'll manage it.
1133          */
1134         xfs_dir2_data_use_free(tp, bp, dup,
1135                 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
1136                 be16_to_cpu(dup->length), &needlog, &needscan);
1137         /*
1138          * Create entry for .
1139          */
1140         dep = (xfs_dir2_data_entry_t *)
1141               ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
1142         dep->inumber = cpu_to_be64(dp->i_ino);
1143         dep->namelen = 1;
1144         dep->name[0] = '.';
1145         tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1146         *tagp = cpu_to_be16((char *)dep - (char *)block);
1147         xfs_dir2_data_log_entry(tp, bp, dep);
1148         blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1149         blp[0].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1150                                 (char *)dep - (char *)block));
1151         /*
1152          * Create entry for ..
1153          */
1154         dep = (xfs_dir2_data_entry_t *)
1155                 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
1156         dep->inumber = cpu_to_be64(XFS_DIR2_SF_GET_INUMBER(sfp, &sfp->hdr.parent));
1157         dep->namelen = 2;
1158         dep->name[0] = dep->name[1] = '.';
1159         tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1160         *tagp = cpu_to_be16((char *)dep - (char *)block);
1161         xfs_dir2_data_log_entry(tp, bp, dep);
1162         blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1163         blp[1].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1164                                 (char *)dep - (char *)block));
1165         offset = XFS_DIR2_DATA_FIRST_OFFSET;
1166         /*
1167          * Loop over existing entries, stuff them in.
1168          */
1169         if ((i = 0) == sfp->hdr.count)
1170                 sfep = NULL;
1171         else
1172                 sfep = XFS_DIR2_SF_FIRSTENTRY(sfp);
1173         /*
1174          * Need to preserve the existing offset values in the sf directory.
1175          * Insert holes (unused entries) where necessary.
1176          */
1177         while (offset < endoffset) {
1178                 /*
1179                  * sfep is null when we reach the end of the list.
1180                  */
1181                 if (sfep == NULL)
1182                         newoffset = endoffset;
1183                 else
1184                         newoffset = XFS_DIR2_SF_GET_OFFSET(sfep);
1185                 /*
1186                  * There should be a hole here, make one.
1187                  */
1188                 if (offset < newoffset) {
1189                         dup = (xfs_dir2_data_unused_t *)
1190                               ((char *)block + offset);
1191                         dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1192                         dup->length = cpu_to_be16(newoffset - offset);
1193                         *XFS_DIR2_DATA_UNUSED_TAG_P(dup) = cpu_to_be16(
1194                                 ((char *)dup - (char *)block));
1195                         xfs_dir2_data_log_unused(tp, bp, dup);
1196                         (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1197                                 dup, &dummy);
1198                         offset += be16_to_cpu(dup->length);
1199                         continue;
1200                 }
1201                 /*
1202                  * Copy a real entry.
1203                  */
1204                 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
1205                 dep->inumber = cpu_to_be64(XFS_DIR2_SF_GET_INUMBER(sfp,
1206                                 XFS_DIR2_SF_INUMBERP(sfep)));
1207                 dep->namelen = sfep->namelen;
1208                 memcpy(dep->name, sfep->name, dep->namelen);
1209                 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
1210                 *tagp = cpu_to_be16((char *)dep - (char *)block);
1211                 xfs_dir2_data_log_entry(tp, bp, dep);
1212                 blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
1213                                         (char *)sfep->name, sfep->namelen));
1214                 blp[2 + i].address = cpu_to_be32(XFS_DIR2_BYTE_TO_DATAPTR(mp,
1215                                                  (char *)dep - (char *)block));
1216                 offset = (int)((char *)(tagp + 1) - (char *)block);
1217                 if (++i == sfp->hdr.count)
1218                         sfep = NULL;
1219                 else
1220                         sfep = XFS_DIR2_SF_NEXTENTRY(sfp, sfep);
1221         }
1222         /* Done with the temporary buffer */
1223         kmem_free(buf, buf_len);
1224         /*
1225          * Sort the leaf entries by hash value.
1226          */
1227         xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1228         /*
1229          * Log the leaf entry area and tail.
1230          * Already logged the header in data_init, ignore needlog.
1231          */
1232         ASSERT(needscan == 0);
1233         xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1234         xfs_dir2_block_log_tail(tp, bp);
1235         xfs_dir2_data_check(dp, bp);
1236         xfs_da_buf_done(bp);
1237         return 0;
1238 }