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