Merge branch 'master' into for-linus
[pandora-kernel.git] / fs / xfs / xfs_dir2_node.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_dir2.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dir2_sf.h"
32 #include "xfs_attr_sf.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_bmap.h"
36 #include "xfs_dir2_data.h"
37 #include "xfs_dir2_leaf.h"
38 #include "xfs_dir2_block.h"
39 #include "xfs_dir2_node.h"
40 #include "xfs_error.h"
41 #include "xfs_trace.h"
42
43 /*
44  * Function declarations.
45  */
46 static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
47 static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
48 #ifdef DEBUG
49 static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
50 #else
51 #define xfs_dir2_leafn_check(dp, bp)
52 #endif
53 static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
54                                     int start_s, xfs_dabuf_t *bp_d, int start_d,
55                                     int count);
56 static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
57                                      xfs_da_state_blk_t *blk1,
58                                      xfs_da_state_blk_t *blk2);
59 static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
60                                  int index, xfs_da_state_blk_t *dblk,
61                                  int *rval);
62 static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
63                                      xfs_da_state_blk_t *fblk);
64
65 /*
66  * Log entries from a freespace block.
67  */
68 STATIC void
69 xfs_dir2_free_log_bests(
70         xfs_trans_t             *tp,            /* transaction pointer */
71         xfs_dabuf_t             *bp,            /* freespace buffer */
72         int                     first,          /* first entry to log */
73         int                     last)           /* last entry to log */
74 {
75         xfs_dir2_free_t         *free;          /* freespace structure */
76
77         free = bp->data;
78         ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
79         xfs_da_log_buf(tp, bp,
80                 (uint)((char *)&free->bests[first] - (char *)free),
81                 (uint)((char *)&free->bests[last] - (char *)free +
82                        sizeof(free->bests[0]) - 1));
83 }
84
85 /*
86  * Log header from a freespace block.
87  */
88 static void
89 xfs_dir2_free_log_header(
90         xfs_trans_t             *tp,            /* transaction pointer */
91         xfs_dabuf_t             *bp)            /* freespace buffer */
92 {
93         xfs_dir2_free_t         *free;          /* freespace structure */
94
95         free = bp->data;
96         ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
97         xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
98                 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
99 }
100
101 /*
102  * Convert a leaf-format directory to a node-format directory.
103  * We need to change the magic number of the leaf block, and copy
104  * the freespace table out of the leaf block into its own block.
105  */
106 int                                             /* error */
107 xfs_dir2_leaf_to_node(
108         xfs_da_args_t           *args,          /* operation arguments */
109         xfs_dabuf_t             *lbp)           /* leaf buffer */
110 {
111         xfs_inode_t             *dp;            /* incore directory inode */
112         int                     error;          /* error return value */
113         xfs_dabuf_t             *fbp;           /* freespace buffer */
114         xfs_dir2_db_t           fdb;            /* freespace block number */
115         xfs_dir2_free_t         *free;          /* freespace structure */
116         __be16                  *from;          /* pointer to freespace entry */
117         int                     i;              /* leaf freespace index */
118         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
119         xfs_dir2_leaf_tail_t    *ltp;           /* leaf tail structure */
120         xfs_mount_t             *mp;            /* filesystem mount point */
121         int                     n;              /* count of live freespc ents */
122         xfs_dir2_data_off_t     off;            /* freespace entry value */
123         __be16                  *to;            /* pointer to freespace entry */
124         xfs_trans_t             *tp;            /* transaction pointer */
125
126         trace_xfs_dir2_leaf_to_node(args);
127
128         dp = args->dp;
129         mp = dp->i_mount;
130         tp = args->trans;
131         /*
132          * Add a freespace block to the directory.
133          */
134         if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
135                 return error;
136         }
137         ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
138         /*
139          * Get the buffer for the new freespace block.
140          */
141         if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
142                         XFS_DATA_FORK))) {
143                 return error;
144         }
145         ASSERT(fbp != NULL);
146         free = fbp->data;
147         leaf = lbp->data;
148         ltp = xfs_dir2_leaf_tail_p(mp, leaf);
149         /*
150          * Initialize the freespace block header.
151          */
152         free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
153         free->hdr.firstdb = 0;
154         ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
155         free->hdr.nvalid = ltp->bestcount;
156         /*
157          * Copy freespace entries from the leaf block to the new block.
158          * Count active entries.
159          */
160         for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
161              i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
162                 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
163                         n++;
164                 *to = cpu_to_be16(off);
165         }
166         free->hdr.nused = cpu_to_be32(n);
167         leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
168         /*
169          * Log everything.
170          */
171         xfs_dir2_leaf_log_header(tp, lbp);
172         xfs_dir2_free_log_header(tp, fbp);
173         xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
174         xfs_da_buf_done(fbp);
175         xfs_dir2_leafn_check(dp, lbp);
176         return 0;
177 }
178
179 /*
180  * Add a leaf entry to a leaf block in a node-form directory.
181  * The other work necessary is done from the caller.
182  */
183 static int                                      /* error */
184 xfs_dir2_leafn_add(
185         xfs_dabuf_t             *bp,            /* leaf buffer */
186         xfs_da_args_t           *args,          /* operation arguments */
187         int                     index)          /* insertion pt for new entry */
188 {
189         int                     compact;        /* compacting stale leaves */
190         xfs_inode_t             *dp;            /* incore directory inode */
191         int                     highstale;      /* next stale entry */
192         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
193         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
194         int                     lfloghigh;      /* high leaf entry logging */
195         int                     lfloglow;       /* low leaf entry logging */
196         int                     lowstale;       /* previous stale entry */
197         xfs_mount_t             *mp;            /* filesystem mount point */
198         xfs_trans_t             *tp;            /* transaction pointer */
199
200         trace_xfs_dir2_leafn_add(args, index);
201
202         dp = args->dp;
203         mp = dp->i_mount;
204         tp = args->trans;
205         leaf = bp->data;
206
207         /*
208          * Quick check just to make sure we are not going to index
209          * into other peoples memory
210          */
211         if (index < 0)
212                 return XFS_ERROR(EFSCORRUPTED);
213
214         /*
215          * If there are already the maximum number of leaf entries in
216          * the block, if there are no stale entries it won't fit.
217          * Caller will do a split.  If there are stale entries we'll do
218          * a compact.
219          */
220
221         if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
222                 if (!leaf->hdr.stale)
223                         return XFS_ERROR(ENOSPC);
224                 compact = be16_to_cpu(leaf->hdr.stale) > 1;
225         } else
226                 compact = 0;
227         ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
228         ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
229                be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
230
231         if (args->op_flags & XFS_DA_OP_JUSTCHECK)
232                 return 0;
233
234         /*
235          * Compact out all but one stale leaf entry.  Leaves behind
236          * the entry closest to index.
237          */
238         if (compact) {
239                 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
240                         &lfloglow, &lfloghigh);
241         }
242         /*
243          * Set impossible logging indices for this case.
244          */
245         else if (leaf->hdr.stale) {
246                 lfloglow = be16_to_cpu(leaf->hdr.count);
247                 lfloghigh = -1;
248         }
249         /*
250          * No stale entries, just insert a space for the new entry.
251          */
252         if (!leaf->hdr.stale) {
253                 lep = &leaf->ents[index];
254                 if (index < be16_to_cpu(leaf->hdr.count))
255                         memmove(lep + 1, lep,
256                                 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
257                 lfloglow = index;
258                 lfloghigh = be16_to_cpu(leaf->hdr.count);
259                 be16_add_cpu(&leaf->hdr.count, 1);
260         }
261         /*
262          * There are stale entries.  We'll use one for the new entry.
263          */
264         else {
265                 /*
266                  * If we didn't do a compact then we need to figure out
267                  * which stale entry will be used.
268                  */
269                 if (compact == 0) {
270                         /*
271                          * Find first stale entry before our insertion point.
272                          */
273                         for (lowstale = index - 1;
274                              lowstale >= 0 &&
275                                 be32_to_cpu(leaf->ents[lowstale].address) !=
276                                 XFS_DIR2_NULL_DATAPTR;
277                              lowstale--)
278                                 continue;
279                         /*
280                          * Find next stale entry after insertion point.
281                          * Stop looking if the answer would be worse than
282                          * lowstale already found.
283                          */
284                         for (highstale = index;
285                              highstale < be16_to_cpu(leaf->hdr.count) &&
286                                 be32_to_cpu(leaf->ents[highstale].address) !=
287                                 XFS_DIR2_NULL_DATAPTR &&
288                                 (lowstale < 0 ||
289                                  index - lowstale - 1 >= highstale - index);
290                              highstale++)
291                                 continue;
292                 }
293                 /*
294                  * Using the low stale entry.
295                  * Shift entries up toward the stale slot.
296                  */
297                 if (lowstale >= 0 &&
298                     (highstale == be16_to_cpu(leaf->hdr.count) ||
299                      index - lowstale - 1 < highstale - index)) {
300                         ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
301                                XFS_DIR2_NULL_DATAPTR);
302                         ASSERT(index - lowstale - 1 >= 0);
303                         if (index - lowstale - 1 > 0)
304                                 memmove(&leaf->ents[lowstale],
305                                         &leaf->ents[lowstale + 1],
306                                         (index - lowstale - 1) * sizeof(*lep));
307                         lep = &leaf->ents[index - 1];
308                         lfloglow = MIN(lowstale, lfloglow);
309                         lfloghigh = MAX(index - 1, lfloghigh);
310                 }
311                 /*
312                  * Using the high stale entry.
313                  * Shift entries down toward the stale slot.
314                  */
315                 else {
316                         ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
317                                XFS_DIR2_NULL_DATAPTR);
318                         ASSERT(highstale - index >= 0);
319                         if (highstale - index > 0)
320                                 memmove(&leaf->ents[index + 1],
321                                         &leaf->ents[index],
322                                         (highstale - index) * sizeof(*lep));
323                         lep = &leaf->ents[index];
324                         lfloglow = MIN(index, lfloglow);
325                         lfloghigh = MAX(highstale, lfloghigh);
326                 }
327                 be16_add_cpu(&leaf->hdr.stale, -1);
328         }
329         /*
330          * Insert the new entry, log everything.
331          */
332         lep->hashval = cpu_to_be32(args->hashval);
333         lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
334                                 args->blkno, args->index));
335         xfs_dir2_leaf_log_header(tp, bp);
336         xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
337         xfs_dir2_leafn_check(dp, bp);
338         return 0;
339 }
340
341 #ifdef DEBUG
342 /*
343  * Check internal consistency of a leafn block.
344  */
345 void
346 xfs_dir2_leafn_check(
347         xfs_inode_t     *dp,                    /* incore directory inode */
348         xfs_dabuf_t     *bp)                    /* leaf buffer */
349 {
350         int             i;                      /* leaf index */
351         xfs_dir2_leaf_t *leaf;                  /* leaf structure */
352         xfs_mount_t     *mp;                    /* filesystem mount point */
353         int             stale;                  /* count of stale leaves */
354
355         leaf = bp->data;
356         mp = dp->i_mount;
357         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
358         ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
359         for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
360                 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
361                         ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
362                                be32_to_cpu(leaf->ents[i + 1].hashval));
363                 }
364                 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
365                         stale++;
366         }
367         ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
368 }
369 #endif  /* DEBUG */
370
371 /*
372  * Return the last hash value in the leaf.
373  * Stale entries are ok.
374  */
375 xfs_dahash_t                                    /* hash value */
376 xfs_dir2_leafn_lasthash(
377         xfs_dabuf_t     *bp,                    /* leaf buffer */
378         int             *count)                 /* count of entries in leaf */
379 {
380         xfs_dir2_leaf_t *leaf;                  /* leaf structure */
381
382         leaf = bp->data;
383         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
384         if (count)
385                 *count = be16_to_cpu(leaf->hdr.count);
386         if (!leaf->hdr.count)
387                 return 0;
388         return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
389 }
390
391 /*
392  * Look up a leaf entry for space to add a name in a node-format leaf block.
393  * The extrablk in state is a freespace block.
394  */
395 STATIC int
396 xfs_dir2_leafn_lookup_for_addname(
397         xfs_dabuf_t             *bp,            /* leaf buffer */
398         xfs_da_args_t           *args,          /* operation arguments */
399         int                     *indexp,        /* out: leaf entry index */
400         xfs_da_state_t          *state)         /* state to fill in */
401 {
402         xfs_dabuf_t             *curbp = NULL;  /* current data/free buffer */
403         xfs_dir2_db_t           curdb = -1;     /* current data block number */
404         xfs_dir2_db_t           curfdb = -1;    /* current free block number */
405         xfs_inode_t             *dp;            /* incore directory inode */
406         int                     error;          /* error return value */
407         int                     fi;             /* free entry index */
408         xfs_dir2_free_t         *free = NULL;   /* free block structure */
409         int                     index;          /* leaf entry index */
410         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
411         int                     length;         /* length of new data entry */
412         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
413         xfs_mount_t             *mp;            /* filesystem mount point */
414         xfs_dir2_db_t           newdb;          /* new data block number */
415         xfs_dir2_db_t           newfdb;         /* new free block number */
416         xfs_trans_t             *tp;            /* transaction pointer */
417
418         dp = args->dp;
419         tp = args->trans;
420         mp = dp->i_mount;
421         leaf = bp->data;
422         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
423 #ifdef __KERNEL__
424         ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
425 #endif
426         xfs_dir2_leafn_check(dp, bp);
427         /*
428          * Look up the hash value in the leaf entries.
429          */
430         index = xfs_dir2_leaf_search_hash(args, bp);
431         /*
432          * Do we have a buffer coming in?
433          */
434         if (state->extravalid) {
435                 /* If so, it's a free block buffer, get the block number. */
436                 curbp = state->extrablk.bp;
437                 curfdb = state->extrablk.blkno;
438                 free = curbp->data;
439                 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
440         }
441         length = xfs_dir2_data_entsize(args->namelen);
442         /*
443          * Loop over leaf entries with the right hash value.
444          */
445         for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
446                                 be32_to_cpu(lep->hashval) == args->hashval;
447                                 lep++, index++) {
448                 /*
449                  * Skip stale leaf entries.
450                  */
451                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
452                         continue;
453                 /*
454                  * Pull the data block number from the entry.
455                  */
456                 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
457                 /*
458                  * For addname, we're looking for a place to put the new entry.
459                  * We want to use a data block with an entry of equal
460                  * hash value to ours if there is one with room.
461                  *
462                  * If this block isn't the data block we already have
463                  * in hand, take a look at it.
464                  */
465                 if (newdb != curdb) {
466                         curdb = newdb;
467                         /*
468                          * Convert the data block to the free block
469                          * holding its freespace information.
470                          */
471                         newfdb = xfs_dir2_db_to_fdb(mp, newdb);
472                         /*
473                          * If it's not the one we have in hand, read it in.
474                          */
475                         if (newfdb != curfdb) {
476                                 /*
477                                  * If we had one before, drop it.
478                                  */
479                                 if (curbp)
480                                         xfs_da_brelse(tp, curbp);
481                                 /*
482                                  * Read the free block.
483                                  */
484                                 error = xfs_da_read_buf(tp, dp,
485                                                 xfs_dir2_db_to_da(mp, newfdb),
486                                                 -1, &curbp, XFS_DATA_FORK);
487                                 if (error)
488                                         return error;
489                                 free = curbp->data;
490                                 ASSERT(be32_to_cpu(free->hdr.magic) ==
491                                         XFS_DIR2_FREE_MAGIC);
492                                 ASSERT((be32_to_cpu(free->hdr.firstdb) %
493                                         XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
494                                 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
495                                 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
496                                         be32_to_cpu(free->hdr.nvalid));
497                         }
498                         /*
499                          * Get the index for our entry.
500                          */
501                         fi = xfs_dir2_db_to_fdindex(mp, curdb);
502                         /*
503                          * If it has room, return it.
504                          */
505                         if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
506                                 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
507                                                         XFS_ERRLEVEL_LOW, mp);
508                                 if (curfdb != newfdb)
509                                         xfs_da_brelse(tp, curbp);
510                                 return XFS_ERROR(EFSCORRUPTED);
511                         }
512                         curfdb = newfdb;
513                         if (be16_to_cpu(free->bests[fi]) >= length)
514                                 goto out;
515                 }
516         }
517         /* Didn't find any space */
518         fi = -1;
519 out:
520         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
521         if (curbp) {
522                 /* Giving back a free block. */
523                 state->extravalid = 1;
524                 state->extrablk.bp = curbp;
525                 state->extrablk.index = fi;
526                 state->extrablk.blkno = curfdb;
527                 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
528         } else {
529                 state->extravalid = 0;
530         }
531         /*
532          * Return the index, that will be the insertion point.
533          */
534         *indexp = index;
535         return XFS_ERROR(ENOENT);
536 }
537
538 /*
539  * Look up a leaf entry in a node-format leaf block.
540  * The extrablk in state a data block.
541  */
542 STATIC int
543 xfs_dir2_leafn_lookup_for_entry(
544         xfs_dabuf_t             *bp,            /* leaf buffer */
545         xfs_da_args_t           *args,          /* operation arguments */
546         int                     *indexp,        /* out: leaf entry index */
547         xfs_da_state_t          *state)         /* state to fill in */
548 {
549         xfs_dabuf_t             *curbp = NULL;  /* current data/free buffer */
550         xfs_dir2_db_t           curdb = -1;     /* current data block number */
551         xfs_dir2_data_entry_t   *dep;           /* data block entry */
552         xfs_inode_t             *dp;            /* incore directory inode */
553         int                     error;          /* error return value */
554         int                     index;          /* leaf entry index */
555         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
556         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
557         xfs_mount_t             *mp;            /* filesystem mount point */
558         xfs_dir2_db_t           newdb;          /* new data block number */
559         xfs_trans_t             *tp;            /* transaction pointer */
560         enum xfs_dacmp          cmp;            /* comparison result */
561
562         dp = args->dp;
563         tp = args->trans;
564         mp = dp->i_mount;
565         leaf = bp->data;
566         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
567 #ifdef __KERNEL__
568         ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
569 #endif
570         xfs_dir2_leafn_check(dp, bp);
571         /*
572          * Look up the hash value in the leaf entries.
573          */
574         index = xfs_dir2_leaf_search_hash(args, bp);
575         /*
576          * Do we have a buffer coming in?
577          */
578         if (state->extravalid) {
579                 curbp = state->extrablk.bp;
580                 curdb = state->extrablk.blkno;
581         }
582         /*
583          * Loop over leaf entries with the right hash value.
584          */
585         for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
586                                 be32_to_cpu(lep->hashval) == args->hashval;
587                                 lep++, index++) {
588                 /*
589                  * Skip stale leaf entries.
590                  */
591                 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
592                         continue;
593                 /*
594                  * Pull the data block number from the entry.
595                  */
596                 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
597                 /*
598                  * Not adding a new entry, so we really want to find
599                  * the name given to us.
600                  *
601                  * If it's a different data block, go get it.
602                  */
603                 if (newdb != curdb) {
604                         /*
605                          * If we had a block before that we aren't saving
606                          * for a CI name, drop it
607                          */
608                         if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
609                                                 curdb != state->extrablk.blkno))
610                                 xfs_da_brelse(tp, curbp);
611                         /*
612                          * If needing the block that is saved with a CI match,
613                          * use it otherwise read in the new data block.
614                          */
615                         if (args->cmpresult != XFS_CMP_DIFFERENT &&
616                                         newdb == state->extrablk.blkno) {
617                                 ASSERT(state->extravalid);
618                                 curbp = state->extrablk.bp;
619                         } else {
620                                 error = xfs_da_read_buf(tp, dp,
621                                                 xfs_dir2_db_to_da(mp, newdb),
622                                                 -1, &curbp, XFS_DATA_FORK);
623                                 if (error)
624                                         return error;
625                         }
626                         xfs_dir2_data_check(dp, curbp);
627                         curdb = newdb;
628                 }
629                 /*
630                  * Point to the data entry.
631                  */
632                 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
633                         xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
634                 /*
635                  * Compare the entry and if it's an exact match, return
636                  * EEXIST immediately. If it's the first case-insensitive
637                  * match, store the block & inode number and continue looking.
638                  */
639                 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
640                 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
641                         /* If there is a CI match block, drop it */
642                         if (args->cmpresult != XFS_CMP_DIFFERENT &&
643                                                 curdb != state->extrablk.blkno)
644                                 xfs_da_brelse(tp, state->extrablk.bp);
645                         args->cmpresult = cmp;
646                         args->inumber = be64_to_cpu(dep->inumber);
647                         *indexp = index;
648                         state->extravalid = 1;
649                         state->extrablk.bp = curbp;
650                         state->extrablk.blkno = curdb;
651                         state->extrablk.index = (int)((char *)dep -
652                                                         (char *)curbp->data);
653                         state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
654                         if (cmp == XFS_CMP_EXACT)
655                                 return XFS_ERROR(EEXIST);
656                 }
657         }
658         ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
659                                         (args->op_flags & XFS_DA_OP_OKNOENT));
660         if (curbp) {
661                 if (args->cmpresult == XFS_CMP_DIFFERENT) {
662                         /* Giving back last used data block. */
663                         state->extravalid = 1;
664                         state->extrablk.bp = curbp;
665                         state->extrablk.index = -1;
666                         state->extrablk.blkno = curdb;
667                         state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
668                 } else {
669                         /* If the curbp is not the CI match block, drop it */
670                         if (state->extrablk.bp != curbp)
671                                 xfs_da_brelse(tp, curbp);
672                 }
673         } else {
674                 state->extravalid = 0;
675         }
676         *indexp = index;
677         return XFS_ERROR(ENOENT);
678 }
679
680 /*
681  * Look up a leaf entry in a node-format leaf block.
682  * If this is an addname then the extrablk in state is a freespace block,
683  * otherwise it's a data block.
684  */
685 int
686 xfs_dir2_leafn_lookup_int(
687         xfs_dabuf_t             *bp,            /* leaf buffer */
688         xfs_da_args_t           *args,          /* operation arguments */
689         int                     *indexp,        /* out: leaf entry index */
690         xfs_da_state_t          *state)         /* state to fill in */
691 {
692         if (args->op_flags & XFS_DA_OP_ADDNAME)
693                 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
694                                                         state);
695         return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
696 }
697
698 /*
699  * Move count leaf entries from source to destination leaf.
700  * Log entries and headers.  Stale entries are preserved.
701  */
702 static void
703 xfs_dir2_leafn_moveents(
704         xfs_da_args_t   *args,                  /* operation arguments */
705         xfs_dabuf_t     *bp_s,                  /* source leaf buffer */
706         int             start_s,                /* source leaf index */
707         xfs_dabuf_t     *bp_d,                  /* destination leaf buffer */
708         int             start_d,                /* destination leaf index */
709         int             count)                  /* count of leaves to copy */
710 {
711         xfs_dir2_leaf_t *leaf_d;                /* destination leaf structure */
712         xfs_dir2_leaf_t *leaf_s;                /* source leaf structure */
713         int             stale;                  /* count stale leaves copied */
714         xfs_trans_t     *tp;                    /* transaction pointer */
715
716         trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
717
718         /*
719          * Silently return if nothing to do.
720          */
721         if (count == 0) {
722                 return;
723         }
724         tp = args->trans;
725         leaf_s = bp_s->data;
726         leaf_d = bp_d->data;
727         /*
728          * If the destination index is not the end of the current
729          * destination leaf entries, open up a hole in the destination
730          * to hold the new entries.
731          */
732         if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
733                 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
734                         (be16_to_cpu(leaf_d->hdr.count) - start_d) *
735                         sizeof(xfs_dir2_leaf_entry_t));
736                 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
737                         count + be16_to_cpu(leaf_d->hdr.count) - 1);
738         }
739         /*
740          * If the source has stale leaves, count the ones in the copy range
741          * so we can update the header correctly.
742          */
743         if (leaf_s->hdr.stale) {
744                 int     i;                      /* temp leaf index */
745
746                 for (i = start_s, stale = 0; i < start_s + count; i++) {
747                         if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
748                                 stale++;
749                 }
750         } else
751                 stale = 0;
752         /*
753          * Copy the leaf entries from source to destination.
754          */
755         memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
756                 count * sizeof(xfs_dir2_leaf_entry_t));
757         xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
758         /*
759          * If there are source entries after the ones we copied,
760          * delete the ones we copied by sliding the next ones down.
761          */
762         if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
763                 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
764                         count * sizeof(xfs_dir2_leaf_entry_t));
765                 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
766         }
767         /*
768          * Update the headers and log them.
769          */
770         be16_add_cpu(&leaf_s->hdr.count, -(count));
771         be16_add_cpu(&leaf_s->hdr.stale, -(stale));
772         be16_add_cpu(&leaf_d->hdr.count, count);
773         be16_add_cpu(&leaf_d->hdr.stale, stale);
774         xfs_dir2_leaf_log_header(tp, bp_s);
775         xfs_dir2_leaf_log_header(tp, bp_d);
776         xfs_dir2_leafn_check(args->dp, bp_s);
777         xfs_dir2_leafn_check(args->dp, bp_d);
778 }
779
780 /*
781  * Determine the sort order of two leaf blocks.
782  * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
783  */
784 int                                             /* sort order */
785 xfs_dir2_leafn_order(
786         xfs_dabuf_t     *leaf1_bp,              /* leaf1 buffer */
787         xfs_dabuf_t     *leaf2_bp)              /* leaf2 buffer */
788 {
789         xfs_dir2_leaf_t *leaf1;                 /* leaf1 structure */
790         xfs_dir2_leaf_t *leaf2;                 /* leaf2 structure */
791
792         leaf1 = leaf1_bp->data;
793         leaf2 = leaf2_bp->data;
794         ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
795         ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
796         if (be16_to_cpu(leaf1->hdr.count) > 0 &&
797             be16_to_cpu(leaf2->hdr.count) > 0 &&
798             (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
799              be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
800              be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
801                 return 1;
802         return 0;
803 }
804
805 /*
806  * Rebalance leaf entries between two leaf blocks.
807  * This is actually only called when the second block is new,
808  * though the code deals with the general case.
809  * A new entry will be inserted in one of the blocks, and that
810  * entry is taken into account when balancing.
811  */
812 static void
813 xfs_dir2_leafn_rebalance(
814         xfs_da_state_t          *state,         /* btree cursor */
815         xfs_da_state_blk_t      *blk1,          /* first btree block */
816         xfs_da_state_blk_t      *blk2)          /* second btree block */
817 {
818         xfs_da_args_t           *args;          /* operation arguments */
819         int                     count;          /* count (& direction) leaves */
820         int                     isleft;         /* new goes in left leaf */
821         xfs_dir2_leaf_t         *leaf1;         /* first leaf structure */
822         xfs_dir2_leaf_t         *leaf2;         /* second leaf structure */
823         int                     mid;            /* midpoint leaf index */
824 #ifdef DEBUG
825         int                     oldstale;       /* old count of stale leaves */
826 #endif
827         int                     oldsum;         /* old total leaf count */
828         int                     swap;           /* swapped leaf blocks */
829
830         args = state->args;
831         /*
832          * If the block order is wrong, swap the arguments.
833          */
834         if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
835                 xfs_da_state_blk_t      *tmp;   /* temp for block swap */
836
837                 tmp = blk1;
838                 blk1 = blk2;
839                 blk2 = tmp;
840         }
841         leaf1 = blk1->bp->data;
842         leaf2 = blk2->bp->data;
843         oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
844 #ifdef DEBUG
845         oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
846 #endif
847         mid = oldsum >> 1;
848         /*
849          * If the old leaf count was odd then the new one will be even,
850          * so we need to divide the new count evenly.
851          */
852         if (oldsum & 1) {
853                 xfs_dahash_t    midhash;        /* middle entry hash value */
854
855                 if (mid >= be16_to_cpu(leaf1->hdr.count))
856                         midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
857                 else
858                         midhash = be32_to_cpu(leaf1->ents[mid].hashval);
859                 isleft = args->hashval <= midhash;
860         }
861         /*
862          * If the old count is even then the new count is odd, so there's
863          * no preferred side for the new entry.
864          * Pick the left one.
865          */
866         else
867                 isleft = 1;
868         /*
869          * Calculate moved entry count.  Positive means left-to-right,
870          * negative means right-to-left.  Then move the entries.
871          */
872         count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
873         if (count > 0)
874                 xfs_dir2_leafn_moveents(args, blk1->bp,
875                         be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
876         else if (count < 0)
877                 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
878                         be16_to_cpu(leaf1->hdr.count), count);
879         ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
880         ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
881         /*
882          * Mark whether we're inserting into the old or new leaf.
883          */
884         if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
885                 state->inleaf = swap;
886         else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
887                 state->inleaf = !swap;
888         else
889                 state->inleaf =
890                         swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
891         /*
892          * Adjust the expected index for insertion.
893          */
894         if (!state->inleaf)
895                 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
896
897         /*
898          * Finally sanity check just to make sure we are not returning a
899          * negative index
900          */
901         if(blk2->index < 0) {
902                 state->inleaf = 1;
903                 blk2->index = 0;
904                 cmn_err(CE_ALERT,
905                         "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
906                         "blk1->index %d\n",
907                         blk1->index);
908         }
909 }
910
911 /*
912  * Remove an entry from a node directory.
913  * This removes the leaf entry and the data entry,
914  * and updates the free block if necessary.
915  */
916 static int                                      /* error */
917 xfs_dir2_leafn_remove(
918         xfs_da_args_t           *args,          /* operation arguments */
919         xfs_dabuf_t             *bp,            /* leaf buffer */
920         int                     index,          /* leaf entry index */
921         xfs_da_state_blk_t      *dblk,          /* data block */
922         int                     *rval)          /* resulting block needs join */
923 {
924         xfs_dir2_data_t         *data;          /* data block structure */
925         xfs_dir2_db_t           db;             /* data block number */
926         xfs_dabuf_t             *dbp;           /* data block buffer */
927         xfs_dir2_data_entry_t   *dep;           /* data block entry */
928         xfs_inode_t             *dp;            /* incore directory inode */
929         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
930         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry */
931         int                     longest;        /* longest data free entry */
932         int                     off;            /* data block entry offset */
933         xfs_mount_t             *mp;            /* filesystem mount point */
934         int                     needlog;        /* need to log data header */
935         int                     needscan;       /* need to rescan data frees */
936         xfs_trans_t             *tp;            /* transaction pointer */
937
938         trace_xfs_dir2_leafn_remove(args, index);
939
940         dp = args->dp;
941         tp = args->trans;
942         mp = dp->i_mount;
943         leaf = bp->data;
944         ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
945         /*
946          * Point to the entry we're removing.
947          */
948         lep = &leaf->ents[index];
949         /*
950          * Extract the data block and offset from the entry.
951          */
952         db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
953         ASSERT(dblk->blkno == db);
954         off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
955         ASSERT(dblk->index == off);
956         /*
957          * Kill the leaf entry by marking it stale.
958          * Log the leaf block changes.
959          */
960         be16_add_cpu(&leaf->hdr.stale, 1);
961         xfs_dir2_leaf_log_header(tp, bp);
962         lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
963         xfs_dir2_leaf_log_ents(tp, bp, index, index);
964         /*
965          * Make the data entry free.  Keep track of the longest freespace
966          * in the data block in case it changes.
967          */
968         dbp = dblk->bp;
969         data = dbp->data;
970         dep = (xfs_dir2_data_entry_t *)((char *)data + off);
971         longest = be16_to_cpu(data->hdr.bestfree[0].length);
972         needlog = needscan = 0;
973         xfs_dir2_data_make_free(tp, dbp, off,
974                 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
975         /*
976          * Rescan the data block freespaces for bestfree.
977          * Log the data block header if needed.
978          */
979         if (needscan)
980                 xfs_dir2_data_freescan(mp, data, &needlog);
981         if (needlog)
982                 xfs_dir2_data_log_header(tp, dbp);
983         xfs_dir2_data_check(dp, dbp);
984         /*
985          * If the longest data block freespace changes, need to update
986          * the corresponding freeblock entry.
987          */
988         if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
989                 int             error;          /* error return value */
990                 xfs_dabuf_t     *fbp;           /* freeblock buffer */
991                 xfs_dir2_db_t   fdb;            /* freeblock block number */
992                 int             findex;         /* index in freeblock entries */
993                 xfs_dir2_free_t *free;          /* freeblock structure */
994                 int             logfree;        /* need to log free entry */
995
996                 /*
997                  * Convert the data block number to a free block,
998                  * read in the free block.
999                  */
1000                 fdb = xfs_dir2_db_to_fdb(mp, db);
1001                 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1002                                 -1, &fbp, XFS_DATA_FORK))) {
1003                         return error;
1004                 }
1005                 free = fbp->data;
1006                 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1007                 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
1008                        XFS_DIR2_MAX_FREE_BESTS(mp) *
1009                        (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1010                 /*
1011                  * Calculate which entry we need to fix.
1012                  */
1013                 findex = xfs_dir2_db_to_fdindex(mp, db);
1014                 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1015                 /*
1016                  * If the data block is now empty we can get rid of it
1017                  * (usually).
1018                  */
1019                 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1020                         /*
1021                          * Try to punch out the data block.
1022                          */
1023                         error = xfs_dir2_shrink_inode(args, db, dbp);
1024                         if (error == 0) {
1025                                 dblk->bp = NULL;
1026                                 data = NULL;
1027                         }
1028                         /*
1029                          * We can get ENOSPC if there's no space reservation.
1030                          * In this case just drop the buffer and some one else
1031                          * will eventually get rid of the empty block.
1032                          */
1033                         else if (error == ENOSPC && args->total == 0)
1034                                 xfs_da_buf_done(dbp);
1035                         else
1036                                 return error;
1037                 }
1038                 /*
1039                  * If we got rid of the data block, we can eliminate that entry
1040                  * in the free block.
1041                  */
1042                 if (data == NULL) {
1043                         /*
1044                          * One less used entry in the free table.
1045                          */
1046                         be32_add_cpu(&free->hdr.nused, -1);
1047                         xfs_dir2_free_log_header(tp, fbp);
1048                         /*
1049                          * If this was the last entry in the table, we can
1050                          * trim the table size back.  There might be other
1051                          * entries at the end referring to non-existent
1052                          * data blocks, get those too.
1053                          */
1054                         if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
1055                                 int     i;              /* free entry index */
1056
1057                                 for (i = findex - 1;
1058                                      i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
1059                                      i--)
1060                                         continue;
1061                                 free->hdr.nvalid = cpu_to_be32(i + 1);
1062                                 logfree = 0;
1063                         }
1064                         /*
1065                          * Not the last entry, just punch it out.
1066                          */
1067                         else {
1068                                 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1069                                 logfree = 1;
1070                         }
1071                         /*
1072                          * If there are no useful entries left in the block,
1073                          * get rid of the block if we can.
1074                          */
1075                         if (!free->hdr.nused) {
1076                                 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1077                                 if (error == 0) {
1078                                         fbp = NULL;
1079                                         logfree = 0;
1080                                 } else if (error != ENOSPC || args->total != 0)
1081                                         return error;
1082                                 /*
1083                                  * It's possible to get ENOSPC if there is no
1084                                  * space reservation.  In this case some one
1085                                  * else will eventually get rid of this block.
1086                                  */
1087                         }
1088                 }
1089                 /*
1090                  * Data block is not empty, just set the free entry to
1091                  * the new value.
1092                  */
1093                 else {
1094                         free->bests[findex] = cpu_to_be16(longest);
1095                         logfree = 1;
1096                 }
1097                 /*
1098                  * Log the free entry that changed, unless we got rid of it.
1099                  */
1100                 if (logfree)
1101                         xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1102                 /*
1103                  * Drop the buffer if we still have it.
1104                  */
1105                 if (fbp)
1106                         xfs_da_buf_done(fbp);
1107         }
1108         xfs_dir2_leafn_check(dp, bp);
1109         /*
1110          * Return indication of whether this leaf block is empty enough
1111          * to justify trying to join it with a neighbor.
1112          */
1113         *rval =
1114                 ((uint)sizeof(leaf->hdr) +
1115                  (uint)sizeof(leaf->ents[0]) *
1116                  (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
1117                 mp->m_dir_magicpct;
1118         return 0;
1119 }
1120
1121 /*
1122  * Split the leaf entries in the old block into old and new blocks.
1123  */
1124 int                                             /* error */
1125 xfs_dir2_leafn_split(
1126         xfs_da_state_t          *state,         /* btree cursor */
1127         xfs_da_state_blk_t      *oldblk,        /* original block */
1128         xfs_da_state_blk_t      *newblk)        /* newly created block */
1129 {
1130         xfs_da_args_t           *args;          /* operation arguments */
1131         xfs_dablk_t             blkno;          /* new leaf block number */
1132         int                     error;          /* error return value */
1133         xfs_mount_t             *mp;            /* filesystem mount point */
1134
1135         /*
1136          * Allocate space for a new leaf node.
1137          */
1138         args = state->args;
1139         mp = args->dp->i_mount;
1140         ASSERT(args != NULL);
1141         ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1142         error = xfs_da_grow_inode(args, &blkno);
1143         if (error) {
1144                 return error;
1145         }
1146         /*
1147          * Initialize the new leaf block.
1148          */
1149         error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
1150                 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1151         if (error) {
1152                 return error;
1153         }
1154         newblk->blkno = blkno;
1155         newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1156         /*
1157          * Rebalance the entries across the two leaves, link the new
1158          * block into the leaves.
1159          */
1160         xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1161         error = xfs_da_blk_link(state, oldblk, newblk);
1162         if (error) {
1163                 return error;
1164         }
1165         /*
1166          * Insert the new entry in the correct block.
1167          */
1168         if (state->inleaf)
1169                 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1170         else
1171                 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1172         /*
1173          * Update last hashval in each block since we added the name.
1174          */
1175         oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1176         newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1177         xfs_dir2_leafn_check(args->dp, oldblk->bp);
1178         xfs_dir2_leafn_check(args->dp, newblk->bp);
1179         return error;
1180 }
1181
1182 /*
1183  * Check a leaf block and its neighbors to see if the block should be
1184  * collapsed into one or the other neighbor.  Always keep the block
1185  * with the smaller block number.
1186  * If the current block is over 50% full, don't try to join it, return 0.
1187  * If the block is empty, fill in the state structure and return 2.
1188  * If it can be collapsed, fill in the state structure and return 1.
1189  * If nothing can be done, return 0.
1190  */
1191 int                                             /* error */
1192 xfs_dir2_leafn_toosmall(
1193         xfs_da_state_t          *state,         /* btree cursor */
1194         int                     *action)        /* resulting action to take */
1195 {
1196         xfs_da_state_blk_t      *blk;           /* leaf block */
1197         xfs_dablk_t             blkno;          /* leaf block number */
1198         xfs_dabuf_t             *bp;            /* leaf buffer */
1199         int                     bytes;          /* bytes in use */
1200         int                     count;          /* leaf live entry count */
1201         int                     error;          /* error return value */
1202         int                     forward;        /* sibling block direction */
1203         int                     i;              /* sibling counter */
1204         xfs_da_blkinfo_t        *info;          /* leaf block header */
1205         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
1206         int                     rval;           /* result from path_shift */
1207
1208         /*
1209          * Check for the degenerate case of the block being over 50% full.
1210          * If so, it's not worth even looking to see if we might be able
1211          * to coalesce with a sibling.
1212          */
1213         blk = &state->path.blk[state->path.active - 1];
1214         info = blk->bp->data;
1215         ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
1216         leaf = (xfs_dir2_leaf_t *)info;
1217         count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1218         bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1219         if (bytes > (state->blocksize >> 1)) {
1220                 /*
1221                  * Blk over 50%, don't try to join.
1222                  */
1223                 *action = 0;
1224                 return 0;
1225         }
1226         /*
1227          * Check for the degenerate case of the block being empty.
1228          * If the block is empty, we'll simply delete it, no need to
1229          * coalesce it with a sibling block.  We choose (arbitrarily)
1230          * to merge with the forward block unless it is NULL.
1231          */
1232         if (count == 0) {
1233                 /*
1234                  * Make altpath point to the block we want to keep and
1235                  * path point to the block we want to drop (this one).
1236                  */
1237                 forward = (info->forw != 0);
1238                 memcpy(&state->altpath, &state->path, sizeof(state->path));
1239                 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1240                         &rval);
1241                 if (error)
1242                         return error;
1243                 *action = rval ? 2 : 0;
1244                 return 0;
1245         }
1246         /*
1247          * Examine each sibling block to see if we can coalesce with
1248          * at least 25% free space to spare.  We need to figure out
1249          * whether to merge with the forward or the backward block.
1250          * We prefer coalescing with the lower numbered sibling so as
1251          * to shrink a directory over time.
1252          */
1253         forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
1254         for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1255                 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
1256                 if (blkno == 0)
1257                         continue;
1258                 /*
1259                  * Read the sibling leaf block.
1260                  */
1261                 if ((error =
1262                     xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1263                             -1, &bp, XFS_DATA_FORK))) {
1264                         return error;
1265                 }
1266                 ASSERT(bp != NULL);
1267                 /*
1268                  * Count bytes in the two blocks combined.
1269                  */
1270                 leaf = (xfs_dir2_leaf_t *)info;
1271                 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1272                 bytes = state->blocksize - (state->blocksize >> 2);
1273                 leaf = bp->data;
1274                 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1275                 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1276                 bytes -= count * (uint)sizeof(leaf->ents[0]);
1277                 /*
1278                  * Fits with at least 25% to spare.
1279                  */
1280                 if (bytes >= 0)
1281                         break;
1282                 xfs_da_brelse(state->args->trans, bp);
1283         }
1284         /*
1285          * Didn't like either block, give up.
1286          */
1287         if (i >= 2) {
1288                 *action = 0;
1289                 return 0;
1290         }
1291         /*
1292          * Done with the sibling leaf block here, drop the dabuf
1293          * so path_shift can get it.
1294          */
1295         xfs_da_buf_done(bp);
1296         /*
1297          * Make altpath point to the block we want to keep (the lower
1298          * numbered block) and path point to the block we want to drop.
1299          */
1300         memcpy(&state->altpath, &state->path, sizeof(state->path));
1301         if (blkno < blk->blkno)
1302                 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1303                         &rval);
1304         else
1305                 error = xfs_da_path_shift(state, &state->path, forward, 0,
1306                         &rval);
1307         if (error) {
1308                 return error;
1309         }
1310         *action = rval ? 0 : 1;
1311         return 0;
1312 }
1313
1314 /*
1315  * Move all the leaf entries from drop_blk to save_blk.
1316  * This is done as part of a join operation.
1317  */
1318 void
1319 xfs_dir2_leafn_unbalance(
1320         xfs_da_state_t          *state,         /* cursor */
1321         xfs_da_state_blk_t      *drop_blk,      /* dead block */
1322         xfs_da_state_blk_t      *save_blk)      /* surviving block */
1323 {
1324         xfs_da_args_t           *args;          /* operation arguments */
1325         xfs_dir2_leaf_t         *drop_leaf;     /* dead leaf structure */
1326         xfs_dir2_leaf_t         *save_leaf;     /* surviving leaf structure */
1327
1328         args = state->args;
1329         ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1330         ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1331         drop_leaf = drop_blk->bp->data;
1332         save_leaf = save_blk->bp->data;
1333         ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1334         ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1335         /*
1336          * If there are any stale leaf entries, take this opportunity
1337          * to purge them.
1338          */
1339         if (drop_leaf->hdr.stale)
1340                 xfs_dir2_leaf_compact(args, drop_blk->bp);
1341         if (save_leaf->hdr.stale)
1342                 xfs_dir2_leaf_compact(args, save_blk->bp);
1343         /*
1344          * Move the entries from drop to the appropriate end of save.
1345          */
1346         drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
1347         if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1348                 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
1349                         be16_to_cpu(drop_leaf->hdr.count));
1350         else
1351                 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
1352                         be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
1353         save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
1354         xfs_dir2_leafn_check(args->dp, save_blk->bp);
1355 }
1356
1357 /*
1358  * Top-level node form directory addname routine.
1359  */
1360 int                                             /* error */
1361 xfs_dir2_node_addname(
1362         xfs_da_args_t           *args)          /* operation arguments */
1363 {
1364         xfs_da_state_blk_t      *blk;           /* leaf block for insert */
1365         int                     error;          /* error return value */
1366         int                     rval;           /* sub-return value */
1367         xfs_da_state_t          *state;         /* btree cursor */
1368
1369         trace_xfs_dir2_node_addname(args);
1370
1371         /*
1372          * Allocate and initialize the state (btree cursor).
1373          */
1374         state = xfs_da_state_alloc();
1375         state->args = args;
1376         state->mp = args->dp->i_mount;
1377         state->blocksize = state->mp->m_dirblksize;
1378         state->node_ents = state->mp->m_dir_node_ents;
1379         /*
1380          * Look up the name.  We're not supposed to find it, but
1381          * this gives us the insertion point.
1382          */
1383         error = xfs_da_node_lookup_int(state, &rval);
1384         if (error)
1385                 rval = error;
1386         if (rval != ENOENT) {
1387                 goto done;
1388         }
1389         /*
1390          * Add the data entry to a data block.
1391          * Extravalid is set to a freeblock found by lookup.
1392          */
1393         rval = xfs_dir2_node_addname_int(args,
1394                 state->extravalid ? &state->extrablk : NULL);
1395         if (rval) {
1396                 goto done;
1397         }
1398         blk = &state->path.blk[state->path.active - 1];
1399         ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1400         /*
1401          * Add the new leaf entry.
1402          */
1403         rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1404         if (rval == 0) {
1405                 /*
1406                  * It worked, fix the hash values up the btree.
1407                  */
1408                 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
1409                         xfs_da_fixhashpath(state, &state->path);
1410         } else {
1411                 /*
1412                  * It didn't work, we need to split the leaf block.
1413                  */
1414                 if (args->total == 0) {
1415                         ASSERT(rval == ENOSPC);
1416                         goto done;
1417                 }
1418                 /*
1419                  * Split the leaf block and insert the new entry.
1420                  */
1421                 rval = xfs_da_split(state);
1422         }
1423 done:
1424         xfs_da_state_free(state);
1425         return rval;
1426 }
1427
1428 /*
1429  * Add the data entry for a node-format directory name addition.
1430  * The leaf entry is added in xfs_dir2_leafn_add.
1431  * We may enter with a freespace block that the lookup found.
1432  */
1433 static int                                      /* error */
1434 xfs_dir2_node_addname_int(
1435         xfs_da_args_t           *args,          /* operation arguments */
1436         xfs_da_state_blk_t      *fblk)          /* optional freespace block */
1437 {
1438         xfs_dir2_data_t         *data;          /* data block structure */
1439         xfs_dir2_db_t           dbno;           /* data block number */
1440         xfs_dabuf_t             *dbp;           /* data block buffer */
1441         xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
1442         xfs_inode_t             *dp;            /* incore directory inode */
1443         xfs_dir2_data_unused_t  *dup;           /* data unused entry pointer */
1444         int                     error;          /* error return value */
1445         xfs_dir2_db_t           fbno;           /* freespace block number */
1446         xfs_dabuf_t             *fbp;           /* freespace buffer */
1447         int                     findex;         /* freespace entry index */
1448         xfs_dir2_free_t         *free=NULL;     /* freespace block structure */
1449         xfs_dir2_db_t           ifbno;          /* initial freespace block no */
1450         xfs_dir2_db_t           lastfbno=0;     /* highest freespace block no */
1451         int                     length;         /* length of the new entry */
1452         int                     logfree;        /* need to log free entry */
1453         xfs_mount_t             *mp;            /* filesystem mount point */
1454         int                     needlog;        /* need to log data header */
1455         int                     needscan;       /* need to rescan data frees */
1456         __be16                  *tagp;          /* data entry tag pointer */
1457         xfs_trans_t             *tp;            /* transaction pointer */
1458
1459         dp = args->dp;
1460         mp = dp->i_mount;
1461         tp = args->trans;
1462         length = xfs_dir2_data_entsize(args->namelen);
1463         /*
1464          * If we came in with a freespace block that means that lookup
1465          * found an entry with our hash value.  This is the freespace
1466          * block for that data entry.
1467          */
1468         if (fblk) {
1469                 fbp = fblk->bp;
1470                 /*
1471                  * Remember initial freespace block number.
1472                  */
1473                 ifbno = fblk->blkno;
1474                 free = fbp->data;
1475                 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1476                 findex = fblk->index;
1477                 /*
1478                  * This means the free entry showed that the data block had
1479                  * space for our entry, so we remembered it.
1480                  * Use that data block.
1481                  */
1482                 if (findex >= 0) {
1483                         ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1484                         ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1485                         ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1486                         dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1487                 }
1488                 /*
1489                  * The data block looked at didn't have enough room.
1490                  * We'll start at the beginning of the freespace entries.
1491                  */
1492                 else {
1493                         dbno = -1;
1494                         findex = 0;
1495                 }
1496         }
1497         /*
1498          * Didn't come in with a freespace block, so don't have a data block.
1499          */
1500         else {
1501                 ifbno = dbno = -1;
1502                 fbp = NULL;
1503                 findex = 0;
1504         }
1505         /*
1506          * If we don't have a data block yet, we're going to scan the
1507          * freespace blocks looking for one.  Figure out what the
1508          * highest freespace block number is.
1509          */
1510         if (dbno == -1) {
1511                 xfs_fileoff_t   fo;             /* freespace block number */
1512
1513                 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1514                         return error;
1515                 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
1516                 fbno = ifbno;
1517         }
1518         /*
1519          * While we haven't identified a data block, search the freeblock
1520          * data for a good data block.  If we find a null freeblock entry,
1521          * indicating a hole in the data blocks, remember that.
1522          */
1523         while (dbno == -1) {
1524                 /*
1525                  * If we don't have a freeblock in hand, get the next one.
1526                  */
1527                 if (fbp == NULL) {
1528                         /*
1529                          * Happens the first time through unless lookup gave
1530                          * us a freespace block to start with.
1531                          */
1532                         if (++fbno == 0)
1533                                 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1534                         /*
1535                          * If it's ifbno we already looked at it.
1536                          */
1537                         if (fbno == ifbno)
1538                                 fbno++;
1539                         /*
1540                          * If it's off the end we're done.
1541                          */
1542                         if (fbno >= lastfbno)
1543                                 break;
1544                         /*
1545                          * Read the block.  There can be holes in the
1546                          * freespace blocks, so this might not succeed.
1547                          * This should be really rare, so there's no reason
1548                          * to avoid it.
1549                          */
1550                         if ((error = xfs_da_read_buf(tp, dp,
1551                                         xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1552                                         XFS_DATA_FORK))) {
1553                                 return error;
1554                         }
1555                         if (unlikely(fbp == NULL)) {
1556                                 continue;
1557                         }
1558                         free = fbp->data;
1559                         ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1560                         findex = 0;
1561                 }
1562                 /*
1563                  * Look at the current free entry.  Is it good enough?
1564                  */
1565                 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1566                     be16_to_cpu(free->bests[findex]) >= length)
1567                         dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1568                 else {
1569                         /*
1570                          * Are we done with the freeblock?
1571                          */
1572                         if (++findex == be32_to_cpu(free->hdr.nvalid)) {
1573                                 /*
1574                                  * Drop the block.
1575                                  */
1576                                 xfs_da_brelse(tp, fbp);
1577                                 fbp = NULL;
1578                                 if (fblk && fblk->bp)
1579                                         fblk->bp = NULL;
1580                         }
1581                 }
1582         }
1583         /*
1584          * If we don't have a data block, we need to allocate one and make
1585          * the freespace entries refer to it.
1586          */
1587         if (unlikely(dbno == -1)) {
1588                 /*
1589                  * Not allowed to allocate, return failure.
1590                  */
1591                 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1592                                                         args->total == 0) {
1593                         /*
1594                          * Drop the freespace buffer unless it came from our
1595                          * caller.
1596                          */
1597                         if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1598                                 xfs_da_buf_done(fbp);
1599                         return XFS_ERROR(ENOSPC);
1600                 }
1601                 /*
1602                  * Allocate and initialize the new data block.
1603                  */
1604                 if (unlikely((error = xfs_dir2_grow_inode(args,
1605                                                          XFS_DIR2_DATA_SPACE,
1606                                                          &dbno)) ||
1607                     (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1608                         /*
1609                          * Drop the freespace buffer unless it came from our
1610                          * caller.
1611                          */
1612                         if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1613                                 xfs_da_buf_done(fbp);
1614                         return error;
1615                 }
1616                 /*
1617                  * If (somehow) we have a freespace block, get rid of it.
1618                  */
1619                 if (fbp)
1620                         xfs_da_brelse(tp, fbp);
1621                 if (fblk && fblk->bp)
1622                         fblk->bp = NULL;
1623
1624                 /*
1625                  * Get the freespace block corresponding to the data block
1626                  * that was just allocated.
1627                  */
1628                 fbno = xfs_dir2_db_to_fdb(mp, dbno);
1629                 if (unlikely(error = xfs_da_read_buf(tp, dp,
1630                                 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
1631                                 XFS_DATA_FORK))) {
1632                         xfs_da_buf_done(dbp);
1633                         return error;
1634                 }
1635                 /*
1636                  * If there wasn't a freespace block, the read will
1637                  * return a NULL fbp.  Allocate and initialize a new one.
1638                  */
1639                 if( fbp == NULL ) {
1640                         if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1641                                                         &fbno))) {
1642                                 return error;
1643                         }
1644
1645                         if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
1646                                 cmn_err(CE_ALERT,
1647                                         "xfs_dir2_node_addname_int: dir ino "
1648                                         "%llu needed freesp block %lld for\n"
1649                                         "  data block %lld, got %lld\n"
1650                                         "  ifbno %llu lastfbno %d\n",
1651                                         (unsigned long long)dp->i_ino,
1652                                         (long long)xfs_dir2_db_to_fdb(mp, dbno),
1653                                         (long long)dbno, (long long)fbno,
1654                                         (unsigned long long)ifbno, lastfbno);
1655                                 if (fblk) {
1656                                         cmn_err(CE_ALERT,
1657                                                 " fblk 0x%p blkno %llu "
1658                                                 "index %d magic 0x%x\n",
1659                                                 fblk,
1660                                                 (unsigned long long)fblk->blkno,
1661                                                 fblk->index,
1662                                                 fblk->magic);
1663                                 } else {
1664                                         cmn_err(CE_ALERT,
1665                                                 " ... fblk is NULL\n");
1666                                 }
1667                                 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1668                                                  XFS_ERRLEVEL_LOW, mp);
1669                                 return XFS_ERROR(EFSCORRUPTED);
1670                         }
1671
1672                         /*
1673                          * Get a buffer for the new block.
1674                          */
1675                         if ((error = xfs_da_get_buf(tp, dp,
1676                                                    xfs_dir2_db_to_da(mp, fbno),
1677                                                    -1, &fbp, XFS_DATA_FORK))) {
1678                                 return error;
1679                         }
1680                         ASSERT(fbp != NULL);
1681
1682                         /*
1683                          * Initialize the new block to be empty, and remember
1684                          * its first slot as our empty slot.
1685                          */
1686                         free = fbp->data;
1687                         free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1688                         free->hdr.firstdb = cpu_to_be32(
1689                                 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1690                                 XFS_DIR2_MAX_FREE_BESTS(mp));
1691                         free->hdr.nvalid = 0;
1692                         free->hdr.nused = 0;
1693                 } else {
1694                         free = fbp->data;
1695                         ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1696                 }
1697
1698                 /*
1699                  * Set the freespace block index from the data block number.
1700                  */
1701                 findex = xfs_dir2_db_to_fdindex(mp, dbno);
1702                 /*
1703                  * If it's after the end of the current entries in the
1704                  * freespace block, extend that table.
1705                  */
1706                 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
1707                         ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
1708                         free->hdr.nvalid = cpu_to_be32(findex + 1);
1709                         /*
1710                          * Tag new entry so nused will go up.
1711                          */
1712                         free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1713                 }
1714                 /*
1715                  * If this entry was for an empty data block
1716                  * (this should always be true) then update the header.
1717                  */
1718                 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
1719                         be32_add_cpu(&free->hdr.nused, 1);
1720                         xfs_dir2_free_log_header(tp, fbp);
1721                 }
1722                 /*
1723                  * Update the real value in the table.
1724                  * We haven't allocated the data entry yet so this will
1725                  * change again.
1726                  */
1727                 data = dbp->data;
1728                 free->bests[findex] = data->hdr.bestfree[0].length;
1729                 logfree = 1;
1730         }
1731         /*
1732          * We had a data block so we don't have to make a new one.
1733          */
1734         else {
1735                 /*
1736                  * If just checking, we succeeded.
1737                  */
1738                 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1739                         if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1740                                 xfs_da_buf_done(fbp);
1741                         return 0;
1742                 }
1743                 /*
1744                  * Read the data block in.
1745                  */
1746                 if (unlikely(
1747                     error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1748                                 -1, &dbp, XFS_DATA_FORK))) {
1749                         if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1750                                 xfs_da_buf_done(fbp);
1751                         return error;
1752                 }
1753                 data = dbp->data;
1754                 logfree = 0;
1755         }
1756         ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
1757         /*
1758          * Point to the existing unused space.
1759          */
1760         dup = (xfs_dir2_data_unused_t *)
1761               ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
1762         needscan = needlog = 0;
1763         /*
1764          * Mark the first part of the unused space, inuse for us.
1765          */
1766         xfs_dir2_data_use_free(tp, dbp, dup,
1767                 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1768                 &needlog, &needscan);
1769         /*
1770          * Fill in the new entry and log it.
1771          */
1772         dep = (xfs_dir2_data_entry_t *)dup;
1773         dep->inumber = cpu_to_be64(args->inumber);
1774         dep->namelen = args->namelen;
1775         memcpy(dep->name, args->name, dep->namelen);
1776         tagp = xfs_dir2_data_entry_tag_p(dep);
1777         *tagp = cpu_to_be16((char *)dep - (char *)data);
1778         xfs_dir2_data_log_entry(tp, dbp, dep);
1779         /*
1780          * Rescan the block for bestfree if needed.
1781          */
1782         if (needscan)
1783                 xfs_dir2_data_freescan(mp, data, &needlog);
1784         /*
1785          * Log the data block header if needed.
1786          */
1787         if (needlog)
1788                 xfs_dir2_data_log_header(tp, dbp);
1789         /*
1790          * If the freespace entry is now wrong, update it.
1791          */
1792         if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
1793                 free->bests[findex] = data->hdr.bestfree[0].length;
1794                 logfree = 1;
1795         }
1796         /*
1797          * Log the freespace entry if needed.
1798          */
1799         if (logfree)
1800                 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1801         /*
1802          * If the caller didn't hand us the freespace block, drop it.
1803          */
1804         if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1805                 xfs_da_buf_done(fbp);
1806         /*
1807          * Return the data block and offset in args, then drop the data block.
1808          */
1809         args->blkno = (xfs_dablk_t)dbno;
1810         args->index = be16_to_cpu(*tagp);
1811         xfs_da_buf_done(dbp);
1812         return 0;
1813 }
1814
1815 /*
1816  * Lookup an entry in a node-format directory.
1817  * All the real work happens in xfs_da_node_lookup_int.
1818  * The only real output is the inode number of the entry.
1819  */
1820 int                                             /* error */
1821 xfs_dir2_node_lookup(
1822         xfs_da_args_t   *args)                  /* operation arguments */
1823 {
1824         int             error;                  /* error return value */
1825         int             i;                      /* btree level */
1826         int             rval;                   /* operation return value */
1827         xfs_da_state_t  *state;                 /* btree cursor */
1828
1829         trace_xfs_dir2_node_lookup(args);
1830
1831         /*
1832          * Allocate and initialize the btree cursor.
1833          */
1834         state = xfs_da_state_alloc();
1835         state->args = args;
1836         state->mp = args->dp->i_mount;
1837         state->blocksize = state->mp->m_dirblksize;
1838         state->node_ents = state->mp->m_dir_node_ents;
1839         /*
1840          * Fill in the path to the entry in the cursor.
1841          */
1842         error = xfs_da_node_lookup_int(state, &rval);
1843         if (error)
1844                 rval = error;
1845         else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1846                 /* If a CI match, dup the actual name and return EEXIST */
1847                 xfs_dir2_data_entry_t   *dep;
1848
1849                 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1850                                                 data + state->extrablk.index);
1851                 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1852         }
1853         /*
1854          * Release the btree blocks and leaf block.
1855          */
1856         for (i = 0; i < state->path.active; i++) {
1857                 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1858                 state->path.blk[i].bp = NULL;
1859         }
1860         /*
1861          * Release the data block if we have it.
1862          */
1863         if (state->extravalid && state->extrablk.bp) {
1864                 xfs_da_brelse(args->trans, state->extrablk.bp);
1865                 state->extrablk.bp = NULL;
1866         }
1867         xfs_da_state_free(state);
1868         return rval;
1869 }
1870
1871 /*
1872  * Remove an entry from a node-format directory.
1873  */
1874 int                                             /* error */
1875 xfs_dir2_node_removename(
1876         xfs_da_args_t           *args)          /* operation arguments */
1877 {
1878         xfs_da_state_blk_t      *blk;           /* leaf block */
1879         int                     error;          /* error return value */
1880         int                     rval;           /* operation return value */
1881         xfs_da_state_t          *state;         /* btree cursor */
1882
1883         trace_xfs_dir2_node_removename(args);
1884
1885         /*
1886          * Allocate and initialize the btree cursor.
1887          */
1888         state = xfs_da_state_alloc();
1889         state->args = args;
1890         state->mp = args->dp->i_mount;
1891         state->blocksize = state->mp->m_dirblksize;
1892         state->node_ents = state->mp->m_dir_node_ents;
1893         /*
1894          * Look up the entry we're deleting, set up the cursor.
1895          */
1896         error = xfs_da_node_lookup_int(state, &rval);
1897         if (error)
1898                 rval = error;
1899         /*
1900          * Didn't find it, upper layer screwed up.
1901          */
1902         if (rval != EEXIST) {
1903                 xfs_da_state_free(state);
1904                 return rval;
1905         }
1906         blk = &state->path.blk[state->path.active - 1];
1907         ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1908         ASSERT(state->extravalid);
1909         /*
1910          * Remove the leaf and data entries.
1911          * Extrablk refers to the data block.
1912          */
1913         error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1914                 &state->extrablk, &rval);
1915         if (error)
1916                 return error;
1917         /*
1918          * Fix the hash values up the btree.
1919          */
1920         xfs_da_fixhashpath(state, &state->path);
1921         /*
1922          * If we need to join leaf blocks, do it.
1923          */
1924         if (rval && state->path.active > 1)
1925                 error = xfs_da_join(state);
1926         /*
1927          * If no errors so far, try conversion to leaf format.
1928          */
1929         if (!error)
1930                 error = xfs_dir2_node_to_leaf(state);
1931         xfs_da_state_free(state);
1932         return error;
1933 }
1934
1935 /*
1936  * Replace an entry's inode number in a node-format directory.
1937  */
1938 int                                             /* error */
1939 xfs_dir2_node_replace(
1940         xfs_da_args_t           *args)          /* operation arguments */
1941 {
1942         xfs_da_state_blk_t      *blk;           /* leaf block */
1943         xfs_dir2_data_t         *data;          /* data block structure */
1944         xfs_dir2_data_entry_t   *dep;           /* data entry changed */
1945         int                     error;          /* error return value */
1946         int                     i;              /* btree level */
1947         xfs_ino_t               inum;           /* new inode number */
1948         xfs_dir2_leaf_t         *leaf;          /* leaf structure */
1949         xfs_dir2_leaf_entry_t   *lep;           /* leaf entry being changed */
1950         int                     rval;           /* internal return value */
1951         xfs_da_state_t          *state;         /* btree cursor */
1952
1953         trace_xfs_dir2_node_replace(args);
1954
1955         /*
1956          * Allocate and initialize the btree cursor.
1957          */
1958         state = xfs_da_state_alloc();
1959         state->args = args;
1960         state->mp = args->dp->i_mount;
1961         state->blocksize = state->mp->m_dirblksize;
1962         state->node_ents = state->mp->m_dir_node_ents;
1963         inum = args->inumber;
1964         /*
1965          * Lookup the entry to change in the btree.
1966          */
1967         error = xfs_da_node_lookup_int(state, &rval);
1968         if (error) {
1969                 rval = error;
1970         }
1971         /*
1972          * It should be found, since the vnodeops layer has looked it up
1973          * and locked it.  But paranoia is good.
1974          */
1975         if (rval == EEXIST) {
1976                 /*
1977                  * Find the leaf entry.
1978                  */
1979                 blk = &state->path.blk[state->path.active - 1];
1980                 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1981                 leaf = blk->bp->data;
1982                 lep = &leaf->ents[blk->index];
1983                 ASSERT(state->extravalid);
1984                 /*
1985                  * Point to the data entry.
1986                  */
1987                 data = state->extrablk.bp->data;
1988                 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1989                 dep = (xfs_dir2_data_entry_t *)
1990                       ((char *)data +
1991                        xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
1992                 ASSERT(inum != be64_to_cpu(dep->inumber));
1993                 /*
1994                  * Fill in the new inode number and log the entry.
1995                  */
1996                 dep->inumber = cpu_to_be64(inum);
1997                 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1998                 rval = 0;
1999         }
2000         /*
2001          * Didn't find it, and we're holding a data block.  Drop it.
2002          */
2003         else if (state->extravalid) {
2004                 xfs_da_brelse(args->trans, state->extrablk.bp);
2005                 state->extrablk.bp = NULL;
2006         }
2007         /*
2008          * Release all the buffers in the cursor.
2009          */
2010         for (i = 0; i < state->path.active; i++) {
2011                 xfs_da_brelse(args->trans, state->path.blk[i].bp);
2012                 state->path.blk[i].bp = NULL;
2013         }
2014         xfs_da_state_free(state);
2015         return rval;
2016 }
2017
2018 /*
2019  * Trim off a trailing empty freespace block.
2020  * Return (in rvalp) 1 if we did it, 0 if not.
2021  */
2022 int                                             /* error */
2023 xfs_dir2_node_trim_free(
2024         xfs_da_args_t           *args,          /* operation arguments */
2025         xfs_fileoff_t           fo,             /* free block number */
2026         int                     *rvalp)         /* out: did something */
2027 {
2028         xfs_dabuf_t             *bp;            /* freespace buffer */
2029         xfs_inode_t             *dp;            /* incore directory inode */
2030         int                     error;          /* error return code */
2031         xfs_dir2_free_t         *free;          /* freespace structure */
2032         xfs_mount_t             *mp;            /* filesystem mount point */
2033         xfs_trans_t             *tp;            /* transaction pointer */
2034
2035         dp = args->dp;
2036         mp = dp->i_mount;
2037         tp = args->trans;
2038         /*
2039          * Read the freespace block.
2040          */
2041         if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2042                         XFS_DATA_FORK))) {
2043                 return error;
2044         }
2045
2046         /*
2047          * There can be holes in freespace.  If fo is a hole, there's
2048          * nothing to do.
2049          */
2050         if (bp == NULL) {
2051                 return 0;
2052         }
2053         free = bp->data;
2054         ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
2055         /*
2056          * If there are used entries, there's nothing to do.
2057          */
2058         if (be32_to_cpu(free->hdr.nused) > 0) {
2059                 xfs_da_brelse(tp, bp);
2060                 *rvalp = 0;
2061                 return 0;
2062         }
2063         /*
2064          * Blow the block away.
2065          */
2066         if ((error =
2067             xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
2068                     bp))) {
2069                 /*
2070                  * Can't fail with ENOSPC since that only happens with no
2071                  * space reservation, when breaking up an extent into two
2072                  * pieces.  This is the last block of an extent.
2073                  */
2074                 ASSERT(error != ENOSPC);
2075                 xfs_da_brelse(tp, bp);
2076                 return error;
2077         }
2078         /*
2079          * Return that we succeeded.
2080          */
2081         *rvalp = 1;
2082         return 0;
2083 }