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