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