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