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