xfs: use generic get_unaligned_beXX helpers
[deliverable/linux.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)
c2066e26 135 xfs_dir2_data_freescan(mp, 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 280 int compact; /* need to compact leaves */
c2066e26 281 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
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 */
2282396d
CH
370 needbytes = 0;
371 if (!leaf->hdr.stale)
372 needbytes += sizeof(xfs_dir2_leaf_entry_t);
373 if (use_block == -1)
374 needbytes += sizeof(xfs_dir2_data_off_t);
375
1da177e4
LT
376 /*
377 * Now kill use_block if it refers to a missing block, so we
378 * can use it as an indication of allocation needed.
379 */
68b3a102 380 if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF)
1da177e4
LT
381 use_block = -1;
382 /*
383 * If we don't have enough free bytes but we can make enough
384 * by compacting out stale entries, we'll do that.
385 */
6a178100
BN
386 if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
387 needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
1da177e4
LT
388 compact = 1;
389 }
390 /*
391 * Otherwise if we don't have enough free bytes we need to
392 * convert to node form.
393 */
6a178100
BN
394 else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
395 leaf->hdr.count)] < needbytes) {
1da177e4
LT
396 /*
397 * Just checking or no space reservation, give up.
398 */
6a178100
BN
399 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
400 args->total == 0) {
1da177e4
LT
401 xfs_da_brelse(tp, lbp);
402 return XFS_ERROR(ENOSPC);
403 }
404 /*
405 * Convert to node form.
406 */
407 error = xfs_dir2_leaf_to_node(args, lbp);
408 xfs_da_buf_done(lbp);
409 if (error)
410 return error;
411 /*
412 * Then add the new entry.
413 */
414 return xfs_dir2_node_addname(args);
415 }
416 /*
417 * Otherwise it will fit without compaction.
418 */
419 else
420 compact = 0;
421 /*
422 * If just checking, then it will fit unless we needed to allocate
423 * a new data block.
424 */
6a178100 425 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1da177e4
LT
426 xfs_da_brelse(tp, lbp);
427 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
428 }
429 /*
430 * If no allocations are allowed, return now before we've
431 * changed anything.
432 */
433 if (args->total == 0 && use_block == -1) {
434 xfs_da_brelse(tp, lbp);
435 return XFS_ERROR(ENOSPC);
436 }
437 /*
438 * Need to compact the leaf entries, removing stale ones.
439 * Leave one stale entry behind - the one closest to our
440 * insertion index - and we'll shift that one to our insertion
441 * point later.
442 */
443 if (compact) {
444 xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
445 &lfloglow, &lfloghigh);
446 }
447 /*
448 * There are stale entries, so we'll need log-low and log-high
449 * impossibly bad values later.
450 */
a818e5de
NS
451 else if (be16_to_cpu(leaf->hdr.stale)) {
452 lfloglow = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
453 lfloghigh = -1;
454 }
455 /*
456 * If there was no data block space found, we need to allocate
457 * a new one.
458 */
459 if (use_block == -1) {
460 /*
461 * Add the new data block.
462 */
463 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
464 &use_block))) {
465 xfs_da_brelse(tp, lbp);
466 return error;
467 }
468 /*
469 * Initialize the block.
470 */
471 if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
472 xfs_da_brelse(tp, lbp);
473 return error;
474 }
475 /*
476 * If we're adding a new data block on the end we need to
477 * extend the bests table. Copy it up one entry.
478 */
afbcb3f9 479 if (use_block >= be32_to_cpu(ltp->bestcount)) {
1da177e4
LT
480 bestsp--;
481 memmove(&bestsp[0], &bestsp[1],
afbcb3f9 482 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
413d57c9 483 be32_add_cpu(&ltp->bestcount, 1);
1da177e4 484 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 485 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
486 }
487 /*
488 * If we're filling in a previously empty block just log it.
489 */
490 else
491 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
c2066e26
CH
492 hdr = dbp->data;
493 bestsp[use_block] = hdr->bestfree[0].length;
1da177e4
LT
494 grown = 1;
495 }
496 /*
497 * Already had space in some data block.
498 * Just read that one in.
499 */
500 else {
501 if ((error =
bbaaf538 502 xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
1da177e4
LT
503 -1, &dbp, XFS_DATA_FORK))) {
504 xfs_da_brelse(tp, lbp);
505 return error;
506 }
c2066e26 507 hdr = dbp->data;
1da177e4
LT
508 grown = 0;
509 }
510 xfs_dir2_data_check(dp, dbp);
511 /*
512 * Point to the biggest freespace in our data block.
513 */
514 dup = (xfs_dir2_data_unused_t *)
c2066e26 515 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
ad354eb3 516 ASSERT(be16_to_cpu(dup->length) >= length);
1da177e4
LT
517 needscan = needlog = 0;
518 /*
519 * Mark the initial part of our freespace in use for the new entry.
520 */
521 xfs_dir2_data_use_free(tp, dbp, dup,
c2066e26 522 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
1da177e4
LT
523 &needlog, &needscan);
524 /*
525 * Initialize our new entry (at last).
526 */
527 dep = (xfs_dir2_data_entry_t *)dup;
ff9901c1 528 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
529 dep->namelen = args->namelen;
530 memcpy(dep->name, args->name, dep->namelen);
bbaaf538 531 tagp = xfs_dir2_data_entry_tag_p(dep);
c2066e26 532 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1da177e4
LT
533 /*
534 * Need to scan fix up the bestfree table.
535 */
536 if (needscan)
c2066e26 537 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
538 /*
539 * Need to log the data block's header.
540 */
541 if (needlog)
542 xfs_dir2_data_log_header(tp, dbp);
543 xfs_dir2_data_log_entry(tp, dbp, dep);
544 /*
545 * If the bests table needs to be changed, do it.
546 * Log the change unless we've already done that.
547 */
c2066e26
CH
548 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
549 bestsp[use_block] = hdr->bestfree[0].length;
1da177e4
LT
550 if (!grown)
551 xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
552 }
4fb44c82
CH
553
554 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
555 highstale, &lfloglow, &lfloghigh);
556
1da177e4
LT
557 /*
558 * Fill in the new leaf entry.
559 */
3c1f9c15 560 lep->hashval = cpu_to_be32(args->hashval);
bbaaf538 561 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
3d693c6e 562 be16_to_cpu(*tagp)));
1da177e4
LT
563 /*
564 * Log the leaf fields and give up the buffers.
565 */
566 xfs_dir2_leaf_log_header(tp, lbp);
567 xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
568 xfs_dir2_leaf_check(dp, lbp);
569 xfs_da_buf_done(lbp);
570 xfs_dir2_data_check(dp, dbp);
571 xfs_da_buf_done(dbp);
572 return 0;
573}
574
575#ifdef DEBUG
576/*
577 * Check the internal consistency of a leaf1 block.
578 * Pop an assert if something is wrong.
579 */
3180e66d 580STATIC void
1da177e4
LT
581xfs_dir2_leaf_check(
582 xfs_inode_t *dp, /* incore directory inode */
583 xfs_dabuf_t *bp) /* leaf's buffer */
584{
585 int i; /* leaf index */
586 xfs_dir2_leaf_t *leaf; /* leaf structure */
587 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
588 xfs_mount_t *mp; /* filesystem mount point */
589 int stale; /* count of stale leaves */
590
591 leaf = bp->data;
592 mp = dp->i_mount;
89da0544 593 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
1da177e4
LT
594 /*
595 * This value is not restrictive enough.
596 * Should factor in the size of the bests table as well.
597 * We can deduce a value for that from di_size.
598 */
bbaaf538
CH
599 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
600 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
601 /*
602 * Leaves and bests don't overlap.
603 */
a818e5de 604 ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
bbaaf538 605 (char *)xfs_dir2_leaf_bests_p(ltp));
1da177e4
LT
606 /*
607 * Check hash value order, count stale entries.
608 */
a818e5de
NS
609 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
610 if (i + 1 < be16_to_cpu(leaf->hdr.count))
3c1f9c15
NS
611 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
612 be32_to_cpu(leaf->ents[i + 1].hashval));
613 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
614 stale++;
615 }
a818e5de 616 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
1da177e4
LT
617}
618#endif /* DEBUG */
619
620/*
621 * Compact out any stale entries in the leaf.
622 * Log the header and changed leaf entries, if any.
623 */
624void
625xfs_dir2_leaf_compact(
626 xfs_da_args_t *args, /* operation arguments */
627 xfs_dabuf_t *bp) /* leaf buffer */
628{
629 int from; /* source leaf index */
630 xfs_dir2_leaf_t *leaf; /* leaf structure */
631 int loglow; /* first leaf entry to log */
632 int to; /* target leaf index */
633
634 leaf = bp->data;
635 if (!leaf->hdr.stale) {
636 return;
637 }
638 /*
639 * Compress out the stale entries in place.
640 */
a818e5de 641 for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
3c1f9c15 642 if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
643 continue;
644 /*
645 * Only actually copy the entries that are different.
646 */
647 if (from > to) {
648 if (loglow == -1)
649 loglow = to;
650 leaf->ents[to] = leaf->ents[from];
651 }
652 to++;
653 }
654 /*
655 * Update and log the header, log the leaf entries.
656 */
a818e5de 657 ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
413d57c9 658 be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
1da177e4
LT
659 leaf->hdr.stale = 0;
660 xfs_dir2_leaf_log_header(args->trans, bp);
661 if (loglow != -1)
662 xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
663}
664
665/*
666 * Compact the leaf entries, removing stale ones.
667 * Leave one stale entry behind - the one closest to our
668 * insertion index - and the caller will shift that one to our insertion
669 * point later.
670 * Return new insertion index, where the remaining stale entry is,
671 * and leaf logging indices.
672 */
673void
674xfs_dir2_leaf_compact_x1(
675 xfs_dabuf_t *bp, /* leaf buffer */
676 int *indexp, /* insertion index */
677 int *lowstalep, /* out: stale entry before us */
678 int *highstalep, /* out: stale entry after us */
679 int *lowlogp, /* out: low log index */
680 int *highlogp) /* out: high log index */
681{
682 int from; /* source copy index */
683 int highstale; /* stale entry at/after index */
684 int index; /* insertion index */
685 int keepstale; /* source index of kept stale */
686 xfs_dir2_leaf_t *leaf; /* leaf structure */
687 int lowstale; /* stale entry before index */
688 int newindex=0; /* new insertion index */
689 int to; /* destination copy index */
690
691 leaf = bp->data;
a818e5de 692 ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
1da177e4
LT
693 index = *indexp;
694 /*
695 * Find the first stale entry before our index, if any.
696 */
697 for (lowstale = index - 1;
698 lowstale >= 0 &&
3c1f9c15 699 be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR;
1da177e4
LT
700 lowstale--)
701 continue;
702 /*
703 * Find the first stale entry at or after our index, if any.
704 * Stop if the answer would be worse than lowstale.
705 */
706 for (highstale = index;
a818e5de 707 highstale < be16_to_cpu(leaf->hdr.count) &&
3c1f9c15 708 be32_to_cpu(leaf->ents[highstale].address) != 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 &&
3c1f9c15 732 be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) {
1da177e4
LT
733 if (from == to)
734 *lowlogp = to;
735 continue;
736 }
737 /*
738 * Record the new keepstale value for the insertion.
739 */
740 if (from == keepstale)
741 lowstale = highstale = to;
742 /*
743 * Copy only the entries that have moved.
744 */
745 if (from > to)
746 leaf->ents[to] = leaf->ents[from];
747 to++;
748 }
749 ASSERT(from > to);
750 /*
751 * If the insertion point was past the last entry,
752 * set the new insertion point accordingly.
753 */
754 if (index == from)
755 newindex = to;
756 *indexp = newindex;
757 /*
758 * Adjust the leaf header values.
759 */
413d57c9 760 be16_add_cpu(&leaf->hdr.count, -(from - to));
a818e5de 761 leaf->hdr.stale = cpu_to_be16(1);
1da177e4
LT
762 /*
763 * Remember the low/high stale value only in the "right"
764 * direction.
765 */
766 if (lowstale >= newindex)
767 lowstale = -1;
768 else
a818e5de
NS
769 highstale = be16_to_cpu(leaf->hdr.count);
770 *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
1da177e4
LT
771 *lowstalep = lowstale;
772 *highstalep = highstale;
773}
774
775/*
776 * Getdents (readdir) for leaf and node directories.
777 * This reads the data blocks only, so is the same for both forms.
778 */
779int /* error */
780xfs_dir2_leaf_getdents(
1da177e4 781 xfs_inode_t *dp, /* incore directory inode */
051e7cd4
CH
782 void *dirent,
783 size_t bufsize,
784 xfs_off_t *offset,
785 filldir_t filldir)
1da177e4
LT
786{
787 xfs_dabuf_t *bp; /* data block buffer */
788 int byteoff; /* offset in current block */
789 xfs_dir2_db_t curdb; /* db for current block */
790 xfs_dir2_off_t curoff; /* current overall offset */
c2066e26 791 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
792 xfs_dir2_data_entry_t *dep; /* data entry */
793 xfs_dir2_data_unused_t *dup; /* unused entry */
f6d75cbe 794 int error = 0; /* error return value */
1da177e4
LT
795 int i; /* temporary loop index */
796 int j; /* temporary loop index */
797 int length; /* temporary length value */
798 xfs_bmbt_irec_t *map; /* map vector for blocks */
799 xfs_extlen_t map_blocks; /* number of fsbs in map */
800 xfs_dablk_t map_off; /* last mapped file offset */
801 int map_size; /* total entries in *map */
802 int map_valid; /* valid entries in *map */
803 xfs_mount_t *mp; /* filesystem mount point */
804 xfs_dir2_off_t newoff; /* new curoff after new blk */
805 int nmap; /* mappings to ask xfs_bmapi */
f6d75cbe 806 char *ptr = NULL; /* pointer to current data */
1da177e4
LT
807 int ra_current; /* number of read-ahead blks */
808 int ra_index; /* *map index for read-ahead */
809 int ra_offset; /* map entry offset for ra */
810 int ra_want; /* readahead count wanted */
811
812 /*
813 * If the offset is at or past the largest allowed value,
051e7cd4 814 * give up right away.
1da177e4 815 */
051e7cd4 816 if (*offset >= XFS_DIR2_MAX_DATAPTR)
1da177e4 817 return 0;
051e7cd4 818
1da177e4 819 mp = dp->i_mount;
051e7cd4 820
1da177e4
LT
821 /*
822 * Set up to bmap a number of blocks based on the caller's
823 * buffer size, the directory block size, and the filesystem
824 * block size.
825 */
051e7cd4 826 map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
1da177e4
LT
827 map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
828 map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
829 bp = NULL;
051e7cd4 830
1da177e4
LT
831 /*
832 * Inside the loop we keep the main offset value as a byte offset
833 * in the directory file.
834 */
051e7cd4
CH
835 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
836
1da177e4
LT
837 /*
838 * Force this conversion through db so we truncate the offset
839 * down to get the start of the data block.
840 */
bbaaf538 841 map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
1da177e4
LT
842 /*
843 * Loop over directory entries until we reach the end offset.
844 * Get more blocks and readahead as necessary.
845 */
846 while (curoff < XFS_DIR2_LEAF_OFFSET) {
847 /*
848 * If we have no buffer, or we're off the end of the
849 * current buffer, need to get another one.
850 */
851 if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
852 /*
853 * If we have a buffer, we need to release it and
854 * take it out of the mapping.
855 */
856 if (bp) {
051e7cd4 857 xfs_da_brelse(NULL, bp);
1da177e4
LT
858 bp = NULL;
859 map_blocks -= mp->m_dirblkfsbs;
860 /*
861 * Loop to get rid of the extents for the
862 * directory block.
863 */
864 for (i = mp->m_dirblkfsbs; i > 0; ) {
865 j = MIN((int)map->br_blockcount, i);
866 map->br_blockcount -= j;
867 map->br_startblock += j;
868 map->br_startoff += j;
869 /*
870 * If mapping is done, pitch it from
871 * the table.
872 */
873 if (!map->br_blockcount && --map_valid)
874 memmove(&map[0], &map[1],
875 sizeof(map[0]) *
876 map_valid);
877 i -= j;
878 }
879 }
880 /*
881 * Recalculate the readahead blocks wanted.
882 */
051e7cd4 883 ra_want = howmany(bufsize + mp->m_dirblksize,
1da177e4 884 mp->m_sb.sb_blocksize) - 1;
8e69ce14 885 ASSERT(ra_want >= 0);
051e7cd4 886
1da177e4
LT
887 /*
888 * If we don't have as many as we want, and we haven't
889 * run out of data blocks, get some more mappings.
890 */
891 if (1 + ra_want > map_blocks &&
892 map_off <
bbaaf538 893 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
1da177e4
LT
894 /*
895 * Get more bmaps, fill in after the ones
896 * we already have in the table.
897 */
898 nmap = map_size - map_valid;
051e7cd4 899 error = xfs_bmapi(NULL, dp,
1da177e4 900 map_off,
bbaaf538 901 xfs_dir2_byte_to_da(mp,
1da177e4
LT
902 XFS_DIR2_LEAF_OFFSET) - map_off,
903 XFS_BMAPI_METADATA, NULL, 0,
b4e9181e 904 &map[map_valid], &nmap, NULL);
1da177e4
LT
905 /*
906 * Don't know if we should ignore this or
907 * try to return an error.
908 * The trouble with returning errors
909 * is that readdir will just stop without
910 * actually passing the error through.
911 */
912 if (error)
913 break; /* XXX */
914 /*
915 * If we got all the mappings we asked for,
916 * set the final map offset based on the
917 * last bmap value received.
918 * Otherwise, we've reached the end.
919 */
920 if (nmap == map_size - map_valid)
921 map_off =
922 map[map_valid + nmap - 1].br_startoff +
923 map[map_valid + nmap - 1].br_blockcount;
924 else
925 map_off =
bbaaf538 926 xfs_dir2_byte_to_da(mp,
1da177e4
LT
927 XFS_DIR2_LEAF_OFFSET);
928 /*
929 * Look for holes in the mapping, and
930 * eliminate them. Count up the valid blocks.
931 */
932 for (i = map_valid; i < map_valid + nmap; ) {
933 if (map[i].br_startblock ==
934 HOLESTARTBLOCK) {
935 nmap--;
936 length = map_valid + nmap - i;
937 if (length)
938 memmove(&map[i],
939 &map[i + 1],
940 sizeof(map[i]) *
941 length);
942 } else {
943 map_blocks +=
944 map[i].br_blockcount;
945 i++;
946 }
947 }
948 map_valid += nmap;
949 }
950 /*
951 * No valid mappings, so no more data blocks.
952 */
953 if (!map_valid) {
bbaaf538 954 curoff = xfs_dir2_da_to_byte(mp, map_off);
1da177e4
LT
955 break;
956 }
957 /*
958 * Read the directory block starting at the first
959 * mapping.
960 */
bbaaf538 961 curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
051e7cd4 962 error = xfs_da_read_buf(NULL, dp, map->br_startoff,
1da177e4
LT
963 map->br_blockcount >= mp->m_dirblkfsbs ?
964 XFS_FSB_TO_DADDR(mp, map->br_startblock) :
965 -1,
966 &bp, XFS_DATA_FORK);
967 /*
968 * Should just skip over the data block instead
969 * of giving up.
970 */
971 if (error)
972 break; /* XXX */
973 /*
974 * Adjust the current amount of read-ahead: we just
975 * read a block that was previously ra.
976 */
977 if (ra_current)
978 ra_current -= mp->m_dirblkfsbs;
979 /*
980 * Do we need more readahead?
981 */
982 for (ra_index = ra_offset = i = 0;
983 ra_want > ra_current && i < map_blocks;
984 i += mp->m_dirblkfsbs) {
985 ASSERT(ra_index < map_valid);
986 /*
987 * Read-ahead a contiguous directory block.
988 */
989 if (i > ra_current &&
990 map[ra_index].br_blockcount >=
991 mp->m_dirblkfsbs) {
1a1a3e97 992 xfs_buf_readahead(mp->m_ddev_targp,
1da177e4
LT
993 XFS_FSB_TO_DADDR(mp,
994 map[ra_index].br_startblock +
995 ra_offset),
996 (int)BTOBB(mp->m_dirblksize));
997 ra_current = i;
998 }
999 /*
1000 * Read-ahead a non-contiguous directory block.
1001 * This doesn't use our mapping, but this
1002 * is a very rare case.
1003 */
1004 else if (i > ra_current) {
051e7cd4 1005 (void)xfs_da_reada_buf(NULL, dp,
1da177e4
LT
1006 map[ra_index].br_startoff +
1007 ra_offset, XFS_DATA_FORK);
1008 ra_current = i;
1009 }
1010 /*
1011 * Advance offset through the mapping table.
1012 */
1013 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1014 /*
1015 * The rest of this extent but not
1016 * more than a dir block.
1017 */
1018 length = MIN(mp->m_dirblkfsbs,
1019 (int)(map[ra_index].br_blockcount -
1020 ra_offset));
1021 j += length;
1022 ra_offset += length;
1023 /*
1024 * Advance to the next mapping if
1025 * this one is used up.
1026 */
1027 if (ra_offset ==
1028 map[ra_index].br_blockcount) {
1029 ra_offset = 0;
1030 ra_index++;
1031 }
1032 }
1033 }
1034 /*
1035 * Having done a read, we need to set a new offset.
1036 */
bbaaf538 1037 newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
1da177e4
LT
1038 /*
1039 * Start of the current block.
1040 */
1041 if (curoff < newoff)
1042 curoff = newoff;
1043 /*
1044 * Make sure we're in the right block.
1045 */
1046 else if (curoff > newoff)
bbaaf538 1047 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
1da177e4 1048 curdb);
0ba9cd84 1049 hdr = bp->data;
1da177e4
LT
1050 xfs_dir2_data_check(dp, bp);
1051 /*
1052 * Find our position in the block.
1053 */
0ba9cd84 1054 ptr = (char *)(hdr + 1);
bbaaf538 1055 byteoff = xfs_dir2_byte_to_off(mp, curoff);
1da177e4
LT
1056 /*
1057 * Skip past the header.
1058 */
1059 if (byteoff == 0)
c2066e26 1060 curoff += (uint)sizeof(*hdr);
1da177e4
LT
1061 /*
1062 * Skip past entries until we reach our offset.
1063 */
1064 else {
c2066e26 1065 while ((char *)ptr - (char *)hdr < byteoff) {
1da177e4
LT
1066 dup = (xfs_dir2_data_unused_t *)ptr;
1067
ad354eb3 1068 if (be16_to_cpu(dup->freetag)
1da177e4
LT
1069 == XFS_DIR2_DATA_FREE_TAG) {
1070
ad354eb3 1071 length = be16_to_cpu(dup->length);
1da177e4
LT
1072 ptr += length;
1073 continue;
1074 }
1075 dep = (xfs_dir2_data_entry_t *)ptr;
1076 length =
bbaaf538 1077 xfs_dir2_data_entsize(dep->namelen);
1da177e4
LT
1078 ptr += length;
1079 }
1080 /*
1081 * Now set our real offset.
1082 */
1083 curoff =
bbaaf538
CH
1084 xfs_dir2_db_off_to_byte(mp,
1085 xfs_dir2_byte_to_db(mp, curoff),
c2066e26
CH
1086 (char *)ptr - (char *)hdr);
1087 if (ptr >= (char *)hdr + mp->m_dirblksize) {
1da177e4
LT
1088 continue;
1089 }
1090 }
1091 }
1092 /*
1093 * We have a pointer to an entry.
1094 * Is it a live one?
1095 */
1096 dup = (xfs_dir2_data_unused_t *)ptr;
1097 /*
1098 * No, it's unused, skip over it.
1099 */
ad354eb3
NS
1100 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1101 length = be16_to_cpu(dup->length);
1da177e4
LT
1102 ptr += length;
1103 curoff += length;
1104 continue;
1105 }
1106
1da177e4 1107 dep = (xfs_dir2_data_entry_t *)ptr;
051e7cd4 1108 length = xfs_dir2_data_entsize(dep->namelen);
1da177e4 1109
4a24cb71 1110 if (filldir(dirent, (char *)dep->name, dep->namelen,
15440319 1111 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
a19d9f88 1112 be64_to_cpu(dep->inumber), DT_UNKNOWN))
1da177e4 1113 break;
051e7cd4 1114
1da177e4
LT
1115 /*
1116 * Advance to next entry in the block.
1117 */
1118 ptr += length;
1119 curoff += length;
8e69ce14
ES
1120 /* bufsize may have just been a guess; don't go negative */
1121 bufsize = bufsize > length ? bufsize - length : 0;
1da177e4
LT
1122 }
1123
1124 /*
1125 * All done. Set output offset value to current offset.
1126 */
bbaaf538 1127 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
15440319 1128 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
1da177e4 1129 else
15440319 1130 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
f0e2d93c 1131 kmem_free(map);
1da177e4 1132 if (bp)
051e7cd4 1133 xfs_da_brelse(NULL, bp);
1da177e4
LT
1134 return error;
1135}
1136
1137/*
1138 * Initialize a new leaf block, leaf1 or leafn magic accepted.
1139 */
1140int
1141xfs_dir2_leaf_init(
1142 xfs_da_args_t *args, /* operation arguments */
1143 xfs_dir2_db_t bno, /* directory block number */
1144 xfs_dabuf_t **bpp, /* out: leaf buffer */
1145 int magic) /* magic number for block */
1146{
1147 xfs_dabuf_t *bp; /* leaf buffer */
1148 xfs_inode_t *dp; /* incore directory inode */
1149 int error; /* error return code */
1150 xfs_dir2_leaf_t *leaf; /* leaf structure */
1151 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1152 xfs_mount_t *mp; /* filesystem mount point */
1153 xfs_trans_t *tp; /* transaction pointer */
1154
1155 dp = args->dp;
1156 ASSERT(dp != NULL);
1157 tp = args->trans;
1158 mp = dp->i_mount;
1159 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
1160 bno < XFS_DIR2_FREE_FIRSTDB(mp));
1161 /*
1162 * Get the buffer for the block.
1163 */
bbaaf538 1164 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
1da177e4
LT
1165 XFS_DATA_FORK);
1166 if (error) {
1167 return error;
1168 }
1169 ASSERT(bp != NULL);
1170 leaf = bp->data;
1171 /*
1172 * Initialize the header.
1173 */
89da0544 1174 leaf->hdr.info.magic = cpu_to_be16(magic);
1da177e4
LT
1175 leaf->hdr.info.forw = 0;
1176 leaf->hdr.info.back = 0;
1177 leaf->hdr.count = 0;
1178 leaf->hdr.stale = 0;
1179 xfs_dir2_leaf_log_header(tp, bp);
1180 /*
1181 * If it's a leaf-format directory initialize the tail.
1182 * In this case our caller has the real bests table to copy into
1183 * the block.
1184 */
1185 if (magic == XFS_DIR2_LEAF1_MAGIC) {
bbaaf538 1186 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
1187 ltp->bestcount = 0;
1188 xfs_dir2_leaf_log_tail(tp, bp);
1189 }
1190 *bpp = bp;
1191 return 0;
1192}
1193
1194/*
1195 * Log the bests entries indicated from a leaf1 block.
1196 */
ba0f32d4 1197static void
1da177e4
LT
1198xfs_dir2_leaf_log_bests(
1199 xfs_trans_t *tp, /* transaction pointer */
1200 xfs_dabuf_t *bp, /* leaf buffer */
1201 int first, /* first entry to log */
1202 int last) /* last entry to log */
1203{
68b3a102
NS
1204 __be16 *firstb; /* pointer to first entry */
1205 __be16 *lastb; /* pointer to last entry */
1da177e4
LT
1206 xfs_dir2_leaf_t *leaf; /* leaf structure */
1207 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1208
1209 leaf = bp->data;
89da0544 1210 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
bbaaf538
CH
1211 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1212 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1213 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1da177e4
LT
1214 xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
1215 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1216}
1217
1218/*
1219 * Log the leaf entries indicated from a leaf1 or leafn block.
1220 */
1221void
1222xfs_dir2_leaf_log_ents(
1223 xfs_trans_t *tp, /* transaction pointer */
1224 xfs_dabuf_t *bp, /* leaf buffer */
1225 int first, /* first entry to log */
1226 int last) /* last entry to log */
1227{
1228 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1229 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1230 xfs_dir2_leaf_t *leaf; /* leaf structure */
1231
1232 leaf = bp->data;
89da0544
NS
1233 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1234 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1235 firstlep = &leaf->ents[first];
1236 lastlep = &leaf->ents[last];
1237 xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
1238 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1239}
1240
1241/*
1242 * Log the header of the leaf1 or leafn block.
1243 */
1244void
1245xfs_dir2_leaf_log_header(
1246 xfs_trans_t *tp, /* transaction pointer */
1247 xfs_dabuf_t *bp) /* leaf buffer */
1248{
1249 xfs_dir2_leaf_t *leaf; /* leaf structure */
1250
1251 leaf = bp->data;
89da0544
NS
1252 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC ||
1253 be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1254 xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
1255 (uint)(sizeof(leaf->hdr) - 1));
1256}
1257
1258/*
1259 * Log the tail of the leaf1 block.
1260 */
ba0f32d4 1261STATIC void
1da177e4
LT
1262xfs_dir2_leaf_log_tail(
1263 xfs_trans_t *tp, /* transaction pointer */
1264 xfs_dabuf_t *bp) /* leaf buffer */
1265{
1266 xfs_dir2_leaf_t *leaf; /* leaf structure */
1267 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1268 xfs_mount_t *mp; /* filesystem mount point */
1269
1270 mp = tp->t_mountp;
1271 leaf = bp->data;
89da0544 1272 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC);
bbaaf538 1273 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1da177e4
LT
1274 xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
1275 (uint)(mp->m_dirblksize - 1));
1276}
1277
1278/*
1279 * Look up the entry referred to by args in the leaf format directory.
1280 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1281 * is also used by the node-format code.
1282 */
1283int
1284xfs_dir2_leaf_lookup(
1285 xfs_da_args_t *args) /* operation arguments */
1286{
1287 xfs_dabuf_t *dbp; /* data block buffer */
1288 xfs_dir2_data_entry_t *dep; /* data block entry */
1289 xfs_inode_t *dp; /* incore directory inode */
1290 int error; /* error return code */
1291 int index; /* found entry index */
1292 xfs_dabuf_t *lbp; /* leaf buffer */
1293 xfs_dir2_leaf_t *leaf; /* leaf structure */
1294 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1295 xfs_trans_t *tp; /* transaction pointer */
1296
0b1b213f
CH
1297 trace_xfs_dir2_leaf_lookup(args);
1298
1da177e4
LT
1299 /*
1300 * Look up name in the leaf block, returning both buffers and index.
1301 */
1302 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1303 return error;
1304 }
1305 tp = args->trans;
1306 dp = args->dp;
1307 xfs_dir2_leaf_check(dp, lbp);
1308 leaf = lbp->data;
1309 /*
1310 * Get to the leaf entry and contained data entry address.
1311 */
1312 lep = &leaf->ents[index];
1313 /*
1314 * Point to the data entry.
1315 */
1316 dep = (xfs_dir2_data_entry_t *)
1317 ((char *)dbp->data +
bbaaf538 1318 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
1da177e4 1319 /*
384f3ced 1320 * Return the found inode number & CI name if appropriate
1da177e4 1321 */
ff9901c1 1322 args->inumber = be64_to_cpu(dep->inumber);
384f3ced 1323 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1da177e4
LT
1324 xfs_da_brelse(tp, dbp);
1325 xfs_da_brelse(tp, lbp);
384f3ced 1326 return XFS_ERROR(error);
1da177e4
LT
1327}
1328
1329/*
1330 * Look up name/hash in the leaf block.
1331 * Fill in indexp with the found index, and dbpp with the data buffer.
1332 * If not found dbpp will be NULL, and ENOENT comes back.
1333 * lbpp will always be filled in with the leaf buffer unless there's an error.
1334 */
1335static int /* error */
1336xfs_dir2_leaf_lookup_int(
1337 xfs_da_args_t *args, /* operation arguments */
1338 xfs_dabuf_t **lbpp, /* out: leaf buffer */
1339 int *indexp, /* out: index in leaf block */
1340 xfs_dabuf_t **dbpp) /* out: data buffer */
1341{
07fe4dd4
BN
1342 xfs_dir2_db_t curdb = -1; /* current data block number */
1343 xfs_dabuf_t *dbp = NULL; /* data buffer */
1da177e4
LT
1344 xfs_dir2_data_entry_t *dep; /* data entry */
1345 xfs_inode_t *dp; /* incore directory inode */
1346 int error; /* error return code */
1347 int index; /* index in leaf block */
1348 xfs_dabuf_t *lbp; /* leaf buffer */
1349 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1350 xfs_dir2_leaf_t *leaf; /* leaf structure */
1351 xfs_mount_t *mp; /* filesystem mount point */
1352 xfs_dir2_db_t newdb; /* new data block number */
1353 xfs_trans_t *tp; /* transaction pointer */
07fe4dd4 1354 xfs_dir2_db_t cidb = -1; /* case match data block no. */
5163f95a 1355 enum xfs_dacmp cmp; /* name compare result */
1da177e4
LT
1356
1357 dp = args->dp;
1358 tp = args->trans;
1359 mp = dp->i_mount;
1360 /*
1361 * Read the leaf block into the buffer.
1362 */
07fe4dd4
BN
1363 error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
1364 XFS_DATA_FORK);
1365 if (error)
1da177e4 1366 return error;
1da177e4
LT
1367 *lbpp = lbp;
1368 leaf = lbp->data;
1369 xfs_dir2_leaf_check(dp, lbp);
1370 /*
1371 * Look for the first leaf entry with our hash value.
1372 */
1373 index = xfs_dir2_leaf_search_hash(args, lbp);
1374 /*
1375 * Loop over all the entries with the right hash value
1376 * looking to match the name.
1377 */
07fe4dd4 1378 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
5163f95a
BN
1379 be32_to_cpu(lep->hashval) == args->hashval;
1380 lep++, index++) {
1da177e4
LT
1381 /*
1382 * Skip over stale leaf entries.
1383 */
3c1f9c15 1384 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
1385 continue;
1386 /*
1387 * Get the new data block number.
1388 */
bbaaf538 1389 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4
LT
1390 /*
1391 * If it's not the same as the old data block number,
1392 * need to pitch the old one and read the new one.
1393 */
1394 if (newdb != curdb) {
07fe4dd4 1395 if (dbp)
1da177e4 1396 xfs_da_brelse(tp, dbp);
5163f95a
BN
1397 error = xfs_da_read_buf(tp, dp,
1398 xfs_dir2_db_to_da(mp, newdb),
1399 -1, &dbp, XFS_DATA_FORK);
1400 if (error) {
1da177e4
LT
1401 xfs_da_brelse(tp, lbp);
1402 return error;
1403 }
1404 xfs_dir2_data_check(dp, dbp);
1405 curdb = newdb;
1406 }
1407 /*
1408 * Point to the data entry.
1409 */
5163f95a
BN
1410 dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
1411 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1da177e4 1412 /*
5163f95a
BN
1413 * Compare name and if it's an exact match, return the index
1414 * and buffer. If it's the first case-insensitive match, store
1415 * the index and buffer and continue looking for an exact match.
1da177e4 1416 */
5163f95a
BN
1417 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1418 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1419 args->cmpresult = cmp;
1da177e4 1420 *indexp = index;
07fe4dd4 1421 /* case exact match: return the current buffer. */
5163f95a 1422 if (cmp == XFS_CMP_EXACT) {
5163f95a
BN
1423 *dbpp = dbp;
1424 return 0;
1425 }
07fe4dd4 1426 cidb = curdb;
1da177e4
LT
1427 }
1428 }
6a178100 1429 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
5163f95a 1430 /*
07fe4dd4
BN
1431 * Here, we can only be doing a lookup (not a rename or remove).
1432 * If a case-insensitive match was found earlier, re-read the
1433 * appropriate data block if required and return it.
5163f95a
BN
1434 */
1435 if (args->cmpresult == XFS_CMP_CASE) {
07fe4dd4
BN
1436 ASSERT(cidb != -1);
1437 if (cidb != curdb) {
5163f95a 1438 xfs_da_brelse(tp, dbp);
07fe4dd4
BN
1439 error = xfs_da_read_buf(tp, dp,
1440 xfs_dir2_db_to_da(mp, cidb),
1441 -1, &dbp, XFS_DATA_FORK);
1442 if (error) {
1443 xfs_da_brelse(tp, lbp);
1444 return error;
1445 }
1446 }
1447 *dbpp = dbp;
5163f95a
BN
1448 return 0;
1449 }
1da177e4
LT
1450 /*
1451 * No match found, return ENOENT.
1452 */
07fe4dd4 1453 ASSERT(cidb == -1);
1da177e4
LT
1454 if (dbp)
1455 xfs_da_brelse(tp, dbp);
1456 xfs_da_brelse(tp, lbp);
1457 return XFS_ERROR(ENOENT);
1458}
1459
1460/*
1461 * Remove an entry from a leaf format directory.
1462 */
1463int /* error */
1464xfs_dir2_leaf_removename(
1465 xfs_da_args_t *args) /* operation arguments */
1466{
68b3a102 1467 __be16 *bestsp; /* leaf block best freespace */
c2066e26 1468 xfs_dir2_data_hdr_t *hdr; /* data block header */
1da177e4
LT
1469 xfs_dir2_db_t db; /* data block number */
1470 xfs_dabuf_t *dbp; /* data block buffer */
1471 xfs_dir2_data_entry_t *dep; /* data entry structure */
1472 xfs_inode_t *dp; /* incore directory inode */
1473 int error; /* error return code */
1474 xfs_dir2_db_t i; /* temporary data block # */
1475 int index; /* index into leaf entries */
1476 xfs_dabuf_t *lbp; /* leaf buffer */
1477 xfs_dir2_leaf_t *leaf; /* leaf structure */
1478 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1479 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1480 xfs_mount_t *mp; /* filesystem mount point */
1481 int needlog; /* need to log data header */
1482 int needscan; /* need to rescan data frees */
1483 xfs_dir2_data_off_t oldbest; /* old value of best free */
1484 xfs_trans_t *tp; /* transaction pointer */
1485
0b1b213f
CH
1486 trace_xfs_dir2_leaf_removename(args);
1487
1da177e4
LT
1488 /*
1489 * Lookup the leaf entry, get the leaf and data blocks read in.
1490 */
1491 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1492 return error;
1493 }
1494 dp = args->dp;
1495 tp = args->trans;
1496 mp = dp->i_mount;
1497 leaf = lbp->data;
c2066e26 1498 hdr = dbp->data;
1da177e4
LT
1499 xfs_dir2_data_check(dp, dbp);
1500 /*
1501 * Point to the leaf entry, use that to point to the data entry.
1502 */
1503 lep = &leaf->ents[index];
bbaaf538 1504 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
1da177e4 1505 dep = (xfs_dir2_data_entry_t *)
c2066e26 1506 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
1da177e4 1507 needscan = needlog = 0;
c2066e26 1508 oldbest = be16_to_cpu(hdr->bestfree[0].length);
bbaaf538
CH
1509 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1510 bestsp = xfs_dir2_leaf_bests_p(ltp);
68b3a102 1511 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
1da177e4
LT
1512 /*
1513 * Mark the former data entry unused.
1514 */
1515 xfs_dir2_data_make_free(tp, dbp,
c2066e26 1516 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
bbaaf538 1517 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
1da177e4
LT
1518 /*
1519 * We just mark the leaf entry stale by putting a null in it.
1520 */
413d57c9 1521 be16_add_cpu(&leaf->hdr.stale, 1);
1da177e4 1522 xfs_dir2_leaf_log_header(tp, lbp);
3c1f9c15 1523 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
1524 xfs_dir2_leaf_log_ents(tp, lbp, index, index);
1525 /*
1526 * Scan the freespace in the data block again if necessary,
1527 * log the data block header if necessary.
1528 */
1529 if (needscan)
c2066e26 1530 xfs_dir2_data_freescan(mp, hdr, &needlog);
1da177e4
LT
1531 if (needlog)
1532 xfs_dir2_data_log_header(tp, dbp);
1533 /*
1534 * If the longest freespace in the data block has changed,
1535 * put the new value in the bests table and log that.
1536 */
c2066e26
CH
1537 if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
1538 bestsp[db] = hdr->bestfree[0].length;
1da177e4
LT
1539 xfs_dir2_leaf_log_bests(tp, lbp, db, db);
1540 }
1541 xfs_dir2_data_check(dp, dbp);
1542 /*
1543 * If the data block is now empty then get rid of the data block.
1544 */
c2066e26
CH
1545 if (be16_to_cpu(hdr->bestfree[0].length) ==
1546 mp->m_dirblksize - (uint)sizeof(*hdr)) {
1da177e4
LT
1547 ASSERT(db != mp->m_dirdatablk);
1548 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1549 /*
1550 * Nope, can't get rid of it because it caused
1551 * allocation of a bmap btree block to do so.
1552 * Just go on, returning success, leaving the
1553 * empty block in place.
1554 */
1555 if (error == ENOSPC && args->total == 0) {
1556 xfs_da_buf_done(dbp);
1557 error = 0;
1558 }
1559 xfs_dir2_leaf_check(dp, lbp);
1560 xfs_da_buf_done(lbp);
1561 return error;
1562 }
1563 dbp = NULL;
1564 /*
1565 * If this is the last data block then compact the
1566 * bests table by getting rid of entries.
1567 */
afbcb3f9 1568 if (db == be32_to_cpu(ltp->bestcount) - 1) {
1da177e4
LT
1569 /*
1570 * Look for the last active entry (i).
1571 */
1572 for (i = db - 1; i > 0; i--) {
68b3a102 1573 if (be16_to_cpu(bestsp[i]) != NULLDATAOFF)
1da177e4
LT
1574 break;
1575 }
1576 /*
1577 * Copy the table down so inactive entries at the
1578 * end are removed.
1579 */
1580 memmove(&bestsp[db - i], bestsp,
afbcb3f9 1581 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
413d57c9 1582 be32_add_cpu(&ltp->bestcount, -(db - i));
1da177e4 1583 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 1584 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4 1585 } else
68b3a102 1586 bestsp[db] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
1587 }
1588 /*
1589 * If the data block was not the first one, drop it.
1590 */
1591 else if (db != mp->m_dirdatablk && dbp != NULL) {
1592 xfs_da_buf_done(dbp);
1593 dbp = NULL;
1594 }
1595 xfs_dir2_leaf_check(dp, lbp);
1596 /*
1597 * See if we can convert to block form.
1598 */
1599 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1600}
1601
1602/*
1603 * Replace the inode number in a leaf format directory entry.
1604 */
1605int /* error */
1606xfs_dir2_leaf_replace(
1607 xfs_da_args_t *args) /* operation arguments */
1608{
1609 xfs_dabuf_t *dbp; /* data block buffer */
1610 xfs_dir2_data_entry_t *dep; /* data block entry */
1611 xfs_inode_t *dp; /* incore directory inode */
1612 int error; /* error return code */
1613 int index; /* index of leaf entry */
1614 xfs_dabuf_t *lbp; /* leaf buffer */
1615 xfs_dir2_leaf_t *leaf; /* leaf structure */
1616 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1617 xfs_trans_t *tp; /* transaction pointer */
1618
0b1b213f
CH
1619 trace_xfs_dir2_leaf_replace(args);
1620
1da177e4
LT
1621 /*
1622 * Look up the entry.
1623 */
1624 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1625 return error;
1626 }
1627 dp = args->dp;
1628 leaf = lbp->data;
1629 /*
1630 * Point to the leaf entry, get data address from it.
1631 */
1632 lep = &leaf->ents[index];
1633 /*
1634 * Point to the data entry.
1635 */
1636 dep = (xfs_dir2_data_entry_t *)
1637 ((char *)dbp->data +
bbaaf538 1638 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
ff9901c1 1639 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1da177e4
LT
1640 /*
1641 * Put the new inode number in, log it.
1642 */
ff9901c1 1643 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
1644 tp = args->trans;
1645 xfs_dir2_data_log_entry(tp, dbp, dep);
1646 xfs_da_buf_done(dbp);
1647 xfs_dir2_leaf_check(dp, lbp);
1648 xfs_da_brelse(tp, lbp);
1649 return 0;
1650}
1651
1652/*
1653 * Return index in the leaf block (lbp) which is either the first
1654 * one with this hash value, or if there are none, the insert point
1655 * for that hash value.
1656 */
1657int /* index value */
1658xfs_dir2_leaf_search_hash(
1659 xfs_da_args_t *args, /* operation arguments */
1660 xfs_dabuf_t *lbp) /* leaf buffer */
1661{
1662 xfs_dahash_t hash=0; /* hash from this entry */
1663 xfs_dahash_t hashwant; /* hash value looking for */
1664 int high; /* high leaf index */
1665 int low; /* low leaf index */
1666 xfs_dir2_leaf_t *leaf; /* leaf structure */
1667 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1668 int mid=0; /* current leaf index */
1669
1670 leaf = lbp->data;
1671#ifndef __KERNEL__
1672 if (!leaf->hdr.count)
1673 return 0;
1674#endif
1675 /*
1676 * Note, the table cannot be empty, so we have to go through the loop.
1677 * Binary search the leaf entries looking for our hash value.
1678 */
a818e5de 1679 for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
1da177e4
LT
1680 hashwant = args->hashval;
1681 low <= high; ) {
1682 mid = (low + high) >> 1;
3c1f9c15 1683 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1da177e4
LT
1684 break;
1685 if (hash < hashwant)
1686 low = mid + 1;
1687 else
1688 high = mid - 1;
1689 }
1690 /*
1691 * Found one, back up through all the equal hash values.
1692 */
1693 if (hash == hashwant) {
3c1f9c15 1694 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1da177e4
LT
1695 mid--;
1696 }
1697 }
1698 /*
1699 * Need to point to an entry higher than ours.
1700 */
1701 else if (hash < hashwant)
1702 mid++;
1703 return mid;
1704}
1705
1706/*
1707 * Trim off a trailing data block. We know it's empty since the leaf
1708 * freespace table says so.
1709 */
1710int /* error */
1711xfs_dir2_leaf_trim_data(
1712 xfs_da_args_t *args, /* operation arguments */
1713 xfs_dabuf_t *lbp, /* leaf buffer */
1714 xfs_dir2_db_t db) /* data block number */
1715{
68b3a102 1716 __be16 *bestsp; /* leaf bests table */
1da177e4
LT
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 }
1da177e4
LT
1735
1736 leaf = lbp->data;
bbaaf538 1737 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
c2066e26
CH
1738
1739#ifdef DEBUG
1740{
1741 struct xfs_dir2_data_hdr *hdr = dbp->data;
1742
1743 ASSERT(be32_to_cpu(hdr->magic) == XFS_DIR2_DATA_MAGIC);
1744 ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
1745 mp->m_dirblksize - (uint)sizeof(*hdr));
afbcb3f9 1746 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
c2066e26
CH
1747}
1748#endif
1749
1da177e4
LT
1750 /*
1751 * Get rid of the data block.
1752 */
1753 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1754 ASSERT(error != ENOSPC);
1755 xfs_da_brelse(tp, dbp);
1756 return error;
1757 }
1758 /*
1759 * Eliminate the last bests entry from the table.
1760 */
bbaaf538 1761 bestsp = xfs_dir2_leaf_bests_p(ltp);
413d57c9 1762 be32_add_cpu(&ltp->bestcount, -1);
afbcb3f9 1763 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1da177e4 1764 xfs_dir2_leaf_log_tail(tp, lbp);
afbcb3f9 1765 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
1766 return 0;
1767}
1768
2282396d
CH
1769static inline size_t
1770xfs_dir2_leaf_size(
1771 struct xfs_dir2_leaf_hdr *hdr,
1772 int counts)
1773{
1774 int entries;
1775
1776 entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
1777 return sizeof(xfs_dir2_leaf_hdr_t) +
1778 entries * sizeof(xfs_dir2_leaf_entry_t) +
1779 counts * sizeof(xfs_dir2_data_off_t) +
1780 sizeof(xfs_dir2_leaf_tail_t);
1781}
1782
1da177e4
LT
1783/*
1784 * Convert node form directory to leaf form directory.
1785 * The root of the node form dir needs to already be a LEAFN block.
1786 * Just return if we can't do anything.
1787 */
1788int /* error */
1789xfs_dir2_node_to_leaf(
1790 xfs_da_state_t *state) /* directory operation state */
1791{
1792 xfs_da_args_t *args; /* operation arguments */
1793 xfs_inode_t *dp; /* incore directory inode */
1794 int error; /* error return code */
1795 xfs_dabuf_t *fbp; /* buffer for freespace block */
1796 xfs_fileoff_t fo; /* freespace file offset */
1797 xfs_dir2_free_t *free; /* freespace structure */
1798 xfs_dabuf_t *lbp; /* buffer for leaf block */
1799 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1800 xfs_dir2_leaf_t *leaf; /* leaf structure */
1801 xfs_mount_t *mp; /* filesystem mount point */
1802 int rval; /* successful free trim? */
1803 xfs_trans_t *tp; /* transaction pointer */
1804
1805 /*
1806 * There's more than a leaf level in the btree, so there must
1807 * be multiple leafn blocks. Give up.
1808 */
1809 if (state->path.active > 1)
1810 return 0;
1811 args = state->args;
0b1b213f
CH
1812
1813 trace_xfs_dir2_node_to_leaf(args);
1814
1da177e4
LT
1815 mp = state->mp;
1816 dp = args->dp;
1817 tp = args->trans;
1818 /*
1819 * Get the last offset in the file.
1820 */
1821 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1822 return error;
1823 }
1824 fo -= mp->m_dirblkfsbs;
1825 /*
1826 * If there are freespace blocks other than the first one,
1827 * take this opportunity to remove trailing empty freespace blocks
1828 * that may have been left behind during no-space-reservation
1829 * operations.
1830 */
1831 while (fo > mp->m_dirfreeblk) {
1832 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1833 return error;
1834 }
1835 if (rval)
1836 fo -= mp->m_dirblkfsbs;
1837 else
1838 return 0;
1839 }
1840 /*
1841 * Now find the block just before the freespace block.
1842 */
1843 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1844 return error;
1845 }
1846 /*
1847 * If it's not the single leaf block, give up.
1848 */
1849 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1850 return 0;
1851 lbp = state->path.blk[0].bp;
1852 leaf = lbp->data;
89da0544 1853 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1854 /*
1855 * Read the freespace block.
1856 */
1857 if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
1858 XFS_DATA_FORK))) {
1859 return error;
1860 }
1861 free = fbp->data;
0ba962ef 1862 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4 1863 ASSERT(!free->hdr.firstdb);
2282396d 1864
1da177e4
LT
1865 /*
1866 * Now see if the leafn and free data will fit in a leaf1.
1867 * If not, release the buffer and give up.
1868 */
2282396d
CH
1869 if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
1870 mp->m_dirblksize) {
1da177e4
LT
1871 xfs_da_brelse(tp, fbp);
1872 return 0;
1873 }
2282396d 1874
1da177e4
LT
1875 /*
1876 * If the leaf has any stale entries in it, compress them out.
1877 * The compact routine will log the header.
1878 */
a818e5de 1879 if (be16_to_cpu(leaf->hdr.stale))
1da177e4
LT
1880 xfs_dir2_leaf_compact(args, lbp);
1881 else
1882 xfs_dir2_leaf_log_header(tp, lbp);
89da0544 1883 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
1da177e4
LT
1884 /*
1885 * Set up the leaf tail from the freespace block.
1886 */
bbaaf538 1887 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
0ba962ef 1888 ltp->bestcount = free->hdr.nvalid;
1da177e4
LT
1889 /*
1890 * Set up the leaf bests table.
1891 */
bbaaf538 1892 memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
2282396d 1893 be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
afbcb3f9 1894 xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1da177e4
LT
1895 xfs_dir2_leaf_log_tail(tp, lbp);
1896 xfs_dir2_leaf_check(dp, lbp);
1897 /*
1898 * Get rid of the freespace block.
1899 */
1900 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1901 if (error) {
1902 /*
1903 * This can't fail here because it can only happen when
1904 * punching out the middle of an extent, and this is an
1905 * isolated block.
1906 */
1907 ASSERT(error != ENOSPC);
1908 return error;
1909 }
1910 fbp = NULL;
1911 /*
1912 * Now see if we can convert the single-leaf directory
1913 * down to a block form directory.
1914 * This routine always kills the dabuf for the leaf, so
1915 * eliminate it from the path.
1916 */
1917 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1918 state->path.blk[0].bp = NULL;
1919 return error;
1920}
This page took 0.671785 seconds and 5 git commands to generate.