Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6-block.git] / fs / xfs / xfs_dir2_leaf.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4
LT
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
a844f451 30#include "xfs_da_btree.h"
1da177e4
LT
31#include "xfs_bmap_btree.h"
32#include "xfs_attr_sf.h"
1da177e4
LT
33#include "xfs_dir2_sf.h"
34#include "xfs_dinode.h"
35#include "xfs_inode.h"
36#include "xfs_bmap.h"
1da177e4
LT
37#include "xfs_dir2_data.h"
38#include "xfs_dir2_leaf.h"
39#include "xfs_dir2_block.h"
40#include "xfs_dir2_node.h"
41#include "xfs_dir2_trace.h"
42#include "xfs_error.h"
1da177e4
LT
43
44/*
45 * Local function declarations.
46 */
47#ifdef DEBUG
48static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49#else
50#define xfs_dir2_leaf_check(dp, bp)
51#endif
52static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
53 int *indexp, xfs_dabuf_t **dbpp);
ba0f32d4
CH
54static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
55 int first, int last);
6add2c42 56static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
ba0f32d4 57
1da177e4
LT
58
59/*
60 * Convert a block form directory to a leaf form directory.
61 */
62int /* error */
63xfs_dir2_block_to_leaf(
64 xfs_da_args_t *args, /* operation arguments */
65 xfs_dabuf_t *dbp) /* input block's buffer */
66{
68b3a102 67 __be16 *bestsp; /* leaf's bestsp entries */
1da177e4
LT
68 xfs_dablk_t blkno; /* leaf block's bno */
69 xfs_dir2_block_t *block; /* block structure */
70 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
71 xfs_dir2_block_tail_t *btp; /* block's tail */
72 xfs_inode_t *dp; /* incore directory inode */
73 int error; /* error return code */
74 xfs_dabuf_t *lbp; /* leaf block's buffer */
75 xfs_dir2_db_t ldb; /* leaf block's bno */
76 xfs_dir2_leaf_t *leaf; /* leaf structure */
77 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
78 xfs_mount_t *mp; /* filesystem mount point */
79 int needlog; /* need to log block header */
80 int needscan; /* need to rescan bestfree */
81 xfs_trans_t *tp; /* transaction pointer */
82
83 xfs_dir2_trace_args_b("block_to_leaf", args, dbp);
84 dp = args->dp;
85 mp = dp->i_mount;
86 tp = args->trans;
87 /*
88 * Add the leaf block to the inode.
89 * This interface will only put blocks in the leaf/node range.
90 * Since that's empty now, we'll get the root (block 0 in range).
91 */
92 if ((error = xfs_da_grow_inode(args, &blkno))) {
93 return error;
94 }
bbaaf538 95 ldb = xfs_dir2_da_to_db(mp, blkno);
1da177e4
LT
96 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
97 /*
98 * Initialize the leaf block, get a buffer for it.
99 */
100 if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
101 return error;
102 }
103 ASSERT(lbp != NULL);
104 leaf = lbp->data;
105 block = dbp->data;
106 xfs_dir2_data_check(dp, dbp);
bbaaf538
CH
107 btp = xfs_dir2_block_tail_p(mp, block);
108 blp = xfs_dir2_block_leaf_p(btp);
1da177e4
LT
109 /*
110 * Set the counts in the leaf header.
111 */
a818e5de
NS
112 leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
113 leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
1da177e4
LT
114 /*
115 * Could compact these but I think we always do the conversion
116 * after squeezing out stale entries.
117 */
e922fffa 118 memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
a818e5de 119 xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
1da177e4
LT
120 needscan = 0;
121 needlog = 1;
122 /*
123 * Make the space formerly occupied by the leaf entries and block
124 * tail be free.
125 */
126 xfs_dir2_data_make_free(tp, dbp,
127 (xfs_dir2_data_aoff_t)((char *)blp - (char *)block),
128 (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize -
129 (char *)blp),
130 &needlog, &needscan);
131 /*
132 * Fix up the block header, make it a data block.
133 */
70e73f59 134 block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
1da177e4 135 if (needscan)
ef497f8a 136 xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog);
1da177e4
LT
137 /*
138 * Set up leaf tail and bests table.
139 */
bbaaf538 140 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
afbcb3f9 141 ltp->bestcount = cpu_to_be32(1);
bbaaf538 142 bestsp = xfs_dir2_leaf_bests_p(ltp);
70e73f59 143 bestsp[0] = block->hdr.bestfree[0].length;
1da177e4
LT
144 /*
145 * Log the data header and leaf bests table.
146 */
147 if (needlog)
148 xfs_dir2_data_log_header(tp, dbp);
149 xfs_dir2_leaf_check(dp, lbp);
150 xfs_dir2_data_check(dp, dbp);
151 xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
152 xfs_da_buf_done(lbp);
153 return 0;
154}
155
156/*
157 * Add an entry to a leaf form directory.
158 */
159int /* error */
160xfs_dir2_leaf_addname(
161 xfs_da_args_t *args) /* operation arguments */
162{
68b3a102 163 __be16 *bestsp; /* freespace table in leaf */
1da177e4
LT
164 int compact; /* need to compact leaves */
165 xfs_dir2_data_t *data; /* data block structure */
166 xfs_dabuf_t *dbp; /* data block buffer */
167 xfs_dir2_data_entry_t *dep; /* data block entry */
168 xfs_inode_t *dp; /* incore directory inode */
169 xfs_dir2_data_unused_t *dup; /* data unused entry */
170 int error; /* error return value */
171 int grown; /* allocated new data block */
172 int highstale; /* index of next stale leaf */
173 int i; /* temporary, index */
174 int index; /* leaf table position */
175 xfs_dabuf_t *lbp; /* leaf's buffer */
176 xfs_dir2_leaf_t *leaf; /* leaf structure */
177 int length; /* length of new entry */
178 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
179 int lfloglow; /* low leaf logging index */
180 int lfloghigh; /* high leaf logging index */
181 int lowstale; /* index of prev stale leaf */
182 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
183 xfs_mount_t *mp; /* filesystem mount point */
184 int needbytes; /* leaf block bytes needed */
185 int needlog; /* need to log data header */
186 int needscan; /* need to rescan data free */
3d693c6e 187 __be16 *tagp; /* end of data entry */
1da177e4
LT
188 xfs_trans_t *tp; /* transaction pointer */
189 xfs_dir2_db_t use_block; /* data block number */
190
191 xfs_dir2_trace_args("leaf_addname", args);
192 dp = args->dp;
193 tp = args->trans;
194 mp = dp->i_mount;
195 /*
196 * Read the leaf block.
197 */
198 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
199 XFS_DATA_FORK);
200 if (error) {
201 return error;
202 }
203 ASSERT(lbp != NULL);
204 /*
205 * Look up the entry by hash value and name.
206 * We know it's not there, our caller has already done a lookup.
207 * So the index is of the entry to insert in front of.
208 * But if there are dup hash values the index is of the first of those.
209 */
210 index = xfs_dir2_leaf_search_hash(args, lbp);
211 leaf = lbp->data;
bbaaf538
CH
212 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
213 bestsp = xfs_dir2_leaf_bests_p(ltp);
214 length = xfs_dir2_data_entsize(args->namelen);
1da177e4
LT
215 /*
216 * See if there are any entries with the same hash value
217 * and space in their block for the new entry.
218 * This is good because it puts multiple same-hash value entries
219 * in a data block, improving the lookup of those entries.
220 */
221 for (use_block = -1, lep = &leaf->ents[index];
3c1f9c15 222 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
1da177e4 223 index++, lep++) {
3c1f9c15 224 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1da177e4 225 continue;
bbaaf538 226 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
afbcb3f9 227 ASSERT(i < be32_to_cpu(ltp->bestcount));
68b3a102
NS
228 ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF);
229 if (be16_to_cpu(bestsp[i]) >= length) {
1da177e4
LT
230 use_block = i;
231 break;
232 }
233 }
234 /*
235 * Didn't find a block yet, linear search all the data blocks.
236 */
237 if (use_block == -1) {
afbcb3f9 238 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
1da177e4
LT
239 /*
240 * Remember a block we see that's missing.
241 */
68b3a102 242 if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1)
1da177e4 243 use_block = i;
68b3a102 244 else if (be16_to_cpu(bestsp[i]) >= length) {
1da177e4
LT
245 use_block = i;
246 break;
247 }
248 }
249 }
250 /*
251 * How many bytes do we need in the leaf block?
252 */
253 needbytes =
254 (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) +
255 (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0]));
256 /*
257 * Now kill use_block if it refers to a missing block, so we
258 * can use it as an indication of allocation needed.
259 */
68b3a102 260 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
1da177e4
LT
261 use_block = -1;
262 /*
263 * If we don't have enough free bytes but we can make enough
264 * by compacting out stale entries, we'll do that.
265 */
a818e5de
NS
266 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] < needbytes &&
267 be16_to_cpu(leaf->hdr.stale) > 1) {
1da177e4
LT
268 compact = 1;
269 }
270 /*
271 * Otherwise if we don't have enough free bytes we need to
272 * convert to node form.
273 */
a818e5de 274 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
1da177e4
LT
275 needbytes) {
276 /*
277 * Just checking or no space reservation, give up.
278 */
279 if (args->justcheck || args->total == 0) {
280 xfs_da_brelse(tp, lbp);
281 return XFS_ERROR(ENOSPC);
282 }
283 /*
284 * Convert to node form.
285 */
286 error = xfs_dir2_leaf_to_node(args, lbp);
287 xfs_da_buf_done(lbp);
288 if (error)
289 return error;
290 /*
291 * Then add the new entry.
292 */
293 return xfs_dir2_node_addname(args);
294 }
295 /*
296 * Otherwise it will fit without compaction.
297 */
298 else
299 compact = 0;
300 /*
301 * If just checking, then it will fit unless we needed to allocate
302 * a new data block.
303 */
304 if (args->justcheck) {
305 xfs_da_brelse(tp, lbp);
306 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
307 }
308 /*
309 * If no allocations are allowed, return now before we've
310 * changed anything.
311 */
312 if (args->total == 0 && use_block == -1) {
313 xfs_da_brelse(tp, lbp);
314 return XFS_ERROR(ENOSPC);
315 }
316 /*
317 * Need to compact the leaf entries, removing stale ones.
318 * Leave one stale entry behind - the one closest to our
319 * insertion index - and we'll shift that one to our insertion
320 * point later.
321 */
322 if (compact) {
323 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
324 &lfloglow, &lfloghigh);
325 }
326 /*
327 * There are stale entries, so we'll need log-low and log-high
328 * impossibly bad values later.
329 */
a818e5de
NS
330 else if (be16_to_cpu(leaf->hdr.stale)) {
331 lfloglow = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
332 lfloghigh = -1;
333 }
334 /*
335 * If there was no data block space found, we need to allocate
336 * a new one.
337 */
338 if (use_block == -1) {
339 /*
340 * Add the new data block.
341 */
342 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
343 &use_block))) {
344 xfs_da_brelse(tp, lbp);
345 return error;
346 }
347 /*
348 * Initialize the block.
349 */
350 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
351 xfs_da_brelse(tp, lbp);
352 return error;
353 }
354 /*
355 * If we're adding a new data block on the end we need to
356 * extend the bests table. Copy it up one entry.
357 */
afbcb3f9 358 if (use_block >= be32_to_cpu(ltp->bestcount)) {
1da177e4
LT
359 bestsp--;
360 memmove(&bestsp[0], &bestsp[1],
afbcb3f9
NS
361 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
362 be32_add(&ltp->bestcount, 1);
1da177e4 363 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 364 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
365 }
366 /*
367 * If we're filling in a previously empty block just log it.
368 */
369 else
370 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
371 data = dbp->data;
70e73f59 372 bestsp[use_block] = data->hdr.bestfree[0].length;
1da177e4
LT
373 grown = 1;
374 }
375 /*
376 * Already had space in some data block.
377 * Just read that one in.
378 */
379 else {
380 if ((error =
bbaaf538 381 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
1da177e4
LT
382 -1, &dbp, XFS_DATA_FORK))) {
383 xfs_da_brelse(tp, lbp);
384 return error;
385 }
386 data = dbp->data;
387 grown = 0;
388 }
389 xfs_dir2_data_check(dp, dbp);
390 /*
391 * Point to the biggest freespace in our data block.
392 */
393 dup = (xfs_dir2_data_unused_t *)
70e73f59 394 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
ad354eb3 395 ASSERT(be16_to_cpu(dup->length) >= length);
1da177e4
LT
396 needscan = needlog = 0;
397 /*
398 * Mark the initial part of our freespace in use for the new entry.
399 */
400 xfs_dir2_data_use_free(tp, dbp, dup,
401 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
402 &needlog, &needscan);
403 /*
404 * Initialize our new entry (at last).
405 */
406 dep = (xfs_dir2_data_entry_t *)dup;
ff9901c1 407 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
408 dep->namelen = args->namelen;
409 memcpy(dep->name, args->name, dep->namelen);
bbaaf538 410 tagp = xfs_dir2_data_entry_tag_p(dep);
3d693c6e 411 *tagp = cpu_to_be16((char *)dep - (char *)data);
1da177e4
LT
412 /*
413 * Need to scan fix up the bestfree table.
414 */
415 if (needscan)
ef497f8a 416 xfs_dir2_data_freescan(mp, data, &needlog);
1da177e4
LT
417 /*
418 * Need to log the data block's header.
419 */
420 if (needlog)
421 xfs_dir2_data_log_header(tp, dbp);
422 xfs_dir2_data_log_entry(tp, dbp, dep);
423 /*
424 * If the bests table needs to be changed, do it.
425 * Log the change unless we've already done that.
426 */
68b3a102 427 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
70e73f59 428 bestsp[use_block] = data->hdr.bestfree[0].length;
1da177e4
LT
429 if (!grown)
430 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
431 }
432 /*
433 * Now we need to make room to insert the leaf entry.
434 * If there are no stale entries, we just insert a hole at index.
435 */
436 if (!leaf->hdr.stale) {
437 /*
438 * lep is still good as the index leaf entry.
439 */
a818e5de 440 if (index < be16_to_cpu(leaf->hdr.count))
1da177e4 441 memmove(lep + 1, lep,
a818e5de 442 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
1da177e4
LT
443 /*
444 * Record low and high logging indices for the leaf.
445 */
446 lfloglow = index;
a818e5de
NS
447 lfloghigh = be16_to_cpu(leaf->hdr.count);
448 be16_add(&leaf->hdr.count, 1);
1da177e4
LT
449 }
450 /*
451 * There are stale entries.
452 * We will use one of them for the new entry.
453 * It's probably not at the right location, so we'll have to
454 * shift some up or down first.
455 */
456 else {
457 /*
458 * If we didn't compact before, we need to find the nearest
459 * stale entries before and after our insertion point.
460 */
461 if (compact == 0) {
462 /*
463 * Find the first stale entry before the insertion
464 * point, if any.
465 */
466 for (lowstale = index - 1;
467 lowstale >= 0 &&
3c1f9c15 468 be32_to_cpu(leaf->ents[lowstale].address) !=
1da177e4
LT
469 XFS_DIR2_NULL_DATAPTR;
470 lowstale--)
471 continue;
472 /*
473 * Find the next stale entry at or after the insertion
474 * point, if any. Stop if we go so far that the
475 * lowstale entry would be better.
476 */
477 for (highstale = index;
a818e5de 478 highstale < be16_to_cpu(leaf->hdr.count) &&
3c1f9c15 479 be32_to_cpu(leaf->ents[highstale].address) !=
1da177e4
LT
480 XFS_DIR2_NULL_DATAPTR &&
481 (lowstale < 0 ||
482 index - lowstale - 1 >= highstale - index);
483 highstale++)
484 continue;
485 }
486 /*
487 * If the low one is better, use it.
488 */
489 if (lowstale >= 0 &&
a818e5de 490 (highstale == be16_to_cpu(leaf->hdr.count) ||
1da177e4
LT
491 index - lowstale - 1 < highstale - index)) {
492 ASSERT(index - lowstale - 1 >= 0);
3c1f9c15 493 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
1da177e4
LT
494 XFS_DIR2_NULL_DATAPTR);
495 /*
496 * Copy entries up to cover the stale entry
497 * and make room for the new entry.
498 */
499 if (index - lowstale - 1 > 0)
500 memmove(&leaf->ents[lowstale],
501 &leaf->ents[lowstale + 1],
502 (index - lowstale - 1) * sizeof(*lep));
503 lep = &leaf->ents[index - 1];
504 lfloglow = MIN(lowstale, lfloglow);
505 lfloghigh = MAX(index - 1, lfloghigh);
506 }
507 /*
508 * The high one is better, so use that one.
509 */
510 else {
511 ASSERT(highstale - index >= 0);
3c1f9c15 512 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
1da177e4
LT
513 XFS_DIR2_NULL_DATAPTR);
514 /*
c41564b5 515 * Copy entries down to cover the stale entry
1da177e4
LT
516 * and make room for the new entry.
517 */
518 if (highstale - index > 0)
519 memmove(&leaf->ents[index + 1],
520 &leaf->ents[index],
521 (highstale - index) * sizeof(*lep));
522 lep = &leaf->ents[index];
523 lfloglow = MIN(index, lfloglow);
524 lfloghigh = MAX(highstale, lfloghigh);
525 }
a818e5de 526 be16_add(&leaf->hdr.stale, -1);
1da177e4
LT
527 }
528 /*
529 * Fill in the new leaf entry.
530 */
3c1f9c15 531 lep->hashval = cpu_to_be32(args->hashval);
bbaaf538 532 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
3d693c6e 533 be16_to_cpu(*tagp)));
1da177e4
LT
534 /*
535 * Log the leaf fields and give up the buffers.
536 */
537 xfs_dir2_leaf_log_header(tp, lbp);
538 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
539 xfs_dir2_leaf_check(dp, lbp);
540 xfs_da_buf_done(lbp);
541 xfs_dir2_data_check(dp, dbp);
542 xfs_da_buf_done(dbp);
543 return 0;
544}
545
546#ifdef DEBUG
547/*
548 * Check the internal consistency of a leaf1 block.
549 * Pop an assert if something is wrong.
550 */
551void
552xfs_dir2_leaf_check(
553 xfs_inode_t *dp, /* incore directory inode */
554 xfs_dabuf_t *bp) /* leaf's buffer */
555{
556 int i; /* leaf index */
557 xfs_dir2_leaf_t *leaf; /* leaf structure */
558 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
559 xfs_mount_t *mp; /* filesystem mount point */
560 int stale; /* count of stale leaves */
561
562 leaf = bp->data;
563 mp = dp->i_mount;
89da0544 564 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1da177e4
LT
565 /*
566 * This value is not restrictive enough.
567 * Should factor in the size of the bests table as well.
568 * We can deduce a value for that from di_size.
569 */
bbaaf538
CH
570 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
571 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
572 /*
573 * Leaves and bests don't overlap.
574 */
a818e5de 575 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
bbaaf538 576 (char *)xfs_dir2_leaf_bests_p(ltp));
1da177e4
LT
577 /*
578 * Check hash value order, count stale entries.
579 */
a818e5de
NS
580 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
581 if (i + 1 < be16_to_cpu(leaf->hdr.count))
3c1f9c15
NS
582 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
583 be32_to_cpu(leaf->ents[i + 1].hashval));
584 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
585 stale++;
586 }
a818e5de 587 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
1da177e4
LT
588}
589#endif /* DEBUG */
590
591/*
592 * Compact out any stale entries in the leaf.
593 * Log the header and changed leaf entries, if any.
594 */
595void
596xfs_dir2_leaf_compact(
597 xfs_da_args_t *args, /* operation arguments */
598 xfs_dabuf_t *bp) /* leaf buffer */
599{
600 int from; /* source leaf index */
601 xfs_dir2_leaf_t *leaf; /* leaf structure */
602 int loglow; /* first leaf entry to log */
603 int to; /* target leaf index */
604
605 leaf = bp->data;
606 if (!leaf->hdr.stale) {
607 return;
608 }
609 /*
610 * Compress out the stale entries in place.
611 */
a818e5de 612 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
3c1f9c15 613 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
614 continue;
615 /*
616 * Only actually copy the entries that are different.
617 */
618 if (from > to) {
619 if (loglow == -1)
620 loglow = to;
621 leaf->ents[to] = leaf->ents[from];
622 }
623 to++;
624 }
625 /*
626 * Update and log the header, log the leaf entries.
627 */
a818e5de
NS
628 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
629 be16_add(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
1da177e4
LT
630 leaf->hdr.stale = 0;
631 xfs_dir2_leaf_log_header(args->trans, bp);
632 if (loglow != -1)
633 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
634}
635
636/*
637 * Compact the leaf entries, removing stale ones.
638 * Leave one stale entry behind - the one closest to our
639 * insertion index - and the caller will shift that one to our insertion
640 * point later.
641 * Return new insertion index, where the remaining stale entry is,
642 * and leaf logging indices.
643 */
644void
645xfs_dir2_leaf_compact_x1(
646 xfs_dabuf_t *bp, /* leaf buffer */
647 int *indexp, /* insertion index */
648 int *lowstalep, /* out: stale entry before us */
649 int *highstalep, /* out: stale entry after us */
650 int *lowlogp, /* out: low log index */
651 int *highlogp) /* out: high log index */
652{
653 int from; /* source copy index */
654 int highstale; /* stale entry at/after index */
655 int index; /* insertion index */
656 int keepstale; /* source index of kept stale */
657 xfs_dir2_leaf_t *leaf; /* leaf structure */
658 int lowstale; /* stale entry before index */
659 int newindex=0; /* new insertion index */
660 int to; /* destination copy index */
661
662 leaf = bp->data;
a818e5de 663 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
1da177e4
LT
664 index = *indexp;
665 /*
666 * Find the first stale entry before our index, if any.
667 */
668 for (lowstale = index - 1;
669 lowstale >= 0 &&
3c1f9c15 670 be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
1da177e4
LT
671 lowstale--)
672 continue;
673 /*
674 * Find the first stale entry at or after our index, if any.
675 * Stop if the answer would be worse than lowstale.
676 */
677 for (highstale = index;
a818e5de 678 highstale < be16_to_cpu(leaf->hdr.count) &&
3c1f9c15 679 be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR &&
1da177e4
LT
680 (lowstale < 0 || index - lowstale > highstale - index);
681 highstale++)
682 continue;
683 /*
684 * Pick the better of lowstale and highstale.
685 */
686 if (lowstale >= 0 &&
a818e5de 687 (highstale == be16_to_cpu(leaf->hdr.count) ||
1da177e4
LT
688 index - lowstale <= highstale - index))
689 keepstale = lowstale;
690 else
691 keepstale = highstale;
692 /*
693 * Copy the entries in place, removing all the stale entries
694 * except keepstale.
695 */
a818e5de 696 for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
1da177e4
LT
697 /*
698 * Notice the new value of index.
699 */
700 if (index == from)
701 newindex = to;
702 if (from != keepstale &&
3c1f9c15 703 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
1da177e4
LT
704 if (from == to)
705 *lowlogp = to;
706 continue;
707 }
708 /*
709 * Record the new keepstale value for the insertion.
710 */
711 if (from == keepstale)
712 lowstale = highstale = to;
713 /*
714 * Copy only the entries that have moved.
715 */
716 if (from > to)
717 leaf->ents[to] = leaf->ents[from];
718 to++;
719 }
720 ASSERT(from > to);
721 /*
722 * If the insertion point was past the last entry,
723 * set the new insertion point accordingly.
724 */
725 if (index == from)
726 newindex = to;
727 *indexp = newindex;
728 /*
729 * Adjust the leaf header values.
730 */
a818e5de
NS
731 be16_add(&leaf->hdr.count, -(from - to));
732 leaf->hdr.stale = cpu_to_be16(1);
1da177e4
LT
733 /*
734 * Remember the low/high stale value only in the "right"
735 * direction.
736 */
737 if (lowstale >= newindex)
738 lowstale = -1;
739 else
a818e5de
NS
740 highstale = be16_to_cpu(leaf->hdr.count);
741 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
1da177e4
LT
742 *lowstalep = lowstale;
743 *highstalep = highstale;
744}
745
746/*
747 * Getdents (readdir) for leaf and node directories.
748 * This reads the data blocks only, so is the same for both forms.
749 */
750int /* error */
751xfs_dir2_leaf_getdents(
1da177e4 752 xfs_inode_t *dp, /* incore directory inode */
051e7cd4
CH
753 void *dirent,
754 size_t bufsize,
755 xfs_off_t *offset,
756 filldir_t filldir)
1da177e4
LT
757{
758 xfs_dabuf_t *bp; /* data block buffer */
759 int byteoff; /* offset in current block */
760 xfs_dir2_db_t curdb; /* db for current block */
761 xfs_dir2_off_t curoff; /* current overall offset */
762 xfs_dir2_data_t *data; /* data block structure */
763 xfs_dir2_data_entry_t *dep; /* data entry */
764 xfs_dir2_data_unused_t *dup; /* unused entry */
f6d75cbe 765 int error = 0; /* error return value */
1da177e4
LT
766 int i; /* temporary loop index */
767 int j; /* temporary loop index */
768 int length; /* temporary length value */
769 xfs_bmbt_irec_t *map; /* map vector for blocks */
770 xfs_extlen_t map_blocks; /* number of fsbs in map */
771 xfs_dablk_t map_off; /* last mapped file offset */
772 int map_size; /* total entries in *map */
773 int map_valid; /* valid entries in *map */
774 xfs_mount_t *mp; /* filesystem mount point */
775 xfs_dir2_off_t newoff; /* new curoff after new blk */
776 int nmap; /* mappings to ask xfs_bmapi */
f6d75cbe 777 char *ptr = NULL; /* pointer to current data */
1da177e4
LT
778 int ra_current; /* number of read-ahead blks */
779 int ra_index; /* *map index for read-ahead */
780 int ra_offset; /* map entry offset for ra */
781 int ra_want; /* readahead count wanted */
051e7cd4 782 xfs_ino_t ino;
1da177e4
LT
783
784 /*
785 * If the offset is at or past the largest allowed value,
051e7cd4 786 * give up right away.
1da177e4 787 */
051e7cd4 788 if (*offset >= XFS_DIR2_MAX_DATAPTR)
1da177e4 789 return 0;
051e7cd4 790
1da177e4 791 mp = dp->i_mount;
051e7cd4 792
1da177e4
LT
793 /*
794 * Set up to bmap a number of blocks based on the caller's
795 * buffer size, the directory block size, and the filesystem
796 * block size.
797 */
051e7cd4 798 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
1da177e4
LT
799 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
800 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
801 bp = NULL;
051e7cd4 802
1da177e4
LT
803 /*
804 * Inside the loop we keep the main offset value as a byte offset
805 * in the directory file.
806 */
051e7cd4
CH
807 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
808
1da177e4
LT
809 /*
810 * Force this conversion through db so we truncate the offset
811 * down to get the start of the data block.
812 */
bbaaf538 813 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
1da177e4
LT
814 /*
815 * Loop over directory entries until we reach the end offset.
816 * Get more blocks and readahead as necessary.
817 */
818 while (curoff < XFS_DIR2_LEAF_OFFSET) {
819 /*
820 * If we have no buffer, or we're off the end of the
821 * current buffer, need to get another one.
822 */
823 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
824 /*
825 * If we have a buffer, we need to release it and
826 * take it out of the mapping.
827 */
828 if (bp) {
051e7cd4 829 xfs_da_brelse(NULL, bp);
1da177e4
LT
830 bp = NULL;
831 map_blocks -= mp->m_dirblkfsbs;
832 /*
833 * Loop to get rid of the extents for the
834 * directory block.
835 */
836 for (i = mp->m_dirblkfsbs; i > 0; ) {
837 j = MIN((int)map->br_blockcount, i);
838 map->br_blockcount -= j;
839 map->br_startblock += j;
840 map->br_startoff += j;
841 /*
842 * If mapping is done, pitch it from
843 * the table.
844 */
845 if (!map->br_blockcount && --map_valid)
846 memmove(&map[0], &map[1],
847 sizeof(map[0]) *
848 map_valid);
849 i -= j;
850 }
851 }
852 /*
853 * Recalculate the readahead blocks wanted.
854 */
051e7cd4 855 ra_want = howmany(bufsize + mp->m_dirblksize,
1da177e4 856 mp->m_sb.sb_blocksize) - 1;
051e7cd4 857
1da177e4
LT
858 /*
859 * If we don't have as many as we want, and we haven't
860 * run out of data blocks, get some more mappings.
861 */
862 if (1 + ra_want > map_blocks &&
863 map_off <
bbaaf538 864 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
1da177e4
LT
865 /*
866 * Get more bmaps, fill in after the ones
867 * we already have in the table.
868 */
869 nmap = map_size - map_valid;
051e7cd4 870 error = xfs_bmapi(NULL, dp,
1da177e4 871 map_off,
bbaaf538 872 xfs_dir2_byte_to_da(mp,
1da177e4
LT
873 XFS_DIR2_LEAF_OFFSET) - map_off,
874 XFS_BMAPI_METADATA, NULL, 0,
3e57ecf6 875 &map[map_valid], &nmap, NULL, NULL);
1da177e4
LT
876 /*
877 * Don't know if we should ignore this or
878 * try to return an error.
879 * The trouble with returning errors
880 * is that readdir will just stop without
881 * actually passing the error through.
882 */
883 if (error)
884 break; /* XXX */
885 /*
886 * If we got all the mappings we asked for,
887 * set the final map offset based on the
888 * last bmap value received.
889 * Otherwise, we've reached the end.
890 */
891 if (nmap == map_size - map_valid)
892 map_off =
893 map[map_valid + nmap - 1].br_startoff +
894 map[map_valid + nmap - 1].br_blockcount;
895 else
896 map_off =
bbaaf538 897 xfs_dir2_byte_to_da(mp,
1da177e4
LT
898 XFS_DIR2_LEAF_OFFSET);
899 /*
900 * Look for holes in the mapping, and
901 * eliminate them. Count up the valid blocks.
902 */
903 for (i = map_valid; i < map_valid + nmap; ) {
904 if (map[i].br_startblock ==
905 HOLESTARTBLOCK) {
906 nmap--;
907 length = map_valid + nmap - i;
908 if (length)
909 memmove(&map[i],
910 &map[i + 1],
911 sizeof(map[i]) *
912 length);
913 } else {
914 map_blocks +=
915 map[i].br_blockcount;
916 i++;
917 }
918 }
919 map_valid += nmap;
920 }
921 /*
922 * No valid mappings, so no more data blocks.
923 */
924 if (!map_valid) {
bbaaf538 925 curoff = xfs_dir2_da_to_byte(mp, map_off);
1da177e4
LT
926 break;
927 }
928 /*
929 * Read the directory block starting at the first
930 * mapping.
931 */
bbaaf538 932 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
051e7cd4 933 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
1da177e4
LT
934 map->br_blockcount >= mp->m_dirblkfsbs ?
935 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
936 -1,
937 &bp, XFS_DATA_FORK);
938 /*
939 * Should just skip over the data block instead
940 * of giving up.
941 */
942 if (error)
943 break; /* XXX */
944 /*
945 * Adjust the current amount of read-ahead: we just
946 * read a block that was previously ra.
947 */
948 if (ra_current)
949 ra_current -= mp->m_dirblkfsbs;
950 /*
951 * Do we need more readahead?
952 */
953 for (ra_index = ra_offset = i = 0;
954 ra_want > ra_current && i < map_blocks;
955 i += mp->m_dirblkfsbs) {
956 ASSERT(ra_index < map_valid);
957 /*
958 * Read-ahead a contiguous directory block.
959 */
960 if (i > ra_current &&
961 map[ra_index].br_blockcount >=
962 mp->m_dirblkfsbs) {
963 xfs_baread(mp->m_ddev_targp,
964 XFS_FSB_TO_DADDR(mp,
965 map[ra_index].br_startblock +
966 ra_offset),
967 (int)BTOBB(mp->m_dirblksize));
968 ra_current = i;
969 }
970 /*
971 * Read-ahead a non-contiguous directory block.
972 * This doesn't use our mapping, but this
973 * is a very rare case.
974 */
975 else if (i > ra_current) {
051e7cd4 976 (void)xfs_da_reada_buf(NULL, dp,
1da177e4
LT
977 map[ra_index].br_startoff +
978 ra_offset, XFS_DATA_FORK);
979 ra_current = i;
980 }
981 /*
982 * Advance offset through the mapping table.
983 */
984 for (j = 0; j < mp->m_dirblkfsbs; j++) {
985 /*
986 * The rest of this extent but not
987 * more than a dir block.
988 */
989 length = MIN(mp->m_dirblkfsbs,
990 (int)(map[ra_index].br_blockcount -
991 ra_offset));
992 j += length;
993 ra_offset += length;
994 /*
995 * Advance to the next mapping if
996 * this one is used up.
997 */
998 if (ra_offset ==
999 map[ra_index].br_blockcount) {
1000 ra_offset = 0;
1001 ra_index++;
1002 }
1003 }
1004 }
1005 /*
1006 * Having done a read, we need to set a new offset.
1007 */
bbaaf538 1008 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1da177e4
LT
1009 /*
1010 * Start of the current block.
1011 */
1012 if (curoff < newoff)
1013 curoff = newoff;
1014 /*
1015 * Make sure we're in the right block.
1016 */
1017 else if (curoff > newoff)
bbaaf538 1018 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1da177e4
LT
1019 curdb);
1020 data = bp->data;
1021 xfs_dir2_data_check(dp, bp);
1022 /*
1023 * Find our position in the block.
1024 */
1025 ptr = (char *)&data->u;
bbaaf538 1026 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1da177e4
LT
1027 /*
1028 * Skip past the header.
1029 */
1030 if (byteoff == 0)
1031 curoff += (uint)sizeof(data->hdr);
1032 /*
1033 * Skip past entries until we reach our offset.
1034 */
1035 else {
1036 while ((char *)ptr - (char *)data < byteoff) {
1037 dup = (xfs_dir2_data_unused_t *)ptr;
1038
ad354eb3 1039 if (be16_to_cpu(dup->freetag)
1da177e4
LT
1040 == XFS_DIR2_DATA_FREE_TAG) {
1041
ad354eb3 1042 length = be16_to_cpu(dup->length);
1da177e4
LT
1043 ptr += length;
1044 continue;
1045 }
1046 dep = (xfs_dir2_data_entry_t *)ptr;
1047 length =
bbaaf538 1048 xfs_dir2_data_entsize(dep->namelen);
1da177e4
LT
1049 ptr += length;
1050 }
1051 /*
1052 * Now set our real offset.
1053 */
1054 curoff =
bbaaf538
CH
1055 xfs_dir2_db_off_to_byte(mp,
1056 xfs_dir2_byte_to_db(mp, curoff),
1da177e4
LT
1057 (char *)ptr - (char *)data);
1058 if (ptr >= (char *)data + mp->m_dirblksize) {
1059 continue;
1060 }
1061 }
1062 }
1063 /*
1064 * We have a pointer to an entry.
1065 * Is it a live one?
1066 */
1067 dup = (xfs_dir2_data_unused_t *)ptr;
1068 /*
1069 * No, it's unused, skip over it.
1070 */
ad354eb3
NS
1071 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1072 length = be16_to_cpu(dup->length);
1da177e4
LT
1073 ptr += length;
1074 curoff += length;
1075 continue;
1076 }
1077
1078 /*
1079 * Copy the entry into the putargs, and try formatting it.
1080 */
1081 dep = (xfs_dir2_data_entry_t *)ptr;
1082
051e7cd4 1083 length = xfs_dir2_data_entsize(dep->namelen);
1da177e4 1084
051e7cd4 1085 ino = be64_to_cpu(dep->inumber);
1da177e4 1086#if XFS_BIG_INUMS
051e7cd4 1087 ino += mp->m_inoadd;
1da177e4 1088#endif
1da177e4
LT
1089
1090 /*
1091 * Won't fit. Return to caller.
1092 */
051e7cd4
CH
1093 if (filldir(dirent, dep->name, dep->namelen,
1094 xfs_dir2_byte_to_dataptr(mp, curoff + length),
1095 ino, DT_UNKNOWN))
1da177e4 1096 break;
051e7cd4 1097
1da177e4
LT
1098 /*
1099 * Advance to next entry in the block.
1100 */
1101 ptr += length;
1102 curoff += length;
051e7cd4 1103 bufsize -= length;
1da177e4
LT
1104 }
1105
1106 /*
1107 * All done. Set output offset value to current offset.
1108 */
bbaaf538 1109 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
051e7cd4 1110 *offset = XFS_DIR2_MAX_DATAPTR;
1da177e4 1111 else
051e7cd4 1112 *offset = xfs_dir2_byte_to_dataptr(mp, curoff);
1da177e4
LT
1113 kmem_free(map, map_size * sizeof(*map));
1114 if (bp)
051e7cd4 1115 xfs_da_brelse(NULL, bp);
1da177e4
LT
1116 return error;
1117}
1118
1119/*
1120 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1121 */
1122int
1123xfs_dir2_leaf_init(
1124 xfs_da_args_t *args, /* operation arguments */
1125 xfs_dir2_db_t bno, /* directory block number */
1126 xfs_dabuf_t **bpp, /* out: leaf buffer */
1127 int magic) /* magic number for block */
1128{
1129 xfs_dabuf_t *bp; /* leaf buffer */
1130 xfs_inode_t *dp; /* incore directory inode */
1131 int error; /* error return code */
1132 xfs_dir2_leaf_t *leaf; /* leaf structure */
1133 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1134 xfs_mount_t *mp; /* filesystem mount point */
1135 xfs_trans_t *tp; /* transaction pointer */
1136
1137 dp = args->dp;
1138 ASSERT(dp != NULL);
1139 tp = args->trans;
1140 mp = dp->i_mount;
1141 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1142 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1143 /*
1144 * Get the buffer for the block.
1145 */
bbaaf538 1146 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1da177e4
LT
1147 XFS_DATA_FORK);
1148 if (error) {
1149 return error;
1150 }
1151 ASSERT(bp != NULL);
1152 leaf = bp->data;
1153 /*
1154 * Initialize the header.
1155 */
89da0544 1156 leaf->hdr.info.magic = cpu_to_be16(magic);
1da177e4
LT
1157 leaf->hdr.info.forw = 0;
1158 leaf->hdr.info.back = 0;
1159 leaf->hdr.count = 0;
1160 leaf->hdr.stale = 0;
1161 xfs_dir2_leaf_log_header(tp, bp);
1162 /*
1163 * If it's a leaf-format directory initialize the tail.
1164 * In this case our caller has the real bests table to copy into
1165 * the block.
1166 */
1167 if (magic == XFS_DIR2_LEAF1_MAGIC) {
bbaaf538 1168 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
1169 ltp->bestcount = 0;
1170 xfs_dir2_leaf_log_tail(tp, bp);
1171 }
1172 *bpp = bp;
1173 return 0;
1174}
1175
1176/*
1177 * Log the bests entries indicated from a leaf1 block.
1178 */
ba0f32d4 1179static void
1da177e4
LT
1180xfs_dir2_leaf_log_bests(
1181 xfs_trans_t *tp, /* transaction pointer */
1182 xfs_dabuf_t *bp, /* leaf buffer */
1183 int first, /* first entry to log */
1184 int last) /* last entry to log */
1185{
68b3a102
NS
1186 __be16 *firstb; /* pointer to first entry */
1187 __be16 *lastb; /* pointer to last entry */
1da177e4
LT
1188 xfs_dir2_leaf_t *leaf; /* leaf structure */
1189 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1190
1191 leaf = bp->data;
89da0544 1192 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
bbaaf538
CH
1193 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1194 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1195 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1da177e4
LT
1196 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1197 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1198}
1199
1200/*
1201 * Log the leaf entries indicated from a leaf1 or leafn block.
1202 */
1203void
1204xfs_dir2_leaf_log_ents(
1205 xfs_trans_t *tp, /* transaction pointer */
1206 xfs_dabuf_t *bp, /* leaf buffer */
1207 int first, /* first entry to log */
1208 int last) /* last entry to log */
1209{
1210 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1211 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1212 xfs_dir2_leaf_t *leaf; /* leaf structure */
1213
1214 leaf = bp->data;
89da0544
NS
1215 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1216 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1217 firstlep = &leaf->ents[first];
1218 lastlep = &leaf->ents[last];
1219 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1220 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1221}
1222
1223/*
1224 * Log the header of the leaf1 or leafn block.
1225 */
1226void
1227xfs_dir2_leaf_log_header(
1228 xfs_trans_t *tp, /* transaction pointer */
1229 xfs_dabuf_t *bp) /* leaf buffer */
1230{
1231 xfs_dir2_leaf_t *leaf; /* leaf structure */
1232
1233 leaf = bp->data;
89da0544
NS
1234 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1235 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1236 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1237 (uint)(sizeof(leaf->hdr) - 1));
1238}
1239
1240/*
1241 * Log the tail of the leaf1 block.
1242 */
ba0f32d4 1243STATIC void
1da177e4
LT
1244xfs_dir2_leaf_log_tail(
1245 xfs_trans_t *tp, /* transaction pointer */
1246 xfs_dabuf_t *bp) /* leaf buffer */
1247{
1248 xfs_dir2_leaf_t *leaf; /* leaf structure */
1249 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1250 xfs_mount_t *mp; /* filesystem mount point */
1251
1252 mp = tp->t_mountp;
1253 leaf = bp->data;
89da0544 1254 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
bbaaf538 1255 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
1256 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1257 (uint)(mp->m_dirblksize - 1));
1258}
1259
1260/*
1261 * Look up the entry referred to by args in the leaf format directory.
1262 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1263 * is also used by the node-format code.
1264 */
1265int
1266xfs_dir2_leaf_lookup(
1267 xfs_da_args_t *args) /* operation arguments */
1268{
1269 xfs_dabuf_t *dbp; /* data block buffer */
1270 xfs_dir2_data_entry_t *dep; /* data block entry */
1271 xfs_inode_t *dp; /* incore directory inode */
1272 int error; /* error return code */
1273 int index; /* found entry index */
1274 xfs_dabuf_t *lbp; /* leaf buffer */
1275 xfs_dir2_leaf_t *leaf; /* leaf structure */
1276 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1277 xfs_trans_t *tp; /* transaction pointer */
1278
1279 xfs_dir2_trace_args("leaf_lookup", args);
1280 /*
1281 * Look up name in the leaf block, returning both buffers and index.
1282 */
1283 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1284 return error;
1285 }
1286 tp = args->trans;
1287 dp = args->dp;
1288 xfs_dir2_leaf_check(dp, lbp);
1289 leaf = lbp->data;
1290 /*
1291 * Get to the leaf entry and contained data entry address.
1292 */
1293 lep = &leaf->ents[index];
1294 /*
1295 * Point to the data entry.
1296 */
1297 dep = (xfs_dir2_data_entry_t *)
1298 ((char *)dbp->data +
bbaaf538 1299 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1da177e4
LT
1300 /*
1301 * Return the found inode number.
1302 */
ff9901c1 1303 args->inumber = be64_to_cpu(dep->inumber);
1da177e4
LT
1304 xfs_da_brelse(tp, dbp);
1305 xfs_da_brelse(tp, lbp);
1306 return XFS_ERROR(EEXIST);
1307}
1308
1309/*
1310 * Look up name/hash in the leaf block.
1311 * Fill in indexp with the found index, and dbpp with the data buffer.
1312 * If not found dbpp will be NULL, and ENOENT comes back.
1313 * lbpp will always be filled in with the leaf buffer unless there's an error.
1314 */
1315static int /* error */
1316xfs_dir2_leaf_lookup_int(
1317 xfs_da_args_t *args, /* operation arguments */
1318 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1319 int *indexp, /* out: index in leaf block */
1320 xfs_dabuf_t **dbpp) /* out: data buffer */
1321{
1322 xfs_dir2_db_t curdb; /* current data block number */
1323 xfs_dabuf_t *dbp; /* data buffer */
1324 xfs_dir2_data_entry_t *dep; /* data entry */
1325 xfs_inode_t *dp; /* incore directory inode */
1326 int error; /* error return code */
1327 int index; /* index in leaf block */
1328 xfs_dabuf_t *lbp; /* leaf buffer */
1329 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1330 xfs_dir2_leaf_t *leaf; /* leaf structure */
1331 xfs_mount_t *mp; /* filesystem mount point */
1332 xfs_dir2_db_t newdb; /* new data block number */
1333 xfs_trans_t *tp; /* transaction pointer */
1334
1335 dp = args->dp;
1336 tp = args->trans;
1337 mp = dp->i_mount;
1338 /*
1339 * Read the leaf block into the buffer.
1340 */
1341 if ((error =
1342 xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1343 XFS_DATA_FORK))) {
1344 return error;
1345 }
1346 *lbpp = lbp;
1347 leaf = lbp->data;
1348 xfs_dir2_leaf_check(dp, lbp);
1349 /*
1350 * Look for the first leaf entry with our hash value.
1351 */
1352 index = xfs_dir2_leaf_search_hash(args, lbp);
1353 /*
1354 * Loop over all the entries with the right hash value
1355 * looking to match the name.
1356 */
1357 for (lep = &leaf->ents[index], dbp = NULL, curdb = -1;
3c1f9c15 1358 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
1da177e4
LT
1359 lep++, index++) {
1360 /*
1361 * Skip over stale leaf entries.
1362 */
3c1f9c15 1363 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
1364 continue;
1365 /*
1366 * Get the new data block number.
1367 */
bbaaf538 1368 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4
LT
1369 /*
1370 * If it's not the same as the old data block number,
1371 * need to pitch the old one and read the new one.
1372 */
1373 if (newdb != curdb) {
1374 if (dbp)
1375 xfs_da_brelse(tp, dbp);
1376 if ((error =
1377 xfs_da_read_buf(tp, dp,
bbaaf538 1378 xfs_dir2_db_to_da(mp, newdb), -1, &dbp,
1da177e4
LT
1379 XFS_DATA_FORK))) {
1380 xfs_da_brelse(tp, lbp);
1381 return error;
1382 }
1383 xfs_dir2_data_check(dp, dbp);
1384 curdb = newdb;
1385 }
1386 /*
1387 * Point to the data entry.
1388 */
1389 dep = (xfs_dir2_data_entry_t *)
1390 ((char *)dbp->data +
bbaaf538 1391 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1da177e4
LT
1392 /*
1393 * If it matches then return it.
1394 */
1395 if (dep->namelen == args->namelen &&
1396 dep->name[0] == args->name[0] &&
1397 memcmp(dep->name, args->name, args->namelen) == 0) {
1398 *dbpp = dbp;
1399 *indexp = index;
1400 return 0;
1401 }
1402 }
1403 /*
1404 * No match found, return ENOENT.
1405 */
1406 ASSERT(args->oknoent);
1407 if (dbp)
1408 xfs_da_brelse(tp, dbp);
1409 xfs_da_brelse(tp, lbp);
1410 return XFS_ERROR(ENOENT);
1411}
1412
1413/*
1414 * Remove an entry from a leaf format directory.
1415 */
1416int /* error */
1417xfs_dir2_leaf_removename(
1418 xfs_da_args_t *args) /* operation arguments */
1419{
68b3a102 1420 __be16 *bestsp; /* leaf block best freespace */
1da177e4
LT
1421 xfs_dir2_data_t *data; /* data block structure */
1422 xfs_dir2_db_t db; /* data block number */
1423 xfs_dabuf_t *dbp; /* data block buffer */
1424 xfs_dir2_data_entry_t *dep; /* data entry structure */
1425 xfs_inode_t *dp; /* incore directory inode */
1426 int error; /* error return code */
1427 xfs_dir2_db_t i; /* temporary data block # */
1428 int index; /* index into leaf entries */
1429 xfs_dabuf_t *lbp; /* leaf buffer */
1430 xfs_dir2_leaf_t *leaf; /* leaf structure */
1431 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1432 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1433 xfs_mount_t *mp; /* filesystem mount point */
1434 int needlog; /* need to log data header */
1435 int needscan; /* need to rescan data frees */
1436 xfs_dir2_data_off_t oldbest; /* old value of best free */
1437 xfs_trans_t *tp; /* transaction pointer */
1438
1439 xfs_dir2_trace_args("leaf_removename", args);
1440 /*
1441 * Lookup the leaf entry, get the leaf and data blocks read in.
1442 */
1443 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1444 return error;
1445 }
1446 dp = args->dp;
1447 tp = args->trans;
1448 mp = dp->i_mount;
1449 leaf = lbp->data;
1450 data = dbp->data;
1451 xfs_dir2_data_check(dp, dbp);
1452 /*
1453 * Point to the leaf entry, use that to point to the data entry.
1454 */
1455 lep = &leaf->ents[index];
bbaaf538 1456 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4 1457 dep = (xfs_dir2_data_entry_t *)
bbaaf538 1458 ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1da177e4 1459 needscan = needlog = 0;
70e73f59 1460 oldbest = be16_to_cpu(data->hdr.bestfree[0].length);
bbaaf538
CH
1461 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1462 bestsp = xfs_dir2_leaf_bests_p(ltp);
68b3a102 1463 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1da177e4
LT
1464 /*
1465 * Mark the former data entry unused.
1466 */
1467 xfs_dir2_data_make_free(tp, dbp,
1468 (xfs_dir2_data_aoff_t)((char *)dep - (char *)data),
bbaaf538 1469 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1da177e4
LT
1470 /*
1471 * We just mark the leaf entry stale by putting a null in it.
1472 */
a818e5de 1473 be16_add(&leaf->hdr.stale, 1);
1da177e4 1474 xfs_dir2_leaf_log_header(tp, lbp);
3c1f9c15 1475 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
1476 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1477 /*
1478 * Scan the freespace in the data block again if necessary,
1479 * log the data block header if necessary.
1480 */
1481 if (needscan)
ef497f8a 1482 xfs_dir2_data_freescan(mp, data, &needlog);
1da177e4
LT
1483 if (needlog)
1484 xfs_dir2_data_log_header(tp, dbp);
1485 /*
1486 * If the longest freespace in the data block has changed,
1487 * put the new value in the bests table and log that.
1488 */
70e73f59
NS
1489 if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) {
1490 bestsp[db] = data->hdr.bestfree[0].length;
1da177e4
LT
1491 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1492 }
1493 xfs_dir2_data_check(dp, dbp);
1494 /*
1495 * If the data block is now empty then get rid of the data block.
1496 */
70e73f59 1497 if (be16_to_cpu(data->hdr.bestfree[0].length) ==
1da177e4
LT
1498 mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1499 ASSERT(db != mp->m_dirdatablk);
1500 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1501 /*
1502 * Nope, can't get rid of it because it caused
1503 * allocation of a bmap btree block to do so.
1504 * Just go on, returning success, leaving the
1505 * empty block in place.
1506 */
1507 if (error == ENOSPC && args->total == 0) {
1508 xfs_da_buf_done(dbp);
1509 error = 0;
1510 }
1511 xfs_dir2_leaf_check(dp, lbp);
1512 xfs_da_buf_done(lbp);
1513 return error;
1514 }
1515 dbp = NULL;
1516 /*
1517 * If this is the last data block then compact the
1518 * bests table by getting rid of entries.
1519 */
afbcb3f9 1520 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1da177e4
LT
1521 /*
1522 * Look for the last active entry (i).
1523 */
1524 for (i = db - 1; i > 0; i--) {
68b3a102 1525 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1da177e4
LT
1526 break;
1527 }
1528 /*
1529 * Copy the table down so inactive entries at the
1530 * end are removed.
1531 */
1532 memmove(&bestsp[db - i], bestsp,
afbcb3f9
NS
1533 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1534 be32_add(&ltp->bestcount, -(db - i));
1da177e4 1535 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 1536 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4 1537 } else
68b3a102 1538 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
1539 }
1540 /*
1541 * If the data block was not the first one, drop it.
1542 */
1543 else if (db != mp->m_dirdatablk && dbp != NULL) {
1544 xfs_da_buf_done(dbp);
1545 dbp = NULL;
1546 }
1547 xfs_dir2_leaf_check(dp, lbp);
1548 /*
1549 * See if we can convert to block form.
1550 */
1551 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1552}
1553
1554/*
1555 * Replace the inode number in a leaf format directory entry.
1556 */
1557int /* error */
1558xfs_dir2_leaf_replace(
1559 xfs_da_args_t *args) /* operation arguments */
1560{
1561 xfs_dabuf_t *dbp; /* data block buffer */
1562 xfs_dir2_data_entry_t *dep; /* data block entry */
1563 xfs_inode_t *dp; /* incore directory inode */
1564 int error; /* error return code */
1565 int index; /* index of leaf entry */
1566 xfs_dabuf_t *lbp; /* leaf buffer */
1567 xfs_dir2_leaf_t *leaf; /* leaf structure */
1568 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1569 xfs_trans_t *tp; /* transaction pointer */
1570
1571 xfs_dir2_trace_args("leaf_replace", args);
1572 /*
1573 * Look up the entry.
1574 */
1575 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1576 return error;
1577 }
1578 dp = args->dp;
1579 leaf = lbp->data;
1580 /*
1581 * Point to the leaf entry, get data address from it.
1582 */
1583 lep = &leaf->ents[index];
1584 /*
1585 * Point to the data entry.
1586 */
1587 dep = (xfs_dir2_data_entry_t *)
1588 ((char *)dbp->data +
bbaaf538 1589 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
ff9901c1 1590 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1da177e4
LT
1591 /*
1592 * Put the new inode number in, log it.
1593 */
ff9901c1 1594 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
1595 tp = args->trans;
1596 xfs_dir2_data_log_entry(tp, dbp, dep);
1597 xfs_da_buf_done(dbp);
1598 xfs_dir2_leaf_check(dp, lbp);
1599 xfs_da_brelse(tp, lbp);
1600 return 0;
1601}
1602
1603/*
1604 * Return index in the leaf block (lbp) which is either the first
1605 * one with this hash value, or if there are none, the insert point
1606 * for that hash value.
1607 */
1608int /* index value */
1609xfs_dir2_leaf_search_hash(
1610 xfs_da_args_t *args, /* operation arguments */
1611 xfs_dabuf_t *lbp) /* leaf buffer */
1612{
1613 xfs_dahash_t hash=0; /* hash from this entry */
1614 xfs_dahash_t hashwant; /* hash value looking for */
1615 int high; /* high leaf index */
1616 int low; /* low leaf index */
1617 xfs_dir2_leaf_t *leaf; /* leaf structure */
1618 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1619 int mid=0; /* current leaf index */
1620
1621 leaf = lbp->data;
1622#ifndef __KERNEL__
1623 if (!leaf->hdr.count)
1624 return 0;
1625#endif
1626 /*
1627 * Note, the table cannot be empty, so we have to go through the loop.
1628 * Binary search the leaf entries looking for our hash value.
1629 */
a818e5de 1630 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1da177e4
LT
1631 hashwant = args->hashval;
1632 low <= high; ) {
1633 mid = (low + high) >> 1;
3c1f9c15 1634 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1da177e4
LT
1635 break;
1636 if (hash < hashwant)
1637 low = mid + 1;
1638 else
1639 high = mid - 1;
1640 }
1641 /*
1642 * Found one, back up through all the equal hash values.
1643 */
1644 if (hash == hashwant) {
3c1f9c15 1645 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1da177e4
LT
1646 mid--;
1647 }
1648 }
1649 /*
1650 * Need to point to an entry higher than ours.
1651 */
1652 else if (hash < hashwant)
1653 mid++;
1654 return mid;
1655}
1656
1657/*
1658 * Trim off a trailing data block. We know it's empty since the leaf
1659 * freespace table says so.
1660 */
1661int /* error */
1662xfs_dir2_leaf_trim_data(
1663 xfs_da_args_t *args, /* operation arguments */
1664 xfs_dabuf_t *lbp, /* leaf buffer */
1665 xfs_dir2_db_t db) /* data block number */
1666{
68b3a102 1667 __be16 *bestsp; /* leaf bests table */
1da177e4
LT
1668#ifdef DEBUG
1669 xfs_dir2_data_t *data; /* data block structure */
1670#endif
1671 xfs_dabuf_t *dbp; /* data block buffer */
1672 xfs_inode_t *dp; /* incore directory inode */
1673 int error; /* error return value */
1674 xfs_dir2_leaf_t *leaf; /* leaf structure */
1675 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1676 xfs_mount_t *mp; /* filesystem mount point */
1677 xfs_trans_t *tp; /* transaction pointer */
1678
1679 dp = args->dp;
1680 mp = dp->i_mount;
1681 tp = args->trans;
1682 /*
1683 * Read the offending data block. We need its buffer.
1684 */
bbaaf538 1685 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
1da177e4
LT
1686 XFS_DATA_FORK))) {
1687 return error;
1688 }
1689#ifdef DEBUG
1690 data = dbp->data;
70e73f59 1691 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1da177e4
LT
1692#endif
1693 /* this seems to be an error
1694 * data is only valid if DEBUG is defined?
1695 * RMC 09/08/1999
1696 */
1697
1698 leaf = lbp->data;
bbaaf538 1699 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
70e73f59 1700 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) ==
1da177e4 1701 mp->m_dirblksize - (uint)sizeof(data->hdr));
afbcb3f9 1702 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
1703 /*
1704 * Get rid of the data block.
1705 */
1706 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1707 ASSERT(error != ENOSPC);
1708 xfs_da_brelse(tp, dbp);
1709 return error;
1710 }
1711 /*
1712 * Eliminate the last bests entry from the table.
1713 */
bbaaf538 1714 bestsp = xfs_dir2_leaf_bests_p(ltp);
afbcb3f9
NS
1715 be32_add(&ltp->bestcount, -1);
1716 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1da177e4 1717 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 1718 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
1719 return 0;
1720}
1721
1722/*
1723 * Convert node form directory to leaf form directory.
1724 * The root of the node form dir needs to already be a LEAFN block.
1725 * Just return if we can't do anything.
1726 */
1727int /* error */
1728xfs_dir2_node_to_leaf(
1729 xfs_da_state_t *state) /* directory operation state */
1730{
1731 xfs_da_args_t *args; /* operation arguments */
1732 xfs_inode_t *dp; /* incore directory inode */
1733 int error; /* error return code */
1734 xfs_dabuf_t *fbp; /* buffer for freespace block */
1735 xfs_fileoff_t fo; /* freespace file offset */
1736 xfs_dir2_free_t *free; /* freespace structure */
1737 xfs_dabuf_t *lbp; /* buffer for leaf block */
1738 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1739 xfs_dir2_leaf_t *leaf; /* leaf structure */
1740 xfs_mount_t *mp; /* filesystem mount point */
1741 int rval; /* successful free trim? */
1742 xfs_trans_t *tp; /* transaction pointer */
1743
1744 /*
1745 * There's more than a leaf level in the btree, so there must
1746 * be multiple leafn blocks. Give up.
1747 */
1748 if (state->path.active > 1)
1749 return 0;
1750 args = state->args;
1751 xfs_dir2_trace_args("node_to_leaf", args);
1752 mp = state->mp;
1753 dp = args->dp;
1754 tp = args->trans;
1755 /*
1756 * Get the last offset in the file.
1757 */
1758 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1759 return error;
1760 }
1761 fo -= mp->m_dirblkfsbs;
1762 /*
1763 * If there are freespace blocks other than the first one,
1764 * take this opportunity to remove trailing empty freespace blocks
1765 * that may have been left behind during no-space-reservation
1766 * operations.
1767 */
1768 while (fo > mp->m_dirfreeblk) {
1769 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1770 return error;
1771 }
1772 if (rval)
1773 fo -= mp->m_dirblkfsbs;
1774 else
1775 return 0;
1776 }
1777 /*
1778 * Now find the block just before the freespace block.
1779 */
1780 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1781 return error;
1782 }
1783 /*
1784 * If it's not the single leaf block, give up.
1785 */
1786 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1787 return 0;
1788 lbp = state->path.blk[0].bp;
1789 leaf = lbp->data;
89da0544 1790 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1791 /*
1792 * Read the freespace block.
1793 */
1794 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1795 XFS_DATA_FORK))) {
1796 return error;
1797 }
1798 free = fbp->data;
0ba962ef 1799 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1800 ASSERT(!free->hdr.firstdb);
1801 /*
1802 * Now see if the leafn and free data will fit in a leaf1.
1803 * If not, release the buffer and give up.
1804 */
1805 if ((uint)sizeof(leaf->hdr) +
a818e5de 1806 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) +
0ba962ef 1807 be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) +
1da177e4
LT
1808 (uint)sizeof(leaf->tail) >
1809 mp->m_dirblksize) {
1810 xfs_da_brelse(tp, fbp);
1811 return 0;
1812 }
1813 /*
1814 * If the leaf has any stale entries in it, compress them out.
1815 * The compact routine will log the header.
1816 */
a818e5de 1817 if (be16_to_cpu(leaf->hdr.stale))
1da177e4
LT
1818 xfs_dir2_leaf_compact(args, lbp);
1819 else
1820 xfs_dir2_leaf_log_header(tp, lbp);
89da0544 1821 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1da177e4
LT
1822 /*
1823 * Set up the leaf tail from the freespace block.
1824 */
bbaaf538 1825 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
0ba962ef 1826 ltp->bestcount = free->hdr.nvalid;
1da177e4
LT
1827 /*
1828 * Set up the leaf bests table.
1829 */
bbaaf538 1830 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
afbcb3f9
NS
1831 be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0]));
1832 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
1833 xfs_dir2_leaf_log_tail(tp, lbp);
1834 xfs_dir2_leaf_check(dp, lbp);
1835 /*
1836 * Get rid of the freespace block.
1837 */
1838 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1839 if (error) {
1840 /*
1841 * This can't fail here because it can only happen when
1842 * punching out the middle of an extent, and this is an
1843 * isolated block.
1844 */
1845 ASSERT(error != ENOSPC);
1846 return error;
1847 }
1848 fbp = NULL;
1849 /*
1850 * Now see if we can convert the single-leaf directory
1851 * down to a block form directory.
1852 * This routine always kills the dabuf for the leaf, so
1853 * eliminate it from the path.
1854 */
1855 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1856 state->path.blk[0].bp = NULL;
1857 return error;
1858}