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