]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_dir2_block.c
f6b919af7b828c468c0d7bdebac6d87fffde64b9
[karo-tx-linux.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, &needlog);
286                         needscan = 0;
287                 }
288         }
289         /*
290          * Set leaf logging boundaries to impossible state.
291          * For the no-stale case they're set explicitly.
292          */
293         else if (btp->stale) {
294                 lfloglow = be32_to_cpu(btp->count);
295                 lfloghigh = -1;
296         }
297         /*
298          * Find the slot that's first lower than our hash value, -1 if none.
299          */
300         for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) {
301                 mid = (low + high) >> 1;
302                 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
303                         break;
304                 if (hash < args->hashval)
305                         low = mid + 1;
306                 else
307                         high = mid - 1;
308         }
309         while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) {
310                 mid--;
311         }
312         /*
313          * No stale entries, will use enddup space to hold new leaf.
314          */
315         if (!btp->stale) {
316                 /*
317                  * Mark the space needed for the new leaf entry, now in use.
318                  */
319                 xfs_dir2_data_use_free(tp, bp, enddup,
320                         (xfs_dir2_data_aoff_t)
321                         ((char *)enddup - (char *)block + be16_to_cpu(enddup->length) -
322                          sizeof(*blp)),
323                         (xfs_dir2_data_aoff_t)sizeof(*blp),
324                         &needlog, &needscan);
325                 /*
326                  * Update the tail (entry count).
327                  */
328                 be32_add(&btp->count, 1);
329                 /*
330                  * If we now need to rebuild the bestfree map, do so.
331                  * This needs to happen before the next call to use_free.
332                  */
333                 if (needscan) {
334                         xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block,
335                                 &needlog);
336                         needscan = 0;
337                 }
338                 /*
339                  * Adjust pointer to the first leaf entry, we're about to move
340                  * the table up one to open up space for the new leaf entry.
341                  * Then adjust our index to match.
342                  */
343                 blp--;
344                 mid++;
345                 if (mid)
346                         memmove(blp, &blp[1], mid * sizeof(*blp));
347                 lfloglow = 0;
348                 lfloghigh = mid;
349         }
350         /*
351          * Use a stale leaf for our new entry.
352          */
353         else {
354                 for (lowstale = mid;
355                      lowstale >= 0 &&
356                         be32_to_cpu(blp[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
357                      lowstale--)
358                         continue;
359                 for (highstale = mid + 1;
360                      highstale < be32_to_cpu(btp->count) &&
361                         be32_to_cpu(blp[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
362                         (lowstale < 0 || mid - lowstale > highstale - mid);
363                      highstale++)
364                         continue;
365                 /*
366                  * Move entries toward the low-numbered stale entry.
367                  */
368                 if (lowstale >= 0 &&
369                     (highstale == be32_to_cpu(btp->count) ||
370                      mid - lowstale <= highstale - mid)) {
371                         if (mid - lowstale)
372                                 memmove(&blp[lowstale], &blp[lowstale + 1],
373                                         (mid - lowstale) * sizeof(*blp));
374                         lfloglow = MIN(lowstale, lfloglow);
375                         lfloghigh = MAX(mid, lfloghigh);
376                 }
377                 /*
378                  * Move entries toward the high-numbered stale entry.
379                  */
380                 else {
381                         ASSERT(highstale < be32_to_cpu(btp->count));
382                         mid++;
383                         if (highstale - mid)
384                                 memmove(&blp[mid + 1], &blp[mid],
385                                         (highstale - mid) * sizeof(*blp));
386                         lfloglow = MIN(mid, lfloglow);
387                         lfloghigh = MAX(highstale, lfloghigh);
388                 }
389                 be32_add(&btp->stale, -1);
390         }
391         /*
392          * Point to the new data entry.
393          */
394         dep = (xfs_dir2_data_entry_t *)dup;
395         /*
396          * Fill in the leaf entry.
397          */
398         blp[mid].hashval = cpu_to_be32(args->hashval);
399         blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
400                                 (char *)dep - (char *)block));
401         xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh);
402         /*
403          * Mark space for the data entry used.
404          */
405         xfs_dir2_data_use_free(tp, bp, dup,
406                 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
407                 (xfs_dir2_data_aoff_t)len, &needlog, &needscan);
408         /*
409          * Create the new data entry.
410          */
411         dep->inumber = cpu_to_be64(args->inumber);
412         dep->namelen = args->namelen;
413         memcpy(dep->name, args->name, args->namelen);
414         tagp = xfs_dir2_data_entry_tag_p(dep);
415         *tagp = cpu_to_be16((char *)dep - (char *)block);
416         /*
417          * Clean up the bestfree array and log the header, tail, and entry.
418          */
419         if (needscan)
420                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
421         if (needlog)
422                 xfs_dir2_data_log_header(tp, bp);
423         xfs_dir2_block_log_tail(tp, bp);
424         xfs_dir2_data_log_entry(tp, bp, dep);
425         xfs_dir2_data_check(dp, bp);
426         xfs_da_buf_done(bp);
427         return 0;
428 }
429
430 /*
431  * Readdir for block directories.
432  */
433 int                                             /* error */
434 xfs_dir2_block_getdents(
435         xfs_inode_t             *dp,            /* incore inode */
436         void                    *dirent,
437         xfs_off_t               *offset,
438         filldir_t               filldir)
439 {
440         xfs_dir2_block_t        *block;         /* directory block structure */
441         xfs_dabuf_t             *bp;            /* buffer for block */
442         xfs_dir2_block_tail_t   *btp;           /* block tail */
443         xfs_dir2_data_entry_t   *dep;           /* block data entry */
444         xfs_dir2_data_unused_t  *dup;           /* block unused entry */
445         char                    *endptr;        /* end of the data entries */
446         int                     error;          /* error return value */
447         xfs_mount_t             *mp;            /* filesystem mount point */
448         char                    *ptr;           /* current data entry */
449         int                     wantoff;        /* starting block offset */
450         xfs_ino_t               ino;
451         xfs_off_t               cook;
452
453         mp = dp->i_mount;
454         /*
455          * If the block number in the offset is out of range, we're done.
456          */
457         if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) {
458                 return 0;
459         }
460         /*
461          * Can't read the block, give up, else get dabuf in bp.
462          */
463         error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1,
464                                 &bp, XFS_DATA_FORK);
465         if (error)
466                 return error;
467
468         ASSERT(bp != NULL);
469         /*
470          * Extract the byte offset we start at from the seek pointer.
471          * We'll skip entries before this.
472          */
473         wantoff = xfs_dir2_dataptr_to_off(mp, *offset);
474         block = bp->data;
475         xfs_dir2_data_check(dp, bp);
476         /*
477          * Set up values for the loop.
478          */
479         btp = xfs_dir2_block_tail_p(mp, block);
480         ptr = (char *)block->u;
481         endptr = (char *)xfs_dir2_block_leaf_p(btp);
482
483         /*
484          * Loop over the data portion of the block.
485          * Each object is a real entry (dep) or an unused one (dup).
486          */
487         while (ptr < endptr) {
488                 dup = (xfs_dir2_data_unused_t *)ptr;
489                 /*
490                  * Unused, skip it.
491                  */
492                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
493                         ptr += be16_to_cpu(dup->length);
494                         continue;
495                 }
496
497                 dep = (xfs_dir2_data_entry_t *)ptr;
498
499                 /*
500                  * Bump pointer for the next iteration.
501                  */
502                 ptr += xfs_dir2_data_entsize(dep->namelen);
503                 /*
504                  * The entry is before the desired starting point, skip it.
505                  */
506                 if ((char *)dep - (char *)block < wantoff)
507                         continue;
508
509                 cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
510                                                     ptr - (char *)block);
511                 ino = be64_to_cpu(dep->inumber);
512 #if XFS_BIG_INUMS
513                 ino += mp->m_inoadd;
514 #endif
515
516                 /*
517                  * If it didn't fit, set the final offset to here & return.
518                  */
519                 if (filldir(dirent, dep->name, dep->namelen, cook,
520                             ino, DT_UNKNOWN)) {
521                         *offset = xfs_dir2_db_off_to_dataptr(mp,
522                                         mp->m_dirdatablk,
523                                         (char *)dep - (char *)block);
524                         xfs_da_brelse(NULL, bp);
525                         return 0;
526                 }
527         }
528
529         /*
530          * Reached the end of the block.
531          * Set the offset to a non-existent block 1 and return.
532          */
533         *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0);
534         xfs_da_brelse(NULL, bp);
535         return 0;
536 }
537
538 /*
539  * Log leaf entries from the block.
540  */
541 static void
542 xfs_dir2_block_log_leaf(
543         xfs_trans_t             *tp,            /* transaction structure */
544         xfs_dabuf_t             *bp,            /* block buffer */
545         int                     first,          /* index of first logged leaf */
546         int                     last)           /* index of last logged leaf */
547 {
548         xfs_dir2_block_t        *block;         /* directory block structure */
549         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
550         xfs_dir2_block_tail_t   *btp;           /* block tail */
551         xfs_mount_t             *mp;            /* filesystem mount point */
552
553         mp = tp->t_mountp;
554         block = bp->data;
555         btp = xfs_dir2_block_tail_p(mp, block);
556         blp = xfs_dir2_block_leaf_p(btp);
557         xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)block),
558                 (uint)((char *)&blp[last + 1] - (char *)block - 1));
559 }
560
561 /*
562  * Log the block tail.
563  */
564 static void
565 xfs_dir2_block_log_tail(
566         xfs_trans_t             *tp,            /* transaction structure */
567         xfs_dabuf_t             *bp)            /* block buffer */
568 {
569         xfs_dir2_block_t        *block;         /* directory block structure */
570         xfs_dir2_block_tail_t   *btp;           /* block tail */
571         xfs_mount_t             *mp;            /* filesystem mount point */
572
573         mp = tp->t_mountp;
574         block = bp->data;
575         btp = xfs_dir2_block_tail_p(mp, block);
576         xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)block),
577                 (uint)((char *)(btp + 1) - (char *)block - 1));
578 }
579
580 /*
581  * Look up an entry in the block.  This is the external routine,
582  * xfs_dir2_block_lookup_int does the real work.
583  */
584 int                                             /* error */
585 xfs_dir2_block_lookup(
586         xfs_da_args_t           *args)          /* dir lookup arguments */
587 {
588         xfs_dir2_block_t        *block;         /* block structure */
589         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
590         xfs_dabuf_t             *bp;            /* block buffer */
591         xfs_dir2_block_tail_t   *btp;           /* block tail */
592         xfs_dir2_data_entry_t   *dep;           /* block data entry */
593         xfs_inode_t             *dp;            /* incore inode */
594         int                     ent;            /* entry index */
595         int                     error;          /* error return value */
596         xfs_mount_t             *mp;            /* filesystem mount point */
597
598         xfs_dir2_trace_args("block_lookup", args);
599         /*
600          * Get the buffer, look up the entry.
601          * If not found (ENOENT) then return, have no buffer.
602          */
603         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent)))
604                 return error;
605         dp = args->dp;
606         mp = dp->i_mount;
607         block = bp->data;
608         xfs_dir2_data_check(dp, bp);
609         btp = xfs_dir2_block_tail_p(mp, block);
610         blp = xfs_dir2_block_leaf_p(btp);
611         /*
612          * Get the offset from the leaf entry, to point to the data.
613          */
614         dep = (xfs_dir2_data_entry_t *)
615               ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
616         /*
617          * Fill in inode number, release the block.
618          */
619         args->inumber = be64_to_cpu(dep->inumber);
620         xfs_da_brelse(args->trans, bp);
621         return XFS_ERROR(EEXIST);
622 }
623
624 /*
625  * Internal block lookup routine.
626  */
627 static int                                      /* error */
628 xfs_dir2_block_lookup_int(
629         xfs_da_args_t           *args,          /* dir lookup arguments */
630         xfs_dabuf_t             **bpp,          /* returned block buffer */
631         int                     *entno)         /* returned entry number */
632 {
633         xfs_dir2_dataptr_t      addr;           /* data entry address */
634         xfs_dir2_block_t        *block;         /* block structure */
635         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
636         xfs_dabuf_t             *bp;            /* block buffer */
637         xfs_dir2_block_tail_t   *btp;           /* block tail */
638         xfs_dir2_data_entry_t   *dep;           /* block data entry */
639         xfs_inode_t             *dp;            /* incore inode */
640         int                     error;          /* error return value */
641         xfs_dahash_t            hash;           /* found hash value */
642         int                     high;           /* binary search high index */
643         int                     low;            /* binary search low index */
644         int                     mid;            /* binary search current idx */
645         xfs_mount_t             *mp;            /* filesystem mount point */
646         xfs_trans_t             *tp;            /* transaction pointer */
647
648         dp = args->dp;
649         tp = args->trans;
650         mp = dp->i_mount;
651         /*
652          * Read the buffer, return error if we can't get it.
653          */
654         if ((error =
655             xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) {
656                 return error;
657         }
658         ASSERT(bp != NULL);
659         block = bp->data;
660         xfs_dir2_data_check(dp, bp);
661         btp = xfs_dir2_block_tail_p(mp, block);
662         blp = xfs_dir2_block_leaf_p(btp);
663         /*
664          * Loop doing a binary search for our hash value.
665          * Find our entry, ENOENT if it's not there.
666          */
667         for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) {
668                 ASSERT(low <= high);
669                 mid = (low + high) >> 1;
670                 if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval)
671                         break;
672                 if (hash < args->hashval)
673                         low = mid + 1;
674                 else
675                         high = mid - 1;
676                 if (low > high) {
677                         ASSERT(args->oknoent);
678                         xfs_da_brelse(tp, bp);
679                         return XFS_ERROR(ENOENT);
680                 }
681         }
682         /*
683          * Back up to the first one with the right hash value.
684          */
685         while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) {
686                 mid--;
687         }
688         /*
689          * Now loop forward through all the entries with the
690          * right hash value looking for our name.
691          */
692         do {
693                 if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR)
694                         continue;
695                 /*
696                  * Get pointer to the entry from the leaf.
697                  */
698                 dep = (xfs_dir2_data_entry_t *)
699                         ((char *)block + xfs_dir2_dataptr_to_off(mp, addr));
700                 /*
701                  * Compare, if it's right give back buffer & entry number.
702                  */
703                 if (dep->namelen == args->namelen &&
704                     dep->name[0] == args->name[0] &&
705                     memcmp(dep->name, args->name, args->namelen) == 0) {
706                         *bpp = bp;
707                         *entno = mid;
708                         return 0;
709                 }
710         } while (++mid < be32_to_cpu(btp->count) && be32_to_cpu(blp[mid].hashval) == hash);
711         /*
712          * No match, release the buffer and return ENOENT.
713          */
714         ASSERT(args->oknoent);
715         xfs_da_brelse(tp, bp);
716         return XFS_ERROR(ENOENT);
717 }
718
719 /*
720  * Remove an entry from a block format directory.
721  * If that makes the block small enough to fit in shortform, transform it.
722  */
723 int                                             /* error */
724 xfs_dir2_block_removename(
725         xfs_da_args_t           *args)          /* directory operation args */
726 {
727         xfs_dir2_block_t        *block;         /* block structure */
728         xfs_dir2_leaf_entry_t   *blp;           /* block leaf pointer */
729         xfs_dabuf_t             *bp;            /* block buffer */
730         xfs_dir2_block_tail_t   *btp;           /* block tail */
731         xfs_dir2_data_entry_t   *dep;           /* block data entry */
732         xfs_inode_t             *dp;            /* incore inode */
733         int                     ent;            /* block leaf entry index */
734         int                     error;          /* error return value */
735         xfs_mount_t             *mp;            /* filesystem mount point */
736         int                     needlog;        /* need to log block header */
737         int                     needscan;       /* need to fixup bestfree */
738         xfs_dir2_sf_hdr_t       sfh;            /* shortform header */
739         int                     size;           /* shortform size */
740         xfs_trans_t             *tp;            /* transaction pointer */
741
742         xfs_dir2_trace_args("block_removename", args);
743         /*
744          * Look up the entry in the block.  Gets the buffer and entry index.
745          * It will always be there, the vnodeops level does a lookup first.
746          */
747         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
748                 return error;
749         }
750         dp = args->dp;
751         tp = args->trans;
752         mp = dp->i_mount;
753         block = bp->data;
754         btp = xfs_dir2_block_tail_p(mp, block);
755         blp = xfs_dir2_block_leaf_p(btp);
756         /*
757          * Point to the data entry using the leaf entry.
758          */
759         dep = (xfs_dir2_data_entry_t *)
760               ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
761         /*
762          * Mark the data entry's space free.
763          */
764         needlog = needscan = 0;
765         xfs_dir2_data_make_free(tp, bp,
766                 (xfs_dir2_data_aoff_t)((char *)dep - (char *)block),
767                 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
768         /*
769          * Fix up the block tail.
770          */
771         be32_add(&btp->stale, 1);
772         xfs_dir2_block_log_tail(tp, bp);
773         /*
774          * Remove the leaf entry by marking it stale.
775          */
776         blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
777         xfs_dir2_block_log_leaf(tp, bp, ent, ent);
778         /*
779          * Fix up bestfree, log the header if necessary.
780          */
781         if (needscan)
782                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
783         if (needlog)
784                 xfs_dir2_data_log_header(tp, bp);
785         xfs_dir2_data_check(dp, bp);
786         /*
787          * See if the size as a shortform is good enough.
788          */
789         if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
790             XFS_IFORK_DSIZE(dp)) {
791                 xfs_da_buf_done(bp);
792                 return 0;
793         }
794         /*
795          * If it works, do the conversion.
796          */
797         return xfs_dir2_block_to_sf(args, bp, size, &sfh);
798 }
799
800 /*
801  * Replace an entry in a V2 block directory.
802  * Change the inode number to the new value.
803  */
804 int                                             /* error */
805 xfs_dir2_block_replace(
806         xfs_da_args_t           *args)          /* directory operation args */
807 {
808         xfs_dir2_block_t        *block;         /* block structure */
809         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
810         xfs_dabuf_t             *bp;            /* block buffer */
811         xfs_dir2_block_tail_t   *btp;           /* block tail */
812         xfs_dir2_data_entry_t   *dep;           /* block data entry */
813         xfs_inode_t             *dp;            /* incore inode */
814         int                     ent;            /* leaf entry index */
815         int                     error;          /* error return value */
816         xfs_mount_t             *mp;            /* filesystem mount point */
817
818         xfs_dir2_trace_args("block_replace", args);
819         /*
820          * Lookup the entry in the directory.  Get buffer and entry index.
821          * This will always succeed since the caller has already done a lookup.
822          */
823         if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) {
824                 return error;
825         }
826         dp = args->dp;
827         mp = dp->i_mount;
828         block = bp->data;
829         btp = xfs_dir2_block_tail_p(mp, block);
830         blp = xfs_dir2_block_leaf_p(btp);
831         /*
832          * Point to the data entry we need to change.
833          */
834         dep = (xfs_dir2_data_entry_t *)
835               ((char *)block + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address)));
836         ASSERT(be64_to_cpu(dep->inumber) != args->inumber);
837         /*
838          * Change the inode number to the new value.
839          */
840         dep->inumber = cpu_to_be64(args->inumber);
841         xfs_dir2_data_log_entry(args->trans, bp, dep);
842         xfs_dir2_data_check(dp, bp);
843         xfs_da_buf_done(bp);
844         return 0;
845 }
846
847 /*
848  * Qsort comparison routine for the block leaf entries.
849  */
850 static int                                      /* sort order */
851 xfs_dir2_block_sort(
852         const void                      *a,     /* first leaf entry */
853         const void                      *b)     /* second leaf entry */
854 {
855         const xfs_dir2_leaf_entry_t     *la;    /* first leaf entry */
856         const xfs_dir2_leaf_entry_t     *lb;    /* second leaf entry */
857
858         la = a;
859         lb = b;
860         return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 :
861                 (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0);
862 }
863
864 /*
865  * Convert a V2 leaf directory to a V2 block directory if possible.
866  */
867 int                                             /* error */
868 xfs_dir2_leaf_to_block(
869         xfs_da_args_t           *args,          /* operation arguments */
870         xfs_dabuf_t             *lbp,           /* leaf buffer */
871         xfs_dabuf_t             *dbp)           /* data buffer */
872 {
873         __be16                  *bestsp;        /* leaf bests table */
874         xfs_dir2_block_t        *block;         /* block structure */
875         xfs_dir2_block_tail_t   *btp;           /* block tail */
876         xfs_inode_t             *dp;            /* incore directory inode */
877         xfs_dir2_data_unused_t  *dup;           /* unused data entry */
878         int                     error;          /* error return value */
879         int                     from;           /* leaf from index */
880         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
881         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
882         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
883         xfs_mount_t             *mp;            /* file system mount point */
884         int                     needlog;        /* need to log data header */
885         int                     needscan;       /* need to scan for bestfree */
886         xfs_dir2_sf_hdr_t       sfh;            /* shortform header */
887         int                     size;           /* bytes used */
888         __be16                  *tagp;          /* end of entry (tag) */
889         int                     to;             /* block/leaf to index */
890         xfs_trans_t             *tp;            /* transaction pointer */
891
892         xfs_dir2_trace_args_bb("leaf_to_block", args, lbp, dbp);
893         dp = args->dp;
894         tp = args->trans;
895         mp = dp->i_mount;
896         leaf = lbp->data;
897         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
898         ltp = xfs_dir2_leaf_tail_p(mp, leaf);
899         /*
900          * If there are data blocks other than the first one, take this
901          * opportunity to remove trailing empty data blocks that may have
902          * been left behind during no-space-reservation operations.
903          * These will show up in the leaf bests table.
904          */
905         while (dp->i_d.di_size > mp->m_dirblksize) {
906                 bestsp = xfs_dir2_leaf_bests_p(ltp);
907                 if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) ==
908                     mp->m_dirblksize - (uint)sizeof(block->hdr)) {
909                         if ((error =
910                             xfs_dir2_leaf_trim_data(args, lbp,
911                                     (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1))))
912                                 goto out;
913                 } else {
914                         error = 0;
915                         goto out;
916                 }
917         }
918         /*
919          * Read the data block if we don't already have it, give up if it fails.
920          */
921         if (dbp == NULL &&
922             (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp,
923                     XFS_DATA_FORK))) {
924                 goto out;
925         }
926         block = dbp->data;
927         ASSERT(be32_to_cpu(block->hdr.magic) == XFS_DIR2_DATA_MAGIC);
928         /*
929          * Size of the "leaf" area in the block.
930          */
931         size = (uint)sizeof(block->tail) +
932                (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
933         /*
934          * Look at the last data entry.
935          */
936         tagp = (__be16 *)((char *)block + mp->m_dirblksize) - 1;
937         dup = (xfs_dir2_data_unused_t *)((char *)block + be16_to_cpu(*tagp));
938         /*
939          * If it's not free or is too short we can't do it.
940          */
941         if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG ||
942             be16_to_cpu(dup->length) < size) {
943                 error = 0;
944                 goto out;
945         }
946         /*
947          * Start converting it to block form.
948          */
949         block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
950         needlog = 1;
951         needscan = 0;
952         /*
953          * Use up the space at the end of the block (blp/btp).
954          */
955         xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size,
956                 &needlog, &needscan);
957         /*
958          * Initialize the block tail.
959          */
960         btp = xfs_dir2_block_tail_p(mp, block);
961         btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale));
962         btp->stale = 0;
963         xfs_dir2_block_log_tail(tp, dbp);
964         /*
965          * Initialize the block leaf area.  We compact out stale entries.
966          */
967         lep = xfs_dir2_block_leaf_p(btp);
968         for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
969                 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
970                         continue;
971                 lep[to++] = leaf->ents[from];
972         }
973         ASSERT(to == be32_to_cpu(btp->count));
974         xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1);
975         /*
976          * Scan the bestfree if we need it and log the data block header.
977          */
978         if (needscan)
979                 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
980         if (needlog)
981                 xfs_dir2_data_log_header(tp, dbp);
982         /*
983          * Pitch the old leaf block.
984          */
985         error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp);
986         lbp = NULL;
987         if (error) {
988                 goto out;
989         }
990         /*
991          * Now see if the resulting block can be shrunken to shortform.
992          */
993         if ((size = xfs_dir2_block_sfsize(dp, block, &sfh)) >
994             XFS_IFORK_DSIZE(dp)) {
995                 error = 0;
996                 goto out;
997         }
998         return xfs_dir2_block_to_sf(args, dbp, size, &sfh);
999 out:
1000         if (lbp)
1001                 xfs_da_buf_done(lbp);
1002         if (dbp)
1003                 xfs_da_buf_done(dbp);
1004         return error;
1005 }
1006
1007 /*
1008  * Convert the shortform directory to block form.
1009  */
1010 int                                             /* error */
1011 xfs_dir2_sf_to_block(
1012         xfs_da_args_t           *args)          /* operation arguments */
1013 {
1014         xfs_dir2_db_t           blkno;          /* dir-relative block # (0) */
1015         xfs_dir2_block_t        *block;         /* block structure */
1016         xfs_dir2_leaf_entry_t   *blp;           /* block leaf entries */
1017         xfs_dabuf_t             *bp;            /* block buffer */
1018         xfs_dir2_block_tail_t   *btp;           /* block tail pointer */
1019         char                    *buf;           /* sf buffer */
1020         int                     buf_len;
1021         xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
1022         xfs_inode_t             *dp;            /* incore directory inode */
1023         int                     dummy;          /* trash */
1024         xfs_dir2_data_unused_t  *dup;           /* unused entry pointer */
1025         int                     endoffset;      /* end of data objects */
1026         int                     error;          /* error return value */
1027         int                     i;              /* index */
1028         xfs_mount_t             *mp;            /* filesystem mount point */
1029         int                     needlog;        /* need to log block header */
1030         int                     needscan;       /* need to scan block freespc */
1031         int                     newoffset;      /* offset from current entry */
1032         int                     offset;         /* target block offset */
1033         xfs_dir2_sf_entry_t     *sfep;          /* sf entry pointer */
1034         xfs_dir2_sf_t           *sfp;           /* shortform structure */
1035         __be16                  *tagp;          /* end of data entry */
1036         xfs_trans_t             *tp;            /* transaction pointer */
1037
1038         xfs_dir2_trace_args("sf_to_block", args);
1039         dp = args->dp;
1040         tp = args->trans;
1041         mp = dp->i_mount;
1042         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
1043         /*
1044          * Bomb out if the shortform directory is way too short.
1045          */
1046         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
1047                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1048                 return XFS_ERROR(EIO);
1049         }
1050         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
1051         ASSERT(dp->i_df.if_u1.if_data != NULL);
1052         sfp = (xfs_dir2_sf_t *)dp->i_df.if_u1.if_data;
1053         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->hdr.i8count));
1054         /*
1055          * Copy the directory into the stack buffer.
1056          * Then pitch the incore inode data so we can make extents.
1057          */
1058
1059         buf_len = dp->i_df.if_bytes;
1060         buf = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP);
1061
1062         memcpy(buf, sfp, dp->i_df.if_bytes);
1063         xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK);
1064         dp->i_d.di_size = 0;
1065         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1066         /*
1067          * Reset pointer - old sfp is gone.
1068          */
1069         sfp = (xfs_dir2_sf_t *)buf;
1070         /*
1071          * Add block 0 to the inode.
1072          */
1073         error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno);
1074         if (error) {
1075                 kmem_free(buf, buf_len);
1076                 return error;
1077         }
1078         /*
1079          * Initialize the data block.
1080          */
1081         error = xfs_dir2_data_init(args, blkno, &bp);
1082         if (error) {
1083                 kmem_free(buf, buf_len);
1084                 return error;
1085         }
1086         block = bp->data;
1087         block->hdr.magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC);
1088         /*
1089          * Compute size of block "tail" area.
1090          */
1091         i = (uint)sizeof(*btp) +
1092             (sfp->hdr.count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t);
1093         /*
1094          * The whole thing is initialized to free by the init routine.
1095          * Say we're using the leaf and tail area.
1096          */
1097         dup = (xfs_dir2_data_unused_t *)block->u;
1098         needlog = needscan = 0;
1099         xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog,
1100                 &needscan);
1101         ASSERT(needscan == 0);
1102         /*
1103          * Fill in the tail.
1104          */
1105         btp = xfs_dir2_block_tail_p(mp, block);
1106         btp->count = cpu_to_be32(sfp->hdr.count + 2);   /* ., .. */
1107         btp->stale = 0;
1108         blp = xfs_dir2_block_leaf_p(btp);
1109         endoffset = (uint)((char *)blp - (char *)block);
1110         /*
1111          * Remove the freespace, we'll manage it.
1112          */
1113         xfs_dir2_data_use_free(tp, bp, dup,
1114                 (xfs_dir2_data_aoff_t)((char *)dup - (char *)block),
1115                 be16_to_cpu(dup->length), &needlog, &needscan);
1116         /*
1117          * Create entry for .
1118          */
1119         dep = (xfs_dir2_data_entry_t *)
1120               ((char *)block + XFS_DIR2_DATA_DOT_OFFSET);
1121         dep->inumber = cpu_to_be64(dp->i_ino);
1122         dep->namelen = 1;
1123         dep->name[0] = '.';
1124         tagp = xfs_dir2_data_entry_tag_p(dep);
1125         *tagp = cpu_to_be16((char *)dep - (char *)block);
1126         xfs_dir2_data_log_entry(tp, bp, dep);
1127         blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot);
1128         blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1129                                 (char *)dep - (char *)block));
1130         /*
1131          * Create entry for ..
1132          */
1133         dep = (xfs_dir2_data_entry_t *)
1134                 ((char *)block + XFS_DIR2_DATA_DOTDOT_OFFSET);
1135         dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp, &sfp->hdr.parent));
1136         dep->namelen = 2;
1137         dep->name[0] = dep->name[1] = '.';
1138         tagp = xfs_dir2_data_entry_tag_p(dep);
1139         *tagp = cpu_to_be16((char *)dep - (char *)block);
1140         xfs_dir2_data_log_entry(tp, bp, dep);
1141         blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot);
1142         blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1143                                 (char *)dep - (char *)block));
1144         offset = XFS_DIR2_DATA_FIRST_OFFSET;
1145         /*
1146          * Loop over existing entries, stuff them in.
1147          */
1148         if ((i = 0) == sfp->hdr.count)
1149                 sfep = NULL;
1150         else
1151                 sfep = xfs_dir2_sf_firstentry(sfp);
1152         /*
1153          * Need to preserve the existing offset values in the sf directory.
1154          * Insert holes (unused entries) where necessary.
1155          */
1156         while (offset < endoffset) {
1157                 /*
1158                  * sfep is null when we reach the end of the list.
1159                  */
1160                 if (sfep == NULL)
1161                         newoffset = endoffset;
1162                 else
1163                         newoffset = xfs_dir2_sf_get_offset(sfep);
1164                 /*
1165                  * There should be a hole here, make one.
1166                  */
1167                 if (offset < newoffset) {
1168                         dup = (xfs_dir2_data_unused_t *)
1169                               ((char *)block + offset);
1170                         dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1171                         dup->length = cpu_to_be16(newoffset - offset);
1172                         *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16(
1173                                 ((char *)dup - (char *)block));
1174                         xfs_dir2_data_log_unused(tp, bp, dup);
1175                         (void)xfs_dir2_data_freeinsert((xfs_dir2_data_t *)block,
1176                                 dup, &dummy);
1177                         offset += be16_to_cpu(dup->length);
1178                         continue;
1179                 }
1180                 /*
1181                  * Copy a real entry.
1182                  */
1183                 dep = (xfs_dir2_data_entry_t *)((char *)block + newoffset);
1184                 dep->inumber = cpu_to_be64(xfs_dir2_sf_get_inumber(sfp,
1185                                 xfs_dir2_sf_inumberp(sfep)));
1186                 dep->namelen = sfep->namelen;
1187                 memcpy(dep->name, sfep->name, dep->namelen);
1188                 tagp = xfs_dir2_data_entry_tag_p(dep);
1189                 *tagp = cpu_to_be16((char *)dep - (char *)block);
1190                 xfs_dir2_data_log_entry(tp, bp, dep);
1191                 blp[2 + i].hashval = cpu_to_be32(xfs_da_hashname(
1192                                         (char *)sfep->name, sfep->namelen));
1193                 blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp,
1194                                                  (char *)dep - (char *)block));
1195                 offset = (int)((char *)(tagp + 1) - (char *)block);
1196                 if (++i == sfp->hdr.count)
1197                         sfep = NULL;
1198                 else
1199                         sfep = xfs_dir2_sf_nextentry(sfp, sfep);
1200         }
1201         /* Done with the temporary buffer */
1202         kmem_free(buf, buf_len);
1203         /*
1204          * Sort the leaf entries by hash value.
1205          */
1206         xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort);
1207         /*
1208          * Log the leaf entry area and tail.
1209          * Already logged the header in data_init, ignore needlog.
1210          */
1211         ASSERT(needscan == 0);
1212         xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1);
1213         xfs_dir2_block_log_tail(tp, bp);
1214         xfs_dir2_data_check(dp, bp);
1215         xfs_da_buf_done(bp);
1216         return 0;
1217 }