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