[XFS] endianess annotations for xfs_da_node_hdr_t
[safe/jmp/linux-2.6] / fs / xfs / xfs_dir_leaf.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_dir.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_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir_sf.h"
34 #include "xfs_dir2_sf.h"
35 #include "xfs_attr_sf.h"
36 #include "xfs_dinode.h"
37 #include "xfs_inode.h"
38 #include "xfs_inode_item.h"
39 #include "xfs_alloc.h"
40 #include "xfs_btree.h"
41 #include "xfs_bmap.h"
42 #include "xfs_dir_leaf.h"
43 #include "xfs_error.h"
44
45 /*
46  * xfs_dir_leaf.c
47  *
48  * Routines to implement leaf blocks of directories as Btrees of hashed names.
49  */
50
51 /*========================================================================
52  * Function prototypes for the kernel.
53  *========================================================================*/
54
55 /*
56  * Routines used for growing the Btree.
57  */
58 STATIC void xfs_dir_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
59                                               int insertion_index,
60                                               int freemap_index);
61 STATIC int xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer,
62                                             int musthave, int justcheck);
63 STATIC void xfs_dir_leaf_rebalance(xfs_da_state_t *state,
64                                                   xfs_da_state_blk_t *blk1,
65                                                   xfs_da_state_blk_t *blk2);
66 STATIC int xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
67                                           xfs_da_state_blk_t *leaf_blk_1,
68                                           xfs_da_state_blk_t *leaf_blk_2,
69                                           int *number_entries_in_blk1,
70                                           int *number_namebytes_in_blk1);
71
72 STATIC int xfs_dir_leaf_create(struct xfs_da_args *args,
73                                 xfs_dablk_t which_block,
74                                 struct xfs_dabuf **bpp);
75
76 /*
77  * Utility routines.
78  */
79 STATIC void xfs_dir_leaf_moveents(xfs_dir_leafblock_t *src_leaf,
80                                               int src_start,
81                                               xfs_dir_leafblock_t *dst_leaf,
82                                               int dst_start, int move_count,
83                                               xfs_mount_t *mp);
84
85
86 /*========================================================================
87  * External routines when dirsize < XFS_IFORK_DSIZE(dp).
88  *========================================================================*/
89
90
91 /*
92  * Validate a given inode number.
93  */
94 int
95 xfs_dir_ino_validate(xfs_mount_t *mp, xfs_ino_t ino)
96 {
97         xfs_agblock_t   agblkno;
98         xfs_agino_t     agino;
99         xfs_agnumber_t  agno;
100         int             ino_ok;
101         int             ioff;
102
103         agno = XFS_INO_TO_AGNO(mp, ino);
104         agblkno = XFS_INO_TO_AGBNO(mp, ino);
105         ioff = XFS_INO_TO_OFFSET(mp, ino);
106         agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
107         ino_ok =
108                 agno < mp->m_sb.sb_agcount &&
109                 agblkno < mp->m_sb.sb_agblocks &&
110                 agblkno != 0 &&
111                 ioff < (1 << mp->m_sb.sb_inopblog) &&
112                 XFS_AGINO_TO_INO(mp, agno, agino) == ino;
113         if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
114                         XFS_RANDOM_DIR_INO_VALIDATE))) {
115                 xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
116                                 (unsigned long long) ino);
117                 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
118                 return XFS_ERROR(EFSCORRUPTED);
119         }
120         return 0;
121 }
122
123 /*
124  * Create the initial contents of a shortform directory.
125  */
126 int
127 xfs_dir_shortform_create(xfs_da_args_t *args, xfs_ino_t parent)
128 {
129         xfs_dir_sf_hdr_t *hdr;
130         xfs_inode_t *dp;
131
132         dp = args->dp;
133         ASSERT(dp != NULL);
134         ASSERT(dp->i_d.di_size == 0);
135         if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
136                 dp->i_df.if_flags &= ~XFS_IFEXTENTS;    /* just in case */
137                 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
138                 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
139                 dp->i_df.if_flags |= XFS_IFINLINE;
140         }
141         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
142         ASSERT(dp->i_df.if_bytes == 0);
143         xfs_idata_realloc(dp, sizeof(*hdr), XFS_DATA_FORK);
144         hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
145         XFS_DIR_SF_PUT_DIRINO(&parent, &hdr->parent);
146
147         hdr->count = 0;
148         dp->i_d.di_size = sizeof(*hdr);
149         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
150         return 0;
151 }
152
153 /*
154  * Add a name to the shortform directory structure.
155  * Overflow from the inode has already been checked for.
156  */
157 int
158 xfs_dir_shortform_addname(xfs_da_args_t *args)
159 {
160         xfs_dir_shortform_t *sf;
161         xfs_dir_sf_entry_t *sfe;
162         int i, offset, size;
163         xfs_inode_t *dp;
164
165         dp = args->dp;
166         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
167         /*
168          * Catch the case where the conversion from shortform to leaf
169          * failed part way through.
170          */
171         if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
172                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
173                 return XFS_ERROR(EIO);
174         }
175         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
176         ASSERT(dp->i_df.if_u1.if_data != NULL);
177         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
178         sfe = &sf->list[0];
179         for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
180                 if (sfe->namelen == args->namelen &&
181                     args->name[0] == sfe->name[0] &&
182                     memcmp(args->name, sfe->name, args->namelen) == 0)
183                         return XFS_ERROR(EEXIST);
184                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
185         }
186
187         offset = (int)((char *)sfe - (char *)sf);
188         size = XFS_DIR_SF_ENTSIZE_BYNAME(args->namelen);
189         xfs_idata_realloc(dp, size, XFS_DATA_FORK);
190         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
191         sfe = (xfs_dir_sf_entry_t *)((char *)sf + offset);
192
193         XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
194         sfe->namelen = args->namelen;
195         memcpy(sfe->name, args->name, sfe->namelen);
196         INT_MOD(sf->hdr.count, ARCH_CONVERT, +1);
197
198         dp->i_d.di_size += size;
199         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
200
201         return 0;
202 }
203
204 /*
205  * Remove a name from the shortform directory structure.
206  */
207 int
208 xfs_dir_shortform_removename(xfs_da_args_t *args)
209 {
210         xfs_dir_shortform_t *sf;
211         xfs_dir_sf_entry_t *sfe;
212         int base, size = 0, i;
213         xfs_inode_t *dp;
214
215         dp = args->dp;
216         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
217         /*
218          * Catch the case where the conversion from shortform to leaf
219          * failed part way through.
220          */
221         if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
222                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
223                 return XFS_ERROR(EIO);
224         }
225         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
226         ASSERT(dp->i_df.if_u1.if_data != NULL);
227         base = sizeof(xfs_dir_sf_hdr_t);
228         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
229         sfe = &sf->list[0];
230         for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
231                 size = XFS_DIR_SF_ENTSIZE_BYENTRY(sfe);
232                 if (sfe->namelen == args->namelen &&
233                     sfe->name[0] == args->name[0] &&
234                     memcmp(sfe->name, args->name, args->namelen) == 0)
235                         break;
236                 base += size;
237                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
238         }
239         if (i < 0) {
240                 ASSERT(args->oknoent);
241                 return XFS_ERROR(ENOENT);
242         }
243
244         if ((base + size) != dp->i_d.di_size) {
245                 memmove(&((char *)sf)[base], &((char *)sf)[base+size],
246                                               dp->i_d.di_size - (base+size));
247         }
248         INT_MOD(sf->hdr.count, ARCH_CONVERT, -1);
249
250         xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
251         dp->i_d.di_size -= size;
252         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
253
254         return 0;
255 }
256
257 /*
258  * Look up a name in a shortform directory structure.
259  */
260 int
261 xfs_dir_shortform_lookup(xfs_da_args_t *args)
262 {
263         xfs_dir_shortform_t *sf;
264         xfs_dir_sf_entry_t *sfe;
265         int i;
266         xfs_inode_t *dp;
267
268         dp = args->dp;
269         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
270         /*
271          * Catch the case where the conversion from shortform to leaf
272          * failed part way through.
273          */
274         if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
275                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
276                 return XFS_ERROR(EIO);
277         }
278         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
279         ASSERT(dp->i_df.if_u1.if_data != NULL);
280         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
281         if (args->namelen == 2 &&
282             args->name[0] == '.' && args->name[1] == '.') {
283                 XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &args->inumber);
284                 return(XFS_ERROR(EEXIST));
285         }
286         if (args->namelen == 1 && args->name[0] == '.') {
287                 args->inumber = dp->i_ino;
288                 return(XFS_ERROR(EEXIST));
289         }
290         sfe = &sf->list[0];
291         for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
292                 if (sfe->namelen == args->namelen &&
293                     sfe->name[0] == args->name[0] &&
294                     memcmp(args->name, sfe->name, args->namelen) == 0) {
295                         XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args->inumber);
296                         return(XFS_ERROR(EEXIST));
297                 }
298                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
299         }
300         ASSERT(args->oknoent);
301         return(XFS_ERROR(ENOENT));
302 }
303
304 /*
305  * Convert from using the shortform to the leaf.
306  */
307 int
308 xfs_dir_shortform_to_leaf(xfs_da_args_t *iargs)
309 {
310         xfs_inode_t *dp;
311         xfs_dir_shortform_t *sf;
312         xfs_dir_sf_entry_t *sfe;
313         xfs_da_args_t args;
314         xfs_ino_t inumber;
315         char *tmpbuffer;
316         int retval, i, size;
317         xfs_dablk_t blkno;
318         xfs_dabuf_t *bp;
319
320         dp = iargs->dp;
321         /*
322          * Catch the case where the conversion from shortform to leaf
323          * failed part way through.
324          */
325         if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
326                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
327                 return XFS_ERROR(EIO);
328         }
329         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
330         ASSERT(dp->i_df.if_u1.if_data != NULL);
331         size = dp->i_df.if_bytes;
332         tmpbuffer = kmem_alloc(size, KM_SLEEP);
333         ASSERT(tmpbuffer != NULL);
334
335         memcpy(tmpbuffer, dp->i_df.if_u1.if_data, size);
336
337         sf = (xfs_dir_shortform_t *)tmpbuffer;
338         XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &inumber);
339
340         xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
341         dp->i_d.di_size = 0;
342         xfs_trans_log_inode(iargs->trans, dp, XFS_ILOG_CORE);
343         retval = xfs_da_grow_inode(iargs, &blkno);
344         if (retval)
345                 goto out;
346
347         ASSERT(blkno == 0);
348         retval = xfs_dir_leaf_create(iargs, blkno, &bp);
349         if (retval)
350                 goto out;
351         xfs_da_buf_done(bp);
352
353         args.name = ".";
354         args.namelen = 1;
355         args.hashval = xfs_dir_hash_dot;
356         args.inumber = dp->i_ino;
357         args.dp = dp;
358         args.firstblock = iargs->firstblock;
359         args.flist = iargs->flist;
360         args.total = iargs->total;
361         args.whichfork = XFS_DATA_FORK;
362         args.trans = iargs->trans;
363         args.justcheck = 0;
364         args.addname = args.oknoent = 1;
365         retval = xfs_dir_leaf_addname(&args);
366         if (retval)
367                 goto out;
368
369         args.name = "..";
370         args.namelen = 2;
371         args.hashval = xfs_dir_hash_dotdot;
372         args.inumber = inumber;
373         retval = xfs_dir_leaf_addname(&args);
374         if (retval)
375                 goto out;
376
377         sfe = &sf->list[0];
378         for (i = 0; i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
379                 args.name = (char *)(sfe->name);
380                 args.namelen = sfe->namelen;
381                 args.hashval = xfs_da_hashname((char *)(sfe->name),
382                                                sfe->namelen);
383                 XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args.inumber);
384                 retval = xfs_dir_leaf_addname(&args);
385                 if (retval)
386                         goto out;
387                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
388         }
389         retval = 0;
390
391 out:
392         kmem_free(tmpbuffer, size);
393         return retval;
394 }
395
396 STATIC int
397 xfs_dir_shortform_compare(const void *a, const void *b)
398 {
399         xfs_dir_sf_sort_t *sa, *sb;
400
401         sa = (xfs_dir_sf_sort_t *)a;
402         sb = (xfs_dir_sf_sort_t *)b;
403         if (sa->hash < sb->hash)
404                 return -1;
405         else if (sa->hash > sb->hash)
406                 return 1;
407         else
408                 return sa->entno - sb->entno;
409 }
410
411 /*
412  * Copy out directory entries for getdents(), for shortform directories.
413  */
414 /*ARGSUSED*/
415 int
416 xfs_dir_shortform_getdents(xfs_inode_t *dp, uio_t *uio, int *eofp,
417                                        xfs_dirent_t *dbp, xfs_dir_put_t put)
418 {
419         xfs_dir_shortform_t *sf;
420         xfs_dir_sf_entry_t *sfe;
421         int retval, i, sbsize, nsbuf, lastresid=0, want_entno;
422         xfs_mount_t *mp;
423         xfs_dahash_t cookhash, hash;
424         xfs_dir_put_args_t p;
425         xfs_dir_sf_sort_t *sbuf, *sbp;
426
427         mp = dp->i_mount;
428         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
429         cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
430         want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
431         nsbuf = INT_GET(sf->hdr.count, ARCH_CONVERT) + 2;
432         sbsize = (nsbuf + 1) * sizeof(*sbuf);
433         sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
434
435         xfs_dir_trace_g_du("sf: start", dp, uio);
436
437         /*
438          * Collect all the entries into the buffer.
439          * Entry 0 is .
440          */
441         sbp->entno = 0;
442         sbp->seqno = 0;
443         sbp->hash = xfs_dir_hash_dot;
444         sbp->ino = dp->i_ino;
445         sbp->name = ".";
446         sbp->namelen = 1;
447         sbp++;
448
449         /*
450          * Entry 1 is ..
451          */
452         sbp->entno = 1;
453         sbp->seqno = 0;
454         sbp->hash = xfs_dir_hash_dotdot;
455         sbp->ino = XFS_GET_DIR_INO8(sf->hdr.parent);
456         sbp->name = "..";
457         sbp->namelen = 2;
458         sbp++;
459
460         /*
461          * Scan the directory data for the rest of the entries.
462          */
463         for (i = 0, sfe = &sf->list[0];
464                         i < INT_GET(sf->hdr.count, ARCH_CONVERT); i++) {
465
466                 if (unlikely(
467                     ((char *)sfe < (char *)sf) ||
468                     ((char *)sfe >= ((char *)sf + dp->i_df.if_bytes)))) {
469                         xfs_dir_trace_g_du("sf: corrupted", dp, uio);
470                         XFS_CORRUPTION_ERROR("xfs_dir_shortform_getdents",
471                                              XFS_ERRLEVEL_LOW, mp, sfe);
472                         kmem_free(sbuf, sbsize);
473                         return XFS_ERROR(EFSCORRUPTED);
474                 }
475
476                 sbp->entno = i + 2;
477                 sbp->seqno = 0;
478                 sbp->hash = xfs_da_hashname((char *)sfe->name, sfe->namelen);
479                 sbp->ino = XFS_GET_DIR_INO8(sfe->inumber);
480                 sbp->name = (char *)sfe->name;
481                 sbp->namelen = sfe->namelen;
482                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
483                 sbp++;
484         }
485
486         /*
487          * Sort the entries on hash then entno.
488          */
489         xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_dir_shortform_compare);
490         /*
491          * Stuff in last entry.
492          */
493         sbp->entno = nsbuf;
494         sbp->hash = XFS_DA_MAXHASH;
495         sbp->seqno = 0;
496         /*
497          * Figure out the sequence numbers in case there's a hash duplicate.
498          */
499         for (hash = sbuf->hash, sbp = sbuf + 1;
500                                 sbp < &sbuf[nsbuf + 1]; sbp++) {
501                 if (sbp->hash == hash)
502                         sbp->seqno = sbp[-1].seqno + 1;
503                 else
504                         hash = sbp->hash;
505         }
506
507         /*
508          * Set up put routine.
509          */
510         p.dbp = dbp;
511         p.put = put;
512         p.uio = uio;
513
514         /*
515          * Find our place.
516          */
517         for (sbp = sbuf; sbp < &sbuf[nsbuf + 1]; sbp++) {
518                 if (sbp->hash > cookhash ||
519                     (sbp->hash == cookhash && sbp->seqno >= want_entno))
520                         break;
521         }
522
523         /*
524          * Did we fail to find anything?  We stop at the last entry,
525          * the one we put maxhash into.
526          */
527         if (sbp == &sbuf[nsbuf]) {
528                 kmem_free(sbuf, sbsize);
529                 xfs_dir_trace_g_du("sf: hash beyond end", dp, uio);
530                 uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
531                 *eofp = 1;
532                 return 0;
533         }
534
535         /*
536          * Loop putting entries into the user buffer.
537          */
538         while (sbp < &sbuf[nsbuf]) {
539                 /*
540                  * Save the first resid in a run of equal-hashval entries
541                  * so that we can back them out if they don't all fit.
542                  */
543                 if (sbp->seqno == 0 || sbp == sbuf)
544                         lastresid = uio->uio_resid;
545                 XFS_PUT_COOKIE(p.cook, mp, 0, sbp[1].seqno, sbp[1].hash);
546                 p.ino = sbp->ino;
547 #if XFS_BIG_INUMS
548                 p.ino += mp->m_inoadd;
549 #endif
550                 p.name = sbp->name;
551                 p.namelen = sbp->namelen;
552                 retval = p.put(&p);
553                 if (!p.done) {
554                         uio->uio_offset =
555                                 XFS_DA_MAKE_COOKIE(mp, 0, 0, sbp->hash);
556                         kmem_free(sbuf, sbsize);
557                         uio->uio_resid = lastresid;
558                         xfs_dir_trace_g_du("sf: E-O-B", dp, uio);
559                         return retval;
560                 }
561                 sbp++;
562         }
563         kmem_free(sbuf, sbsize);
564         uio->uio_offset = p.cook.o;
565         *eofp = 1;
566         xfs_dir_trace_g_du("sf: E-O-F", dp, uio);
567         return 0;
568 }
569
570 /*
571  * Look up a name in a shortform directory structure, replace the inode number.
572  */
573 int
574 xfs_dir_shortform_replace(xfs_da_args_t *args)
575 {
576         xfs_dir_shortform_t *sf;
577         xfs_dir_sf_entry_t *sfe;
578         xfs_inode_t *dp;
579         int i;
580
581         dp = args->dp;
582         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
583         /*
584          * Catch the case where the conversion from shortform to leaf
585          * failed part way through.
586          */
587         if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
588                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
589                 return XFS_ERROR(EIO);
590         }
591         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
592         ASSERT(dp->i_df.if_u1.if_data != NULL);
593         sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
594         if (args->namelen == 2 &&
595             args->name[0] == '.' && args->name[1] == '.') {
596                 /* XXX - replace assert? */
597                 XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sf->hdr.parent);
598                 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
599                 return 0;
600         }
601         ASSERT(args->namelen != 1 || args->name[0] != '.');
602         sfe = &sf->list[0];
603         for (i = INT_GET(sf->hdr.count, ARCH_CONVERT)-1; i >= 0; i--) {
604                 if (sfe->namelen == args->namelen &&
605                     sfe->name[0] == args->name[0] &&
606                     memcmp(args->name, sfe->name, args->namelen) == 0) {
607                         ASSERT(memcmp((char *)&args->inumber,
608                                 (char *)&sfe->inumber, sizeof(xfs_ino_t)));
609                         XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
610                         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
611                         return 0;
612                 }
613                 sfe = XFS_DIR_SF_NEXTENTRY(sfe);
614         }
615         ASSERT(args->oknoent);
616         return XFS_ERROR(ENOENT);
617 }
618
619 /*
620  * Convert a leaf directory to shortform structure
621  */
622 int
623 xfs_dir_leaf_to_shortform(xfs_da_args_t *iargs)
624 {
625         xfs_dir_leafblock_t *leaf;
626         xfs_dir_leaf_hdr_t *hdr;
627         xfs_dir_leaf_entry_t *entry;
628         xfs_dir_leaf_name_t *namest;
629         xfs_da_args_t args;
630         xfs_inode_t *dp;
631         xfs_ino_t parent = 0;
632         char *tmpbuffer;
633         int retval, i;
634         xfs_dabuf_t *bp;
635
636         dp = iargs->dp;
637         tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
638         ASSERT(tmpbuffer != NULL);
639
640         retval = xfs_da_read_buf(iargs->trans, iargs->dp, 0, -1, &bp,
641                                                XFS_DATA_FORK);
642         if (retval)
643                 goto out;
644         ASSERT(bp != NULL);
645         memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
646         leaf = (xfs_dir_leafblock_t *)tmpbuffer;
647         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
648         memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
649
650         /*
651          * Find and special case the parent inode number
652          */
653         hdr = &leaf->hdr;
654         entry = &leaf->entries[0];
655         for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
656                 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
657                 if ((entry->namelen == 2) &&
658                     (namest->name[0] == '.') &&
659                     (namest->name[1] == '.')) {
660                         XFS_DIR_SF_GET_DIRINO(&namest->inumber, &parent);
661                         entry->nameidx = 0;
662                 } else if ((entry->namelen == 1) && (namest->name[0] == '.')) {
663                         entry->nameidx = 0;
664                 }
665         }
666         retval = xfs_da_shrink_inode(iargs, 0, bp);
667         if (retval)
668                 goto out;
669         retval = xfs_dir_shortform_create(iargs, parent);
670         if (retval)
671                 goto out;
672
673         /*
674          * Copy the rest of the filenames
675          */
676         entry = &leaf->entries[0];
677         args.dp = dp;
678         args.firstblock = iargs->firstblock;
679         args.flist = iargs->flist;
680         args.total = iargs->total;
681         args.whichfork = XFS_DATA_FORK;
682         args.trans = iargs->trans;
683         args.justcheck = 0;
684         args.addname = args.oknoent = 1;
685         for (i = 0; i < INT_GET(hdr->count, ARCH_CONVERT); entry++, i++) {
686                 if (!entry->nameidx)
687                         continue;
688                 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
689                 args.name = (char *)(namest->name);
690                 args.namelen = entry->namelen;
691                 args.hashval = INT_GET(entry->hashval, ARCH_CONVERT);
692                 XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args.inumber);
693                 xfs_dir_shortform_addname(&args);
694         }
695
696 out:
697         kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
698         return retval;
699 }
700
701 /*
702  * Convert from using a single leaf to a root node and a leaf.
703  */
704 int
705 xfs_dir_leaf_to_node(xfs_da_args_t *args)
706 {
707         xfs_dir_leafblock_t *leaf;
708         xfs_da_intnode_t *node;
709         xfs_inode_t *dp;
710         xfs_dabuf_t *bp1, *bp2;
711         xfs_dablk_t blkno;
712         int retval;
713
714         dp = args->dp;
715         retval = xfs_da_grow_inode(args, &blkno);
716         ASSERT(blkno == 1);
717         if (retval)
718                 return retval;
719         retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
720                                               XFS_DATA_FORK);
721         if (retval)
722                 return retval;
723         ASSERT(bp1 != NULL);
724         retval = xfs_da_get_buf(args->trans, args->dp, 1, -1, &bp2,
725                                              XFS_DATA_FORK);
726         if (retval) {
727                 xfs_da_buf_done(bp1);
728                 return retval;
729         }
730         ASSERT(bp2 != NULL);
731         memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
732         xfs_da_buf_done(bp1);
733         xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
734
735         /*
736          * Set up the new root node.
737          */
738         retval = xfs_da_node_create(args, 0, 1, &bp1, XFS_DATA_FORK);
739         if (retval) {
740                 xfs_da_buf_done(bp2);
741                 return retval;
742         }
743         node = bp1->data;
744         leaf = bp2->data;
745         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
746         node->btree[0].hashval = cpu_to_be32(
747                 INT_GET(leaf->entries[
748                         INT_GET(leaf->hdr.count, ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
749         xfs_da_buf_done(bp2);
750         node->btree[0].before = cpu_to_be32(blkno);
751         node->hdr.count = cpu_to_be16(1);
752         xfs_da_log_buf(args->trans, bp1,
753                 XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
754         xfs_da_buf_done(bp1);
755
756         return retval;
757 }
758
759
760 /*========================================================================
761  * Routines used for growing the Btree.
762  *========================================================================*/
763
764 /*
765  * Create the initial contents of a leaf directory
766  * or a leaf in a node directory.
767  */
768 STATIC int
769 xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
770 {
771         xfs_dir_leafblock_t *leaf;
772         xfs_dir_leaf_hdr_t *hdr;
773         xfs_inode_t *dp;
774         xfs_dabuf_t *bp;
775         int retval;
776
777         dp = args->dp;
778         ASSERT(dp != NULL);
779         retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
780         if (retval)
781                 return retval;
782         ASSERT(bp != NULL);
783         leaf = bp->data;
784         memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
785         hdr = &leaf->hdr;
786         hdr->info.magic = cpu_to_be16(XFS_DIR_LEAF_MAGIC);
787         INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
788         if (!hdr->firstused)
789                 INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount) - 1);
790         INT_SET(hdr->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
791         INT_SET(hdr->freemap[0].size, ARCH_CONVERT, INT_GET(hdr->firstused, ARCH_CONVERT) - INT_GET(hdr->freemap[0].base, ARCH_CONVERT));
792
793         xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
794
795         *bpp = bp;
796         return 0;
797 }
798
799 /*
800  * Split the leaf node, rebalance, then add the new entry.
801  */
802 int
803 xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
804                                   xfs_da_state_blk_t *newblk)
805 {
806         xfs_dablk_t blkno;
807         xfs_da_args_t *args;
808         int error;
809
810         /*
811          * Allocate space for a new leaf node.
812          */
813         args = state->args;
814         ASSERT(args != NULL);
815         ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
816         error = xfs_da_grow_inode(args, &blkno);
817         if (error)
818                 return error;
819         error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
820         if (error)
821                 return error;
822         newblk->blkno = blkno;
823         newblk->magic = XFS_DIR_LEAF_MAGIC;
824
825         /*
826          * Rebalance the entries across the two leaves.
827          */
828         xfs_dir_leaf_rebalance(state, oldblk, newblk);
829         error = xfs_da_blk_link(state, oldblk, newblk);
830         if (error)
831                 return error;
832
833         /*
834          * Insert the new entry in the correct block.
835          */
836         if (state->inleaf) {
837                 error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
838         } else {
839                 error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
840         }
841
842         /*
843          * Update last hashval in each block since we added the name.
844          */
845         oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
846         newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
847         return error;
848 }
849
850 /*
851  * Add a name to the leaf directory structure.
852  *
853  * Must take into account fragmented leaves and leaves where spacemap has
854  * lost some freespace information (ie: holes).
855  */
856 int
857 xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
858 {
859         xfs_dir_leafblock_t *leaf;
860         xfs_dir_leaf_hdr_t *hdr;
861         xfs_dir_leaf_map_t *map;
862         int tablesize, entsize, sum, i, tmp, error;
863
864         leaf = bp->data;
865         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
866         ASSERT((index >= 0) && (index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
867         hdr = &leaf->hdr;
868         entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
869
870         /*
871          * Search through freemap for first-fit on new name length.
872          * (may need to figure in size of entry struct too)
873          */
874         tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1) * (uint)sizeof(xfs_dir_leaf_entry_t)
875                         + (uint)sizeof(xfs_dir_leaf_hdr_t);
876         map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
877         for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
878                 if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
879                         sum += INT_GET(map->size, ARCH_CONVERT);
880                         continue;
881                 }
882                 if (!map->size)
883                         continue;       /* no space in this map */
884                 tmp = entsize;
885                 if (INT_GET(map->base, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
886                         tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
887                 if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
888                         if (!args->justcheck)
889                                 xfs_dir_leaf_add_work(bp, args, index, i);
890                         return 0;
891                 }
892                 sum += INT_GET(map->size, ARCH_CONVERT);
893         }
894
895         /*
896          * If there are no holes in the address space of the block,
897          * and we don't have enough freespace, then compaction will do us
898          * no good and we should just give up.
899          */
900         if (!hdr->holes && (sum < entsize))
901                 return XFS_ERROR(ENOSPC);
902
903         /*
904          * Compact the entries to coalesce free space.
905          * Pass the justcheck flag so the checking pass can return
906          * an error, without changing anything, if it won't fit.
907          */
908         error = xfs_dir_leaf_compact(args->trans, bp,
909                         args->total == 0 ?
910                                 entsize +
911                                 (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
912                         args->justcheck);
913         if (error)
914                 return error;
915         /*
916          * After compaction, the block is guaranteed to have only one
917          * free region, in freemap[0].  If it is not big enough, give up.
918          */
919         if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT) <
920             (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
921                 return XFS_ERROR(ENOSPC);
922
923         if (!args->justcheck)
924                 xfs_dir_leaf_add_work(bp, args, index, 0);
925         return 0;
926 }
927
928 /*
929  * Add a name to a leaf directory structure.
930  */
931 STATIC void
932 xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
933                       int mapindex)
934 {
935         xfs_dir_leafblock_t *leaf;
936         xfs_dir_leaf_hdr_t *hdr;
937         xfs_dir_leaf_entry_t *entry;
938         xfs_dir_leaf_name_t *namest;
939         xfs_dir_leaf_map_t *map;
940         /* REFERENCED */
941         xfs_mount_t *mp;
942         int tmp, i;
943
944         leaf = bp->data;
945         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
946         hdr = &leaf->hdr;
947         ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
948         ASSERT((index >= 0) && (index <= INT_GET(hdr->count, ARCH_CONVERT)));
949
950         /*
951          * Force open some space in the entry array and fill it in.
952          */
953         entry = &leaf->entries[index];
954         if (index < INT_GET(hdr->count, ARCH_CONVERT)) {
955                 tmp  = INT_GET(hdr->count, ARCH_CONVERT) - index;
956                 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
957                 memmove(entry + 1, entry, tmp);
958                 xfs_da_log_buf(args->trans, bp,
959                     XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
960         }
961         INT_MOD(hdr->count, ARCH_CONVERT, +1);
962
963         /*
964          * Allocate space for the new string (at the end of the run).
965          */
966         map = &hdr->freemap[mapindex];
967         mp = args->trans->t_mountp;
968         ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
969         ASSERT(INT_GET(map->size, ARCH_CONVERT) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
970         ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
971         INT_MOD(map->size, ARCH_CONVERT, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
972         INT_SET(entry->nameidx, ARCH_CONVERT, INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT));
973         INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
974         entry->namelen = args->namelen;
975         xfs_da_log_buf(args->trans, bp,
976             XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
977
978         /*
979          * Copy the string and inode number into the new space.
980          */
981         namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
982         XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
983         memcpy(namest->name, args->name, args->namelen);
984         xfs_da_log_buf(args->trans, bp,
985             XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
986
987         /*
988          * Update the control info for this leaf node
989          */
990         if (INT_GET(entry->nameidx, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
991                 INT_COPY(hdr->firstused, entry->nameidx, ARCH_CONVERT);
992         ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
993         tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
994                         + (uint)sizeof(xfs_dir_leaf_hdr_t);
995         map = &hdr->freemap[0];
996         for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
997                 if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
998                         INT_MOD(map->base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
999                         INT_MOD(map->size, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1000                 }
1001         }
1002         INT_MOD(hdr->namebytes, ARCH_CONVERT, args->namelen);
1003         xfs_da_log_buf(args->trans, bp,
1004                 XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1005 }
1006
1007 /*
1008  * Garbage collect a leaf directory block by copying it to a new buffer.
1009  */
1010 STATIC int
1011 xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
1012                      int justcheck)
1013 {
1014         xfs_dir_leafblock_t *leaf_s, *leaf_d;
1015         xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1016         xfs_mount_t *mp;
1017         char *tmpbuffer;
1018         char *tmpbuffer2=NULL;
1019         int rval;
1020         int lbsize;
1021
1022         mp = trans->t_mountp;
1023         lbsize = XFS_LBSIZE(mp);
1024         tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
1025         ASSERT(tmpbuffer != NULL);
1026         memcpy(tmpbuffer, bp->data, lbsize);
1027
1028         /*
1029          * Make a second copy in case xfs_dir_leaf_moveents()
1030          * below destroys the original.
1031          */
1032         if (musthave || justcheck) {
1033                 tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
1034                 memcpy(tmpbuffer2, bp->data, lbsize);
1035         }
1036         memset(bp->data, 0, lbsize);
1037
1038         /*
1039          * Copy basic information
1040          */
1041         leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
1042         leaf_d = bp->data;
1043         hdr_s = &leaf_s->hdr;
1044         hdr_d = &leaf_d->hdr;
1045         hdr_d->info = hdr_s->info;      /* struct copy */
1046         INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize);
1047         if (!hdr_d->firstused)
1048                 INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize - 1);
1049         hdr_d->namebytes = 0;
1050         hdr_d->count = 0;
1051         hdr_d->holes = 0;
1052         INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
1053         INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1054
1055         /*
1056          * Copy all entry's in the same (sorted) order,
1057          * but allocate filenames packed and in sequence.
1058          * This changes the source (leaf_s) as well.
1059          */
1060         xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
1061
1062         if (musthave && INT_GET(hdr_d->freemap[0].size, ARCH_CONVERT) < musthave)
1063                 rval = XFS_ERROR(ENOSPC);
1064         else
1065                 rval = 0;
1066
1067         if (justcheck || rval == ENOSPC) {
1068                 ASSERT(tmpbuffer2);
1069                 memcpy(bp->data, tmpbuffer2, lbsize);
1070         } else {
1071                 xfs_da_log_buf(trans, bp, 0, lbsize - 1);
1072         }
1073
1074         kmem_free(tmpbuffer, lbsize);
1075         if (musthave || justcheck)
1076                 kmem_free(tmpbuffer2, lbsize);
1077         return rval;
1078 }
1079
1080 /*
1081  * Redistribute the directory entries between two leaf nodes,
1082  * taking into account the size of the new entry.
1083  *
1084  * NOTE: if new block is empty, then it will get the upper half of old block.
1085  */
1086 STATIC void
1087 xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
1088                                       xfs_da_state_blk_t *blk2)
1089 {
1090         xfs_da_state_blk_t *tmp_blk;
1091         xfs_dir_leafblock_t *leaf1, *leaf2;
1092         xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1093         int count, totallen, max, space, swap;
1094
1095         /*
1096          * Set up environment.
1097          */
1098         ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
1099         ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
1100         leaf1 = blk1->bp->data;
1101         leaf2 = blk2->bp->data;
1102         ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1103         ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1104
1105         /*
1106          * Check ordering of blocks, reverse if it makes things simpler.
1107          */
1108         swap = 0;
1109         if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
1110                 tmp_blk = blk1;
1111                 blk1 = blk2;
1112                 blk2 = tmp_blk;
1113                 leaf1 = blk1->bp->data;
1114                 leaf2 = blk2->bp->data;
1115                 swap = 1;
1116         }
1117         hdr1 = &leaf1->hdr;
1118         hdr2 = &leaf2->hdr;
1119
1120         /*
1121          * Examine entries until we reduce the absolute difference in
1122          * byte usage between the two blocks to a minimum.  Then get
1123          * the direction to copy and the number of elements to move.
1124          */
1125         state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
1126                                                            &count, &totallen);
1127         if (swap)
1128                 state->inleaf = !state->inleaf;
1129
1130         /*
1131          * Move any entries required from leaf to leaf:
1132          */
1133         if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
1134                 /*
1135                  * Figure the total bytes to be added to the destination leaf.
1136                  */
1137                 count = INT_GET(hdr1->count, ARCH_CONVERT) - count;     /* number entries being moved */
1138                 space  = INT_GET(hdr1->namebytes, ARCH_CONVERT) - totallen;
1139                 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1140                 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1141
1142                 /*
1143                  * leaf2 is the destination, compact it if it looks tight.
1144                  */
1145                 max  = INT_GET(hdr2->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1146                 max -= INT_GET(hdr2->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1147                 if (space > max) {
1148                         xfs_dir_leaf_compact(state->args->trans, blk2->bp,
1149                                                                  0, 0);
1150                 }
1151
1152                 /*
1153                  * Move high entries from leaf1 to low end of leaf2.
1154                  */
1155                 xfs_dir_leaf_moveents(leaf1, INT_GET(hdr1->count, ARCH_CONVERT) - count,
1156                                              leaf2, 0, count, state->mp);
1157
1158                 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1159                                                    state->blocksize-1);
1160                 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1161                                                    state->blocksize-1);
1162
1163         } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
1164                 /*
1165                  * Figure the total bytes to be added to the destination leaf.
1166                  */
1167                 count -= INT_GET(hdr1->count, ARCH_CONVERT);            /* number entries being moved */
1168                 space  = totallen - INT_GET(hdr1->namebytes, ARCH_CONVERT);
1169                 space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
1170                 space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
1171
1172                 /*
1173                  * leaf1 is the destination, compact it if it looks tight.
1174                  */
1175                 max  = INT_GET(hdr1->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
1176                 max -= INT_GET(hdr1->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1177                 if (space > max) {
1178                         xfs_dir_leaf_compact(state->args->trans, blk1->bp,
1179                                                                  0, 0);
1180                 }
1181
1182                 /*
1183                  * Move low entries from leaf2 to high end of leaf1.
1184                  */
1185                 xfs_dir_leaf_moveents(leaf2, 0, leaf1, (int)INT_GET(hdr1->count, ARCH_CONVERT),
1186                                              count, state->mp);
1187
1188                 xfs_da_log_buf(state->args->trans, blk1->bp, 0,
1189                                                    state->blocksize-1);
1190                 xfs_da_log_buf(state->args->trans, blk2->bp, 0,
1191                                                    state->blocksize-1);
1192         }
1193
1194         /*
1195          * Copy out last hashval in each block for B-tree code.
1196          */
1197         blk1->hashval = INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1198         blk2->hashval = INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1199
1200         /*
1201          * Adjust the expected index for insertion.
1202          * GROT: this doesn't work unless blk2 was originally empty.
1203          */
1204         if (!state->inleaf) {
1205                 blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
1206         }
1207 }
1208
1209 /*
1210  * Examine entries until we reduce the absolute difference in
1211  * byte usage between the two blocks to a minimum.
1212  * GROT: Is this really necessary?  With other than a 512 byte blocksize,
1213  * GROT: there will always be enough room in either block for a new entry.
1214  * GROT: Do a double-split for this case?
1215  */
1216 STATIC int
1217 xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
1218                                            xfs_da_state_blk_t *blk1,
1219                                            xfs_da_state_blk_t *blk2,
1220                                            int *countarg, int *namebytesarg)
1221 {
1222         xfs_dir_leafblock_t *leaf1, *leaf2;
1223         xfs_dir_leaf_hdr_t *hdr1, *hdr2;
1224         xfs_dir_leaf_entry_t *entry;
1225         int count, max, totallen, half;
1226         int lastdelta, foundit, tmp;
1227
1228         /*
1229          * Set up environment.
1230          */
1231         leaf1 = blk1->bp->data;
1232         leaf2 = blk2->bp->data;
1233         hdr1 = &leaf1->hdr;
1234         hdr2 = &leaf2->hdr;
1235         foundit = 0;
1236         totallen = 0;
1237
1238         /*
1239          * Examine entries until we reduce the absolute difference in
1240          * byte usage between the two blocks to a minimum.
1241          */
1242         max = INT_GET(hdr1->count, ARCH_CONVERT) + INT_GET(hdr2->count, ARCH_CONVERT);
1243         half  = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1244         half += INT_GET(hdr1->namebytes, ARCH_CONVERT) + INT_GET(hdr2->namebytes, ARCH_CONVERT) + state->args->namelen;
1245         half /= 2;
1246         lastdelta = state->blocksize;
1247         entry = &leaf1->entries[0];
1248         for (count = 0; count < max; entry++, count++) {
1249
1250 #define XFS_DIR_ABS(A)  (((A) < 0) ? -(A) : (A))
1251                 /*
1252                  * The new entry is in the first block, account for it.
1253                  */
1254                 if (count == blk1->index) {
1255                         tmp = totallen + (uint)sizeof(*entry)
1256                                 + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
1257                         if (XFS_DIR_ABS(half - tmp) > lastdelta)
1258                                 break;
1259                         lastdelta = XFS_DIR_ABS(half - tmp);
1260                         totallen = tmp;
1261                         foundit = 1;
1262                 }
1263
1264                 /*
1265                  * Wrap around into the second block if necessary.
1266                  */
1267                 if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
1268                         leaf1 = leaf2;
1269                         entry = &leaf1->entries[0];
1270                 }
1271
1272                 /*
1273                  * Figure out if next leaf entry would be too much.
1274                  */
1275                 tmp = totallen + (uint)sizeof(*entry)
1276                                 + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1277                 if (XFS_DIR_ABS(half - tmp) > lastdelta)
1278                         break;
1279                 lastdelta = XFS_DIR_ABS(half - tmp);
1280                 totallen = tmp;
1281 #undef XFS_DIR_ABS
1282         }
1283
1284         /*
1285          * Calculate the number of namebytes that will end up in lower block.
1286          * If new entry not in lower block, fix up the count.
1287          */
1288         totallen -=
1289                 count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
1290         if (foundit) {
1291                 totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
1292                             state->args->namelen;
1293         }
1294
1295         *countarg = count;
1296         *namebytesarg = totallen;
1297         return foundit;
1298 }
1299
1300 /*========================================================================
1301  * Routines used for shrinking the Btree.
1302  *========================================================================*/
1303
1304 /*
1305  * Check a leaf block and its neighbors to see if the block should be
1306  * collapsed into one or the other neighbor.  Always keep the block
1307  * with the smaller block number.
1308  * If the current block is over 50% full, don't try to join it, return 0.
1309  * If the block is empty, fill in the state structure and return 2.
1310  * If it can be collapsed, fill in the state structure and return 1.
1311  * If nothing can be done, return 0.
1312  */
1313 int
1314 xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
1315 {
1316         xfs_dir_leafblock_t *leaf;
1317         xfs_da_state_blk_t *blk;
1318         xfs_da_blkinfo_t *info;
1319         int count, bytes, forward, error, retval, i;
1320         xfs_dablk_t blkno;
1321         xfs_dabuf_t *bp;
1322
1323         /*
1324          * Check for the degenerate case of the block being over 50% full.
1325          * If so, it's not worth even looking to see if we might be able
1326          * to coalesce with a sibling.
1327          */
1328         blk = &state->path.blk[ state->path.active-1 ];
1329         info = blk->bp->data;
1330         ASSERT(be16_to_cpu(info->magic) == XFS_DIR_LEAF_MAGIC);
1331         leaf = (xfs_dir_leafblock_t *)info;
1332         count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1333         bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
1334                 count * (uint)sizeof(xfs_dir_leaf_entry_t) +
1335                 count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
1336                 INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1337         if (bytes > (state->blocksize >> 1)) {
1338                 *action = 0;    /* blk over 50%, don't try to join */
1339                 return 0;
1340         }
1341
1342         /*
1343          * Check for the degenerate case of the block being empty.
1344          * If the block is empty, we'll simply delete it, no need to
1345          * coalesce it with a sibling block.  We choose (aribtrarily)
1346          * to merge with the forward block unless it is NULL.
1347          */
1348         if (count == 0) {
1349                 /*
1350                  * Make altpath point to the block we want to keep and
1351                  * path point to the block we want to drop (this one).
1352                  */
1353                 forward = (info->forw != 0);
1354                 memcpy(&state->altpath, &state->path, sizeof(state->path));
1355                 error = xfs_da_path_shift(state, &state->altpath, forward,
1356                                                  0, &retval);
1357                 if (error)
1358                         return error;
1359                 if (retval) {
1360                         *action = 0;
1361                 } else {
1362                         *action = 2;
1363                 }
1364                 return 0;
1365         }
1366
1367         /*
1368          * Examine each sibling block to see if we can coalesce with
1369          * at least 25% free space to spare.  We need to figure out
1370          * whether to merge with the forward or the backward block.
1371          * We prefer coalescing with the lower numbered sibling so as
1372          * to shrink a directory over time.
1373          */
1374         forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));  /* start with smaller blk num */
1375         for (i = 0; i < 2; forward = !forward, i++) {
1376                 if (forward)
1377                         blkno = be32_to_cpu(info->forw);
1378                 else
1379                         blkno = be32_to_cpu(info->back);
1380                 if (blkno == 0)
1381                         continue;
1382                 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1383                                                             blkno, -1, &bp,
1384                                                             XFS_DATA_FORK);
1385                 if (error)
1386                         return error;
1387                 ASSERT(bp != NULL);
1388
1389                 leaf = (xfs_dir_leafblock_t *)info;
1390                 count  = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1391                 bytes  = state->blocksize - (state->blocksize>>2);
1392                 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1393                 leaf = bp->data;
1394                 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1395                 count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
1396                 bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1397                 bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1398                 bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
1399                 bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
1400                 if (bytes >= 0)
1401                         break;  /* fits with at least 25% to spare */
1402
1403                 xfs_da_brelse(state->args->trans, bp);
1404         }
1405         if (i >= 2) {
1406                 *action = 0;
1407                 return 0;
1408         }
1409         xfs_da_buf_done(bp);
1410
1411         /*
1412          * Make altpath point to the block we want to keep (the lower
1413          * numbered block) and path point to the block we want to drop.
1414          */
1415         memcpy(&state->altpath, &state->path, sizeof(state->path));
1416         if (blkno < blk->blkno) {
1417                 error = xfs_da_path_shift(state, &state->altpath, forward,
1418                                                  0, &retval);
1419         } else {
1420                 error = xfs_da_path_shift(state, &state->path, forward,
1421                                                  0, &retval);
1422         }
1423         if (error)
1424                 return error;
1425         if (retval) {
1426                 *action = 0;
1427         } else {
1428                 *action = 1;
1429         }
1430         return 0;
1431 }
1432
1433 /*
1434  * Remove a name from the leaf directory structure.
1435  *
1436  * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
1437  * If two leaves are 37% full, when combined they will leave 25% free.
1438  */
1439 int
1440 xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
1441 {
1442         xfs_dir_leafblock_t *leaf;
1443         xfs_dir_leaf_hdr_t *hdr;
1444         xfs_dir_leaf_map_t *map;
1445         xfs_dir_leaf_entry_t *entry;
1446         xfs_dir_leaf_name_t *namest;
1447         int before, after, smallest, entsize;
1448         int tablesize, tmp, i;
1449         xfs_mount_t *mp;
1450
1451         leaf = bp->data;
1452         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1453         hdr = &leaf->hdr;
1454         mp = trans->t_mountp;
1455         ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0) && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1456         ASSERT((index >= 0) && (index < INT_GET(hdr->count, ARCH_CONVERT)));
1457         ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
1458         entry = &leaf->entries[index];
1459         ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1460         ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1461
1462         /*
1463          * Scan through free region table:
1464          *    check for adjacency of free'd entry with an existing one,
1465          *    find smallest free region in case we need to replace it,
1466          *    adjust any map that borders the entry table,
1467          */
1468         tablesize = INT_GET(hdr->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1469                         + (uint)sizeof(xfs_dir_leaf_hdr_t);
1470         map = &hdr->freemap[0];
1471         tmp = INT_GET(map->size, ARCH_CONVERT);
1472         before = after = -1;
1473         smallest = XFS_DIR_LEAF_MAPSIZE - 1;
1474         entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
1475         for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
1476                 ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
1477                 ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
1478                 if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
1479                         INT_MOD(map->base, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
1480                         INT_MOD(map->size, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
1481                 }
1482
1483                 if ((INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT)) == INT_GET(entry->nameidx, ARCH_CONVERT)) {
1484                         before = i;
1485                 } else if (INT_GET(map->base, ARCH_CONVERT) == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
1486                         after = i;
1487                 } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
1488                         tmp = INT_GET(map->size, ARCH_CONVERT);
1489                         smallest = i;
1490                 }
1491         }
1492
1493         /*
1494          * Coalesce adjacent freemap regions,
1495          * or replace the smallest region.
1496          */
1497         if ((before >= 0) || (after >= 0)) {
1498                 if ((before >= 0) && (after >= 0)) {
1499                         map = &hdr->freemap[before];
1500                         INT_MOD(map->size, ARCH_CONVERT, entsize);
1501                         INT_MOD(map->size, ARCH_CONVERT, INT_GET(hdr->freemap[after].size, ARCH_CONVERT));
1502                         hdr->freemap[after].base = 0;
1503                         hdr->freemap[after].size = 0;
1504                 } else if (before >= 0) {
1505                         map = &hdr->freemap[before];
1506                         INT_MOD(map->size, ARCH_CONVERT, entsize);
1507                 } else {
1508                         map = &hdr->freemap[after];
1509                         INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1510                         INT_MOD(map->size, ARCH_CONVERT, entsize);
1511                 }
1512         } else {
1513                 /*
1514                  * Replace smallest region (if it is smaller than free'd entry)
1515                  */
1516                 map = &hdr->freemap[smallest];
1517                 if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
1518                         INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
1519                         INT_SET(map->size, ARCH_CONVERT, entsize);
1520                 }
1521         }
1522
1523         /*
1524          * Did we remove the first entry?
1525          */
1526         if (INT_GET(entry->nameidx, ARCH_CONVERT) == INT_GET(hdr->firstused, ARCH_CONVERT))
1527                 smallest = 1;
1528         else
1529                 smallest = 0;
1530
1531         /*
1532          * Compress the remaining entries and zero out the removed stuff.
1533          */
1534         namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1535         memset((char *)namest, 0, entsize);
1536         xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
1537
1538         INT_MOD(hdr->namebytes, ARCH_CONVERT, -(entry->namelen));
1539         tmp = (INT_GET(hdr->count, ARCH_CONVERT) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
1540         memmove(entry, entry + 1, tmp);
1541         INT_MOD(hdr->count, ARCH_CONVERT, -1);
1542         xfs_da_log_buf(trans, bp,
1543             XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
1544         entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
1545         memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
1546
1547         /*
1548          * If we removed the first entry, re-find the first used byte
1549          * in the name area.  Note that if the entry was the "firstused",
1550          * then we don't have a "hole" in our block resulting from
1551          * removing the name.
1552          */
1553         if (smallest) {
1554                 tmp = XFS_LBSIZE(mp);
1555                 entry = &leaf->entries[0];
1556                 for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
1557                         ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
1558                         ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
1559                         if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
1560                                 tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
1561                 }
1562                 INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
1563                 if (!hdr->firstused)
1564                         INT_SET(hdr->firstused, ARCH_CONVERT, tmp - 1);
1565         } else {
1566                 hdr->holes = 1;         /* mark as needing compaction */
1567         }
1568
1569         xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
1570
1571         /*
1572          * Check if leaf is less than 50% full, caller may want to
1573          * "join" the leaf with a sibling if so.
1574          */
1575         tmp  = (uint)sizeof(xfs_dir_leaf_hdr_t);
1576         tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
1577         tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
1578         tmp += INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
1579         if (tmp < mp->m_dir_magicpct)
1580                 return 1;                       /* leaf is < 37% full */
1581         return 0;
1582 }
1583
1584 /*
1585  * Move all the directory entries from drop_leaf into save_leaf.
1586  */
1587 void
1588 xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
1589                                       xfs_da_state_blk_t *save_blk)
1590 {
1591         xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
1592         xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
1593         xfs_mount_t *mp;
1594         char *tmpbuffer;
1595
1596         /*
1597          * Set up environment.
1598          */
1599         mp = state->mp;
1600         ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
1601         ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
1602         drop_leaf = drop_blk->bp->data;
1603         save_leaf = save_blk->bp->data;
1604         ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1605         ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1606         drop_hdr = &drop_leaf->hdr;
1607         save_hdr = &save_leaf->hdr;
1608
1609         /*
1610          * Save last hashval from dying block for later Btree fixup.
1611          */
1612         drop_blk->hashval = INT_GET(drop_leaf->entries[ drop_leaf->hdr.count-1 ].hashval, ARCH_CONVERT);
1613
1614         /*
1615          * Check if we need a temp buffer, or can we do it in place.
1616          * Note that we don't check "leaf" for holes because we will
1617          * always be dropping it, toosmall() decided that for us already.
1618          */
1619         if (save_hdr->holes == 0) {
1620                 /*
1621                  * dest leaf has no holes, so we add there.  May need
1622                  * to make some room in the entry array.
1623                  */
1624                 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1625                         xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
1626                                                  (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1627                 } else {
1628                         xfs_dir_leaf_moveents(drop_leaf, 0,
1629                                               save_leaf, INT_GET(save_hdr->count, ARCH_CONVERT),
1630                                               (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1631                 }
1632         } else {
1633                 /*
1634                  * Destination has holes, so we make a temporary copy
1635                  * of the leaf and add them both to that.
1636                  */
1637                 tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
1638                 ASSERT(tmpbuffer != NULL);
1639                 memset(tmpbuffer, 0, state->blocksize);
1640                 tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
1641                 tmp_hdr = &tmp_leaf->hdr;
1642                 tmp_hdr->info = save_hdr->info; /* struct copy */
1643                 tmp_hdr->count = 0;
1644                 INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
1645                 if (!tmp_hdr->firstused)
1646                         INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize - 1);
1647                 tmp_hdr->namebytes = 0;
1648                 if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
1649                         xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
1650                                                  (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1651                         xfs_dir_leaf_moveents(save_leaf, 0,
1652                                               tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1653                                               (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1654                 } else {
1655                         xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
1656                                                  (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
1657                         xfs_dir_leaf_moveents(drop_leaf, 0,
1658                                               tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
1659                                               (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
1660                 }
1661                 memcpy(save_leaf, tmp_leaf, state->blocksize);
1662                 kmem_free(tmpbuffer, state->blocksize);
1663         }
1664
1665         xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
1666                                            state->blocksize - 1);
1667
1668         /*
1669          * Copy out last hashval in each block for B-tree code.
1670          */
1671         save_blk->hashval = INT_GET(save_leaf->entries[ INT_GET(save_leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
1672 }
1673
1674 /*========================================================================
1675  * Routines used for finding things in the Btree.
1676  *========================================================================*/
1677
1678 /*
1679  * Look up a name in a leaf directory structure.
1680  * This is the internal routine, it uses the caller's buffer.
1681  *
1682  * Note that duplicate keys are allowed, but only check within the
1683  * current leaf node.  The Btree code must check in adjacent leaf nodes.
1684  *
1685  * Return in *index the index into the entry[] array of either the found
1686  * entry, or where the entry should have been (insert before that entry).
1687  *
1688  * Don't change the args->inumber unless we find the filename.
1689  */
1690 int
1691 xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
1692 {
1693         xfs_dir_leafblock_t *leaf;
1694         xfs_dir_leaf_entry_t *entry;
1695         xfs_dir_leaf_name_t *namest;
1696         int probe, span;
1697         xfs_dahash_t hashval;
1698
1699         leaf = bp->data;
1700         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1701         ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) < (XFS_LBSIZE(args->dp->i_mount)/8));
1702
1703         /*
1704          * Binary search.  (note: small blocks will skip this loop)
1705          */
1706         hashval = args->hashval;
1707         probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
1708         for (entry = &leaf->entries[probe]; span > 4;
1709                    entry = &leaf->entries[probe]) {
1710                 span /= 2;
1711                 if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
1712                         probe += span;
1713                 else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
1714                         probe -= span;
1715                 else
1716                         break;
1717         }
1718         ASSERT((probe >= 0) && \
1719                ((!leaf->hdr.count) || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
1720         ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT) == hashval));
1721
1722         /*
1723          * Since we may have duplicate hashval's, find the first matching
1724          * hashval in the leaf.
1725          */
1726         while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT) >= hashval)) {
1727                 entry--;
1728                 probe--;
1729         }
1730         while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
1731                 entry++;
1732                 probe++;
1733         }
1734         if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT)) || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
1735                 *index = probe;
1736                 ASSERT(args->oknoent);
1737                 return XFS_ERROR(ENOENT);
1738         }
1739
1740         /*
1741          * Duplicate keys may be present, so search all of them for a match.
1742          */
1743         while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval)) {
1744                 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
1745                 if (entry->namelen == args->namelen &&
1746                     namest->name[0] == args->name[0] &&
1747                     memcmp(args->name, namest->name, args->namelen) == 0) {
1748                         XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
1749                         *index = probe;
1750                         return XFS_ERROR(EEXIST);
1751                 }
1752                 entry++;
1753                 probe++;
1754         }
1755         *index = probe;
1756         ASSERT(probe == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
1757         return XFS_ERROR(ENOENT);
1758 }
1759
1760 /*========================================================================
1761  * Utility routines.
1762  *========================================================================*/
1763
1764 /*
1765  * Move the indicated entries from one leaf to another.
1766  * NOTE: this routine modifies both source and destination leaves.
1767  */
1768 /* ARGSUSED */
1769 STATIC void
1770 xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
1771                       xfs_dir_leafblock_t *leaf_d, int start_d,
1772                       int count, xfs_mount_t *mp)
1773 {
1774         xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
1775         xfs_dir_leaf_entry_t *entry_s, *entry_d;
1776         int tmp, i;
1777
1778         /*
1779          * Check for nothing to do.
1780          */
1781         if (count == 0)
1782                 return;
1783
1784         /*
1785          * Set up environment.
1786          */
1787         ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1788         ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1789         hdr_s = &leaf_s->hdr;
1790         hdr_d = &leaf_d->hdr;
1791         ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0) && (INT_GET(hdr_s->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
1792         ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
1793                 ((INT_GET(hdr_s->count, ARCH_CONVERT)*sizeof(*entry_s))+sizeof(*hdr_s)));
1794         ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
1795         ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
1796                 ((INT_GET(hdr_d->count, ARCH_CONVERT)*sizeof(*entry_d))+sizeof(*hdr_d)));
1797
1798         ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
1799         ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
1800         ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
1801
1802         /*
1803          * Move the entries in the destination leaf up to make a hole?
1804          */
1805         if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
1806                 tmp  = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
1807                 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1808                 entry_s = &leaf_d->entries[start_d];
1809                 entry_d = &leaf_d->entries[start_d + count];
1810                 memcpy(entry_d, entry_s, tmp);
1811         }
1812
1813         /*
1814          * Copy all entry's in the same (sorted) order,
1815          * but allocate filenames packed and in sequence.
1816          */
1817         entry_s = &leaf_s->entries[start_s];
1818         entry_d = &leaf_d->entries[start_d];
1819         for (i = 0; i < count; entry_s++, entry_d++, i++) {
1820                 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
1821                 tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
1822                 INT_MOD(hdr_d->firstused, ARCH_CONVERT, -(tmp));
1823                 entry_d->hashval = entry_s->hashval; /* INT_: direct copy */
1824                 INT_COPY(entry_d->nameidx, hdr_d->firstused, ARCH_CONVERT);
1825                 entry_d->namelen = entry_s->namelen;
1826                 ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1827                 memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, INT_GET(entry_d->nameidx, ARCH_CONVERT)),
1828                        XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)), tmp);
1829                 ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
1830                 memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)),
1831                       0, tmp);
1832                 INT_MOD(hdr_s->namebytes, ARCH_CONVERT, -(entry_d->namelen));
1833                 INT_MOD(hdr_d->namebytes, ARCH_CONVERT, entry_d->namelen);
1834                 INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
1835                 INT_MOD(hdr_d->count, ARCH_CONVERT, +1);
1836                 tmp  = INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
1837                                 + (uint)sizeof(xfs_dir_leaf_hdr_t);
1838                 ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
1839
1840         }
1841
1842         /*
1843          * Zero out the entries we just copied.
1844          */
1845         if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
1846                 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1847                 entry_s = &leaf_s->entries[start_s];
1848                 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1849                 memset((char *)entry_s, 0, tmp);
1850         } else {
1851                 /*
1852                  * Move the remaining entries down to fill the hole,
1853                  * then zero the entries at the top.
1854                  */
1855                 tmp  = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
1856                 tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
1857                 entry_s = &leaf_s->entries[start_s + count];
1858                 entry_d = &leaf_s->entries[start_s];
1859                 memcpy(entry_d, entry_s, tmp);
1860
1861                 tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
1862                 entry_s = &leaf_s->entries[INT_GET(hdr_s->count, ARCH_CONVERT)];
1863                 ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
1864                 memset((char *)entry_s, 0, tmp);
1865         }
1866
1867         /*
1868          * Fill in the freemap information
1869          */
1870         INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_hdr_t));
1871         INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT, INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t));
1872         INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
1873         INT_SET(hdr_d->freemap[1].base, ARCH_CONVERT, (hdr_d->freemap[2].base = 0));
1874         INT_SET(hdr_d->freemap[1].size, ARCH_CONVERT, (hdr_d->freemap[2].size = 0));
1875         hdr_s->holes = 1;       /* leaf may not be compact */
1876 }
1877
1878 /*
1879  * Compare two leaf blocks "order".
1880  */
1881 int
1882 xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
1883 {
1884         xfs_dir_leafblock_t *leaf1, *leaf2;
1885
1886         leaf1 = leaf1_bp->data;
1887         leaf2 = leaf2_bp->data;
1888         ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC) &&
1889                (be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC));
1890         if ((INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0) &&
1891             ((INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
1892               INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT)) ||
1893              (INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
1894               INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
1895                 return 1;
1896         }
1897         return 0;
1898 }
1899
1900 /*
1901  * Pick up the last hashvalue from a leaf block.
1902  */
1903 xfs_dahash_t
1904 xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
1905 {
1906         xfs_dir_leafblock_t *leaf;
1907
1908         leaf = bp->data;
1909         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
1910         if (count)
1911                 *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
1912         if (!leaf->hdr.count)
1913                 return(0);
1914         return(INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
1915 }
1916
1917 /*
1918  * Copy out directory entries for getdents(), for leaf directories.
1919  */
1920 int
1921 xfs_dir_leaf_getdents_int(
1922         xfs_dabuf_t     *bp,
1923         xfs_inode_t     *dp,
1924         xfs_dablk_t     bno,
1925         uio_t           *uio,
1926         int             *eobp,
1927         xfs_dirent_t    *dbp,
1928         xfs_dir_put_t   put,
1929         xfs_daddr_t             nextda)
1930 {
1931         xfs_dir_leafblock_t     *leaf;
1932         xfs_dir_leaf_entry_t    *entry;
1933         xfs_dir_leaf_name_t     *namest;
1934         int                     entno, want_entno, i, nextentno;
1935         xfs_mount_t             *mp;
1936         xfs_dahash_t            cookhash;
1937         xfs_dahash_t            nexthash = 0;
1938 #if (BITS_PER_LONG == 32)
1939         xfs_dahash_t            lasthash = XFS_DA_MAXHASH;
1940 #endif
1941         xfs_dir_put_args_t      p;
1942
1943         mp = dp->i_mount;
1944         leaf = bp->data;
1945         if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC) {
1946                 *eobp = 1;
1947                 return XFS_ERROR(ENOENT);       /* XXX wrong code */
1948         }
1949
1950         want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
1951
1952         cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
1953
1954         xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
1955
1956         /*
1957          * Re-find our place.
1958          */
1959         for (i = entno = 0, entry = &leaf->entries[0];
1960                      i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
1961                              entry++, i++) {
1962
1963                 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
1964                                     INT_GET(entry->nameidx, ARCH_CONVERT));
1965
1966                 if (unlikely(
1967                     ((char *)namest < (char *)leaf) ||
1968                     ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
1969                         XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
1970                                              XFS_ERRLEVEL_LOW, mp, leaf);
1971                         xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
1972                         return XFS_ERROR(EFSCORRUPTED);
1973                 }
1974                 if (INT_GET(entry->hashval, ARCH_CONVERT) >= cookhash) {
1975                         if (   entno < want_entno
1976                             && INT_GET(entry->hashval, ARCH_CONVERT)
1977                                                         == cookhash) {
1978                                 /*
1979                                  * Trying to get to a particular offset in a
1980                                  * run of equal-hashval entries.
1981                                  */
1982                                 entno++;
1983                         } else if (   want_entno > 0
1984                                    && entno == want_entno
1985                                    && INT_GET(entry->hashval, ARCH_CONVERT)
1986                                                         == cookhash) {
1987                                 break;
1988                         } else {
1989                                 entno = 0;
1990                                 break;
1991                         }
1992                 }
1993         }
1994
1995         if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
1996                 xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
1997                 if (!leaf->hdr.info.forw)
1998                         uio->uio_offset =
1999                                 XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
2000                 /*
2001                  * Don't set uio_offset if there's another block:
2002                  * the node code will be setting uio_offset anyway.
2003                  */
2004                 *eobp = 0;
2005                 return 0;
2006         }
2007         xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
2008
2009         p.dbp = dbp;
2010         p.put = put;
2011         p.uio = uio;
2012
2013         /*
2014          * We're synchronized, start copying entries out to the user.
2015          */
2016         for (; entno >= 0 && i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
2017                              entry++, i++, (entno = nextentno)) {
2018                 int lastresid=0, retval;
2019                 xfs_dircook_t lastoffset;
2020                 xfs_dahash_t thishash;
2021
2022                 /*
2023                  * Check for a damaged directory leaf block and pick up
2024                  * the inode number from this entry.
2025                  */
2026                 namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
2027                                     INT_GET(entry->nameidx, ARCH_CONVERT));
2028
2029                 if (unlikely(
2030                     ((char *)namest < (char *)leaf) ||
2031                     ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
2032                         XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
2033                                              XFS_ERRLEVEL_LOW, mp, leaf);
2034                         xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
2035                         return XFS_ERROR(EFSCORRUPTED);
2036                 }
2037
2038                 xfs_dir_trace_g_duc("leaf: middle cookie  ",
2039                                                    dp, uio, p.cook.o);
2040
2041                 if (i < (INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1)) {
2042                         nexthash = INT_GET(entry[1].hashval, ARCH_CONVERT);
2043
2044                         if (nexthash == INT_GET(entry->hashval, ARCH_CONVERT))
2045                                 nextentno = entno + 1;
2046                         else
2047                                 nextentno = 0;
2048                         XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
2049                         xfs_dir_trace_g_duc("leaf: middle cookie  ",
2050                                                    dp, uio, p.cook.o);
2051
2052                 } else if ((thishash = be32_to_cpu(leaf->hdr.info.forw))) {
2053                         xfs_dabuf_t *bp2;
2054                         xfs_dir_leafblock_t *leaf2;
2055
2056                         ASSERT(nextda != -1);
2057
2058                         retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
2059                                                  nextda, &bp2, XFS_DATA_FORK);
2060                         if (retval)
2061                                 return retval;
2062
2063                         ASSERT(bp2 != NULL);
2064
2065                         leaf2 = bp2->data;
2066
2067                         if (unlikely(
2068                                (be16_to_cpu(leaf2->hdr.info.magic)
2069                                                 != XFS_DIR_LEAF_MAGIC)
2070                             || (be32_to_cpu(leaf2->hdr.info.back)
2071                                                 != bno))) {     /* GROT */
2072                                 XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
2073                                                      XFS_ERRLEVEL_LOW, mp,
2074                                                      leaf2);
2075                                 xfs_da_brelse(dp->i_transp, bp2);
2076
2077                                 return XFS_ERROR(EFSCORRUPTED);
2078                         }
2079
2080                         nexthash = INT_GET(leaf2->entries[0].hashval,
2081                                                                 ARCH_CONVERT);
2082                         nextentno = -1;
2083                         XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
2084                         xfs_da_brelse(dp->i_transp, bp2);
2085                         xfs_dir_trace_g_duc("leaf: next blk cookie",
2086                                                    dp, uio, p.cook.o);
2087                 } else {
2088                         nextentno = -1;
2089                         XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
2090                 }
2091
2092                 /*
2093                  * Save off the cookie so we can fall back should the
2094                  * 'put' into the outgoing buffer fails.  To handle a run
2095                  * of equal-hashvals, the off_t structure on 64bit
2096                  * builds has entno built into the cookie to ID the
2097                  * entry.  On 32bit builds, we only have space for the
2098                  * hashval so we can't ID specific entries within a group
2099                  * of same hashval entries.   For this, lastoffset is set
2100                  * to the first in the run of equal hashvals so we don't
2101                  * include any entries unless we can include all entries
2102                  * that share the same hashval.  Hopefully the buffer
2103                  * provided is big enough to handle it (see pv763517).
2104                  */
2105 #if (BITS_PER_LONG == 32)
2106                 if ((thishash = INT_GET(entry->hashval, ARCH_CONVERT))
2107                                                                 != lasthash) {
2108                         XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2109                         lastresid = uio->uio_resid;
2110                         lasthash = thishash;
2111                 } else {
2112                         xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
2113                                                    dp, uio, p.cook.o);
2114                 }
2115 #else
2116                 thishash = INT_GET(entry->hashval, ARCH_CONVERT);
2117                 XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
2118                 lastresid = uio->uio_resid;
2119 #endif /* BITS_PER_LONG == 32 */
2120
2121                 /*
2122                  * Put the current entry into the outgoing buffer.  If we fail
2123                  * then restore the UIO to the first entry in the current
2124                  * run of equal-hashval entries (probably one 1 entry long).
2125                  */
2126                 p.ino = XFS_GET_DIR_INO8(namest->inumber);
2127 #if XFS_BIG_INUMS
2128                 p.ino += mp->m_inoadd;
2129 #endif
2130                 p.name = (char *)namest->name;
2131                 p.namelen = entry->namelen;
2132
2133                 retval = p.put(&p);
2134
2135                 if (!p.done) {
2136                         uio->uio_offset = lastoffset.o;
2137                         uio->uio_resid = lastresid;
2138
2139                         *eobp = 1;
2140
2141                         xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
2142
2143                         return retval;
2144                 }
2145         }
2146
2147         uio->uio_offset = p.cook.o;
2148
2149         *eobp = 0;
2150
2151         xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
2152
2153         return 0;
2154 }
2155
2156 /*
2157  * Format a dirent64 structure and copy it out the the user's buffer.
2158  */
2159 int
2160 xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
2161 {
2162         iovec_t *iovp;
2163         int reclen, namelen;
2164         xfs_dirent_t *idbp;
2165         uio_t *uio;
2166
2167         namelen = pa->namelen;
2168         reclen = DIRENTSIZE(namelen);
2169         uio = pa->uio;
2170         if (reclen > uio->uio_resid) {
2171                 pa->done = 0;
2172                 return 0;
2173         }
2174         iovp = uio->uio_iov;
2175         idbp = (xfs_dirent_t *)iovp->iov_base;
2176         iovp->iov_base = (char *)idbp + reclen;
2177         iovp->iov_len -= reclen;
2178         uio->uio_resid -= reclen;
2179         idbp->d_reclen = reclen;
2180         idbp->d_ino = pa->ino;
2181         idbp->d_off = pa->cook.o;
2182         idbp->d_name[namelen] = '\0';
2183         pa->done = 1;
2184         memcpy(idbp->d_name, pa->name, namelen);
2185         return 0;
2186 }
2187
2188 /*
2189  * Format a dirent64 structure and copy it out the the user's buffer.
2190  */
2191 int
2192 xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
2193 {
2194         int             retval, reclen, namelen;
2195         xfs_dirent_t    *idbp;
2196         uio_t           *uio;
2197
2198         namelen = pa->namelen;
2199         reclen = DIRENTSIZE(namelen);
2200         uio = pa->uio;
2201         if (reclen > uio->uio_resid) {
2202                 pa->done = 0;
2203                 return 0;
2204         }
2205         idbp = pa->dbp;
2206         idbp->d_reclen = reclen;
2207         idbp->d_ino = pa->ino;
2208         idbp->d_off = pa->cook.o;
2209         idbp->d_name[namelen] = '\0';
2210         memcpy(idbp->d_name, pa->name, namelen);
2211         retval = uio_read((caddr_t)idbp, reclen, uio);
2212         pa->done = (retval == 0);
2213         return retval;
2214 }