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