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