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