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