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